XieJiann commented on code in PR #36397: URL: https://github.com/apache/doris/pull/36397#discussion_r1663368604
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PullUpJoinFromUnion.java: ########## @@ -0,0 +1,460 @@ +// 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.catalog.constraint.TableIdentifier; +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.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.EqualTo; +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.SlotReference; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.logical.LogicalCatalogRelation; +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.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import javax.annotation.Nullable; + +/** + * Pull up join from union all rules. + * Union + * / \ + * Join Join + * / \ / \ + * t1 t2 t1 t3 + * =====> + * Join + * / \ + * Union t1 + * / \ + * t2 t3 + */ +public class PullUpJoinFromUnion extends OneRewriteRuleFactory { + @Override + public Rule build() { + return logicalUnion() + .when(union -> union.getQualifier() != Qualifier.DISTINCT) + .then(union -> { + // map union child slot to union slot + Map<Slot, Slot> originChildSlotToUnion = new HashMap<>(); + // if union child is project, map the join slot to the project + Map<Slot, NamedExpression> joinSlotToProject = new HashMap<>(); + // save the constant alias in project above join + Map<Plan, Set<NamedExpression>> constantAlias = new HashMap<>(); + // save the miss slot of other child that not in the union, we need pull up them in new union + Map<Plan, List<Slot>> missSlotMap = new HashMap<>(); + HashMap<Plan, List<Pair<LogicalJoin<?, ?>, Plan>>> commonChildCount = Review Comment: The Count means the count of the same join child behind the union. For (a join1 b) union all (a join2 c), it will be {a: [(join1, a), (join2, a)], b: [(join1, b)], c: [(join2, c)]} -- 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