Skip to content
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: ORDER BY column and aggregate orders only by aggregate #50355

Closed
astefan opened this issue Dec 19, 2019 · 3 comments · Fixed by #51894
Closed

SQL: ORDER BY column and aggregate orders only by aggregate #50355

astefan opened this issue Dec 19, 2019 · 3 comments · Fixed by #51894
Assignees
Labels

Comments

@astefan
Copy link
Contributor

astefan commented Dec 19, 2019

Query to test on SQL's test data: SELECT gender, MIN(salary) AS min, COUNT(*) AS c, MAX(salary) FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY gender ASC, MAX(salary) DESC which gives:

    gender     |      min      |       c       |  MAX(salary)  
---------------+---------------+---------------+---------------
M              |25945.0        |57             |74999.0        
F              |25976.0        |33             |74572.0        
null           |25324.0        |10             |73717.0        

The result seems to indicate that the ordering is done ONLY on the aggregate. Changing the directions of ORDER BYs - SELECT gender, MIN(salary) AS min, COUNT(*) AS c, MAX(salary) FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY gender DESC, MAX(salary) ASC seems to support the above assumption:

    gender     |      min      |       c       |  MAX(salary)  
---------------+---------------+---------------+---------------
null           |25324.0        |10             |73717.0        
F              |25976.0        |33             |74572.0        
M              |25945.0        |57             |74999.0        

Our tests DO cover this scenario but only partially.
In agg-ordering.sql-spec we have

SELECT gender, MIN(salary) AS min, COUNT(*) AS c FROM test_emp GROUP BY gender HAVING c > 1 ORDER BY gender, MAX(salary)

which runs ok, but only because ordering by gender and ordering by MAX(salary) offer the same results for ASC ordering. A better test should also return MAX(salary) as a projection and should mix ASC and DESC as directions of ORDER BY.

@elasticmachine
Copy link
Collaborator

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

matriv added a commit to matriv/elasticsearch that referenced this issue Feb 4, 2020
Previously, in the in-memory sorting module
`LocalAggregationSorterListener` only the aggregate functions where used
(grabbed by the `sortingColumns`). As a consequence, if the ORDER BY
was also using columns of the GROUP BY clause, (especially in the case
of higher priority - before the aggregate functions) wrong results were
produced. E.g.:
```
SELECT gender, MAX(salary) AS max FROM test_emp
GROUP BY gender
ORDER BY gender, max
```

Add all columns of the ORDER BY to the `sortingColumns` so that the
`LocalAggregationSorterListener` can use the correct comparators in
the underlying PriorityQueue used to implement the in-memory sorting.

Fixes: elastic#50355
@matriv matriv self-assigned this Feb 4, 2020
matriv added a commit that referenced this issue Feb 12, 2020
Previously, in the in-memory sorting module
`LocalAggregationSorterListener` only the aggregate functions where used
(grabbed by the `sortingColumns`). As a consequence, if the ORDER BY
was also using columns of the GROUP BY clause, (especially in the case
of higher priority - before the aggregate functions) wrong results were
produced. E.g.:
```
SELECT gender, MAX(salary) AS max FROM test_emp
GROUP BY gender
ORDER BY gender, max
```

Add all columns of the ORDER BY to the `sortingColumns` so that the
`LocalAggregationSorterListener` can use the correct comparators in
the underlying PriorityQueue used to implement the in-memory sorting.

Fixes: #50355
matriv added a commit that referenced this issue Feb 12, 2020
Previously, in the in-memory sorting module
`LocalAggregationSorterListener` only the aggregate functions where used
(grabbed by the `sortingColumns`). As a consequence, if the ORDER BY
was also using columns of the GROUP BY clause, (especially in the case
of higher priority - before the aggregate functions) wrong results were
produced. E.g.:
```
SELECT gender, MAX(salary) AS max FROM test_emp
GROUP BY gender
ORDER BY gender, max
```

Add all columns of the ORDER BY to the `sortingColumns` so that the
`LocalAggregationSorterListener` can use the correct comparators in
the underlying PriorityQueue used to implement the in-memory sorting.

Fixes: #50355
(cherry picked from commit be680af)
matriv added a commit that referenced this issue Feb 12, 2020
Previously, in the in-memory sorting module
`LocalAggregationSorterListener` only the aggregate functions where used
(grabbed by the `sortingColumns`). As a consequence, if the ORDER BY
was also using columns of the GROUP BY clause, (especially in the case
of higher priority - before the aggregate functions) wrong results were
produced. E.g.:
```
SELECT gender, MAX(salary) AS max FROM test_emp
GROUP BY gender
ORDER BY gender, max
```

Add all columns of the ORDER BY to the `sortingColumns` so that the
`LocalAggregationSorterListener` can use the correct comparators in
the underlying PriorityQueue used to implement the in-memory sorting.

Fixes: #50355
(cherry picked from commit be680af)
@matriv
Copy link
Contributor

matriv commented Feb 12, 2020

master : be680af
7.x : daab242
7.6 : 66e1092

matriv added a commit that referenced this issue Feb 12, 2020
Previously, in the in-memory sorting module
`LocalAggregationSorterListener` only the aggregate functions where used
(grabbed by the `sortingColumns`). As a consequence, if the ORDER BY
was also using columns of the GROUP BY clause, (especially in the case
of higher priority - before the aggregate functions) wrong results were
produced. E.g.:
```
SELECT gender, MAX(salary) AS max FROM test_emp
GROUP BY gender
ORDER BY gender, max
```

Add all columns of the ORDER BY to the `sortingColumns` so that the
`LocalAggregationSorterListener` can use the correct comparators in
the underlying PriorityQueue used to implement the in-memory sorting.

Fixes: #50355
(cherry picked from commit be680af)
@matriv
Copy link
Contributor

matriv commented Feb 12, 2020

6.8 : 45ec73d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants