@Value.Enclosing public class AggregateExpandWithinDistinctRule extends RelRule<AggregateExpandWithinDistinctRule.Config>
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 SETs.
| Modifier and Type | Class and Description |
|---|---|
static interface |
AggregateExpandWithinDistinctRule.Config
Rule configuration.
|
RelRule.Done, RelRule.MatchHandler<R extends RelOptRule>, RelRule.OperandBuilder, RelRule.OperandDetailBuilder<R extends RelNode>, RelRule.OperandTransformRelOptRule.ConverterRelOptRuleOperanddescription, operands, relBuilderFactory| Modifier | Constructor and Description |
|---|---|
protected |
AggregateExpandWithinDistinctRule(AggregateExpandWithinDistinctRule.Config config)
Creates an AggregateExpandWithinDistinctRule.
|
| Modifier and Type | Method and Description |
|---|---|
void |
onMatch(RelOptRuleCall call)
Receives notification about a rule match.
|
any, convert, convert, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, none, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unorderedprotected AggregateExpandWithinDistinctRule(AggregateExpandWithinDistinctRule.Config config)
public void onMatch(RelOptRuleCall call)
RelOptRulecall.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.
onMatch in class RelOptRulecall - Rule callRelOptRule.matches(RelOptRuleCall)Copyright © 2012-2022 Apache Software Foundation. All Rights Reserved.