org.apache.calcite.rel.core.Aggregate Java Examples
The following examples show how to use
org.apache.calcite.rel.core.Aggregate.
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: AggregateReduceFunctionsRule.java From Bats with Apache License 2.0 | 6 votes |
private AggregateCall createAggregateCallWithBinding( RelDataTypeFactory typeFactory, SqlAggFunction aggFunction, RelDataType operandType, Aggregate oldAggRel, AggregateCall oldCall, int argOrdinal, int filter) { final Aggregate.AggCallBinding binding = new Aggregate.AggCallBinding(typeFactory, aggFunction, ImmutableList.of(operandType), oldAggRel.getGroupCount(), filter >= 0); return AggregateCall.create(aggFunction, oldCall.isDistinct(), oldCall.isApproximate(), ImmutableIntList.of(argOrdinal), filter, oldCall.collation, aggFunction.inferReturnType(binding), null); }
Example #2
Source File: AggregateReduceFunctionsRule.java From calcite with Apache License 2.0 | 6 votes |
private RexNode getSumAggregatedRexNodeWithBinding(Aggregate oldAggRel, AggregateCall oldCall, List<AggregateCall> newCalls, Map<AggregateCall, RexNode> aggCallMapping, RelDataType operandType, int argOrdinal, int filter) { RelOptCluster cluster = oldAggRel.getCluster(); final AggregateCall sumArgSquaredAggCall = createAggregateCallWithBinding(cluster.getTypeFactory(), SqlStdOperatorTable.SUM, operandType, oldAggRel, oldCall, argOrdinal, filter); return cluster.getRexBuilder().addAggCall(sumArgSquaredAggCall, oldAggRel.getGroupCount(), newCalls, aggCallMapping, ImmutableList.of(sumArgSquaredAggCall.getType())); }
Example #3
Source File: RelMdSelectivity.java From calcite with Apache License 2.0 | 6 votes |
public Double getSelectivity(Aggregate rel, RelMetadataQuery mq, RexNode predicate) { final List<RexNode> notPushable = new ArrayList<>(); final List<RexNode> pushable = new ArrayList<>(); RelOptUtil.splitFilters( rel.getGroupSet(), predicate, pushable, notPushable); final RexBuilder rexBuilder = rel.getCluster().getRexBuilder(); RexNode childPred = RexUtil.composeConjunction(rexBuilder, pushable, true); Double selectivity = mq.getSelectivity(rel.getInput(), childPred); if (selectivity == null) { return null; } else { RexNode pred = RexUtil.composeConjunction(rexBuilder, notPushable, true); return selectivity * RelMdUtil.guessSelectivity(pred); } }
Example #4
Source File: MaterializedViewOnlyAggregateRule.java From calcite with Apache License 2.0 | 6 votes |
public MaterializedViewOnlyAggregateRule(RelOptRuleOperand operand, RelBuilderFactory relBuilderFactory, String description, boolean generateUnionRewriting, HepProgram unionRewritingPullProgram, RelOptRule filterProjectTransposeRule, RelOptRule filterAggregateTransposeRule, RelOptRule aggregateProjectPullUpConstantsRule, RelOptRule projectMergeRule) { super( operand(Aggregate.class, any()), relBuilderFactory, "MaterializedViewAggregateRule(Aggregate)", generateUnionRewriting, unionRewritingPullProgram, filterProjectTransposeRule, filterAggregateTransposeRule, aggregateProjectPullUpConstantsRule, projectMergeRule); }
Example #5
Source File: ExtendedAggregateExtractProjectRule.java From flink with Apache License 2.0 | 6 votes |
private List<RelBuilder.AggCall> getNewAggCallList( Aggregate oldAggregate, RelBuilder relBuilder, Mapping mapping) { final List<RelBuilder.AggCall> newAggCallList = new ArrayList<>(); for (AggregateCall aggCall : oldAggregate.getAggCallList()) { final RexNode filterArg = aggCall.filterArg < 0 ? null : relBuilder.field(Mappings.apply(mapping, aggCall.filterArg)); newAggCallList.add( relBuilder .aggregateCall( aggCall.getAggregation(), relBuilder.fields(Mappings.apply2(mapping, aggCall.getArgList()))) .distinct(aggCall.isDistinct()) .filter(filterArg) .approximate(aggCall.isApproximate()) .sort(relBuilder.fields(aggCall.collation)) .as(aggCall.name)); } return newAggCallList; }
Example #6
Source File: FlinkAggregateRemoveRule.java From flink with Apache License 2.0 | 6 votes |
public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final RelNode input = call.rel(1); // Distinct is "GROUP BY c1, c2" (where c1, c2 are a set of columns on // which the input is unique, i.e. contain a key) and has no aggregate // functions or the functions we enumerated. It can be removed. final RelNode newInput = convert(input, aggregate.getTraitSet().simplify()); // If aggregate was projecting a subset of columns, add a project for the // same effect. final RelBuilder relBuilder = call.builder(); relBuilder.push(newInput); List<Integer> projectIndices = new ArrayList<>(aggregate.getGroupSet().asList()); for (AggregateCall aggCall : aggregate.getAggCallList()) { projectIndices.addAll(aggCall.getArgList()); } relBuilder.project(relBuilder.fields(projectIndices)); // Create a project if some of the columns have become // NOT NULL due to aggregate functions are removed relBuilder.convert(aggregate.getRowType(), true); call.transformTo(relBuilder.build()); }
Example #7
Source File: AggregateJoinTransposeRule.java From calcite with Apache License 2.0 | 6 votes |
private static boolean isAggregateSupported(Aggregate aggregate, boolean allowFunctions) { if (!allowFunctions && !aggregate.getAggCallList().isEmpty()) { return false; } if (aggregate.getGroupType() != Aggregate.Group.SIMPLE) { return false; } // If any aggregate functions do not support splitting, bail out // If any aggregate call has a filter or is distinct, bail out for (AggregateCall aggregateCall : aggregate.getAggCallList()) { if (aggregateCall.getAggregation().unwrap(SqlSplittableAggFunction.class) == null) { return false; } if (aggregateCall.filterArg >= 0 || aggregateCall.isDistinct()) { return false; } } return true; }
Example #8
Source File: AggregateExtractProjectRule.java From Bats with Apache License 2.0 | 5 votes |
/** * Creates an AggregateExtractProjectRule. * * @param relBuilderFactory Builder for relational expressions */ public AggregateExtractProjectRule( Class<? extends Aggregate> aggregateClass, Class<? extends RelNode> inputClass, RelBuilderFactory relBuilderFactory) { // Predicate prevents matching against an Aggregate whose input // is already a Project. Prevents this rule firing repeatedly. this( operand(aggregateClass, operandJ(inputClass, null, r -> !(r instanceof Project), any())), relBuilderFactory); }
Example #9
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 #10
Source File: CountToDirectScanUtils.java From Bats with Apache License 2.0 | 5 votes |
/** * For each aggregate call creates field based on its name with bigint type. * Constructs record type for created fields. * * @param aggregateRel aggregate relation expression * @param fieldNames field names * @return record type */ public static RelDataType constructDataType(Aggregate aggregateRel, Collection<String> fieldNames) { List<RelDataTypeField> fields = new ArrayList<>(); int fieldIndex = 0; for (String name : fieldNames) { RelDataTypeField field = new RelDataTypeFieldImpl( name, fieldIndex++, aggregateRel.getCluster().getTypeFactory().createSqlType(SqlTypeName.BIGINT)); fields.add(field); } return new RelRecordType(fields); }
Example #11
Source File: AggregateValuesRule.java From Bats with Apache License 2.0 | 5 votes |
@Override public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final Values values = call.rel(1); Util.discard(values); final RelBuilder relBuilder = call.builder(); final RexBuilder rexBuilder = relBuilder.getRexBuilder(); final List<RexLiteral> literals = new ArrayList<>(); for (final AggregateCall aggregateCall : aggregate.getAggCallList()) { switch (aggregateCall.getAggregation().getKind()) { case COUNT: case SUM0: literals.add((RexLiteral) rexBuilder.makeLiteral( BigDecimal.ZERO, aggregateCall.getType(), false)); break; case MIN: case MAX: case SUM: literals.add((RexLiteral) rexBuilder.makeCast( aggregateCall.getType(), rexBuilder.constantNull())); break; default: // Unknown what this aggregate call should do on empty Values. Bail out to be safe. return; } } call.transformTo( relBuilder.values(ImmutableList.of(literals), aggregate.getRowType()) .build()); // New plan is absolutely better than old plan. call.getPlanner().setImportance(aggregate, 0.0); }
Example #12
Source File: AggregateValuesRule.java From Bats with Apache License 2.0 | 5 votes |
/** * Creates an AggregateValuesRule. * * @param relBuilderFactory Builder for relational expressions */ public AggregateValuesRule(RelBuilderFactory relBuilderFactory) { super( operandJ(Aggregate.class, null, aggregate -> aggregate.getGroupCount() == 0, operandJ(Values.class, null, values -> values.getTuples().isEmpty(), none())), relBuilderFactory, null); }
Example #13
Source File: ExtendedAggregateExtractProjectRule.java From flink with Apache License 2.0 | 5 votes |
/** * Extract a project from the input table aggregate and return a new table aggregate. */ private RelNode performExtractForTableAggregate(TableAggregate aggregate, RelNode input, RelBuilder relBuilder) { RelNode newAggregate = performExtractForAggregate(aggregate.getCorrespondingAggregate(), input, relBuilder); if (aggregate instanceof LogicalTableAggregate) { return LogicalTableAggregate.create((Aggregate) newAggregate); } else { return LogicalWindowTableAggregate.create((LogicalWindowAggregate) newAggregate); } }
Example #14
Source File: RelToSqlConverter.java From calcite with Apache License 2.0 | 5 votes |
/** * Gets the {@link org.apache.calcite.rel.rel2sql.SqlImplementor.Builder} for * the given {@link Aggregate} node. * * @param e Aggregate node * @param inputResult Result from the input * @param inputIsProject Whether the input is a Project * @return A SQL builder */ protected Builder getAggregateBuilder(Aggregate e, Result inputResult, boolean inputIsProject) { if (inputIsProject) { final Builder builder = inputResult.builder(e); builder.clauses.add(Clause.GROUP_BY); return builder; } else { return inputResult.builder(e, Clause.GROUP_BY); } }
Example #15
Source File: AggregateUnionAggregateRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final Aggregate topAggRel = call.rel(0); final Union union = call.rel(1); // If distincts haven't been removed yet, defer invoking this rule if (!union.all) { return; } final RelBuilder relBuilder = call.builder(); final Aggregate bottomAggRel; if (call.rel(3) instanceof Aggregate) { // Aggregate is the second input bottomAggRel = call.rel(3); relBuilder.push(call.rel(2)) .push(call.rel(3).getInput(0)); } else if (call.rel(2) instanceof Aggregate) { // Aggregate is the first input bottomAggRel = call.rel(2); relBuilder.push(call.rel(2).getInput(0)) .push(call.rel(3)); } else { return; } // Only pull up aggregates if they are there just to remove distincts if (!topAggRel.getAggCallList().isEmpty() || !bottomAggRel.getAggCallList().isEmpty()) { return; } relBuilder.union(true); relBuilder.aggregate(relBuilder.groupKey(topAggRel.getGroupSet()), topAggRel.getAggCallList()); call.transformTo(relBuilder.build()); }
Example #16
Source File: ExtendedAggregateExtractProjectRule.java From flink with Apache License 2.0 | 5 votes |
/** * Extract a project from the input table aggregate and return a new table aggregate. */ private RelNode performExtractForTableAggregate(TableAggregate aggregate, RelNode input, RelBuilder relBuilder) { RelNode newAggregate = performExtractForAggregate(aggregate.getCorrespondingAggregate(), input, relBuilder); if (aggregate instanceof LogicalTableAggregate) { return LogicalTableAggregate.create((Aggregate) newAggregate); } else { return LogicalWindowTableAggregate.create((LogicalWindowAggregate) newAggregate); } }
Example #17
Source File: MaterializedViewProjectAggregateRule.java From calcite with Apache License 2.0 | 5 votes |
public MaterializedViewProjectAggregateRule(RelBuilderFactory relBuilderFactory, boolean generateUnionRewriting, HepProgram unionRewritingPullProgram) { super( operand(Project.class, operand(Aggregate.class, any())), relBuilderFactory, "MaterializedViewAggregateRule(Project-Aggregate)", generateUnionRewriting, unionRewritingPullProgram); }
Example #18
Source File: RelMdProjectableAggregate.java From dremio-oss with Apache License 2.0 | 5 votes |
public Double getMaxRowCount(Aggregate rel, RelMetadataQuery mq) { if (ProjectableSqlAggFunctions.isProjectableAggregate(rel)) { return mq.getMaxRowCount(rel); } // try the next handler return null; }
Example #19
Source File: AggregateJoinTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass, RelFactories.AggregateFactory aggregateFactory, Class<? extends Join> joinClass, RelFactories.JoinFactory joinFactory, RelFactories.ProjectFactory projectFactory, boolean allowFunctions) { this(aggregateClass, joinClass, RelBuilder.proto(aggregateFactory, joinFactory, projectFactory), allowFunctions); }
Example #20
Source File: AggregateJoinTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass, RelFactories.AggregateFactory aggregateFactory, Class<? extends Join> joinClass, RelFactories.JoinFactory joinFactory, boolean allowFunctions) { this(aggregateClass, joinClass, RelBuilder.proto(aggregateFactory, joinFactory), allowFunctions); }
Example #21
Source File: AggregateJoinTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
@Deprecated // to be removed before 2.0 public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass, RelFactories.AggregateFactory aggregateFactory, Class<? extends Join> joinClass, RelFactories.JoinFactory joinFactory) { this(aggregateClass, joinClass, RelBuilder.proto(aggregateFactory, joinFactory), false); }
Example #22
Source File: AggregateJoinTransposeRule.java From Bats with Apache License 2.0 | 5 votes |
/** Creates an AggregateJoinTransposeRule. */ public AggregateJoinTransposeRule(Class<? extends Aggregate> aggregateClass, Class<? extends Join> joinClass, RelBuilderFactory relBuilderFactory, boolean allowFunctions) { super( operandJ(aggregateClass, null, agg -> isAggregateSupported(agg, allowFunctions), operandJ(joinClass, null, join -> join.getJoinType() == JoinRelType.INNER, any())), relBuilderFactory, null); this.allowFunctions = allowFunctions; }
Example #23
Source File: AggregateProjectMergeRule.java From Bats with Apache License 2.0 | 5 votes |
public void onMatch(RelOptRuleCall call) { final Aggregate aggregate = call.rel(0); final Project project = call.rel(1); RelNode x = apply(call, aggregate, project); if (x != null) { call.transformTo(x); } }
Example #24
Source File: RelBuilder.java From calcite with Apache License 2.0 | 5 votes |
/** @deprecated Now that indicator is deprecated, use * {@link #groupKey(Iterable, Iterable)}, which has the same behavior as * calling this method with {@code indicator = false}. */ @Deprecated // to be removed before 2.0 public GroupKey groupKey(Iterable<? extends RexNode> nodes, boolean indicator, Iterable<? extends Iterable<? extends RexNode>> nodeLists) { Aggregate.checkIndicator(indicator); return groupKey_(nodes, nodeLists); }
Example #25
Source File: ElasticsearchAggregate.java From calcite with Apache License 2.0 | 5 votes |
@Override public Aggregate copy(RelTraitSet traitSet, RelNode input, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) { try { return new ElasticsearchAggregate(getCluster(), traitSet, input, groupSet, groupSets, aggCalls); } catch (InvalidRelException e) { throw new AssertionError(e); } }
Example #26
Source File: RelMdSize.java From calcite with Apache License 2.0 | 5 votes |
public List<Double> averageColumnSizes(Aggregate rel, RelMetadataQuery mq) { final List<Double> inputColumnSizes = mq.getAverageColumnSizesNotNull(rel.getInput()); final ImmutableList.Builder<Double> list = ImmutableList.builder(); for (int key : rel.getGroupSet()) { list.add(inputColumnSizes.get(key)); } for (AggregateCall aggregateCall : rel.getAggCallList()) { list.add(averageTypeValueSize(aggregateCall.type)); } return list.build(); }
Example #27
Source File: StreamRules.java From calcite with Apache License 2.0 | 5 votes |
/** * Creates a DeltaAggregateTransposeRule. * * @param relBuilderFactory Builder for relational expressions */ public DeltaAggregateTransposeRule(RelBuilderFactory relBuilderFactory) { super( operand(Delta.class, operandJ(Aggregate.class, null, Aggregate::isSimple, any())), relBuilderFactory, null); }
Example #28
Source File: FlinkAggregateExpandDistinctAggregatesRule.java From flink with Apache License 2.0 | 5 votes |
/** * Converts an aggregate relational expression that contains just one * distinct aggregate function (or perhaps several over the same arguments) * and no non-distinct aggregate functions. */ private RelBuilder convertMonopole(RelBuilder relBuilder, Aggregate aggregate, List<Integer> argList, int filterArg) { // For example, // SELECT deptno, COUNT(DISTINCT sal), SUM(DISTINCT sal) // FROM emp // GROUP BY deptno // // becomes // // SELECT deptno, COUNT(distinct_sal), SUM(distinct_sal) // FROM ( // SELECT DISTINCT deptno, sal AS distinct_sal // FROM EMP GROUP BY deptno) // GROUP BY deptno // Project the columns of the GROUP BY plus the arguments // to the agg function. final Map<Integer, Integer> sourceOf = new HashMap<>(); createSelectDistinct(relBuilder, aggregate, argList, filterArg, sourceOf); // Create an aggregate on top, with the new aggregate list. final List<AggregateCall> newAggCalls = com.google.common.collect.Lists.newArrayList(aggregate.getAggCallList()); rewriteAggCalls(newAggCalls, argList, sourceOf); final int cardinality = aggregate.getGroupSet().cardinality(); relBuilder.push( aggregate.copy(aggregate.getTraitSet(), relBuilder.build(), ImmutableBitSet.range(cardinality), null, newAggCalls)); return relBuilder; }
Example #29
Source File: HashAggPrel.java From dremio-oss with Apache License 2.0 | 5 votes |
@Override public Aggregate copy(RelTraitSet traitSet, RelNode input, boolean indicator, ImmutableBitSet groupSet, List<ImmutableBitSet> groupSets, List<AggregateCall> aggCalls) { try { return HashAggPrel.create(getCluster(), traitSet, input, indicator, groupSet, groupSets, aggCalls, this.getOperatorPhase()); } catch (InvalidRelException e) { throw new AssertionError(e); } }
Example #30
Source File: ExtendedAggregateExtractProjectRule.java From flink with Apache License 2.0 | 5 votes |
private RelNode getNewAggregate(Aggregate oldAggregate, RelBuilder relBuilder, Mapping mapping) { final ImmutableBitSet newGroupSet = Mappings.apply(mapping, oldAggregate.getGroupSet()); final Iterable<ImmutableBitSet> newGroupSets = oldAggregate.getGroupSets().stream() .map(bitSet -> Mappings.apply(mapping, bitSet)) .collect(Collectors.toList()); final List<RelBuilder.AggCall> newAggCallList = getNewAggCallList(oldAggregate, relBuilder, mapping); final RelBuilder.GroupKey groupKey = relBuilder.groupKey(newGroupSet, newGroupSets); if (oldAggregate instanceof LogicalWindowAggregate) { if (newGroupSet.size() == 0 && newAggCallList.size() == 0) { // Return the old LogicalWindowAggregate directly, as we can't get an empty Aggregate // from the relBuilder. return oldAggregate; } else { relBuilder.aggregate(groupKey, newAggCallList); Aggregate newAggregate = (Aggregate) relBuilder.build(); LogicalWindowAggregate oldLogicalWindowAggregate = (LogicalWindowAggregate) oldAggregate; return LogicalWindowAggregate.create( oldLogicalWindowAggregate.getWindow(), oldLogicalWindowAggregate.getNamedProperties(), newAggregate); } } else { relBuilder.aggregate(groupKey, newAggCallList); return relBuilder.build(); } }