Search in sources :

Example 66 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveTableScan method buildColIndxsFrmReloptHT.

private static Pair<ImmutableList<Integer>, ImmutableSet<Integer>> buildColIndxsFrmReloptHT(RelOptHiveTable relOptHTable, RelDataType scanRowType) {
    RelDataType relOptHtRowtype = relOptHTable.getRowType();
    ImmutableList<Integer> neededColIndxsFrmReloptHT;
    Builder<Integer> neededColIndxsFrmReloptHTBldr = new ImmutableList.Builder<Integer>();
    ImmutableSet<Integer> viurtualOrPartColIndxsInTS;
    ImmutableSet.Builder<Integer> viurtualOrPartColIndxsInTSBldr = new ImmutableSet.Builder<Integer>();
    Map<String, Integer> colNameToPosInReloptHT = HiveCalciteUtil.getRowColNameIndxMap(relOptHtRowtype.getFieldList());
    List<String> colNamesInScanRowType = scanRowType.getFieldNames();
    int partOrVirtualColStartPosInrelOptHtRowtype = relOptHTable.getNonPartColumns().size();
    int tmp;
    for (int i = 0; i < colNamesInScanRowType.size(); i++) {
        tmp = colNameToPosInReloptHT.get(colNamesInScanRowType.get(i));
        neededColIndxsFrmReloptHTBldr.add(tmp);
        if (tmp >= partOrVirtualColStartPosInrelOptHtRowtype) {
            viurtualOrPartColIndxsInTSBldr.add(i);
        }
    }
    neededColIndxsFrmReloptHT = neededColIndxsFrmReloptHTBldr.build();
    viurtualOrPartColIndxsInTS = viurtualOrPartColIndxsInTSBldr.build();
    return new Pair<ImmutableList<Integer>, ImmutableSet<Integer>>(neededColIndxsFrmReloptHT, viurtualOrPartColIndxsInTS);
}
Also used : RelBuilder(org.apache.calcite.tools.RelBuilder) RexBuilder(org.apache.calcite.rex.RexBuilder) Builder(com.google.common.collect.ImmutableList.Builder) RelDataType(org.apache.calcite.rel.type.RelDataType) ImmutableSet(com.google.common.collect.ImmutableSet) Pair(org.apache.calcite.util.Pair)

Example 67 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method fields.

/** Returns references to the fields of a given input. */
public ImmutableList<RexNode> fields(int inputCount, int inputOrdinal) {
    final RelNode input = peek(inputCount, inputOrdinal);
    final RelDataType rowType = input.getRowType();
    final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
    for (int fieldOrdinal : Util.range(rowType.getFieldCount())) {
        nodes.add(field(inputCount, inputOrdinal, fieldOrdinal));
    }
    return nodes.build();
}
Also used : RelNode(org.apache.calcite.rel.RelNode) ImmutableList(com.google.common.collect.ImmutableList) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 68 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method sortLimit.

/** Creates a {@link Sort} by a list of expressions, with limit and offset.
   *
   * @param offset Number of rows to skip; non-positive means don't skip any
   * @param fetch Maximum number of rows to fetch; negative means no limit
   * @param nodes Sort expressions
   */
public HiveSubQRemoveRelBuilder sortLimit(int offset, int fetch, Iterable<? extends RexNode> nodes) {
    final List<RelFieldCollation> fieldCollations = new ArrayList<>();
    final RelDataType inputRowType = peek().getRowType();
    final List<RexNode> extraNodes = projects(inputRowType);
    final List<RexNode> originalExtraNodes = ImmutableList.copyOf(extraNodes);
    for (RexNode node : nodes) {
        fieldCollations.add(collation(node, RelFieldCollation.Direction.ASCENDING, null, extraNodes));
    }
    final RexNode offsetNode = offset <= 0 ? null : literal(offset);
    final RexNode fetchNode = fetch < 0 ? null : literal(fetch);
    if (offsetNode == null && fetch == 0) {
        return empty();
    }
    if (offsetNode == null && fetchNode == null && fieldCollations.isEmpty()) {
        // sort is trivial
        return this;
    }
    final boolean addedFields = extraNodes.size() > originalExtraNodes.size();
    if (fieldCollations.isEmpty()) {
        assert !addedFields;
        RelNode top = peek();
        if (top instanceof Sort) {
            final Sort sort2 = (Sort) top;
            if (sort2.offset == null && sort2.fetch == null) {
                stack.pop();
                push(sort2.getInput());
                final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
                push(sort);
                return this;
            }
        }
        if (top instanceof Project) {
            final Project project = (Project) top;
            if (project.getInput() instanceof Sort) {
                final Sort sort2 = (Sort) project.getInput();
                if (sort2.offset == null && sort2.fetch == null) {
                    stack.pop();
                    push(sort2.getInput());
                    final RelNode sort = sortFactory.createSort(build(), sort2.collation, offsetNode, fetchNode);
                    push(sort);
                    project(project.getProjects());
                    return this;
                }
            }
        }
    }
    if (addedFields) {
        project(extraNodes);
    }
    final RelNode sort = sortFactory.createSort(build(), RelCollations.of(fieldCollations), offsetNode, fetchNode);
    push(sort);
    if (addedFields) {
        project(originalExtraNodes);
    }
    return this;
}
Also used : Project(org.apache.calcite.rel.core.Project) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Sort(org.apache.calcite.rel.core.Sort) RelDataType(org.apache.calcite.rel.type.RelDataType) RexNode(org.apache.calcite.rex.RexNode)

Example 69 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveSubQRemoveRelBuilder method aggregate.

/** Creates an {@link org.apache.calcite.rel.core.Aggregate} with a list of
   * calls. */
public HiveSubQRemoveRelBuilder aggregate(GroupKey groupKey, Iterable<AggCall> aggCalls) {
    final RelDataType inputRowType = peek().getRowType();
    final List<RexNode> extraNodes = projects(inputRowType);
    final GroupKeyImpl groupKey_ = (GroupKeyImpl) groupKey;
    final ImmutableBitSet groupSet = ImmutableBitSet.of(registerExpressions(extraNodes, groupKey_.nodes));
    final ImmutableList<ImmutableBitSet> groupSets;
    if (groupKey_.nodeLists != null) {
        final int sizeBefore = extraNodes.size();
        final SortedSet<ImmutableBitSet> groupSetSet = new TreeSet<>(ImmutableBitSet.ORDERING);
        for (ImmutableList<RexNode> nodeList : groupKey_.nodeLists) {
            final ImmutableBitSet groupSet2 = ImmutableBitSet.of(registerExpressions(extraNodes, nodeList));
            if (!groupSet.contains(groupSet2)) {
                throw new IllegalArgumentException("group set element " + nodeList + " must be a subset of group key");
            }
            groupSetSet.add(groupSet2);
        }
        groupSets = ImmutableList.copyOf(groupSetSet);
        if (extraNodes.size() > sizeBefore) {
            throw new IllegalArgumentException("group sets contained expressions not in group key: " + extraNodes.subList(sizeBefore, extraNodes.size()));
        }
    } else {
        groupSets = ImmutableList.of(groupSet);
    }
    for (AggCall aggCall : aggCalls) {
        if (aggCall instanceof AggCallImpl) {
            final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
            registerExpressions(extraNodes, aggCall1.operands);
            if (aggCall1.filter != null) {
                registerExpression(extraNodes, aggCall1.filter);
            }
        }
    }
    if (extraNodes.size() > inputRowType.getFieldCount()) {
        project(extraNodes);
    }
    final RelNode r = build();
    final List<AggregateCall> aggregateCalls = new ArrayList<>();
    for (AggCall aggCall : aggCalls) {
        final AggregateCall aggregateCall;
        if (aggCall instanceof AggCallImpl) {
            final AggCallImpl aggCall1 = (AggCallImpl) aggCall;
            final List<Integer> args = registerExpressions(extraNodes, aggCall1.operands);
            final int filterArg = aggCall1.filter == null ? -1 : registerExpression(extraNodes, aggCall1.filter);
            aggregateCall = AggregateCall.create(aggCall1.aggFunction, aggCall1.distinct, args, filterArg, groupSet.cardinality(), r, null, aggCall1.alias);
        } else {
            aggregateCall = ((AggCallImpl2) aggCall).aggregateCall;
        }
        aggregateCalls.add(aggregateCall);
    }
    assert ImmutableBitSet.ORDERING.isStrictlyOrdered(groupSets) : groupSets;
    for (ImmutableBitSet set : groupSets) {
        assert groupSet.contains(set);
    }
    RelNode aggregate = aggregateFactory.createAggregate(r, groupKey_.indicator, groupSet, groupSets, aggregateCalls);
    push(aggregate);
    return this;
}
Also used : ImmutableBitSet(org.apache.calcite.util.ImmutableBitSet) ArrayList(java.util.ArrayList) RelDataType(org.apache.calcite.rel.type.RelDataType) AggregateCall(org.apache.calcite.rel.core.AggregateCall) RelNode(org.apache.calcite.rel.RelNode) TreeSet(java.util.TreeSet) RexNode(org.apache.calcite.rex.RexNode)

Example 70 with RelDataType

use of org.apache.calcite.rel.type.RelDataType in project hive by apache.

the class HiveCalciteUtil method createUDTFForSetOp.

public static HiveTableFunctionScan createUDTFForSetOp(RelOptCluster cluster, RelNode input) throws SemanticException {
    RelTraitSet traitSet = TraitsUtil.getDefaultTraitSet(cluster);
    List<RexNode> originalInputRefs = Lists.transform(input.getRowType().getFieldList(), new Function<RelDataTypeField, RexNode>() {

        @Override
        public RexNode apply(RelDataTypeField input) {
            return new RexInputRef(input.getIndex(), input.getType());
        }
    });
    ImmutableList.Builder<RelDataType> argTypeBldr = ImmutableList.<RelDataType>builder();
    for (int i = 0; i < originalInputRefs.size(); i++) {
        argTypeBldr.add(originalInputRefs.get(i).getType());
    }
    RelDataType retType = input.getRowType();
    String funcName = "replicate_rows";
    FunctionInfo fi = FunctionRegistry.getFunctionInfo(funcName);
    SqlOperator calciteOp = SqlFunctionConverter.getCalciteOperator(funcName, fi.getGenericUDTF(), argTypeBldr.build(), retType);
    // Hive UDTF only has a single input
    List<RelNode> list = new ArrayList<>();
    list.add(input);
    RexNode rexNode = cluster.getRexBuilder().makeCall(calciteOp, originalInputRefs);
    return HiveTableFunctionScan.create(cluster, traitSet, list, rexNode, null, retType, null);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) SqlOperator(org.apache.calcite.sql.SqlOperator) ArrayList(java.util.ArrayList) FunctionInfo(org.apache.hadoop.hive.ql.exec.FunctionInfo) RelDataType(org.apache.calcite.rel.type.RelDataType) RelTraitSet(org.apache.calcite.plan.RelTraitSet) RelDataTypeField(org.apache.calcite.rel.type.RelDataTypeField) RelNode(org.apache.calcite.rel.RelNode) RexInputRef(org.apache.calcite.rex.RexInputRef) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)88 RexNode (org.apache.calcite.rex.RexNode)48 RexBuilder (org.apache.calcite.rex.RexBuilder)28 RelNode (org.apache.calcite.rel.RelNode)27 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)25 ArrayList (java.util.ArrayList)21 RexInputRef (org.apache.calcite.rex.RexInputRef)16 RelDataTypeFactory (org.apache.calcite.rel.type.RelDataTypeFactory)14 AggregateCall (org.apache.calcite.rel.core.AggregateCall)13 ImmutableList (com.google.common.collect.ImmutableList)9 BigDecimal (java.math.BigDecimal)8 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)8 SqlAggFunction (org.apache.calcite.sql.SqlAggFunction)7 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)7 RelOptCluster (org.apache.calcite.plan.RelOptCluster)6 RelBuilder (org.apache.calcite.tools.RelBuilder)6 Prel (org.apache.drill.exec.planner.physical.Prel)6 ProjectPrel (org.apache.drill.exec.planner.physical.ProjectPrel)6 Builder (com.google.common.collect.ImmutableList.Builder)5 LinkedList (java.util.LinkedList)5