feiniaofeiafei commented on code in PR #65172:
URL: https://github.com/apache/doris/pull/65172#discussion_r3533488586


##########
fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PushDownAggThroughJoinOnPkFkTest.java:
##########
@@ -179,4 +227,81 @@ void testMissSlot() {
                 .matches(logicalAggregate(logicalProject(logicalJoin())))
                 .printlnTree();
     }
+
+    @Test
+    void testPushDownAggThroughTreeJoin() {
+        // Test pushing agg through a tree-structured join (like TPC-H Q10):
+        // Join(t_primary.fk_to_other = t_other_primary.other_pk_id)  ← root 
(pk-fk: other is pk)
+        //   /  \
+        // Join(t_foreign1.fk1_id = t_foreign2.fk_to_foreign1)        
t_other_primary
+        //   /  \
+        // Join(t_primary.pk_id = t_foreign1.fk_to_primary)           
t_foreign2
+        //   /  \
+        // t_primary  t_foreign1
+        // The pk-fk join with t_other_primary as pk is recognized,
+        // and agg should be pushed down through it.
+        String sql = "select t_primary.pk_id, t_primary.fk_to_other, 
sum(t_foreign2.fk2_id) "
+                + "from t_primary "
+                + "inner join t_foreign1 on t_primary.pk_id = 
t_foreign1.fk_to_primary "
+                + "inner join t_foreign2 on t_foreign1.fk1_id = 
t_foreign2.fk_to_foreign1 "
+                + "inner join t_other_primary on t_primary.fk_to_other = 
t_other_primary.other_pk_id "
+                + "group by t_primary.pk_id, t_primary.fk_to_other";
+        PlanChecker.from(connectContext)
+                .analyze(sql)
+                .rewrite()
+                .matches(logicalJoin(logicalAggregate(), any()))
+                .printlnTree();
+    }
+
+    @Test
+    void testPushDownAggThroughTreeJoinOtherSide() {
+        // When the pk-fk join's primary is t_primary (the connector table that
+        // also joins to other tables), the reconstruction of the non-primary
+        // side fails because the remaining leaves form disconnected 
components.
+        // The rule should gracefully return null (no push down).
+        // Note: the join to t_other_primary uses t_primary.name = 
t_other_primary.name
+        // (a non-PK-FK join) to avoid an alternative push-down path through
+        // t_other_primary as the primary side, which would succeed since the
+        // query outputs no t_other_primary columns.
+        String sql = "select t_primary.pk_id, t_foreign1.fk_to_primary, 
sum(t_foreign2.fk2_id) "

Review Comment:
   Currently, this case cannot trigger the bug. The SQL needs to be changed to 
the following one in order to hit it, and then an error will be reported:  
java.lang.ArrayIndexOutOfBoundsException
   ```sql
   select t_primary.pk_id, t_foreign1.fk_to_primary, sum(t_foreign2.fk2_id) 
   from t_primary 
   inner join t_foreign1 on t_primary.pk_id = t_foreign1.fk_to_primary 
   inner join t_foreign2 on t_foreign1.fk1_id = t_foreign2.fk_to_foreign1 
   inner join t_other_primary on t_foreign2.name = t_other_primary.name 
   group by t_primary.pk_id, t_foreign1.fk_to_primary;  
   ```



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