-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Introduce client-side sorting of groups based on aggregate functions. To allow this, the Analyzer has been extended to push down to underlying Aggregate, aggregate function and the Querier has been extended to identify the case and consume the results in order and sort them based on the given columns. The underlying QueryContainer has been slightly modified to allow a view of the underlying values being extracted as the columns used for sorting might not be requested by the user. The PR also adds minor tweaks, mainly related to tree output. Close #35118 (cherry picked from commit 783c9ed)
- Loading branch information
Showing
60 changed files
with
1,343 additions
and
401 deletions.
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 | ||
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.