forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[opt](nereids) infer in-predicate from or-predicate (apache#46468)
### What problem does this PR solve? previous verion has follow drawbacks 1. inferred some in-predicates, which cannot be pushed down to storage layer 2. it is easy to lead dead loop, because other expression rewrite rule may remove its flag in expression's mutableState in order to solve above issues, we implemented a new version first, it is a plan node level rule to avoid to be applied to the same expression repeatedly second, we define replace mode and extract mode. if in replace mode, the original expression should be equivalent to the inferred in-pred, which is used for all plan node's expressions except filter. for example, orig = "(a=1 and b=1) or (a=2 and c=2)" is equivalent to "a in (1, 2) and (a=1 and b=1) or (a=2 and c=2)". orig is a filter condition, "a in (1, 2)" can be pushed down to storage layer, and this infer is useful. But if this is orig is an other join condition, this inferrence is useless. So in extract mode, "a in (1, 2)" is inferred, but in replace mode, it is not.
- Loading branch information
Showing
25 changed files
with
229 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/InferInPredicateFromOr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 org.apache.doris.nereids.rules.rewrite; | ||
|
||
import org.apache.doris.nereids.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.rules.expression.rules.OrToIn; | ||
import org.apache.doris.nereids.trees.expressions.And; | ||
import org.apache.doris.nereids.trees.expressions.Expression; | ||
import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
import org.apache.doris.nereids.util.ExpressionUtils; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.Lists; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* infer In-predicate from Or | ||
* | ||
*/ | ||
public class InferInPredicateFromOr implements RewriteRuleFactory { | ||
|
||
@Override | ||
public List<Rule> buildRules() { | ||
return ImmutableList.of( | ||
logicalFilter().then(this::rewriteFilterExpression).toRule(RuleType.EXTRACT_IN_PREDICATE_FROM_OR), | ||
logicalProject().then(this::rewriteProject).toRule(RuleType.EXTRACT_IN_PREDICATE_FROM_OR), | ||
logicalJoin().whenNot(LogicalJoin::isMarkJoin) | ||
.then(this::rewriteJoin).toRule(RuleType.EXTRACT_IN_PREDICATE_FROM_OR) | ||
); | ||
} | ||
|
||
private LogicalFilter<Plan> rewriteFilterExpression(LogicalFilter<Plan> filter) { | ||
Expression rewritten = OrToIn.EXTRACT_MODE_INSTANCE.rewriteTree(filter.getPredicate()); | ||
Set<Expression> set = new LinkedHashSet<>(ExpressionUtils.extractConjunction(rewritten)); | ||
return filter.withConjuncts(set); | ||
} | ||
|
||
private LogicalProject<Plan> rewriteProject(LogicalProject<Plan> project) { | ||
List<NamedExpression> newProjections = Lists.newArrayList(); | ||
for (NamedExpression proj : project.getProjects()) { | ||
if (proj instanceof SlotReference) { | ||
newProjections.add(proj); | ||
} else { | ||
Expression rewritten = OrToIn.REPLACE_MODE_INSTANCE.rewriteTree(proj); | ||
newProjections.add((NamedExpression) rewritten); | ||
} | ||
} | ||
return project.withProjects(newProjections); | ||
} | ||
|
||
private LogicalJoin<Plan, Plan> rewriteJoin(LogicalJoin<Plan, Plan> join) { | ||
if (!join.isMarkJoin()) { | ||
Expression otherCondition; | ||
if (join.getOtherJoinConjuncts().isEmpty()) { | ||
return join; | ||
} else if (join.getOtherJoinConjuncts().size() == 1) { | ||
otherCondition = join.getOtherJoinConjuncts().get(0); | ||
} else { | ||
otherCondition = new And(join.getOtherJoinConjuncts()); | ||
} | ||
Expression rewritten = OrToIn.REPLACE_MODE_INSTANCE.rewriteTree(otherCondition); | ||
join = join.withJoinConjuncts(join.getHashJoinConjuncts(), ExpressionUtils.extractConjunction(rewritten), | ||
join.getJoinReorderContext()); | ||
} | ||
return join; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.