forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQL: Allow sorting of groups by aggregates
Introduce client-side, custom 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 elastic#35118
- Loading branch information
Showing
55 changed files
with
1,217 additions
and
370 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
// | ||
// 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; | ||
|
||
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); | ||
|
||
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); | ||
|
||
aggNotSpecifiedInTheAggregateWithHaving | ||
SELECT MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY 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); |
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.