org.eigenbase.rel.rules
Class RemoveDistinctAggregateRule

java.lang.Object
  extended by org.eigenbase.relopt.RelOptRule
      extended by org.eigenbase.rel.rules.RemoveDistinctAggregateRule

public final class RemoveDistinctAggregateRule
extends RelOptRule

Rule to remove distinct aggregates from a AggregateRel.

Since:
3 February, 2006
Version:
$Id: //open/dev/farrago/src/org/eigenbase/rel/rules/RemoveDistinctAggregateRule.java#2 $
Author:
jhyde

Field Summary
static RemoveDistinctAggregateRule instance
          The singleton.
 
Fields inherited from class org.eigenbase.relopt.RelOptRule
ANY, description, operands
 
Constructor Summary
private RemoveDistinctAggregateRule()
           
 
Method Summary
private  RelNode convertMonopole(AggregateRel aggregate, List<Integer> argList)
          Converts an aggregrate relational expression which contains just one distinct aggregate function (or perhaps several over the same arguments) and no non-distinct aggregate functions.
private static AggregateRel createSelectDistinct(AggregateRel aggregate, List<Integer> argList, Map<Integer,Integer> sourceOf)
          Given an AggregateRel 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  RelNode doRewrite(AggregateRel aggregate, RelNode left, List<Integer> argList, RexInputRef[] refs)
          Converts all distinct aggregate calls to a given set of arguments.
private static boolean equals(int[] args, List<Integer> argList)
          Returns whether an integer array has the same content as an integer list.
 void onMatch(RelOptRuleCall call)
          Receives notification about a rule match.
private static void rewriteAggCalls(List<AggregateCall> newAggCalls, List<Integer> argList, Map<Integer,Integer> sourceOf)
           
 
Methods inherited from class org.eigenbase.relopt.RelOptRule
convert, equals, equals, getOperand, getOperands, getOutConvention, getOutTrait, hashCode, matches, mergeTraitsAndConvert, mergeTraitsAndConvert, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

instance

public static final RemoveDistinctAggregateRule instance
The singleton.

Constructor Detail

RemoveDistinctAggregateRule

private RemoveDistinctAggregateRule()
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.eigenbase.rel.RelNode) to register the expression.

Specified by:
onMatch in class RelOptRule
Parameters:
call - Rule call
See Also:
RelOptRule.matches(RelOptRuleCall)

convertMonopole

private RelNode convertMonopole(AggregateRel aggregate,
                                List<Integer> argList)
Converts an aggregrate relational expression which contains just one distinct aggregate function (or perhaps several over the same arguments) and no non-distinct aggregate functions.


doRewrite

private RelNode doRewrite(AggregateRel aggregate,
                          RelNode left,
                          List<Integer> argList,
                          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 aggregate
left - Child relational expression (either the original aggregate, the output from the previous call to this method, or null in the case where we're converting the first distinct aggregate in a query with no non-distinct aggregates)
argList - Arguments to the distinct aggregate function
refs - Array of expressions which will be the projected by the result of this rule. Those relating to this arg list will be modified
Returns:
Relational expression

rewriteAggCalls

private static void rewriteAggCalls(List<AggregateCall> newAggCalls,
                                    List<Integer> argList,
                                    Map<Integer,Integer> sourceOf)

createSelectDistinct

private static AggregateRel createSelectDistinct(AggregateRel aggregate,
                                                 List<Integer> argList,
                                                 Map<Integer,Integer> sourceOf)
Given an AggregateRel 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 arglist
{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:
aggregate - Aggregate relational expression
argList - Ordinals of columns to distinctify
sourceOf - Out paramer, is populated with a map of where each output field came from
Returns:
Aggregate relational expression which projects the required columns

equals

private static boolean equals(int[] args,
                              List<Integer> argList)
Returns whether an integer array has the same content as an integer list.