This is an automated email from the ASF dual-hosted git repository.

xiejiann pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 935484ba734 [fix](Nereids) Add Primary Key by Primary Relation Instead 
of Foreign Relation (#36008)
935484ba734 is described below

commit 935484ba7348103860ad667cafc364a8a060da17
Author: 谢健 <jianx...@gmail.com>
AuthorDate: Wed Jun 12 10:35:59 2024 +0800

    [fix](Nereids) Add Primary Key by Primary Relation Instead of Foreign 
Relation (#36008)
    
    Ensure the primary key is added by the primary relation, enhancing
    efficiency by expiring keys post joins to avoid eliminating redundant
    joins based on primary-foreign key relationships.
---
 .../apache/doris/nereids/rules/rewrite/ForeignKeyContext.java |  9 ++++++++-
 .../doris/nereids/rules/rewrite/EliminateJoinByFkTest.java    | 11 +++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
index 600f3d1ac5a..3668ae68337 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/ForeignKeyContext.java
@@ -67,6 +67,7 @@ public class ForeignKeyContext {
             public Void visitLogicalRelation(LogicalRelation relation, 
ForeignKeyContext context) {
                 if (relation instanceof LogicalCatalogRelation) {
                     context.putAllForeignKeys(((LogicalCatalogRelation) 
relation).getTable());
+                    context.putAllPrimaryKeys(((LogicalCatalogRelation) 
relation).getTable());
                     relation.getOutput().stream()
                             .filter(SlotReference.class::isInstance)
                             .map(SlotReference.class::cast)
@@ -101,7 +102,13 @@ public class ForeignKeyContext {
             Map<Column, Column> constraint = c.getForeignToPrimary(table);
             constraints.add(c.getForeignToPrimary(table));
             foreignKeys.addAll(constraint.keySet());
-            primaryKeys.addAll(constraint.values());
+        });
+    }
+
+    void putAllPrimaryKeys(TableIf table) {
+        table.getPrimaryKeyConstraints().forEach(c -> {
+            Set<Column> primaryKey = c.getPrimaryKeys(table);
+            primaryKeys.addAll(primaryKey);
         });
     }
 
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFkTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFkTest.java
index 86b37af0169..86d3ef9700a 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFkTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateJoinByFkTest.java
@@ -57,6 +57,17 @@ class EliminateJoinByFkTest extends TestWithFeService 
implements MemoPatternMatc
         
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
     }
 
+    @Test
+    void testPriWithJoin() {
+        String sql = "select pri.id1 from (select p1.id1 from pri as p1 cross 
join pri as p2) pri "
+                + "inner join foreign_not_null on pri.id1 = 
foreign_not_null.id2";
+        PlanChecker.from(connectContext)
+                .analyze(sql)
+                .rewrite()
+                .matches(logicalJoin())
+                .printlnTree();
+    }
+
     @Test
     void testNotNull() {
         String sql = "select pri.id1 from pri inner join foreign_not_null on 
pri.id1 = foreign_not_null.id2";


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to