xzj7019 commented on code in PR #28486:
URL: https://github.com/apache/doris/pull/28486#discussion_r1432647239


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFK.java:
##########
@@ -0,0 +1,333 @@
+// 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.Column;
+import org.apache.doris.catalog.Table;
+import org.apache.doris.catalog.TableIf;
+import org.apache.doris.nereids.jobs.JobContext;
+import org.apache.doris.nereids.trees.expressions.Alias;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.NamedExpression;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
+import org.apache.doris.nereids.trees.expressions.functions.ExpressionTrait;
+import org.apache.doris.nereids.trees.plans.Plan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+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.LogicalLimit;
+import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
+import org.apache.doris.nereids.trees.plans.logical.LogicalRelation;
+import org.apache.doris.nereids.trees.plans.logical.LogicalSort;
+import org.apache.doris.nereids.trees.plans.logical.LogicalTopN;
+import org.apache.doris.nereids.trees.plans.logical.LogicalWindow;
+import org.apache.doris.nereids.trees.plans.visitor.CustomRewriter;
+import org.apache.doris.nereids.trees.plans.visitor.DefaultPlanRewriter;
+import org.apache.doris.nereids.util.ImmutableEquivalenceSet;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Sets;
+
+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 java.util.stream.Collectors;
+import javax.annotation.Nullable;
+
+/**
+ * Eliminate join by foreign.
+ */
+public class EliminateJoinByFK extends DefaultPlanRewriter<JobContext> 
implements CustomRewriter {
+    @Override
+    public Plan rewriteRoot(Plan plan, JobContext jobContext) {
+        EliminateJoinByFKHelper helper = new EliminateJoinByFKHelper();
+        return helper.rewriteRoot(plan, jobContext);
+    }
+
+    private static class EliminateJoinByFKHelper
+            extends DefaultPlanRewriter<ForeignKeyContext> implements 
CustomRewriter {
+
+        @Override
+        public Plan rewriteRoot(Plan plan, JobContext jobContext) {
+            return plan.accept(this, new ForeignKeyContext());
+        }
+
+        @Override
+        public Plan visit(Plan plan, ForeignKeyContext context) {
+            Plan newPlan = visitChildren(this, plan, context);
+            context.passThroughPlan(plan);
+            return newPlan;
+        }
+
+        @Override
+        public Plan visitLogicalRelation(LogicalRelation relation, 
ForeignKeyContext context) {
+            if (!(relation instanceof LogicalCatalogRelation)) {
+                return relation;
+            }
+            if (!(((LogicalCatalogRelation) relation).getTable() instanceof 
Table)) {
+                return relation;
+            }
+            context.putAllForeignKeys(((LogicalCatalogRelation) 
relation).getTable());
+            relation.getOutput().stream()
+                    .filter(SlotReference.class::isInstance)
+                    .map(SlotReference.class::cast)
+                    .forEach(context::putSlot);
+            return relation;
+        }
+
+        private boolean canEliminate(LogicalJoin<?, ?> join, BiMap<Slot, Slot> 
equalSlots, ForeignKeyContext context) {
+            if (!join.getOtherJoinConjuncts().isEmpty()) {
+                return false;
+            }
+            if (!join.getJoinType().isInnerJoin() && 
!join.getJoinType().isSemiJoin()) {
+                return false;
+            }
+            return context.satisfyConstraint(equalSlots, join);
+        }
+
+        private boolean isForeignKeyAndUnique(Plan plan,
+                Set<Slot> keySet, ForeignKeyContext context) {
+            boolean unique = keySet.stream()
+                    .allMatch(s -> 
plan.getLogicalProperties().getFunctionalDependencies().isUnique(s));
+            return unique && context.isForeignKey(keySet);
+        }
+
+        private @Nullable Map<Expression, Expression> 
tryGetOutputToChildMap(Plan child,
+                Set<Slot> output, BiMap<Slot, Slot> equalSlots) {
+            Set<Slot> residual = Sets.difference(output, child.getOutputSet());
+            if (equalSlots.keySet().containsAll(residual)) {
+                return residual.stream()
+                        .collect(ImmutableMap.toImmutableMap(e -> e, 
equalSlots::get));
+            }
+            if (equalSlots.values().containsAll(residual)) {
+                return residual.stream()
+                        .collect(ImmutableMap.toImmutableMap(e -> e, e -> 
equalSlots.inverse().get(e)));
+            }
+            return null;
+        }
+
+        private Plan applyNullCompensationFilter(Plan child, Set<Slot> 
childSlots) {
+            Set<Expression> predicates = childSlots.stream()
+                    .filter(ExpressionTrait::nullable)
+                    .map(s -> new Not(new IsNull(s)))
+                    .collect(ImmutableSet.toImmutableSet());
+            if (predicates.isEmpty()) {
+                return child;
+            }
+            return new LogicalFilter<>(predicates, child);
+        }
+
+        private @Nullable Plan 
tryConstructPlanWithJoinChild(LogicalProject<LogicalJoin<?, ?>> project, Plan 
child,
+                BiMap<Slot, Slot> equalSlots, ForeignKeyContext context) {
+            Set<Slot> output = project.getInputSlots();
+            Set<Slot> keySet = 
child.getOutputSet().containsAll(equalSlots.keySet())
+                    ? equalSlots.keySet()
+                    : equalSlots.values();
+            Map<Expression, Expression> outputToRight = 
tryGetOutputToChildMap(child, output, equalSlots);
+            if (outputToRight != null && isForeignKeyAndUnique(child, keySet, 
context)) {

Review Comment:
   unique is not necessary



-- 
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]

Reply via email to