-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[enhance](nereids) add rewrite rule SplitJoinForNullSkew #44357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
defb2d9
initial commit
feiniaofeiafei 1ed5934
add test
feiniaofeiafei 56f4fae
fix feut
feiniaofeiafei 7111d92
add test
feiniaofeiafei 9daeb11
simplify avoid duplicate application of rules
feiniaofeiafei 2068444
add agg skew rewrite
feiniaofeiafei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
143 changes: 143 additions & 0 deletions
143
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/JoinSplitForNullSkew.java
This file contains hidden or 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,143 @@ | ||
| // 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; | ||
|
|
||
| import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; | ||
| import org.apache.doris.nereids.trees.copier.DeepCopierContext; | ||
| import org.apache.doris.nereids.trees.copier.LogicalPlanDeepCopier; | ||
| import org.apache.doris.nereids.trees.expressions.Alias; | ||
| import org.apache.doris.nereids.trees.expressions.EqualTo; | ||
| import org.apache.doris.nereids.trees.expressions.Expression; | ||
| import org.apache.doris.nereids.trees.expressions.IsNull; | ||
| import org.apache.doris.nereids.trees.expressions.NamedExpression; | ||
| import org.apache.doris.nereids.trees.expressions.Not; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
| import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
| import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; | ||
| import org.apache.doris.nereids.trees.plans.Plan; | ||
| import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; | ||
| 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.trees.plans.logical.LogicalUnion; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * LogicalLeftOuterJoin(hashConjuncts:t1.a=t2.a) | ||
| * +--Plan1(output:t1.a) | ||
| * +--Plan2(output:t2.a) | ||
| * -> | ||
| * LogicalUnion | ||
| * +--LogicalProject | ||
| * +--LogicalFilter(t1.a is null) | ||
| * +--Plan1 | ||
| * +--LogicalLeftOuterJoin(t1.a=t2.a) | ||
| * +--LogicalFilter(t1.a is not null) | ||
| * +--Plan1 | ||
| * +--Plan2 | ||
| * | ||
| * LogicalRightOuterJoin(hashConjuncts:t1.a=t2.a) | ||
| * +--Plan1(output:t1.a) | ||
| * +--Plan2(output:t2.a) | ||
| * -> | ||
| * LogicalUnion | ||
| * +--LogicalProject | ||
| * +--LogicalFilter(t2.a is null) | ||
| * +--Plan2 | ||
| * +--LogicalRightOuterJoin(t1.a=t2.a) | ||
| * +--Plan1 | ||
| * +--LogicalFilter(t2.a is not null) | ||
| * +--Plan2 | ||
| * */ | ||
| public class JoinSplitForNullSkew extends OneRewriteRuleFactory { | ||
| @Override | ||
| public Rule build() { | ||
| return logicalJoin() | ||
| .when(join -> join.getJoinType().isOneSideOuterJoin()) | ||
| .whenNot(join -> join.isMarkJoin() || !join.getMarkJoinConjuncts().isEmpty()) | ||
| .when(join -> join.getHashJoinConjuncts().size() == 1) | ||
| .then(this::splitJoin) | ||
| .toRule(RuleType.JOIN_SPLIT_FOR_NULL_SKEW); | ||
| } | ||
|
|
||
| private Plan splitJoin(LogicalJoin<Plan, Plan> join) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should not rewrite if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| boolean isLeftJoin = join.getJoinType().isLeftOuterJoin(); | ||
| Plan primarySide = isLeftJoin ? join.left() : join.right(); | ||
| Plan associatedSide = isLeftJoin ? join.right() : join.left(); | ||
| Expression conjunct = join.getHashJoinConjuncts().get(0); | ||
| if (!(conjunct instanceof EqualTo)) { | ||
| return null; | ||
| } | ||
| EqualTo equalTo = (EqualTo) conjunct; | ||
| Expression splitExpr; | ||
| if (primarySide.getOutputSet().containsAll(equalTo.left().getInputSlots())) { | ||
| splitExpr = equalTo.left(); | ||
| } else { | ||
| splitExpr = equalTo.right(); | ||
| } | ||
| if (!splitExpr.nullable()) { | ||
| return null; | ||
| } | ||
| // avoid duplicate application of rules | ||
| Expression isNotNull = new Not(new IsNull(splitExpr)); | ||
| if (primarySide instanceof LogicalFilter | ||
| && ((LogicalFilter<?>) primarySide).getConjuncts().contains(isNotNull)) { | ||
| return null; | ||
| } | ||
|
|
||
| // is not null side construct | ||
| LogicalFilter<Plan> isNotNullFilter = new LogicalFilter<>(ImmutableSet.of(isNotNull), primarySide); | ||
| LogicalJoin<Plan, Plan> newJoin; | ||
| if (isLeftJoin) { | ||
| newJoin = join.withChildren(ImmutableList.of(isNotNullFilter, associatedSide)); | ||
| } else { | ||
| newJoin = join.withChildren(ImmutableList.of(associatedSide, isNotNullFilter)); | ||
| } | ||
| Plan deepCopyJoin = LogicalPlanDeepCopier.INSTANCE.deepCopy(newJoin, new DeepCopierContext()); | ||
|
|
||
| // is null side construct | ||
| LogicalFilter<Plan> isNullFilter = new LogicalFilter<>(ImmutableSet.of(new IsNull(splitExpr)), primarySide); | ||
| Plan deepCopyFilter = LogicalPlanDeepCopier.INSTANCE.deepCopy(isNullFilter, new DeepCopierContext()); | ||
| List<NamedExpression> newProjects = new ArrayList<>(join.getOutput().size()); | ||
| if (isLeftJoin) { | ||
| newProjects.addAll(deepCopyFilter.getOutput()); | ||
| for (Slot slot : associatedSide.getOutput()) { | ||
| newProjects.add(new Alias(new NullLiteral(slot.getDataType()))); | ||
| } | ||
| } else { | ||
| for (Slot slot : associatedSide.getOutput()) { | ||
| newProjects.add(new Alias(new NullLiteral(slot.getDataType()))); | ||
| } | ||
| newProjects.addAll(deepCopyFilter.getOutput()); | ||
| } | ||
| LogicalProject<Plan> isNullProject = new LogicalProject<>(newProjects, deepCopyFilter); | ||
|
|
||
| // regularChildrenOutputs construct | ||
| List<List<SlotReference>> regularChildrenOutputs = new ArrayList<>(); | ||
| regularChildrenOutputs.add((List) isNullProject.getOutput()); | ||
| regularChildrenOutputs.add((List) deepCopyJoin.getOutput()); | ||
|
|
||
| return new LogicalUnion(Qualifier.ALL, (List) join.getOutput(), regularChildrenOutputs, | ||
| ImmutableList.of(), false, ImmutableList.of(isNullProject, deepCopyJoin)); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should check mark join conjuncts