Class AggregateExpandDistinctAggregatesRule
- java.lang.Object
-
- org.apache.calcite.plan.RelOptRule
-
- org.apache.calcite.rel.rules.AggregateExpandDistinctAggregatesRule
-
public final class AggregateExpandDistinctAggregatesRule extends RelOptRule
Planner rule that expands distinct aggregates (such asCOUNT(DISTINCT x)
) from aAggregate
.How this is done depends upon the arguments to the function. If all functions have the same argument (e.g.
COUNT(DISTINCT x), SUM(DISTINCT x)
both have the argumentx
) then one extraAggregate
is sufficient.If there are multiple arguments (e.g.
COUNT(DISTINCT x), COUNT(DISTINCT y)
) the rule creates separateAggregate
s and combines using aJoin
.
-
-
Field Summary
Fields Modifier and Type Field Description static AggregateExpandDistinctAggregatesRule
INSTANCE
The default instance of the rule; operates only on logical expressions.static AggregateExpandDistinctAggregatesRule
JOIN
Instance of the rule that operates only on logical expressions and generates a join.boolean
useGroupingSets
-
Fields inherited from class org.apache.calcite.plan.RelOptRule
description, operands, relBuilderFactory
-
-
Constructor Summary
Constructors Constructor Description AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends Aggregate> clazz, boolean useGroupingSets, RelBuilderFactory relBuilderFactory)
AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends LogicalAggregate> clazz, boolean useGroupingSets, RelFactories.JoinFactory joinFactory)
Deprecated.AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends LogicalAggregate> clazz, RelFactories.JoinFactory joinFactory)
Deprecated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private RelBuilder
convertMonopole(RelBuilder relBuilder, Aggregate aggregate, java.util.List<java.lang.Integer> argList, int filterArg)
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
convertSingletonDistinct(RelBuilder relBuilder, Aggregate aggregate, java.util.Set<Pair<java.util.List<java.lang.Integer>,java.lang.Integer>> argLists)
Converts an aggregate with one distinct aggregate and one or more non-distinct aggregates to multi-phase aggregates (see reference example below).private RelBuilder
createSelectDistinct(RelBuilder relBuilder, Aggregate aggregate, java.util.List<java.lang.Integer> argList, int filterArg, java.util.Map<java.lang.Integer,java.lang.Integer> sourceOf)
Given anAggregate
and the ordinals of the arguments to a particular call to an aggregate function, creates a 'select distinct' relational expression which projects the group columns and those arguments but nothing else.private void
doRewrite(RelBuilder relBuilder, Aggregate aggregate, int n, java.util.List<java.lang.Integer> argList, int filterArg, java.util.List<RexInputRef> refs)
Converts all distinct aggregate calls to a given set of arguments.private static long
groupValue(ImmutableBitSet fullGroupSet, ImmutableBitSet groupSet)
void
onMatch(RelOptRuleCall call)
Receives notification about a rule match.private static int
remap(ImmutableBitSet groupSet, int arg)
private static com.google.common.collect.ImmutableList<ImmutableBitSet>
remap(ImmutableBitSet groupSet, java.lang.Iterable<ImmutableBitSet> bitSets)
private static java.util.List<java.lang.Integer>
remap(ImmutableBitSet groupSet, java.util.List<java.lang.Integer> argList)
private static ImmutableBitSet
remap(ImmutableBitSet groupSet, ImmutableBitSet bitSet)
private static void
rewriteAggCalls(java.util.List<AggregateCall> newAggCalls, java.util.List<java.lang.Integer> argList, java.util.Map<java.lang.Integer,java.lang.Integer> sourceOf)
private void
rewriteUsingGroupingSets(RelOptRuleCall call, Aggregate aggregate)
-
Methods inherited from class org.apache.calcite.plan.RelOptRule
any, convert, convert, convertList, convertOperand, convertOperand, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, none, operand, operand, operand, operand, operand, operandJ, operandJ, some, toString, unordered
-
-
-
-
Field Detail
-
INSTANCE
public static final AggregateExpandDistinctAggregatesRule INSTANCE
The default instance of the rule; operates only on logical expressions.
-
JOIN
public static final AggregateExpandDistinctAggregatesRule JOIN
Instance of the rule that operates only on logical expressions and generates a join.
-
useGroupingSets
public final boolean useGroupingSets
-
-
Constructor Detail
-
AggregateExpandDistinctAggregatesRule
public AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends Aggregate> clazz, boolean useGroupingSets, RelBuilderFactory relBuilderFactory)
-
AggregateExpandDistinctAggregatesRule
@Deprecated public AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends LogicalAggregate> clazz, boolean useGroupingSets, RelFactories.JoinFactory joinFactory)
Deprecated.
-
AggregateExpandDistinctAggregatesRule
@Deprecated public AggregateExpandDistinctAggregatesRule(java.lang.Class<? extends LogicalAggregate> clazz, RelFactories.JoinFactory joinFactory)
Deprecated.
-
-
Method Detail
-
onMatch
public void onMatch(RelOptRuleCall call)
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>)
to register the expression.- Specified by:
onMatch
in classRelOptRule
- Parameters:
call
- Rule call- See Also:
RelOptRule.matches(RelOptRuleCall)
-
convertSingletonDistinct
private RelBuilder convertSingletonDistinct(RelBuilder relBuilder, Aggregate aggregate, java.util.Set<Pair<java.util.List<java.lang.Integer>,java.lang.Integer>> argLists)
Converts an aggregate with one distinct aggregate and one or more non-distinct aggregates to multi-phase aggregates (see reference example below).- Parameters:
relBuilder
- Contains the input relational expressionaggregate
- Original aggregateargLists
- Arguments and filters to the distinct aggregate function
-
rewriteUsingGroupingSets
private void rewriteUsingGroupingSets(RelOptRuleCall call, Aggregate aggregate)
-
groupValue
private static long groupValue(ImmutableBitSet fullGroupSet, ImmutableBitSet groupSet)
-
remap
private static ImmutableBitSet remap(ImmutableBitSet groupSet, ImmutableBitSet bitSet)
-
remap
private static com.google.common.collect.ImmutableList<ImmutableBitSet> remap(ImmutableBitSet groupSet, java.lang.Iterable<ImmutableBitSet> bitSets)
-
remap
private static java.util.List<java.lang.Integer> remap(ImmutableBitSet groupSet, java.util.List<java.lang.Integer> argList)
-
remap
private static int remap(ImmutableBitSet groupSet, int arg)
-
convertMonopole
private RelBuilder convertMonopole(RelBuilder relBuilder, Aggregate aggregate, java.util.List<java.lang.Integer> argList, int filterArg)
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.
-
doRewrite
private void doRewrite(RelBuilder relBuilder, Aggregate aggregate, int n, java.util.List<java.lang.Integer> argList, int filterArg, java.util.List<RexInputRef> refs)
Converts all distinct aggregate calls to a given set of arguments.This method is called several times, one for each set of arguments. Each time it is called, it generates a JOIN to a new SELECT DISTINCT relational expression, and modifies the set of top-level calls.
- Parameters:
aggregate
- Original aggregaten
- Ordinal of this in a join.relBuilder
contains the input relational expression (either the original aggregate, the output from the previous call to this method.n
is 0 if we're converting the first distinct aggregate in a query with no non-distinct aggregates)argList
- Arguments to the distinct aggregate functionfilterArg
- Argument that filters input to aggregate function, or -1refs
- Array of expressions which will be the projected by the result of this rule. Those relating to this arg list will be modified @return Relational expression
-
rewriteAggCalls
private static void rewriteAggCalls(java.util.List<AggregateCall> newAggCalls, java.util.List<java.lang.Integer> argList, java.util.Map<java.lang.Integer,java.lang.Integer> sourceOf)
-
createSelectDistinct
private RelBuilder createSelectDistinct(RelBuilder relBuilder, Aggregate aggregate, java.util.List<java.lang.Integer> argList, int filterArg, java.util.Map<java.lang.Integer,java.lang.Integer> sourceOf)
Given anAggregate
and the ordinals of the arguments to a particular call to an aggregate function, creates a 'select distinct' relational expression which projects the group columns and those arguments but nothing else.For example, given
select f0, count(distinct f1), count(distinct f2) from t group by f0
and the argument list
{2}
returns
select distinct f0, f2 from t
The
sourceOf
map is populated with the source of each column; in this case sourceOf.get(0) = 0, and sourceOf.get(1) = 2.- Parameters:
relBuilder
- Relational expression builderaggregate
- Aggregate relational expressionargList
- Ordinals of columns to make distinctfilterArg
- Ordinal of column to filter on, or -1sourceOf
- Out parameter, is populated with a map of where each output field came from- Returns:
- Aggregate relational expression which projects the required columns
-
-