morrySnow commented on code in PR #12985: URL: https://github.com/apache/doris/pull/12985#discussion_r980675248
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateOuter.java: ########## @@ -0,0 +1,88 @@ +// 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.common.Pair; +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.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.JoinUtils; + +import com.google.common.collect.ImmutableMap; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Eliminate outer. + */ +public class EliminateOuter extends OneRewriteRuleFactory { + public static EliminateOuter INSTANCE = new EliminateOuter(); + + // right nullable + public static Map<JoinType, JoinType> ELIMINATE_LEFT_MAP = ImmutableMap.of( + JoinType.LEFT_OUTER_JOIN, JoinType.INNER_JOIN, + JoinType.FULL_OUTER_JOIN, JoinType.RIGHT_OUTER_JOIN + ); + + // left nullable + public static Map<JoinType, JoinType> ELIMINATE_RIGHT_MAP = ImmutableMap.of( + JoinType.RIGHT_OUTER_JOIN, JoinType.INNER_JOIN, + JoinType.FULL_OUTER_JOIN, JoinType.LEFT_OUTER_JOIN + ); + + @Override + public Rule build() { + return logicalFilter(logicalJoin()) + .when(filter -> filter.child().getJoinType().isOuterJoin()) + .then(filter -> { + List<Expression> predicates = ExpressionUtils.extractConjunction(filter.getPredicates()); + Set<Slot> notNullSlots = new HashSet<>(); + for (Expression predicate : predicates) { + // TODO: more case. + if (predicate instanceof ComparisonPredicate) { + notNullSlots.addAll(predicate.getInputSlots()); + } + } + LogicalJoin<GroupPlan, GroupPlan> join = filter.child(); + Pair<List<Slot>, List<Slot>> onClauseUsedSlots = JoinUtils.getOnClauseUsedSlots(join); + JoinType joinType = join.getJoinType(); + if (!joinType.isLeftOuterJoin() && notNullSlots.containsAll(onClauseUsedSlots.first)) { Review Comment: not only on clause, we could eliminate right outer if not null slots contains any slots of left child. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateOuter.java: ########## @@ -0,0 +1,88 @@ +// 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.common.Pair; +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.ComparisonPredicate; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.JoinUtils; + +import com.google.common.collect.ImmutableMap; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Eliminate outer. + */ +public class EliminateOuter extends OneRewriteRuleFactory { + public static EliminateOuter INSTANCE = new EliminateOuter(); + + // right nullable + public static Map<JoinType, JoinType> ELIMINATE_LEFT_MAP = ImmutableMap.of( + JoinType.LEFT_OUTER_JOIN, JoinType.INNER_JOIN, + JoinType.FULL_OUTER_JOIN, JoinType.RIGHT_OUTER_JOIN + ); + + // left nullable + public static Map<JoinType, JoinType> ELIMINATE_RIGHT_MAP = ImmutableMap.of( + JoinType.RIGHT_OUTER_JOIN, JoinType.INNER_JOIN, + JoinType.FULL_OUTER_JOIN, JoinType.LEFT_OUTER_JOIN + ); + + @Override + public Rule build() { + return logicalFilter(logicalJoin()) + .when(filter -> filter.child().getJoinType().isOuterJoin()) + .then(filter -> { Review Comment: i think this rule could cause infinite loop? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/util/JoinUtils.java: ########## @@ -185,6 +186,42 @@ public static Pair<List<ExprId>, List<ExprId>> getOnClauseUsedSlots( return childSlotsExprId; } + /** + * Get all used slots from onClause of join. + */ + public static Pair<List<Slot>, List<Slot>> getOnClauseUsedSlots( Review Comment: we have a similar function i think -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org