Package org.apache.calcite.rel.rules
Class AggregateExpandWithinDistinctRule
java.lang.Object
org.apache.calcite.plan.RelOptRule
org.apache.calcite.plan.RelRule<AggregateExpandWithinDistinctRule.Config>
org.apache.calcite.rel.rules.AggregateExpandWithinDistinctRule
@Enclosing
public class AggregateExpandWithinDistinctRule
extends RelRule<AggregateExpandWithinDistinctRule.Config>
Planner rule that rewrites an
Aggregate
that contains
WITHIN DISTINCT
aggregate functions.
For example,
SELECT o.paymentType, COUNT(*) as "count", COUNT(*) WITHIN DISTINCT (o.orderId) AS orderCount, SUM(o.shipping) WITHIN DISTINCT (o.orderId) as sumShipping, SUM(i.units) as sumUnits FROM Orders AS o JOIN OrderItems AS i USING (orderId) GROUP BY o.paymentType
becomes
SELECT paymentType, COUNT(*) as "count", COUNT(*) FILTER (WHERE g = 0) AS orderCount, SUM(minShipping) FILTER (WHERE g = 0) AS sumShipping, SUM(sumUnits) FILTER (WHERE g = 1) as sumUnits FROM ( SELECT o.paymentType, GROUPING(o.orderId) AS g, SUM(o.shipping) AS sumShipping, MIN(o.shipping) AS minShipping, SUM(i.units) AS sumUnits FROM Orders AS o JOIN OrderItems ON o.orderId = i.orderId GROUP BY GROUPING SETS ((o.paymentType), (o.paymentType, o.orderId))) GROUP BY o.paymentType
By the way, note that COUNT(*) WITHIN DISTINCT (o.orderId)
is identical to COUNT(DISTINCT o.orderId)
.
WITHIN DISTINCT
is a generalization of aggregate(DISTINCT).
So, it is perhaps not surprising that the rewrite to GROUPING SETS
is similar.
If there are multiple arguments
(e.g. SUM(a) WITHIN DISTINCT (x), SUM(a) WITHIN DISTINCT (y)
)
the rule creates separate GROUPING SET
s.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Rule configuration.Nested classes/interfaces inherited from class org.apache.calcite.plan.RelRule
RelRule.Done, RelRule.MatchHandler<R extends RelOptRule>, RelRule.OperandBuilder, RelRule.OperandDetailBuilder<R extends RelNode>, RelRule.OperandTransform
Nested classes/interfaces inherited from class org.apache.calcite.plan.RelOptRule
RelOptRule.ConverterRelOptRuleOperand
-
Field Summary
Fields inherited from class org.apache.calcite.plan.RelOptRule
description, operands, relBuilderFactory
-
Constructor Summary
ModifierConstructorDescriptionprotected
Creates an AggregateExpandWithinDistinctRule. -
Method Summary
Modifier and TypeMethodDescriptionvoid
onMatch
(RelOptRuleCall call) Receives notification about a rule match.Methods inherited from class org.apache.calcite.plan.RelOptRule
any, convert, convert, convert, convert, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, none, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unordered
-
Constructor Details
-
AggregateExpandWithinDistinctRule
Creates an AggregateExpandWithinDistinctRule.
-
-
Method Details
-
onMatch
Description copied from class:RelOptRule
Receives notification about a rule match. At the time that this method is called,call.rels
holds the set of relational expressions which match the operands to the rule;call.rels[0]
is the root expression.Typically a rule would check that the nodes are valid matches, creates a new expression, then calls back
RelOptRuleCall.transformTo(org.apache.calcite.rel.RelNode, java.util.Map<org.apache.calcite.rel.RelNode, org.apache.calcite.rel.RelNode>, org.apache.calcite.plan.RelHintsPropagator)
to register the expression.- Specified by:
onMatch
in classRelOptRule
- Parameters:
call
- Rule call- See Also:
-