Search in sources :

Example 51 with RexInputRef

use of org.apache.calcite.rex.RexInputRef in project drill by apache.

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 (field.getName().startsWith(StarColumnHelper.STAR_COLUMN)) {
                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>() {

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

            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);
    }
}
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)

Aggregations

RexInputRef (org.apache.calcite.rex.RexInputRef)51 RexNode (org.apache.calcite.rex.RexNode)43 ArrayList (java.util.ArrayList)22 RelNode (org.apache.calcite.rel.RelNode)21 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)21 RelDataType (org.apache.calcite.rel.type.RelDataType)16 RexCall (org.apache.calcite.rex.RexCall)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)11 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)11 ImmutableList (com.google.common.collect.ImmutableList)10 HashMap (java.util.HashMap)10 RexBuilder (org.apache.calcite.rex.RexBuilder)10 Pair (org.apache.calcite.util.Pair)8 HashSet (java.util.HashSet)7 BigDecimal (java.math.BigDecimal)6 RexLiteral (org.apache.calcite.rex.RexLiteral)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 List (java.util.List)5 Builder (com.google.common.collect.ImmutableList.Builder)4 ISE (io.druid.java.util.common.ISE)4