Fly-a-Kite opened a new issue, #26488:
URL: https://github.com/apache/datafusion/issues/26488

   ### Describe the bug
   
   
   ### A subquery that ends in ORDER BY ... OFFSET n returns the wrong 
surviving rows when its consumer is an aggregate. The logical plan keeps the 
Sort, but the physical plan drops the SortExec below the GlobalLimitExec 
because the aggregate does not require ordered input. OFFSET still has to skip 
rows in the declared order, so the result changes when the physical input row 
order changes.
   ### version:
   ```text
    DataFusion on 53.0.0, 54.0.0 and 55.0.0
   ```
   
   ### To Reproduce
   
   ```python
   
   CREATE TABLE t(id BIGINT, g BIGINT, v BIGINT, s VARCHAR);
   INSERT INTO t VALUES
     (0, 0,  3, '中文'),
     (1, 0,  0, 'a'),
     (2, 1, -1, 'a'),
     (3, 1, -1, 'a'),
     (4, 0,  3, '中文'),
     (5, 0,  3, NULL);
   
   SELECT g, SUM(v) AS sv
   FROM (
     SELECT *
     FROM t
     ORDER BY s ASC NULLS LAST, id ASC NULLS LAST
     OFFSET 1
   ) q
   GROUP BY g
   ORDER BY g;
   
   ```
   
   ### Expected behavior
   
   Expected (ORDER BY s, id is a total order; the two 中文 rows are identical):
   ```text
   g | sv
   0 |  9
   1 | -2
   ```
   DataFusion 55.0.0:
   ```text 
   
   g | sv
   0 |  6
   1 | -2
   ```
   The offset skipped a 中文 row instead of the first a row. It is not an 
aggregate-order artifact: the aggregate reads a different set of rows.
   
   ### Additional context
   
   _No response_
   
   ### Willingness to contribute
   
   None


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