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

   ### Is your feature request related to a problem or challenge?
   
   Currently, datafusion supports pushing down the `10` in such queries into 
the aggregate operation, but when there is no `order by` clause in the query, 
it cannot be pushed down.
   
   ```sql
   SELECT "UserID", MIN("AdvEngineID") FROM hits GROUP BY "UserID" order by 
MIN("AdvEngineID") LIMIT 10;
   ```
   
   ### Describe the solution you'd like
   
    For the following query without an ORDER BY clause, it is a 
non-deterministic query where returning aggregate results for any 10 keys is 
valid. Therefore, the simplest optimization method is to make the aggregate 
operation work in an ordered manner.
    
    ```sql
   SELECT "UserID", MIN("AdvEngineID") FROM hits GROUP BY "UserID" order by 
MIN("AdvEngineID") LIMIT 10;
   ```
   
   When using ORDER BY, it runs faster
   
   ```bash
   > SELECT "UserID", MIN("AdvEngineID") FROM hits GROUP BY "UserID" order by 
MIN("AdvEngineID") LIMIT 10;
   +---------------------+-----------------------+
   | UserID              | min(hits.AdvEngineID) |
   +---------------------+-----------------------+
   | 8265925904823819813 | 0                     |
   | 4187744066815097140 | 0                     |
   | 7970073217952173230 | 0                     |
   | 1427747163043990597 | 0                     |
   | 844431718317972727  | 0                     |
   | 5777095312382298493 | 0                     |
   | 1726996514541263413 | 0                     |
   | 9126602271142633613 | 0                     |
   | 137961262398427389  | 0                     |
   | 1015195936484711824 | 0                     |
   +---------------------+-----------------------+
   10 row(s) fetched.
   Elapsed 0.389 seconds.
   
   > SELECT "UserID", MIN("AdvEngineID") FROM hits GROUP BY "UserID" LIMIT 10;
   +--------------------+-----------------------+
   | UserID             | min(hits.AdvEngineID) |
   +--------------------+-----------------------+
   | 572919489234519776 | 0                     |
   | 573305205053433738 | 0                     |
   | 573316808606264402 | 0                     |
   | 573764012887855352 | 0                     |
   | 573878311420505425 | 0                     |
   | 574390391777319344 | 0                     |
   | 574747808491966822 | 0                     |
   | 574822330152391359 | 0                     |
   | 574882842092706724 | 0                     |
   | 576132580282803970 | 0                     |
   +--------------------+-----------------------+
   10 row(s) fetched.
   Elapsed 0.632 seconds.
   ```
   
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   _No response_


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