Skip to content

Conversation

@matriv
Copy link
Contributor

@matriv matriv commented Feb 11, 2020

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

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
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/SQL)

@elasticmachine
Copy link
Collaborator

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
Copy link
Contributor

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:
Copy link
Contributor

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"

Comment on lines 127 to 135
--------------------------------------------------
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);
--------------------------------------------------
Copy link
Contributor

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.

Copy link
Contributor

@bpintea bpintea left a 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.

@matriv matriv removed request for astefan and costin February 11, 2020 18:44
@matriv
Copy link
Contributor Author

matriv commented Feb 11, 2020

Not 100% correct. Need to recheck the behaviour first.

@matriv matriv requested review from astefan, bpintea and costin February 11, 2020 19:47
@matriv
Copy link
Contributor Author

matriv commented Feb 11, 2020

Fixed the limitation to mention only the aggregate functions.
Scalars on top of the grouping keys can be applied in the ORDER BY.

Copy link
Contributor

@astefan astefan left a 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
Copy link
Contributor

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

Copy link
Contributor

@bpintea bpintea left a 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.

@matriv matriv merged commit 78a1185 into elastic:master Feb 12, 2020
@matriv matriv deleted the fix-52204 branch February 12, 2020 11:54
matriv added a commit that referenced this pull request Feb 12, 2020
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
(cherry picked from commit 78a1185)
matriv added a commit that referenced this pull request Feb 12, 2020
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
(cherry picked from commit 78a1185)
matriv added a commit that referenced this pull request Feb 12, 2020
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
(cherry picked from commit 78a1185)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: [Docs] Add limitation for scalars and Agg Order By

5 participants