com.hazelcast.org.apache.calcite.rel.rules.FilterAggregateTransposeRule Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.org.apache.calcite.rel.rules;
import com.hazelcast.org.apache.calcite.plan.Contexts;
import com.hazelcast.org.apache.calcite.plan.RelOptRule;
import com.hazelcast.org.apache.calcite.plan.RelOptRuleCall;
import com.hazelcast.org.apache.calcite.plan.RelOptRuleOperand;
import com.hazelcast.org.apache.calcite.plan.RelOptUtil;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.Aggregate;
import com.hazelcast.org.apache.calcite.rel.core.Aggregate.Group;
import com.hazelcast.org.apache.calcite.rel.core.Filter;
import com.hazelcast.org.apache.calcite.rel.core.RelFactories;
import com.hazelcast.org.apache.calcite.rel.type.RelDataTypeField;
import com.hazelcast.org.apache.calcite.rex.RexBuilder;
import com.hazelcast.org.apache.calcite.rex.RexNode;
import com.hazelcast.org.apache.calcite.tools.RelBuilder;
import com.hazelcast.org.apache.calcite.tools.RelBuilderFactory;
import com.hazelcast.org.apache.calcite.util.ImmutableBitSet;
import com.hazelcast.com.google.com.hazelcast.com.on.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
/**
* Planner rule that pushes a {@link com.hazelcast.org.apache.calcite.rel.core.Filter}
* past a {@link com.hazelcast.org.apache.calcite.rel.core.Aggregate}.
*
* @see com.hazelcast.org.apache.calcite.rel.rules.AggregateFilterTransposeRule
*/
public class FilterAggregateTransposeRule extends RelOptRule implements TransformationRule {
/** The default instance of
* {@link FilterAggregateTransposeRule}.
*
* It matches any kind of agg. or filter */
public static final FilterAggregateTransposeRule INSTANCE =
new FilterAggregateTransposeRule(Filter.class,
RelFactories.LOGICAL_BUILDER, Aggregate.class);
//~ Constructors -----------------------------------------------------------
/**
* Creates a FilterAggregateTransposeRule.
*
*
If {@code filterFactory} is null, creates the same kind of filter as
* matched in the rule. Similarly {@code aggregateFactory}.
*/
public FilterAggregateTransposeRule(
Class filterClass,
RelBuilderFactory builderFactory,
Class aggregateClass) {
this(
operand(filterClass,
operand(aggregateClass, any())),
builderFactory);
}
protected FilterAggregateTransposeRule(RelOptRuleOperand operand,
RelBuilderFactory builderFactory) {
super(operand, builderFactory, null);
}
@Deprecated // to be removed before 2.0
public FilterAggregateTransposeRule(
Class filterClass,
RelFactories.FilterFactory filterFactory,
Class aggregateClass) {
this(filterClass, RelBuilder.proto(Contexts.of(filterFactory)),
aggregateClass);
}
//~ Methods ----------------------------------------------------------------
public void onMatch(RelOptRuleCall call) {
final Filter filterRel = call.rel(0);
final Aggregate aggRel = call.rel(1);
final List conditions =
RelOptUtil.conjunctions(filterRel.getCondition());
final RexBuilder rexBuilder = filterRel.getCluster().getRexBuilder();
final List origFields =
aggRel.getRowType().getFieldList();
final int[] adjustments = new int[origFields.size()];
int j = 0;
for (int i : aggRel.getGroupSet()) {
adjustments[j] = i - j;
j++;
}
final List pushedConditions = new ArrayList<>();
final List remainingConditions = new ArrayList<>();
for (RexNode condition : conditions) {
ImmutableBitSet rCols = RelOptUtil.InputFinder.bits(condition);
if (canPush(aggRel, rCols)) {
pushedConditions.add(
condition.accept(
new RelOptUtil.RexInputConverter(rexBuilder, origFields,
aggRel.getInput(0).getRowType().getFieldList(),
adjustments)));
} else {
remainingConditions.add(condition);
}
}
final RelBuilder builder = call.builder();
RelNode rel =
builder.push(aggRel.getInput()).filter(pushedConditions).build();
if (rel == aggRel.getInput(0)) {
return;
}
rel = aggRel.copy(aggRel.getTraitSet(), ImmutableList.of(rel));
rel = builder.push(rel).filter(remainingConditions).build();
call.transformTo(rel);
}
private boolean canPush(Aggregate aggregate, ImmutableBitSet rCols) {
// If the filter references columns not in the group key, we cannot push
final ImmutableBitSet groupKeys =
ImmutableBitSet.range(0, aggregate.getGroupSet().cardinality());
if (!groupKeys.contains(rCols)) {
return false;
}
if (aggregate.getGroupType() != Group.SIMPLE) {
// If grouping sets are used, the filter can be pushed if
// the columns referenced in the predicate are present in
// all the grouping sets.
for (ImmutableBitSet groupingSet : aggregate.getGroupSets()) {
if (!groupingSet.contains(rCols)) {
return false;
}
}
}
return true;
}
}