cning112 opened a new issue, #25850:
URL: https://github.com/apache/datafusion/issues/25850

   ### Describe the bug
   With the default physical optimizer rules, a grouped or distinct query that 
sorts by a single group key and applies a limit drops the NULL group.
   
   ### To Reproduce
   DataFusion 53.1.0:
   
   ```sql
   CREATE TABLE t(x BIGINT); -- rows: -301, 100, 500, NULL
   SELECT DISTINCT x FROM t ORDER BY x LIMIT 10;        -- -301, 100, 500       
 (NULL missing)
   SELECT x FROM t GROUP BY x ORDER BY x DESC LIMIT 10; -- 500, 100, -301       
 (NULL missing)
   SELECT DISTINCT x, g FROM t ORDER BY x, g LIMIT 20;  -- NULL rows present 
(two sort keys)
   ```
   
   Setting `optimizer.enable_topk_aggregation = false` restores the NULL group 
in both failing queries.
   
   ### Expected behaviour
   The NULL group is returned in its normal sort position (nulls-last by 
default).
   
   ### Additional context
   Found while cross-checking a SQL engine against DuckDB, where both queries 
return the NULL group. The rule involved is `TopKAggregation` 
(`enable_topk_aggregation`).
   


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