Skip to content

Commit

Permalink
[fix](nereids)forbid some join reorder rules for mark join (apache#31966
Browse files Browse the repository at this point in the history
)
  • Loading branch information
starocean999 authored Mar 8, 2024
1 parent bff89ab commit 8e7b514
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public Rule build() {
// null aware mark join will be translated to null aware left semi/anti join
// we don't support null aware right semi/anti join, so should not commute
.whenNot(join -> JoinUtils.isNullAwareMarkJoin(join))
// commuting nest loop mark join is not supported by be
.whenNot(join -> join.isMarkJoin() && join.getHashJoinConjuncts().isEmpty())
// commuting nest loop mark join or left anti mark join is not supported by be
.whenNot(join -> join.isMarkJoin() && (join.getHashJoinConjuncts().isEmpty()
|| join.getJoinType().isLeftAntiJoin()))
.then(join -> {
LogicalJoin<Plan, Plan> newJoin = join.withTypeChildren(join.getJoinType().swap(),
join.right(), join.left(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.doris.nereids.trees.plans.GroupPlan;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
import org.apache.doris.nereids.util.JoinUtils;

import com.google.common.collect.ImmutableList;

Expand All @@ -46,6 +47,9 @@ public List<Rule> buildRules() {
.whenNot(topJoin -> topJoin.hasDistributeHint() || topJoin.left().hasDistributeHint())
.then(topJoin -> {
LogicalJoin<GroupPlan, GroupPlan> bottomJoin = topJoin.left();
if (!JoinUtils.checkReorderPrecondition(topJoin, bottomJoin)) {
return null;
}
GroupPlan a = bottomJoin.left();
GroupPlan b = bottomJoin.right();
GroupPlan c = topJoin.right();
Expand All @@ -61,6 +65,9 @@ public List<Rule> buildRules() {
.whenNot(topJoin -> topJoin.hasDistributeHint() || topJoin.right().hasDistributeHint())
.then(topJoin -> {
LogicalJoin<GroupPlan, GroupPlan> bottomJoin = topJoin.right();
if (!JoinUtils.checkReorderPrecondition(topJoin, bottomJoin)) {
return null;
}
GroupPlan a = topJoin.left();
GroupPlan b = bottomJoin.left();
GroupPlan c = bottomJoin.right();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.doris.nereids.trees.plans.GroupPlan;
import org.apache.doris.nereids.trees.plans.Plan;
import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
import org.apache.doris.nereids.util.JoinUtils;

import com.google.common.collect.ImmutableList;

Expand All @@ -48,6 +49,9 @@ public List<Rule> buildRules() {
.when(join -> join.left().isAllSlots())
.then(topJoin -> {
LogicalJoin<GroupPlan, GroupPlan> bottomJoin = topJoin.left().child();
if (!JoinUtils.checkReorderPrecondition(topJoin, bottomJoin)) {
return null;
}
GroupPlan a = bottomJoin.left();
GroupPlan b = bottomJoin.right();
GroupPlan c = topJoin.right();
Expand All @@ -68,6 +72,9 @@ public List<Rule> buildRules() {
.when(join -> join.right().isAllSlots())
.then(topJoin -> {
LogicalJoin<GroupPlan, GroupPlan> bottomJoin = topJoin.right().child();
if (!JoinUtils.checkReorderPrecondition(topJoin, bottomJoin)) {
return null;
}
GroupPlan a = topJoin.left();
GroupPlan b = bottomJoin.left();
GroupPlan c = bottomJoin.right();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.doris.nereids.trees.expressions.EqualPredicate;
import org.apache.doris.nereids.trees.expressions.ExprId;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.Slot;
import org.apache.doris.nereids.trees.expressions.functions.scalar.BitmapContains;
Expand Down Expand Up @@ -392,4 +393,15 @@ public static boolean isNullAwareMarkJoin(Join join) {
// and translate join type to NULL_AWARE_LEFT_SEMI_JOIN or NULL_AWARE_LEFT_ANTI_JOIN
return join.getHashJoinConjuncts().isEmpty() && !join.getMarkJoinConjuncts().isEmpty();
}

/**
* forbid join reorder if top join's condition use mark join slot produced by bottom join
*/
public static boolean checkReorderPrecondition(LogicalJoin<?, ?> top, LogicalJoin<?, ?> bottom) {
Set<Slot> markSlots = top.getConditionSlot().stream()
.filter(MarkJoinSlotReference.class::isInstance)
.collect(Collectors.toSet());
markSlots.retainAll(bottom.getOutputSet());
return markSlots.isEmpty();
}
}

0 comments on commit 8e7b514

Please sign in to comment.