operand(FilterRelBase.class, any())));
}
public void onMatch(RelOptRuleCall call) {
final AggregateRelBase aggregate = call.rel(0);
final FilterRelBase filter = call.rel(1);
// Do the columns used by the filter appear in the output of the aggregate?
final BitSet filterColumns =
RelOptUtil.InputFinder.bits(filter.getCondition());
final BitSet newGroupSet =
BitSets.union(aggregate.getGroupSet(), filterColumns);
final RelNode input = filter.getChild();
final Boolean unique =
RelMetadataQuery.areColumnsUnique(input, newGroupSet);
if (unique != null && unique) {
// The input is already unique on the grouping columns, so there's little
// advantage of aggregating again. More important, without this check,
// the rule fires forever: A-F => A-F-A => A-A-F-A => A-A-A-F-A => ...
return;
}
final AggregateRelBase newAggregate =
aggregate.copy(aggregate.getTraitSet(), input, newGroupSet,
aggregate.getAggCallList());
final Mappings.TargetMapping mapping = Mappings.target(
new Function<Integer, Integer>() {
public Integer apply(Integer a0) {
return BitSets.toList(newGroupSet).indexOf(a0);
}
},
input.getRowType().getFieldCount(),
newGroupSet.cardinality());
final RexNode newCondition =
RexUtil.apply(mapping, filter.getCondition());
final FilterRelBase newFilter = filter.copy(filter.getTraitSet(),
newAggregate, newCondition);
if (BitSets.contains(aggregate.getGroupSet(), filterColumns)) {
// Everything needed by the filter is returned by the aggregate.
assert newGroupSet.equals(aggregate.getGroupSet());
call.transformTo(newFilter);