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.
[Feature] (Nereids) support un equals conjuncts in un scalar sub query (
apache#15591) support un equals conjuncts in un scalar sub query. [fix] in correlated subquery wrong result
- Loading branch information
Showing
13 changed files
with
440 additions
and
113 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
97 changes: 97 additions & 0 deletions
97
...java/org/apache/doris/nereids/rules/rewrite/logical/EliminateFilterUnderApplyProject.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,97 @@ | ||
// 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.logical; | ||
|
||
import org.apache.doris.nereids.rules.Rule; | ||
import org.apache.doris.nereids.rules.RuleType; | ||
import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; | ||
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.GroupPlan; | ||
import org.apache.doris.nereids.trees.plans.Plan; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalApply; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; | ||
import org.apache.doris.nereids.trees.plans.logical.LogicalProject; | ||
import org.apache.doris.nereids.util.ExpressionUtils; | ||
import org.apache.doris.nereids.util.PlanUtils; | ||
import org.apache.doris.nereids.util.Utils; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Adjust the order of Project and apply in correlated subqueries. | ||
* | ||
* before: | ||
* apply | ||
* / \ | ||
* Input(output:b) Project(output:a) | ||
* | | ||
* Filter(Correlated predicate/UnCorrelated predicate) | ||
* | | ||
* child | ||
* | ||
* after: | ||
* apply(Correlated predicate) | ||
* / \ | ||
* Input(output:b) Project(output:a) | ||
* | | ||
* Filter(UnCorrelated predicate) | ||
* | | ||
* child | ||
*/ | ||
public class EliminateFilterUnderApplyProject extends OneRewriteRuleFactory { | ||
@Override | ||
public Rule build() { | ||
return logicalApply(group(), logicalProject(logicalFilter())) | ||
.when(LogicalApply::isCorrelated) | ||
.when(LogicalApply::isIn) | ||
.then(apply -> { | ||
LogicalProject<LogicalFilter<GroupPlan>> project = apply.right(); | ||
LogicalFilter<GroupPlan> filter = project.child(); | ||
Set<Expression> conjuncts = filter.getConjuncts(); | ||
Map<Boolean, List<Expression>> split = Utils.splitCorrelatedConjuncts( | ||
conjuncts, apply.getCorrelationSlot()); | ||
List<Expression> correlatedPredicate = split.get(true); | ||
List<Expression> unCorrelatedPredicate = split.get(false); | ||
|
||
// the representative has experienced the rule and added the correlated predicate to the apply node | ||
if (correlatedPredicate.isEmpty()) { | ||
return apply; | ||
} | ||
|
||
Plan child = PlanUtils.filterOrSelf(ImmutableSet.copyOf(unCorrelatedPredicate), filter.child()); | ||
List<NamedExpression> projects = new ArrayList<>(); | ||
projects.addAll(project.getProjects()); | ||
ExpressionUtils.collect(correlatedPredicate, SlotReference.class::isInstance).stream() | ||
.filter(e -> filter.child().getOutput().contains(e)) | ||
.filter(e -> !projects.contains(e)) | ||
.map(NamedExpression.class::cast) | ||
.forEach(projects::add); | ||
LogicalProject newProject = new LogicalProject(projects, child); | ||
return new LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(), | ||
ExpressionUtils.optionalAnd(correlatedPredicate), | ||
apply.left(), newProject); | ||
}).toRule(RuleType.ELIMINATE_FILTER_UNDER_APPLY_PROJECT); | ||
} | ||
} |
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
Oops, something went wrong.