go-mysql-server
go-mysql-server copied to clipboard
sort node inappropriately pushed below group by
This plan is invalid:
tmp3> explain select * from b group by x order by x;
+---------------------------------------+
| plan |
+---------------------------------------+
| GroupBy |
| ├─ SelectedExprs(b.x) |
| ├─ Grouping(b.x) |
| └─ Sort(b.x ASC) |
| └─ Projected table access on [x] |
| └─ Exchange(parallelism=12) |
| └─ Table(b) |
there are two considerations:
-
A grouping operator that assumed input sorted by the grouping column would be faster. In this special case, GROUP_BY can wrap SORT.
-
The SORT / GROUP_BY ordering is not valid for all other circumstances. The SORT should wrap the GROUP BY or else the post-hash results may be misordered.