Search in sources :

Example 76 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by axbaretto.

the class TopProjectVisitor method addTopProjectPrel.

/**
 * Adds top project to ensure final output field names are preserved.
 * In case of duplicated column names, will rename duplicates.
 * Top project will be added only if top project is non-trivial and
 * child physical relational node is not project.
 *
 * @param prel physical relational node
 * @param validatedRowType final output row type
 * @return physical relational node with top project if necessary
 */
private Prel addTopProjectPrel(Prel prel, RelDataType validatedRowType) {
    RelDataType rowType = prel.getRowType();
    if (rowType.getFieldCount() != validatedRowType.getFieldCount()) {
        return prel;
    }
    RexBuilder rexBuilder = prel.getCluster().getRexBuilder();
    List<RexNode> projections = new ArrayList<>();
    int projectCount = rowType.getFieldList().size();
    for (int i = 0; i < projectCount; i++) {
        projections.add(rexBuilder.makeInputRef(prel, i));
    }
    List<String> fieldNames = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(), SqlValidatorUtil.EXPR_SUGGESTER, prel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
    RelDataType newRowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), projections, fieldNames, null);
    ProjectPrel topProject = new ProjectPrel(prel.getCluster(), prel.getTraitSet(), prel, projections, newRowType, // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
    true);
    if (prel instanceof Project && DrillRelOptUtil.isTrivialProject(topProject, true)) {
        return new ProjectPrel(prel.getCluster(), prel.getTraitSet(), ((Project) prel).getInput(), ((Project) prel).getProjects(), prel.getRowType(), // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
        true);
    } else {
        return topProject;
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) Project(org.apache.calcite.rel.core.Project) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 77 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by axbaretto.

the class SqlHandlerUtil method qualifyPartitionCol.

/**
 *  Resolve the partition columns specified in "PARTITION BY" clause of CTAS statement.
 *
 *  A partition column is resolved, either (1) the same column appear in the select list of CTAS
 *  or (2) CTAS has a * in select list.
 *
 *  In the second case, a PROJECT with ITEM expression would be created and returned.
 *  Throw validation error if a partition column is not resolved correctly.
 *
 * @param input : the RelNode represents the select statement in CTAS.
 * @param partitionColumns : the list of partition columns.
 * @return : 1) the original RelNode input, if all partition columns are in select list of CTAS
 *           2) a New Project, if a partition column is resolved to * column in select list
 *           3) validation error, if partition column is not resolved.
 */
public static RelNode qualifyPartitionCol(RelNode input, List<String> partitionColumns) {
    final RelDataType inputRowType = input.getRowType();
    final List<RexNode> colRefStarExprs = Lists.newArrayList();
    final List<String> colRefStarNames = Lists.newArrayList();
    final RexBuilder builder = input.getCluster().getRexBuilder();
    final int originalFieldSize = inputRowType.getFieldCount();
    for (final String col : partitionColumns) {
        final RelDataTypeField field = inputRowType.getField(col, false, false);
        if (field == null) {
            throw UserException.validationError().message("Partition column %s is not in the SELECT list of CTAS!", col).build(logger);
        } else {
            if (SchemaPath.DYNAMIC_STAR.equals(field.getName())) {
                colRefStarNames.add(col);
                final List<RexNode> operands = Lists.newArrayList();
                operands.add(new RexInputRef(field.getIndex(), field.getType()));
                operands.add(builder.makeLiteral(col));
                final RexNode item = builder.makeCall(SqlStdOperatorTable.ITEM, operands);
                colRefStarExprs.add(item);
            }
        }
    }
    if (colRefStarExprs.isEmpty()) {
        return input;
    } else {
        final List<String> names = new AbstractList<String>() {

            @Override
            public String get(int index) {
                if (index < originalFieldSize) {
                    return inputRowType.getFieldNames().get(index);
                } else {
                    return colRefStarNames.get(index - originalFieldSize);
                }
            }

            @Override
            public int size() {
                return originalFieldSize + colRefStarExprs.size();
            }
        };
        final List<RexNode> refs = new AbstractList<RexNode>() {

            @Override
            public int size() {
                return originalFieldSize + colRefStarExprs.size();
            }

            @Override
            public RexNode get(int index) {
                if (index < originalFieldSize) {
                    return RexInputRef.of(index, inputRowType.getFieldList());
                } else {
                    return colRefStarExprs.get(index - originalFieldSize);
                }
            }
        };
        return RelOptUtil.createProject(input, refs, names, false, DrillRelFactories.LOGICAL_BUILDER.create(input.getCluster(), null));
    }
}
Also used : AbstractList(java.util.AbstractList) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RexBuilder(org.apache.calcite.rex.RexBuilder) RexInputRef(org.apache.calcite.rex.RexInputRef) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 78 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.

the class ConvertHiveParquetScanToDrillParquetScan method createProjectRel.

/**
 * Create a project that converts the native scan output to expected output of Hive scan.
 */
private DrillProjectRel createProjectRel(final DrillScanRel hiveScanRel, final Map<String, String> partitionColMapping, final DrillScanRel nativeScanRel) {
    final List<RexNode> rexNodes = new ArrayList<>();
    final RexBuilder rb = hiveScanRel.getCluster().getRexBuilder();
    final RelDataType hiveScanRowType = hiveScanRel.getRowType();
    for (String colName : hiveScanRowType.getFieldNames()) {
        final String dirColName = partitionColMapping.get(colName);
        if (dirColName != null) {
            rexNodes.add(createPartitionColumnCast(hiveScanRel, nativeScanRel, colName, dirColName, rb));
        } else {
            rexNodes.add(createColumnFormatConversion(hiveScanRel, nativeScanRel, colName, rb));
        }
    }
    return DrillProjectRel.create(hiveScanRel.getCluster(), hiveScanRel.getTraitSet(), nativeScanRel, rexNodes, hiveScanRowType);
}
Also used : ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 79 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.

the class SqlConverter method toRel.

public RelRoot toRel(final SqlNode validatedNode) {
    initCluster(initPlanner());
    DrillViewExpander viewExpander = new DrillViewExpander(this);
    final SqlToRelConverter sqlToRelConverter = new SqlToRelConverter(viewExpander, validator, catalog, cluster, DrillConvertletTable.INSTANCE, sqlToRelConverterConfig);
    boolean topLevelQuery = !isInnerQuery || isExpandedView;
    RelRoot rel = sqlToRelConverter.convertQuery(validatedNode, false, topLevelQuery);
    // add another project to remove them.
    if (topLevelQuery && rel.rel.getRowType().getFieldCount() - rel.fields.size() > 0) {
        RexBuilder builder = rel.rel.getCluster().getRexBuilder();
        RelNode relNode = rel.rel;
        List<RexNode> expressions = rel.fields.stream().map(f -> builder.makeInputRef(relNode, f.left)).collect(Collectors.toList());
        RelNode project = LogicalProject.create(rel.rel, expressions, rel.validatedRowType);
        rel = RelRoot.of(project, rel.validatedRowType, rel.kind);
    }
    return rel.withRel(sqlToRelConverter.flattenTypes(rel.rel, true));
}
Also used : Arrays(java.util.Arrays) UserException(org.apache.drill.common.exceptions.UserException) DrillSqlParseException(org.apache.drill.exec.planner.sql.parser.impl.DrillSqlParseException) LoggerFactory(org.slf4j.LoggerFactory) RelRoot(org.apache.calcite.rel.RelRoot) DrillParserConfig(org.apache.drill.exec.planner.sql.DrillParserConfig) DrillRelDataTypeSystem(org.apache.drill.exec.planner.types.DrillRelDataTypeSystem) FunctionImplementationRegistry(org.apache.drill.exec.expr.fn.FunctionImplementationRegistry) Utilities(org.apache.drill.exec.util.Utilities) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) SqlNode(org.apache.calcite.sql.SqlNode) RexNode(org.apache.calcite.rex.RexNode) DynamicSchema(org.apache.calcite.jdbc.DynamicSchema) ConventionTraitDef(org.apache.calcite.plan.ConventionTraitDef) RelOptCluster(org.apache.calcite.plan.RelOptCluster) JavaTypeFactory(org.apache.calcite.adapter.java.JavaTypeFactory) RelDataType(org.apache.calcite.rel.type.RelDataType) QueryContext(org.apache.drill.exec.ops.QueryContext) SchemaUtilites(org.apache.drill.exec.planner.sql.SchemaUtilites) RelOptCostFactory(org.apache.calcite.plan.RelOptCostFactory) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Logger(org.slf4j.Logger) RelCollationTraitDef(org.apache.calcite.rel.RelCollationTraitDef) LogicalProject(org.apache.calcite.rel.logical.LogicalProject) RexBuilder(org.apache.calcite.rex.RexBuilder) DrillDistributionTraitDef(org.apache.drill.exec.planner.physical.DrillDistributionTraitDef) SqlOperatorTable(org.apache.calcite.sql.SqlOperatorTable) DrillConstExecutor(org.apache.drill.exec.planner.logical.DrillConstExecutor) JavaTypeFactoryImpl(org.apache.calcite.jdbc.JavaTypeFactoryImpl) RelNode(org.apache.calcite.rel.RelNode) Collectors(java.util.stream.Collectors) ChainedSqlOperatorTable(org.apache.calcite.sql.util.ChainedSqlOperatorTable) List(java.util.List) DrillConfig(org.apache.drill.common.config.DrillConfig) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DrillConvertletTable(org.apache.drill.exec.planner.sql.DrillConvertletTable) SqlParser(org.apache.calcite.sql.parser.SqlParser) DrillCostBase(org.apache.drill.exec.planner.cost.DrillCostBase) VolcanoPlanner(org.apache.calcite.plan.volcano.VolcanoPlanner) ExecConstants(org.apache.drill.exec.ExecConstants) DrillRelFactories(org.apache.drill.exec.planner.logical.DrillRelFactories) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) RelOptPlanner(org.apache.calcite.plan.RelOptPlanner) Hook(org.apache.calcite.runtime.Hook) UserSession(org.apache.drill.exec.rpc.user.UserSession) JaninoRelMetadataProvider(org.apache.calcite.rel.metadata.JaninoRelMetadataProvider) SqlToRelConverter(org.apache.calcite.sql2rel.SqlToRelConverter) RelNode(org.apache.calcite.rel.RelNode) RexBuilder(org.apache.calcite.rex.RexBuilder) RelRoot(org.apache.calcite.rel.RelRoot) RexNode(org.apache.calcite.rex.RexNode)

Example 80 with RexBuilder

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.rex.RexBuilder in project drill by apache.

the class TopProjectVisitor method addTopProjectPrel.

/**
 * Adds top project to ensure final output field names are preserved.
 * In case of duplicated column names, will rename duplicates.
 * Top project will be added only if top project is non-trivial and
 * child physical relational node is not project.
 *
 * @param prel physical relational node
 * @param validatedRowType final output row type
 * @return physical relational node with top project if necessary
 */
private Prel addTopProjectPrel(Prel prel, RelDataType validatedRowType) {
    RelDataType rowType = prel.getRowType();
    if (rowType.getFieldCount() != validatedRowType.getFieldCount()) {
        return prel;
    }
    RexBuilder rexBuilder = prel.getCluster().getRexBuilder();
    List<RexNode> projections = new ArrayList<>();
    int projectCount = rowType.getFieldList().size();
    for (int i = 0; i < projectCount; i++) {
        projections.add(rexBuilder.makeInputRef(prel, i));
    }
    List<String> fieldNames = SqlValidatorUtil.uniquify(validatedRowType.getFieldNames(), SqlValidatorUtil.EXPR_SUGGESTER, prel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
    RelDataType newRowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), projections, fieldNames, null);
    ProjectPrel topProject = new ProjectPrel(prel.getCluster(), prel.getTraitSet(), prel, projections, newRowType, // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
    true);
    if (prel instanceof Project && DrillRelOptUtil.isTrivialProject(topProject, true)) {
        return new ProjectPrel(prel.getCluster(), prel.getTraitSet(), ((Project) prel).getInput(), ((Project) prel).getProjects(), prel.getRowType(), // outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
        true);
    } else {
        return topProject;
    }
}
Also used : ProjectPrel(org.apache.drill.exec.planner.physical.ProjectPrel) Project(org.apache.calcite.rel.core.Project) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RexBuilder (org.apache.calcite.rex.RexBuilder)314 RexNode (org.apache.calcite.rex.RexNode)248 ArrayList (java.util.ArrayList)151 RelNode (org.apache.calcite.rel.RelNode)121 RelDataType (org.apache.calcite.rel.type.RelDataType)121 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)77 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)59 RexInputRef (org.apache.calcite.rex.RexInputRef)49 RelBuilder (org.apache.calcite.tools.RelBuilder)49 AggregateCall (org.apache.calcite.rel.core.AggregateCall)48 List (java.util.List)41 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)41 RelOptCluster (org.apache.calcite.plan.RelOptCluster)36 HashMap (java.util.HashMap)33 RelOptPredicateList (org.apache.calcite.plan.RelOptPredicateList)24 Project (org.apache.calcite.rel.core.Project)24 RexCall (org.apache.calcite.rex.RexCall)24 BigDecimal (java.math.BigDecimal)23 Collectors (java.util.stream.Collectors)23 RelMetadataQuery (org.apache.calcite.rel.metadata.RelMetadataQuery)22