Search in sources :

Example 16 with HiveAggregate

use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate in project hive by apache.

the class HiveRelFieldTrimmer method rewriteGBConstantKeys.

/**
 * This method replaces group by 'constant key' with group by true (boolean)
 * if and only if
 *  group by doesn't have grouping sets
 *  all keys in group by are constant
 *  none of the relnode above aggregate refers to these keys
 *
 *  If all of above is true then group by is rewritten and a new project is introduced
 *  underneath aggregate
 *
 *  This is mainly done so that hive is able to push down queries with
 *  group by 'constant key with type not supported by druid' into druid.
 */
private Aggregate rewriteGBConstantKeys(Aggregate aggregate, ImmutableBitSet fieldsUsed, ImmutableBitSet aggCallFields) {
    if ((aggregate.getIndicatorCount() > 0) || (aggregate.getGroupSet().isEmpty()) || fieldsUsed.contains(aggregate.getGroupSet())) {
        return aggregate;
    }
    final RelNode input = aggregate.getInput();
    final RelDataType rowType = input.getRowType();
    RexBuilder rexBuilder = aggregate.getCluster().getRexBuilder();
    final List<RexNode> newProjects = new ArrayList<>();
    final List<RexNode> inputExprs = input instanceof Project ? ((Project) input).getProjects() : null;
    if (inputExprs == null || inputExprs.isEmpty()) {
        return aggregate;
    }
    boolean allConstants = true;
    for (int key : aggregate.getGroupSet()) {
        // getChildExprs on Join could return less number of expressions than there are coming out of join
        if (inputExprs.size() <= key || !isRexLiteral(inputExprs.get(key))) {
            allConstants = false;
            break;
        }
    }
    if (allConstants) {
        for (int i = 0; i < rowType.getFieldCount(); i++) {
            if (aggregate.getGroupSet().get(i) && !aggCallFields.get(i)) {
                newProjects.add(rexBuilder.makeLiteral(true));
            } else {
                newProjects.add(rexBuilder.makeInputRef(input, i));
            }
        }
        final RelBuilder relBuilder = REL_BUILDER.get();
        relBuilder.push(input);
        relBuilder.project(newProjects);
        Aggregate newAggregate = new HiveAggregate(aggregate.getCluster(), aggregate.getTraitSet(), relBuilder.build(), aggregate.getGroupSet(), aggregate.getGroupSets(), aggregate.getAggCallList());
        return newAggregate;
    }
    return aggregate;
}
Also used : HiveProject(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject) Project(org.apache.calcite.rel.core.Project) HiveAggregate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate) RelBuilder(org.apache.calcite.tools.RelBuilder) RelNode(org.apache.calcite.rel.RelNode) ArrayList(java.util.ArrayList) RexBuilder(org.apache.calcite.rex.RexBuilder) RelDataType(org.apache.calcite.rel.type.RelDataType) HiveAggregate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate) Aggregate(org.apache.calcite.rel.core.Aggregate) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with HiveAggregate

use of org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate in project hive by apache.

the class HiveRelColumnsAlignment method align.

public RelNode align(Aggregate rel, List<RelFieldCollation> collations) {
    // 1) We extract the group by positions that are part of the collations and
    // sort them so they respect it
    LinkedHashSet<Integer> aggregateColumnsOrder = new LinkedHashSet<>();
    ImmutableList.Builder<RelFieldCollation> propagateCollations = ImmutableList.builder();
    if (rel.getGroupType() == Group.SIMPLE && !collations.isEmpty()) {
        for (RelFieldCollation c : collations) {
            if (c.getFieldIndex() < rel.getGroupCount()) {
                // Group column found
                if (aggregateColumnsOrder.add(c.getFieldIndex())) {
                    propagateCollations.add(c.copy(rel.getGroupSet().nth(c.getFieldIndex())));
                }
            }
        }
    }
    for (int i = 0; i < rel.getGroupCount(); i++) {
        if (!aggregateColumnsOrder.contains(i)) {
            // Not included in the input collations, but can be propagated as this Aggregate
            // will enforce it
            propagateCollations.add(new RelFieldCollation(rel.getGroupSet().nth(i)));
        }
    }
    // 2) We propagate
    final RelNode child = dispatchAlign(rel.getInput(), propagateCollations.build());
    // 3) We annotate the Aggregate operator with this info
    final HiveAggregate newAggregate = (HiveAggregate) rel.copy(rel.getTraitSet(), ImmutableList.of(child));
    newAggregate.setAggregateColumnsOrder(aggregateColumnsOrder);
    return newAggregate;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HiveAggregate(org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate) RelNode(org.apache.calcite.rel.RelNode) ImmutableList(com.google.common.collect.ImmutableList) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation)

Aggregations

HiveAggregate (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate)17 RelNode (org.apache.calcite.rel.RelNode)11 AggregateCall (org.apache.calcite.rel.core.AggregateCall)11 RexNode (org.apache.calcite.rex.RexNode)10 ImmutableBitSet (org.apache.calcite.util.ImmutableBitSet)9 ArrayList (java.util.ArrayList)8 RelDataType (org.apache.calcite.rel.type.RelDataType)7 RexBuilder (org.apache.calcite.rex.RexBuilder)6 HiveRelNode (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveRelNode)6 ImmutableList (com.google.common.collect.ImmutableList)5 BigDecimal (java.math.BigDecimal)5 Aggregate (org.apache.calcite.rel.core.Aggregate)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)5 RexInputRef (org.apache.calcite.rex.RexInputRef)5 CalciteSemanticException (org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException)4 HiveProject (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject)4 HashMap (java.util.HashMap)3 RelOptCluster (org.apache.calcite.plan.RelOptCluster)3 Project (org.apache.calcite.rel.core.Project)3 HiveTableFunctionScan (org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveTableFunctionScan)3