com.hazelcast.org.apache.calcite.rel.rules.FilterCorrelateRule 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 compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.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.RelOptRuleCall;
import com.hazelcast.org.apache.calcite.plan.RelOptUtil;
import com.hazelcast.org.apache.calcite.plan.RelRule;
import com.hazelcast.org.apache.calcite.rel.RelNode;
import com.hazelcast.org.apache.calcite.rel.core.Correlate;
import com.hazelcast.org.apache.calcite.rel.core.Filter;
import com.hazelcast.org.apache.calcite.rel.core.RelFactories;
import com.hazelcast.org.apache.calcite.rex.RexBuilder;
import com.hazelcast.org.apache.calcite.rex.RexNode;
import com.hazelcast.org.apache.calcite.rex.RexUtil;
import com.hazelcast.org.apache.calcite.tools.RelBuilder;
import com.hazelcast.org.apache.calcite.tools.RelBuilderFactory;
import com.hazelcast.com.google.common.collect.ImmutableList;
import org.immutables.value.Value;
import java.util.ArrayList;
import java.util.List;
/**
* Planner rule that pushes a {@link Filter} above a {@link Correlate} into the
* inputs of the Correlate.
*
* @see CoreRules#FILTER_CORRELATE
*/
@Value.Enclosing
public class FilterCorrelateRule
extends RelRule
implements TransformationRule {
/** Creates a FilterCorrelateRule. */
protected FilterCorrelateRule(Config config) {
super(config);
}
@Deprecated // to be removed before 2.0
public FilterCorrelateRule(RelBuilderFactory relBuilderFactory) {
this(Config.DEFAULT.withRelBuilderFactory(relBuilderFactory)
.as(Config.class));
}
@Deprecated // to be removed before 2.0
public FilterCorrelateRule(RelFactories.FilterFactory filterFactory,
RelFactories.ProjectFactory projectFactory) {
this(RelBuilder.proto(filterFactory, projectFactory));
}
//~ Methods ----------------------------------------------------------------
@Override public void onMatch(RelOptRuleCall call) {
final Filter filter = call.rel(0);
final Correlate corr = call.rel(1);
final List aboveFilters =
RelOptUtil.conjunctions(filter.getCondition());
final List leftFilters = new ArrayList<>();
final List rightFilters = new ArrayList<>();
// Try to push down above filters. These are typically where clause
// filters. They can be pushed down if they are not on the NULL
// generating side.
RelOptUtil.classifyFilters(
corr,
aboveFilters,
false,
true,
corr.getJoinType().canPushRightFromAbove(),
aboveFilters,
leftFilters,
rightFilters);
if (leftFilters.isEmpty()
&& rightFilters.isEmpty()) {
// no filters got pushed
return;
}
// Create Filters on top of the children if any filters were
// pushed to them.
final RexBuilder rexBuilder = corr.getCluster().getRexBuilder();
final RelBuilder relBuilder = call.builder();
final RelNode leftRel =
relBuilder.push(corr.getLeft()).filter(leftFilters).build();
final RelNode rightRel =
relBuilder.push(corr.getRight()).filter(rightFilters).build();
// Create the new Correlate
RelNode newCorrRel =
corr.copy(corr.getTraitSet(), ImmutableList.of(leftRel, rightRel));
call.getPlanner().onCopy(corr, newCorrRel);
if (!leftFilters.isEmpty()) {
call.getPlanner().onCopy(filter, leftRel);
}
if (!rightFilters.isEmpty()) {
call.getPlanner().onCopy(filter, rightRel);
}
// Create a Filter on top of the join if needed
relBuilder.push(newCorrRel);
relBuilder.filter(
RexUtil.fixUp(rexBuilder, aboveFilters,
RelOptUtil.getFieldTypeList(relBuilder.peek().getRowType())));
call.transformTo(relBuilder.build());
}
/** Rule configuration. */
@Value.Immutable
public interface Config extends RelRule.Config {
Config DEFAULT = ImmutableFilterCorrelateRule.Config.of()
.withOperandFor(Filter.class, Correlate.class);
@Override default FilterCorrelateRule toRule() {
return new FilterCorrelateRule(this);
}
/** Defines an operand tree for the given classes. */
default Config withOperandFor(Class filterClass,
Class correlateClass) {
return withOperandSupplier(b0 ->
b0.operand(filterClass).oneInput(b1 ->
b1.operand(correlateClass).anyInputs()))
.as(Config.class);
}
}
}