[fix](nereids)forbid some join reorder rules for mark join (#31966)

This commit is contained in:
starocean999
2024-03-08 10:15:05 +08:00
committed by yiguolei
parent f968d96545
commit 1721bfb87a
4 changed files with 29 additions and 2 deletions

View File

@ -63,8 +63,9 @@ public class JoinCommute extends OneExplorationRuleFactory {
// 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);

View File

@ -23,6 +23,7 @@ import org.apache.doris.nereids.rules.exploration.ExplorationRuleFactory;
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;
@ -46,6 +47,9 @@ public class LogicalJoinSemiJoinTranspose implements ExplorationRuleFactory {
.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();
@ -61,6 +65,9 @@ public class LogicalJoinSemiJoinTranspose implements ExplorationRuleFactory {
.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();

View File

@ -24,6 +24,7 @@ import org.apache.doris.nereids.rules.exploration.ExplorationRuleFactory;
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;
@ -48,6 +49,9 @@ public class LogicalJoinSemiJoinTransposeProject implements ExplorationRuleFacto
.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();
@ -68,6 +72,9 @@ public class LogicalJoinSemiJoinTransposeProject implements ExplorationRuleFacto
.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();

View File

@ -29,6 +29,7 @@ import org.apache.doris.nereids.rules.rewrite.ForeignKeyContext;
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;
@ -392,4 +393,15 @@ public class JoinUtils {
// 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();
}
}