-
Notifications
You must be signed in to change notification settings - Fork 25.7k
SQL: [Docs] Add limitation for sorting on aggs #52210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Add a sections to point out that when ordering by an aggregate certain restrictions are applied to the columns used in the `ORDER BY` clause. Only the exact same aggregate function(s) as in the `SELECT` clause and/or the exact same grouping keys must be used. Fixes: elastic#52204
|
Pinging @elastic/es-search (:Search/SQL) |
|
Pinging @elastic/es-docs (>docs) |
| It is possible to run the same queries without a `LIMIT` however in that case if the maximum size (*10000*) is passed, | ||
| an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. | ||
|
|
||
| Moreover, if there are aggregation(s) in the `ORDER BY`, they must be the exact the same exact aggregation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"they must be the exact the same exact aggregation"
| an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. | ||
|
|
||
| Moreover, if there are aggregation(s) in the `ORDER BY`, they must be the exact the same exact aggregation | ||
| expressions(s), as used in the `SELECT` clause. The same applies for the fields that are defined the `GROUP BY` clause: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"that are defined in the GROUP BY"
| -------------------------------------------------- | ||
| SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age % 10, MAX(salary); | ||
| SELECT age, MAX(salary) FROM test GROUP BY gender ORDER BY age, CAST(MAX(salary) AS FLOAT); | ||
| SELECT age % 10, MAX(salary) FROM test GROUP BY gender ORDER BY age, MAX(salary); | ||
| SELECT age, MAX(salary) / 12 AS max_salary FROM test GROUP BY ORDER BY age, MAX(salary); | ||
| -------------------------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be useful to add one-two examples on how to change the statement, so that it works. Up to you.
bpintea
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, left some minor needed corrections.
|
Not 100% correct. Need to recheck the behaviour first. |
|
Fixed the limitation to mention only the aggregate functions. |
astefan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| It is possible to run the same queries without a `LIMIT` however in that case if the maximum size (*10000*) is passed, | ||
| an exception will be returned as {es-sql} is unable to track (and sort) all the results returned. | ||
|
|
||
| Moreover, the aggregation(s) used in the `ORDER BY`, must be only plain aggregate functions. No scalar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"must be only plain aggregate functions" could be rephrased to "can only be ..."
bpintea
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, only left a slight improvement optional suggestion.
Add a section to point out that when ordering by an aggregate
only plain aggregate functions are allowed, no scalars/operators
can be used on top of them.
Fixes: #52204