morrySnow commented on code in PR #22203: URL: https://github.com/apache/doris/pull/22203#discussion_r1278756374
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateEmptyRelation.java: ########## @@ -0,0 +1,113 @@ +// 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.ExprId; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.EmptyRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * try to eliminate sub plan tree which contains EmptyRelation + */ +public class EliminateEmptyRelation implements RewriteRuleFactory { + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + // join->empty + logicalJoin(any(), any()) + .when(this::hasEmptyRelationChild) + .then(join -> { + if (canReplaceJoinByEmptyRelation(join) + || (join.getJoinType() == JoinType.LEFT_OUTER_JOIN + && join.left() instanceof EmptyRelation) + || (join.getJoinType() == JoinType.RIGHT_OUTER_JOIN + && join.right() instanceof EmptyRelation)) { Review Comment: put these condition into function `canReplaceJoinByEmptyRelation` ########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateEmptyRelation.java: ########## @@ -0,0 +1,113 @@ +// 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.ExprId; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.plans.JoinType; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.EmptyRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalEmptyRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalJoin; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.qe.ConnectContext; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * try to eliminate sub plan tree which contains EmptyRelation + */ +public class EliminateEmptyRelation implements RewriteRuleFactory { + + @Override + public List<Rule> buildRules() { + return ImmutableList.of( + // join->empty + logicalJoin(any(), any()) + .when(this::hasEmptyRelationChild) + .then(join -> { + if (canReplaceJoinByEmptyRelation(join) + || (join.getJoinType() == JoinType.LEFT_OUTER_JOIN + && join.left() instanceof EmptyRelation) + || (join.getJoinType() == JoinType.RIGHT_OUTER_JOIN + && join.right() instanceof EmptyRelation)) { + return new LogicalEmptyRelation( + ConnectContext.get().getStatementContext().getNextRelationId(), + join.getOutput()); + } + return join; + }) + .toRule(RuleType.ELIMINATE_JOIN_ON_EMPTYRELATION), + logicalFilter(logicalEmptyRelation()) + .then(filter -> new LogicalEmptyRelation( + ConnectContext.get().getStatementContext().getNextRelationId(), + filter.getOutput()) + ).toRule(RuleType.ELIMINATE_FILTER_ON_EMPTYRELATION), + logicalAggregate(logicalEmptyRelation()) + .when(agg -> !agg.getGroupByExpressions().isEmpty()) + .then(agg -> new LogicalEmptyRelation( + ConnectContext.get().getStatementContext().getNextRelationId(), + agg.getOutput()) + ).toRule(RuleType.ELIMINATE_AGG_ON_EMPTYRELATION), + logicalUnion(any(), any()).then(union -> { + Plan child; + if (union.child(0) instanceof EmptyRelation) { + child = union.child(1); + } else if (union.child(1) instanceof EmptyRelation) { + child = union.child(0); + } else { + child = null; + } + if (child == null) { + return null; + } Review Comment: it is better to process all match EmptyRelation here too -- 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]
