You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "TheBuilderJR (via GitHub)" <gi...@apache.org> on 2024/12/25 21:23:00 UTC
[I] SELECT * FROM subquery ignores ordering [datafusion]
TheBuilderJR opened a new issue, #13904:
URL: https://github.com/apache/datafusion/issues/13904
### Describe the bug
Generally for almost all other sql query engines I expect
```
SELECT * FROM (
SOME_ORDERED_SUB_QUERY_HERE
) AS query LIMIT 10000
```
to maintain the same order as the subquery, eg.
SELECT * FROM (
SELECT
session.access_token,
COUNT(1) as c
FROM quizchat_chats
GROUP BY session.access_token
ORDER BY c
) AS query LIMIT 10000
But in datafusion, it doesn't! It seemingly returns a randomly ordered list.
### To Reproduce
Run a SELECT * FROM over an ordered subquery
### Expected behavior
ordering is preserved
### 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: github-unsubscribe@datafusion.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] SELECT * FROM subquery ignores ordering [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead closed issue #13904: SELECT * FROM subquery ignores ordering
URL: https://github.com/apache/datafusion/issues/13904
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] SELECT * FROM subquery ignores ordering [datafusion]
Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on issue #13904:
URL: https://github.com/apache/datafusion/issues/13904#issuecomment-2562113713
I think SQL standard doesn't enforce such ordering, but it's possible most engines implemented it as ordered
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] SELECT * FROM subquery ignores ordering [datafusion]
Posted by "jonahgao (via GitHub)" <gi...@apache.org>.
jonahgao commented on issue #13904:
URL: https://github.com/apache/datafusion/issues/13904#issuecomment-2562220507
A subquery is a derived table/relation. A relation is unordered, and you can't rely on its tuples being ordered; different database implementations or queries have different behaviors. In #12003, DataFusion intentionally optimized away unnecessary ordering in subqueries.
Some discussions of other databases:
- [SQLite: Any way to preserve subquery result order in outer query](https://sqlite.org/forum/info/2d5a6e83bb501dffea1207cce3ae09292d1effd656c09471dce1130e9dcf83e6)
- [MariaDB FAQ: Why is ORDER BY in a FROM Subquery Ignored?](https://mariadb.com/kb/en/why-is-order-by-in-a-from-subquery-ignored/)
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] SELECT * FROM subquery ignores ordering [datafusion]
Posted by "comphead (via GitHub)" <gi...@apache.org>.
comphead commented on issue #13904:
URL: https://github.com/apache/datafusion/issues/13904#issuecomment-2564417913
@TheBuilderJR thanks for opening the issue and please let us know if anything still needed. as @ozankabak correctly mentioned the if there is no top level ordering the engine cannot guarantee the final order.
The LIMIT clause gets evaluated after the ORDER clause, so if you rewrite your query
```
SELECT * FROM (
SELECT
session.access_token,
COUNT(1) as c
FROM quizchat_chats
GROUP BY session.access_token
) AS query ORDER BY c LIMIT 10000
``` it should work
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [I] SELECT * FROM subquery ignores ordering [datafusion]
Posted by "ozankabak (via GitHub)" <gi...@apache.org>.
ozankabak commented on issue #13904:
URL: https://github.com/apache/datafusion/issues/13904#issuecomment-2563926173
@jonahgao is correct -- this is not a bug. If the top-level query doesn't specify an ordering, the engine is free to optimize the subquery ordering away.
--
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: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org