jayzhan211 commented on code in PR #27584:
URL: https://github.com/apache/datafusion/pull/27584#discussion_r4656046493


##########
datafusion/physical-plan/src/sorts/partitioned_topk.rs:
##########
@@ -347,7 +347,10 @@ impl PartitionedTopKExec {
             }
         };
         let partition_exprs = sort_exprs[..partition_prefix_len].to_vec();
-        eq_properties.reorder(sort_exprs)?;
+
+        eq_properties.clear_groupings();
+        eq_properties.clear_orderings();
+        eq_properties.add_ordering(sort_exprs);

Review Comment:
   Optional cleanup: a short comment here would keep the next person from 
"simplifying" this back to `reorder`, which is what `SortExec` uses and looks 
equivalent:
   
   ```suggestion
   
           // Not `reorder`: that keeps a longer input ordering when 
`sort_exprs`
           // is already satisfied, but rows that tie on `sort_exprs` leave the
           // heap in any order, so only `sort_exprs` holds (#27459).
           eq_properties.clear_groupings();
           eq_properties.clear_orderings();
           eq_properties.add_ordering(sort_exprs);
   ```
   



##########
datafusion/physical-plan/src/sorts/partitioned_topk.rs:
##########
@@ -347,7 +347,10 @@ impl PartitionedTopKExec {
             }
         };
         let partition_exprs = sort_exprs[..partition_prefix_len].to_vec();
-        eq_properties.reorder(sort_exprs)?;
+
+        eq_properties.clear_groupings();
+        eq_properties.clear_orderings();
+        eq_properties.add_ordering(sort_exprs);

Review Comment:
   +1. Here is one in `window_topn.slt` that I ran locally: all three records 
fail on `main` (rows `1 5 1` and `2 4 7` come out of order, and the plan has no 
`SortExec`) and pass on this branch. It covers `rank` too, which goes through a 
different heap than `row_number`.
   
   <details>
   <summary>Test to append to 
<code>datafusion/sqllogictest/test_files/window_topn.slt</code></summary>
   
   ```
   # PartitionedTopKExec must not claim the input's longer ordering: rows that 
tie
   # on the window ORDER BY key come out of its heap in any order, so the outer
   # ORDER BY on the extra column still needs a SortExec (#27459).
   statement ok
   SET datafusion.optimizer.enable_window_topn = true;
   
   statement ok
   COPY (SELECT * FROM (VALUES (1, 5, 1), (1, 5, 2), (1, 5, 3), (2, 4, 7), (2, 
4, 8)) AS v(pk, ok, x))
   TO 'test_files/scratch/window_topn/sorted_ties.parquet';
   
   statement ok
   CREATE EXTERNAL TABLE window_topn_sorted_ties (pk INT NOT NULL, ok INT NOT 
NULL, x INT NOT NULL)
   STORED AS PARQUET
   WITH ORDER (pk, ok, x)
   LOCATION 'test_files/scratch/window_topn/sorted_ties.parquet';
   
   query III
   SELECT pk, ok, x FROM (
     SELECT pk, ok, x, rank() OVER (PARTITION BY pk ORDER BY ok) AS rk
     FROM window_topn_sorted_ties
   ) WHERE rk <= 3
   ORDER BY pk, ok, x;
   ----
   1 5 1
   1 5 2
   1 5 3
   2 4 7
   2 4 8
   
   query III
   SELECT pk, ok, x FROM (
     SELECT pk, ok, x, row_number() OVER (PARTITION BY pk ORDER BY ok) AS rn
     FROM window_topn_sorted_ties
   ) WHERE rn <= 3
   ORDER BY pk, ok, x;
   ----
   1 5 1
   1 5 2
   1 5 3
   2 4 7
   2 4 8
   
   statement ok
   SET datafusion.explain.physical_plan_only = true;
   
   query TT
   EXPLAIN SELECT pk, ok, x FROM (
     SELECT pk, ok, x, row_number() OVER (PARTITION BY pk ORDER BY ok) AS rn
     FROM window_topn_sorted_ties
   ) WHERE rn <= 3
   ORDER BY pk, ok, x;
   ----
   physical_plan
   01)SortExec: expr=[pk@0 ASC NULLS LAST, ok@1 ASC NULLS LAST, x@2 ASC NULLS 
LAST], preserve_partitioning=[false]
   02)--ProjectionExec: expr=[pk@0 as pk, ok@1 as ok, x@2 as x]
   03)----PartitionedTopKExec: fn=row_number, fetch=3, partition=[pk@0], 
order=[ok@1 ASC NULLS LAST], emit=[row_number() PARTITION BY 
[window_topn_sorted_ties.pk] ORDER BY [window_topn_sorted_ties.ok ASC NULLS 
LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]
   04)------DataSourceExec: file_groups={1 group: 
[[WORKSPACE_ROOT/datafusion/sqllogictest/test_files/scratch/window_topn/sorted_ties.parquet]]},
 projection=[pk, ok, x], output_ordering=[pk@0 ASC NULLS LAST, ok@1 ASC NULLS 
LAST, x@2 ASC NULLS LAST], file_type=parquet
   
   statement ok
   SET datafusion.explain.physical_plan_only = false;
   
   statement ok
   RESET datafusion.optimizer.enable_window_topn;
   
   statement ok
   DROP TABLE window_topn_sorted_ties;
   ```
   
   </details>
   



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