-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
SQL: Allow sorting of groups by aggregates #38042
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e694c10
SQL: Allow sorting of groups by aggregates
costin d3e3174
Pass column size explicitly since the schema is lost on paging calls
costin 4e69a52
First round of feedback
costin 19cc344
Merge remote-tracking branch 'remotes/upstream/master' into fix-35118
costin a745da8
Address feedback
costin a4d3434
wip
costin b4dc577
Merge branch 'master' into fix-35118
costin c772e69
wip
costin 8b4792a
Merge branch 'master' into fix-35118
costin 292cdef
Address feedback
costin 492b3d3
Tie breaker tests
costin cd2239f
Merge remote-tracking branch 'remotes/upstream/master' into fix-35118
costin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
x-pack/plugin/sql/qa/src/main/resources/agg-ordering.sql-spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Custom sorting/ordering on aggregates | ||
// | ||
|
||
countWithImplicitGroupBy | ||
SELECT MAX(salary) AS m FROM test_emp ORDER BY COUNT(*); | ||
|
||
countWithImplicitGroupByWithHaving | ||
SELECT MAX(salary) AS m FROM test_emp HAVING MIN(salary) > 1 ORDER BY COUNT(*); | ||
|
||
countAndMaxWithImplicitGroupBy | ||
SELECT MAX(salary) AS m FROM test_emp ORDER BY MAX(salary), COUNT(*); | ||
|
||
maxWithAliasWithImplicitGroupBy | ||
SELECT MAX(salary) AS m FROM test_emp ORDER BY m; | ||
|
||
maxWithAliasWithImplicitGroupByAndHaving | ||
SELECT MAX(salary) AS m FROM test_emp HAVING COUNT(*) > 1 ORDER BY m; | ||
|
||
multipleOrderWithImplicitGroupByWithHaving | ||
SELECT MAX(salary) AS m FROM test_emp HAVING MIN(salary) > 1 ORDER BY COUNT(*), m DESC; | ||
|
||
multipleOrderWithImplicitGroupByWithoutAlias | ||
SELECT MAX(salary) AS m FROM test_emp HAVING MIN(salary) > 1 ORDER BY COUNT(*), MIN(salary) DESC; | ||
|
||
multipleOrderWithImplicitGroupByOfOrdinals | ||
SELECT MAX(salary) AS max, MIN(salary) AS min FROM test_emp HAVING MIN(salary) > 1 ORDER BY 1, COUNT(*), 2 DESC; | ||
|
||
aggWithoutAlias | ||
SELECT MAX(salary) AS max FROM test_emp GROUP BY gender ORDER BY MAX(salary); | ||
|
||
aggWithAlias | ||
SELECT MAX(salary) AS m FROM test_emp GROUP BY gender ORDER BY m; | ||
|
||
multipleAggsThatGetRewrittenWithoutAlias | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. max and avg and count get rewritten/replaced with a Stats agg. |
||
SELECT MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY gender ORDER BY MAX(salary); | ||
|
||
multipleAggsThatGetRewrittenWithAliasDesc | ||
SELECT MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY gender ORDER BY 1 DESC; | ||
|
||
multipleAggsThatGetRewrittenWithAlias | ||
SELECT MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY gender ORDER BY max; | ||
|
||
aggNotSpecifiedInTheAggregate | ||
SELECT MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender ORDER BY MAX(salary); | ||
|
||
aggNotSpecifiedInTheAggregatePlusOrdinal | ||
SELECT MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender ORDER BY MAX(salary), 2 DESC; | ||
|
||
aggNotSpecifiedInTheAggregateWithHaving | ||
SELECT MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY MAX(salary); | ||
|
||
aggNotSpecifiedInTheAggregateWithHavingDesc | ||
SELECT MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY MAX(salary) DESC; | ||
|
||
aggNotSpecifiedInTheAggregateAndGroupWithHaving | ||
SELECT gender, MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY MAX(salary), gender; | ||
|
||
groupAndAggNotSpecifiedInTheAggregateWithHaving | ||
SELECT gender, MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY gender, MAX(salary); | ||
|
||
multipleAggsThatGetRewrittenWithAliasOnAMediumGroupBy | ||
SELECT languages, MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY languages ORDER BY max; | ||
|
||
multipleAggsThatGetRewrittenWithAliasOnALargeGroupBy | ||
SELECT emp_no, MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY emp_no ORDER BY max; | ||
|
||
multipleAggsThatGetRewrittenWithAliasOnAMediumGroupByWithHaving | ||
SELECT languages, MAX(salary) AS max, MIN(salary) AS min FROM test_emp GROUP BY languages HAVING min BETWEEN 1000 AND 99999 ORDER BY max; | ||
|
||
aggNotSpecifiedInTheAggregatemultipleAggsThatGetRewrittenWithAliasOnALargeGroupBy | ||
SELECT emp_no, MIN(salary) AS min FROM test_emp GROUP BY emp_no ORDER BY MAX(salary); | ||
|
||
aggNotSpecifiedWithHavingOnLargeGroupBy | ||
SELECT MAX(salary) AS max FROM test_emp GROUP BY emp_no HAVING AVG(salary) > 1000 ORDER BY MIN(salary); | ||
|
||
aggWithTieBreakerDescAsc | ||
SELECT emp_no, MIN(languages) AS min FROM test_emp GROUP BY emp_no ORDER BY MIN(languages) DESC NULLS FIRST, emp_no ASC; | ||
|
||
aggWithTieBreakerDescDesc | ||
SELECT emp_no, MIN(languages) AS min FROM test_emp GROUP BY emp_no ORDER BY MIN(languages) DESC NULLS FIRST, emp_no DESC; | ||
|
||
aggWithTieBreakerAscDesc | ||
SELECT emp_no, MIN(languages) AS min FROM test_emp GROUP BY emp_no ORDER BY MAX(languages) ASC NULLS FIRST, emp_no DESC; | ||
|
||
aggWithMixOfOrdinals | ||
SELECT gender AS g, MAX(salary) AS m FROM test_emp GROUP BY gender ORDER BY 2 DESC LIMIT 3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don't find the value of having all these different cases without group by, as only 1 value is returned and the ORDER BY doesn't really apply, or am I missing something?
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.
Indeed but there's a dedicated rule in the optimizer for this case that excludes the order (as there's no physical group for it).