morrySnow commented on code in PR #67796:
URL: https://github.com/apache/doris/pull/67796#discussion_r4011634139


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownTopNDistinctThroughJoin.java:
##########
@@ -197,6 +201,34 @@ private List<OrderKey> getPushedOrderKeys(Set<Slot> 
groupBySlots, Set<Slot> join
                 notFound = true;
             }
         }
-        return pushedOrderKeys.build();
+        List<OrderKey> pushedOrderKeyList = pushedOrderKeys.build();
+        if (pushedOrderKeyList.size() == orderKeys.size()
+                || isOrderKeyPrefixUniqueAfterDistinct(joinChild, 
pushedOrderKeyList)) {
+            return pushedOrderKeyList;
+        }
+        return ImmutableList.of();
+    }
+
+    /**
+     * A partial order-key prefix is safe for a hard limit only when it 
uniquely orders the rows produced by
+     * {@link PlanUtils#distinct(Plan)}. This is true when a leading part of 
the prefix either is already a
+     * non-null unique key, covers every child output, or functionally 
determines every remaining child output.
+     */
+    private boolean isOrderKeyPrefixUniqueAfterDistinct(Plan joinChild, 
List<OrderKey> orderKeyPrefix) {
+        Set<Slot> childOutput = joinChild.getOutputSet();
+        DataTrait childTrait = joinChild.getLogicalProperties().getTrait();
+        FuncDeps validFuncDeps = childTrait.getAllValidFuncDeps(childOutput);

Review Comment:
   Fixed in 1866bef17b7. Empty prefixes now return before touching child 
outputs/traits, full child-output coverage returns before trait lookup, and 
non-null uniqueness short-circuits before any FD work. The remaining proof uses 
a slot-indexed closure work queue over direct FD graph edges, so it no longer 
materializes the full transitive relation or rescans it once per prefix slot; 
graph expansion is O(V + E). Added closure coverage for transitive and compound 
determinants. The targeted FE run passed PushDownTopNDistinctThroughJoinTest, 
FuncDepsDGTest, and FdTest (27 tests, 0 failures/errors/skips).



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