go-mysql-server icon indicating copy to clipboard operation
go-mysql-server copied to clipboard

sort node inappropriately pushed below group by

Open max-hoffman opened this issue 2 years ago • 0 comments

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:

  1. A grouping operator that assumed input sorted by the grouping column would be faster. In this special case, GROUP_BY can wrap SORT.

  2. 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.

max-hoffman avatar Mar 15 '22 23:03 max-hoffman