All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.druid.sql.calcite.rel.DruidRels Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.druid.sql.calcite.rel;

import org.apache.druid.segment.column.RowSignature;
import org.apache.druid.sql.calcite.table.DruidTable;

import java.util.Optional;

public class DruidRels
{
  /**
   * Returns the DataSource involved in a leaf query of class {@link DruidQueryRel}.
   */
  public static Optional druidTableIfLeafRel(final DruidRel druidRel)
  {
    if (druidRel instanceof DruidQueryRel) {
      return Optional.of(((DruidQueryRel) druidRel).getDruidTable());
    } else {
      return Optional.empty();
    }
  }

  /**
   * Check if a druidRel is a simple table scan, or a projection that merely remaps columns without transforming them.
   * Like {@link #isScanOrProject} but more restrictive: only remappings are allowed.
   *
   * @param druidRel         the rel to check
   * @param canBeJoinOrUnion consider a {@link DruidJoinQueryRel} or {@link DruidUnionDataSourceRel} as possible
   *                         scans-and-mappings too.
   */
  public static boolean isScanOrMapping(final DruidRel druidRel, final boolean canBeJoinOrUnion)
  {
    if (isScanOrProject(druidRel, canBeJoinOrUnion)) {
      // Like isScanOrProject, but don't allow transforming projections.
      final PartialDruidQuery partialQuery = druidRel.getPartialDruidQuery();
      return partialQuery.getSelectProject() == null || partialQuery.getSelectProject().isMapping();
    } else {
      return false;
    }
  }

  /**
   * Check if a druidRel is a simple table scan or a scan + projection.
   *
   * @param druidRel         the rel to check
   * @param canBeJoinOrUnion consider a {@link DruidJoinQueryRel} or {@link DruidUnionDataSourceRel} as possible
   *                         scans-and-mappings too.
   */
  public static boolean isScanOrProject(final DruidRel druidRel, final boolean canBeJoinOrUnion)
  {
    if (druidRel instanceof DruidQueryRel || (canBeJoinOrUnion && (druidRel instanceof DruidJoinQueryRel || druidRel instanceof DruidCorrelateUnnestRel
                                                                   || druidRel instanceof DruidUnionDataSourceRel))) {
      final PartialDruidQuery partialQuery = druidRel.getPartialDruidQuery();
      final PartialDruidQuery.Stage stage = partialQuery.stage();
      return (stage == PartialDruidQuery.Stage.SCAN || stage == PartialDruidQuery.Stage.SELECT_PROJECT)
             && partialQuery.getWhereFilter() == null;
    } else {
      return false;
    }
  }

  /**
   * Returns the signature of the datasource of a {@link DruidRel}.
   *
   * This is not the signature of the {@link DruidRel} itself: in particular, it ignores any operations that are layered
   * on top of the datasource.
   */
  public static RowSignature dataSourceSignature(final DruidRel druidRel)
  {
    if (druidRel instanceof DruidQueryRel) {
      // Get signature directly from the table.
      return ((DruidQueryRel) druidRel).getDruidTable().getRowSignature();
    } else {
      // Build the query with a no-op PartialDruidQuery.
      return druidRel.withPartialQuery(
          PartialDruidQuery.create(druidRel.getPartialDruidQuery().getScan())
      ).toDruidQuery(false).getOutputRowSignature();
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy