Java Code Examples for org.apache.calcite.rel.core.Aggregate#getInput()
The following examples show how to use
org.apache.calcite.rel.core.Aggregate#getInput() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RelMdPredicates.java From Bats with Apache License 2.0 | 5 votes |
/** * Infers predicates for an Aggregate. * * <p>Pulls up predicates that only contains references to columns in the * GroupSet. For e.g. * * <blockquote><pre> * inputPullUpExprs : { a > 7, b + c < 10, a + e = 9} * groupSet : { a, b} * pulledUpExprs : { a > 7} * </pre></blockquote> */ public RelOptPredicateList getPredicates(Aggregate agg, RelMetadataQuery mq) { final RelNode input = agg.getInput(); final RexBuilder rexBuilder = agg.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> aggPullUpPredicates = new ArrayList<>(); ImmutableBitSet groupKeys = agg.getGroupSet(); if (groupKeys.isEmpty()) { // "GROUP BY ()" can convert an empty relation to a non-empty relation, so // it is not valid to pull up predicates. In particular, consider the // predicate "false": it is valid on all input rows (trivially - there are // no rows!) but not on the output (there is one row). return RelOptPredicateList.EMPTY; } Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), agg.getRowType().getFieldCount()); int i = 0; for (int j : groupKeys) { m.set(j, i++); } for (RexNode r : inputInfo.pulledUpPredicates) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r); if (groupKeys.contains(rCols)) { r = r.accept(new RexPermuteInputsShuttle(m, input)); aggPullUpPredicates.add(r); } } return RelOptPredicateList.of(rexBuilder, aggPullUpPredicates); }
Example 2
Source File: RelToSqlConverter.java From Bats with Apache License 2.0 | 5 votes |
/** @see #dispatch */ public Result visit(Aggregate e) { // "select a, b, sum(x) from ( ... ) group by a, b" final Result x = visitChild(0, e.getInput()); final Builder builder; if (e.getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.GROUP_BY); } else { builder = x.builder(e, Clause.GROUP_BY); } List<SqlNode> groupByList = FluentListUtils.list(); final List<SqlNode> selectList = new ArrayList<>(); for (int group : e.getGroupSet()) { final SqlNode field = builder.context.field(group); addSelect(selectList, field, e.getRowType()); groupByList.add(field); } for (AggregateCall aggCall : e.getAggCallList()) { SqlNode aggCallSqlNode = builder.context.toSql(aggCall); if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) { aggCallSqlNode = dialect. rewriteSingleValueExpr(aggCallSqlNode); } addSelect(selectList, aggCallSqlNode, e.getRowType()); } builder.setSelect(new SqlNodeList(selectList, POS)); if (!groupByList.isEmpty() || e.getAggCallList().isEmpty()) { // Some databases don't support "GROUP BY ()". We can omit it as long // as there is at least one aggregate function. builder.setGroupBy(new SqlNodeList(groupByList, POS)); } return builder.result(); }
Example 3
Source File: RelToSqlConverter.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * @see #dispatch */ public Result visit(Aggregate e) { // "select a, b, sum(x) from ( ... ) group by a, b" final Result x = visitChild(0, e.getInput()); final Builder builder; if (e.getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.GROUP_BY); } else { builder = x.builder(e, Clause.GROUP_BY); } List<SqlNode> groupByList = Expressions.list(); final List<SqlNode> selectList = new ArrayList<>(); for (int group : e.getGroupSet()) { final SqlNode field = builder.context.field(group); addSelect(selectList, field, e.getRowType()); groupByList.add(field); } for (AggregateCall aggCall : e.getAggCallList()) { SqlNode aggCallSqlNode = builder.context.toSql(aggCall); if (aggCall.getAggregation() instanceof SqlSingleValueAggFunction) { aggCallSqlNode = dialect. rewriteSingleValueExpr(aggCallSqlNode); } addSelect(selectList, aggCallSqlNode, e.getRowType()); } builder.setSelect(new SqlNodeList(selectList, POS)); if (!groupByList.isEmpty() || e.getAggCallList().isEmpty()) { // Some databases don't support "GROUP BY ()". We can omit it as long // as there is at least one aggregate function. builder.setGroupBy(new SqlNodeList(groupByList, POS)); } return builder.result(); }
Example 4
Source File: FilterAggStarRule.java From quark with Apache License 2.0 | 5 votes |
/** * Pushes a {@link org.apache.calcite.rel.core.Filter} * past a {@link org.apache.calcite.rel.core.Aggregate}. */ RelNode filterAggregateTranspose(RelOptRuleCall call, Filter filterRel, Aggregate aggRel) { final List<RexNode> conditions = RelOptUtil.conjunctions(filterRel.getCondition()); final RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder(); final List<RelDataTypeField> origFields = aggRel.getRowType().getFieldList(); final int[] adjustments = new int[origFields.size()]; int i = 0; for (int key : aggRel.getGroupSet()) { adjustments[i] = key - i; i++; } final List<RexNode> pushedConditions = Lists.newArrayList(); for (RexNode condition : conditions) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(condition); if (canPush(aggRel, rCols)) { pushedConditions.add( condition.accept( new RelOptUtil.RexInputConverter(rexBuilder, origFields, aggRel.getInput(0).getRowType().getFieldList(), adjustments))); } else { return null; } } final RelBuilder builder = call.builder(); RelNode rel = builder.push(aggRel.getInput()).filter(pushedConditions).build(); if (rel == aggRel.getInput(0)) { return null; } rel = aggRel.copy(aggRel.getTraitSet(), ImmutableList.of(rel)); return rel; }
Example 5
Source File: RelMdPredicates.java From calcite with Apache License 2.0 | 5 votes |
/** * Infers predicates for an Aggregate. * * <p>Pulls up predicates that only contains references to columns in the * GroupSet. For e.g. * * <blockquote><pre> * inputPullUpExprs : { a > 7, b + c < 10, a + e = 9} * groupSet : { a, b} * pulledUpExprs : { a > 7} * </pre></blockquote> */ public RelOptPredicateList getPredicates(Aggregate agg, RelMetadataQuery mq) { final RelNode input = agg.getInput(); final RexBuilder rexBuilder = agg.getCluster().getRexBuilder(); final RelOptPredicateList inputInfo = mq.getPulledUpPredicates(input); final List<RexNode> aggPullUpPredicates = new ArrayList<>(); ImmutableBitSet groupKeys = agg.getGroupSet(); if (groupKeys.isEmpty()) { // "GROUP BY ()" can convert an empty relation to a non-empty relation, so // it is not valid to pull up predicates. In particular, consider the // predicate "false": it is valid on all input rows (trivially - there are // no rows!) but not on the output (there is one row). return RelOptPredicateList.EMPTY; } Mapping m = Mappings.create(MappingType.PARTIAL_FUNCTION, input.getRowType().getFieldCount(), agg.getRowType().getFieldCount()); int i = 0; for (int j : groupKeys) { m.set(j, i++); } for (RexNode r : inputInfo.pulledUpPredicates) { ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(r); if (groupKeys.contains(rCols)) { r = r.accept(new RexPermuteInputsShuttle(m, input)); aggPullUpPredicates.add(r); } } return RelOptPredicateList.of(rexBuilder, aggPullUpPredicates); }
Example 6
Source File: AggregateUnionAggregateRule.java From calcite with Apache License 2.0 | 5 votes |
/** * Returns an input with the same row type with the input Aggregate, * create a Project node if needed. */ private RelNode getInputWithSameRowType(Aggregate bottomAggRel) { if (RelOptUtil.areRowTypesEqual( bottomAggRel.getRowType(), bottomAggRel.getInput(0).getRowType(), false)) { return bottomAggRel.getInput(0); } else { return RelOptUtil.createProject( bottomAggRel.getInput(), bottomAggRel.getGroupSet().asList()); } }
Example 7
Source File: RelToSqlConverter.java From calcite with Apache License 2.0 | 5 votes |
private Result visitAggregate(Aggregate e, List<Integer> groupKeyList) { // "select a, b, sum(x) from ( ... ) group by a, b" final Result x = visitChild(0, e.getInput()); final Builder builder; if (e.getInput() instanceof Project) { builder = x.builder(e); builder.clauses.add(Clause.GROUP_BY); } else { builder = x.builder(e, Clause.GROUP_BY); } final List<SqlNode> selectList = new ArrayList<>(); final List<SqlNode> groupByList = generateGroupList(builder, selectList, e, groupKeyList); return buildAggregate(e, builder, selectList, groupByList); }
Example 8
Source File: AbstractMaterializedViewRule.java From Bats with Apache License 2.0 | 4 votes |
@Override protected RelNode rewriteQuery(RelBuilder relBuilder, RexBuilder rexBuilder, RexSimplify simplify, RelMetadataQuery mq, RexNode compensationColumnsEquiPred, RexNode otherCompensationPred, Project topProject, RelNode node, BiMap<RelTableRef, RelTableRef> queryToViewTableMapping, EquivalenceClasses viewEC, EquivalenceClasses queryEC) { Aggregate aggregate = (Aggregate) node; // Our target node is the node below the root, which should have the maximum // number of available expressions in the tree in order to maximize our // number of rewritings. // If the program is available, we execute it to maximize rewriting opportunities. // For instance, a program might pull up all the expressions that are below the // aggregate so we can introduce compensation filters easily. This is important // depending on the planner strategy. RelNode newAggregateInput = aggregate.getInput(0); RelNode target = aggregate.getInput(0); if (unionRewritingPullProgram != null) { final HepPlanner tmpPlanner = new HepPlanner(unionRewritingPullProgram); tmpPlanner.setRoot(newAggregateInput); newAggregateInput = tmpPlanner.findBestExp(); target = newAggregateInput.getInput(0); } // We need to check that all columns required by compensating predicates // are contained in the query. List<RexNode> queryExprs = extractReferences(rexBuilder, target); if (!compensationColumnsEquiPred.isAlwaysTrue()) { compensationColumnsEquiPred = rewriteExpression(rexBuilder, mq, target, target, queryExprs, queryToViewTableMapping, queryEC, false, compensationColumnsEquiPred); if (compensationColumnsEquiPred == null) { // Skip it return null; } } // For the rest, we use the query equivalence classes if (!otherCompensationPred.isAlwaysTrue()) { otherCompensationPred = rewriteExpression(rexBuilder, mq, target, target, queryExprs, queryToViewTableMapping, viewEC, true, otherCompensationPred); if (otherCompensationPred == null) { // Skip it return null; } } final RexNode queryCompensationPred = RexUtil.not(RexUtil.composeConjunction(rexBuilder, ImmutableList.of(compensationColumnsEquiPred, otherCompensationPred))); // Generate query rewriting. RelNode rewrittenPlan = relBuilder.push(target) .filter(simplify.simplifyUnknownAsFalse(queryCompensationPred)).build(); if (unionRewritingPullProgram != null) { return aggregate.copy(aggregate.getTraitSet(), ImmutableList .of(newAggregateInput.copy(newAggregateInput.getTraitSet(), ImmutableList.of(rewrittenPlan)))); } return aggregate.copy(aggregate.getTraitSet(), ImmutableList.of(rewrittenPlan)); }
Example 9
Source File: DrillReduceAggregatesRule.java From Bats with Apache License 2.0 | 4 votes |
/** * Reduces all calls to AVG, STDDEV_POP, STDDEV_SAMP, VAR_POP, VAR_SAMP in * the aggregates list to. * * <p>It handles newly generated common subexpressions since this was done * at the sql2rel stage. */ private void reduceAggs( RelOptRuleCall ruleCall, Aggregate oldAggRel) { RexBuilder rexBuilder = oldAggRel.getCluster().getRexBuilder(); List<AggregateCall> oldCalls = oldAggRel.getAggCallList(); final int nGroups = oldAggRel.getGroupCount(); List<AggregateCall> newCalls = new ArrayList<>(); Map<AggregateCall, RexNode> aggCallMapping = new HashMap<>(); List<RexNode> projList = new ArrayList<>(); // pass through group key for (int i = 0; i < nGroups; ++i) { projList.add( rexBuilder.makeInputRef( getFieldType(oldAggRel, i), i)); } // List of input expressions. If a particular aggregate needs more, it // will add an expression to the end, and we will create an extra // project. RelNode input = oldAggRel.getInput(); List<RexNode> inputExprs = new ArrayList<>(); for (RelDataTypeField field : input.getRowType().getFieldList()) { inputExprs.add( rexBuilder.makeInputRef( field.getType(), inputExprs.size())); } // create new agg function calls and rest of project list together for (AggregateCall oldCall : oldCalls) { projList.add( reduceAgg( oldAggRel, oldCall, newCalls, aggCallMapping, inputExprs)); } final int extraArgCount = inputExprs.size() - input.getRowType().getFieldCount(); if (extraArgCount > 0) { input = relBuilderFactory .create(input.getCluster(), null) .push(input) .projectNamed( inputExprs, CompositeList.of( input.getRowType().getFieldNames(), Collections.nCopies( extraArgCount, null)), true) .build(); } Aggregate newAggRel = newAggregateRel( oldAggRel, input, newCalls); RelNode projectRel = relBuilderFactory .create(newAggRel.getCluster(), null) .push(newAggRel) .projectNamed(projList, oldAggRel.getRowType().getFieldNames(), true) .build(); ruleCall.transformTo(projectRel); }
Example 10
Source File: MaterializedViewAggregateRule.java From calcite with Apache License 2.0 | 4 votes |
@Override protected RelNode rewriteQuery( RelBuilder relBuilder, RexBuilder rexBuilder, RexSimplify simplify, RelMetadataQuery mq, RexNode compensationColumnsEquiPred, RexNode otherCompensationPred, Project topProject, RelNode node, BiMap<RelTableRef, RelTableRef> queryToViewTableMapping, EquivalenceClasses viewEC, EquivalenceClasses queryEC) { Aggregate aggregate = (Aggregate) node; // Our target node is the node below the root, which should have the maximum // number of available expressions in the tree in order to maximize our // number of rewritings. // If the program is available, we execute it to maximize rewriting opportunities. // For instance, a program might pull up all the expressions that are below the // aggregate so we can introduce compensation filters easily. This is important // depending on the planner strategy. RelNode newAggregateInput = aggregate.getInput(0); RelNode target = aggregate.getInput(0); if (unionRewritingPullProgram != null) { final HepPlanner tmpPlanner = new HepPlanner(unionRewritingPullProgram); tmpPlanner.setRoot(newAggregateInput); newAggregateInput = tmpPlanner.findBestExp(); target = newAggregateInput.getInput(0); } // We need to check that all columns required by compensating predicates // are contained in the query. List<RexNode> queryExprs = extractReferences(rexBuilder, target); if (!compensationColumnsEquiPred.isAlwaysTrue()) { compensationColumnsEquiPred = rewriteExpression(rexBuilder, mq, target, target, queryExprs, queryToViewTableMapping, queryEC, false, compensationColumnsEquiPred); if (compensationColumnsEquiPred == null) { // Skip it return null; } } // For the rest, we use the query equivalence classes if (!otherCompensationPred.isAlwaysTrue()) { otherCompensationPred = rewriteExpression(rexBuilder, mq, target, target, queryExprs, queryToViewTableMapping, viewEC, true, otherCompensationPred); if (otherCompensationPred == null) { // Skip it return null; } } final RexNode queryCompensationPred = RexUtil.not( RexUtil.composeConjunction(rexBuilder, ImmutableList.of(compensationColumnsEquiPred, otherCompensationPred))); // Generate query rewriting. RelNode rewrittenPlan = relBuilder .push(target) .filter(simplify.simplifyUnknownAsFalse(queryCompensationPred)) .build(); if (unionRewritingPullProgram != null) { return aggregate.copy(aggregate.getTraitSet(), ImmutableList.of( newAggregateInput.copy(newAggregateInput.getTraitSet(), ImmutableList.of(rewrittenPlan)))); } return aggregate.copy(aggregate.getTraitSet(), ImmutableList.of(rewrittenPlan)); }
Example 11
Source File: AggregateRemoveRule.java From calcite with Apache License 2.0 | 4 votes |
public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final RelNode input = aggregate.getInput(); final RelMetadataQuery mq = call.getMetadataQuery(); if (!SqlFunctions.isTrue(mq.areColumnsUnique(input, aggregate.getGroupSet()))) { return; } final RelBuilder relBuilder = call.builder(); final RexBuilder rexBuilder = relBuilder.getRexBuilder(); final List<RexNode> projects = new ArrayList<>(); for (AggregateCall aggCall : aggregate.getAggCallList()) { final SqlAggFunction aggregation = aggCall.getAggregation(); if (aggregation.getKind() == SqlKind.SUM0) { // Bail out for SUM0 to avoid potential infinite rule matching, // because it may be generated by transforming SUM aggregate // function to SUM0 and COUNT. return; } final SqlSplittableAggFunction splitter = Objects.requireNonNull( aggregation.unwrap(SqlSplittableAggFunction.class)); final RexNode singleton = splitter.singleton( rexBuilder, input.getRowType(), aggCall); projects.add(singleton); } final RelNode newInput = convert(input, aggregate.getTraitSet().simplify()); relBuilder.push(newInput); if (!projects.isEmpty()) { projects.addAll(0, relBuilder.fields(aggregate.getGroupSet())); relBuilder.project(projects); } else if (newInput.getRowType().getFieldCount() > aggregate.getRowType().getFieldCount()) { // If aggregate was projecting a subset of columns, and there were no // aggregate functions, add a project for the same effect. relBuilder.project(relBuilder.fields(aggregate.getGroupSet())); } call.getPlanner().prune(aggregate); call.transformTo(relBuilder.build()); }