morrySnow opened a new pull request, #67796:
URL: https://github.com/apache/doris/pull/67796

   ### What problem does this PR solve?
   
   For a `SELECT DISTINCT` over an outer or cross join, a mixed `ORDER BY` can 
begin with columns from one join child and continue with columns from the other 
child. The TopN pushdown rule used to keep only that child's leading order-key 
prefix and apply a hard `LIMIT` before the join.
   
   If the prefix contains ties, the remaining order keys decide which rows 
belong to the final TopN. Applying the limit to an arbitrary subset of tied 
child rows can therefore remove the true result before the join and return a 
wrong row.
   
   For example, with multiple left rows sharing the same `k`, this shape is 
unsafe:
   
   ```sql
   SELECT DISTINCT l.id, l.k, r.score
   FROM left_table l LEFT JOIN right_table r ON l.id = r.id
   ORDER BY l.k, r.score
   LIMIT 1;
   ```
   
   ### What is changed and how does it work?
   
   The rule now applies a partial order-key prefix only when that prefix is 
provably unique after the child-side `DISTINCT`. The proof accepts a prefix 
that:
   
   - covers all output columns of the child;
   - contains a non-null unique key; or
   - functionally determines every remaining child output column.
   
   An order-key sequence that comes entirely from one child remains eligible 
for pushdown. A mixed sequence without a uniqueness proof is kept above the 
join only.
   
   The change adds focused unit coverage for rejected non-unique prefixes and 
accepted complete, full-output, and functionally determining prefixes. It also 
adds result regressions for tied prefixes, mixed-side ordering, descending 
ordering, and offsets.
   
   ### Tests
   
   - `./run-fe-ut.sh --run 
org.apache.doris.nereids.rules.rewrite.PushDownTopNDistinctThroughJoinTest`
   - `./run-regression-test.sh --run -f 
regression-test/suites/nereids_rules_p0/push_down_top_n/push_down_top_n_distinct_through_join.groovy
 ...`
   - `DISABLE_BUILD_UI=ON ./build.sh --fe`
   
   


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