morrySnow commented on code in PR #67044: URL: https://github.com/apache/doris/pull/67044#discussion_r3859565420
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByConstantOneRowRelation.java: ########## @@ -0,0 +1,133 @@ +// 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; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Sleep; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +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.LogicalOneRowRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * For an INNER / CROSS join whose one side is a single-row of constant expressions + * (typically produced by inlining a constant CTE), rewrite the join into a plain + * {@code Project + Filter} on the other side. + */ +public class EliminateJoinByConstantOneRowRelation implements RewriteRuleFactory { + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalJoin(any(), logicalOneRowRelation()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ true)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION), + logicalJoin(logicalOneRowRelation(), any()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ false)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION)); + } + + private static boolean isEnabled() { + ConnectContext ctx = ConnectContext.get(); + return ctx != null && ctx.getSessionVariable().enableEliminateJoinByConstantOneRowRelation; Review Comment: Why is an additional switch needed? Isn't this an unnecessary and undesirable optimization? ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByConstantOneRowRelation.java: ########## @@ -0,0 +1,133 @@ +// 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; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Sleep; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +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.LogicalOneRowRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * For an INNER / CROSS join whose one side is a single-row of constant expressions + * (typically produced by inlining a constant CTE), rewrite the join into a plain + * {@code Project + Filter} on the other side. + */ +public class EliminateJoinByConstantOneRowRelation implements RewriteRuleFactory { + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalJoin(any(), logicalOneRowRelation()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ true)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION), + logicalJoin(logicalOneRowRelation(), any()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ false)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION)); + } + + private static boolean isEnabled() { + ConnectContext ctx = ConnectContext.get(); + return ctx != null && ctx.getSessionVariable().enableEliminateJoinByConstantOneRowRelation; + } + + private static boolean supportedJoinType(JoinType joinType) { + return joinType.isInnerJoin() || joinType.isCrossJoin(); + } + + private static Plan tryRewrite(LogicalJoin<? extends Plan, ? extends Plan> join, boolean constantOnRight) { + LogicalOneRowRelation constantSide = constantOnRight + ? (LogicalOneRowRelation) join.right() + : (LogicalOneRowRelation) join.left(); + Plan otherSide = constantOnRight ? join.left() : join.right(); + + Map<Slot, Expression> slotToConstant = buildSlotToConstantMap(constantSide); + if (slotToConstant == null) { + return null; + } + + Set<Expression> filterConjuncts = new LinkedHashSet<>(); + collectRewrittenConjuncts(join.getHashJoinConjuncts(), slotToConstant, filterConjuncts); + collectRewrittenConjuncts(join.getOtherJoinConjuncts(), slotToConstant, filterConjuncts); + collectRewrittenConjuncts(join.getMarkJoinConjuncts(), slotToConstant, filterConjuncts); + + List<NamedExpression> newProjects = new ArrayList<>(otherSide.getOutput().size() + + constantSide.getProjects().size()); + newProjects.addAll(otherSide.getOutput()); + newProjects.addAll(constantSide.getProjects()); + + Plan rewritten = new LogicalProject<>(newProjects, otherSide); + if (!filterConjuncts.isEmpty()) { + rewritten = new LogicalFilter<>(filterConjuncts, rewritten); + } + return rewritten; + } + + private static Map<Slot, Expression> buildSlotToConstantMap(LogicalOneRowRelation relation) { + Map<Slot, Expression> map = new LinkedHashMap<>(); + for (NamedExpression project : relation.getProjects()) { + Expression payload = project instanceof Alias ? project.child(0) : project; + if (!isSafeConstantExpr(payload)) { + return null; + } + map.put(project.toSlot(), payload); + } + return map; + } + + private static boolean isSafeConstantExpr(Expression e) { + return !e.containsVolatileExpression() + && !e.anyMatch(Sleep.class::isInstance) + && !e.anyMatch(SubqueryExpr.class::isInstance); Review Comment: Why is it necessary to evaluate the subquery here? During the rewrite process, subqueries should not exist. ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByConstantOneRowRelation.java: ########## @@ -0,0 +1,133 @@ +// 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; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SubqueryExpr; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Sleep; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +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.LogicalOneRowRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * For an INNER / CROSS join whose one side is a single-row of constant expressions + * (typically produced by inlining a constant CTE), rewrite the join into a plain + * {@code Project + Filter} on the other side. + */ +public class EliminateJoinByConstantOneRowRelation implements RewriteRuleFactory { + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + logicalJoin(any(), logicalOneRowRelation()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ true)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION), + logicalJoin(logicalOneRowRelation(), any()) + .when(join -> isEnabled()) + .whenNot(LogicalJoin::isMarkJoin) + .when(join -> supportedJoinType(join.getJoinType())) + .then(join -> tryRewrite(join, /* constantOnRight= */ false)) + .toRule(RuleType.ELIMINATE_JOIN_BY_CONSTANT_ONE_ROW_RELATION)); + } + + private static boolean isEnabled() { + ConnectContext ctx = ConnectContext.get(); + return ctx != null && ctx.getSessionVariable().enableEliminateJoinByConstantOneRowRelation; + } + + private static boolean supportedJoinType(JoinType joinType) { + return joinType.isInnerJoin() || joinType.isCrossJoin(); + } + + private static Plan tryRewrite(LogicalJoin<? extends Plan, ? extends Plan> join, boolean constantOnRight) { + LogicalOneRowRelation constantSide = constantOnRight + ? (LogicalOneRowRelation) join.right() + : (LogicalOneRowRelation) join.left(); + Plan otherSide = constantOnRight ? join.left() : join.right(); + + Map<Slot, Expression> slotToConstant = buildSlotToConstantMap(constantSide); + if (slotToConstant == null) { + return null; + } + + Set<Expression> filterConjuncts = new LinkedHashSet<>(); + collectRewrittenConjuncts(join.getHashJoinConjuncts(), slotToConstant, filterConjuncts); + collectRewrittenConjuncts(join.getOtherJoinConjuncts(), slotToConstant, filterConjuncts); + collectRewrittenConjuncts(join.getMarkJoinConjuncts(), slotToConstant, filterConjuncts); Review Comment: just reject mark join -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
