[MDEV-13694] Wrong result upon GROUP BY with orderby_uses_equalities=on Created: 2017-08-31 Updated: 2020-09-23 Resolved: 2020-07-09 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Optimizer |
| Affects Version/s: | 10.1, 10.2, 10.3, 10.4, 10.5 |
| Fix Version/s: | 10.6.0 |
| Type: | Bug | Priority: | Critical |
| Reporter: | Elena Stepanova | Assignee: | Varun Gupta (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||
| Description |
|
Originally reported at StackOverflow
The second result is correct. |
| Comments |
| Comment by Sergei Petrunia [ 2017-09-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-09-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-09-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2017-09-12 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This might be related to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-09-16 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Simplified query
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-09-16 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-09-19 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Idea that is decided to be implemented
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2017-12-20 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Decided during the optimizer call to fix this in 10.3 rather than fixing in stable versions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2018-05-04 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Patch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2019-09-21 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Changed priority to Critical as this is required for MDEV-8306 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-09-24 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Review input: http://lists.askmonty.org/pipermail/commits/2019-September/014011.html Needs to be addressed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2019-09-28 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Patch addressing the review | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Sergei Petrunia [ 2019-10-10 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Ok to push the last version of the patch. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-07-09 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
On version earlier than 10.6, the query plan is like
With 10.6 onwards, the query plan is
The sjm scan table uses filesort. There is no usage of temp table from 10.6 onwards for sorting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2020-09-22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
The initially reported wrong result was fixed in 10.2+ by the patch for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Varun Gupta (Inactive) [ 2020-09-23 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
So a bit more summary regarding the changes introduces in this mdev. On 10.2 before
|
MariaDB [test]> EXPLAIN SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 3 | Using filesort |
|
| 1 | PRIMARY | t1 | eq_ref | PRIMARY | PRIMARY | 4 | test.t2.a | 1 | |
|
| 2 | MATERIALIZED | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
3 rows in set (0.00 sec)
|
MariaDB [test]> SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+-----+--------------------+
|
| a | group_concat(t1.b) |
|
+-----+--------------------+
|
| 273 | 3,3,3 |
|
+-----+--------------------+
|
1 row in set (0.00 sec)
|
The plan here is using filesort on the SJM scan table.
This returns incorrect results on 10.2 onwards.
MariaDB [test]> EXPLAIN SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+---------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+---------------------------------+
|
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 3 | Using temporary; Using filesort |
|
| 1 | PRIMARY | t1 | eq_ref | PRIMARY | PRIMARY | 4 | test.t2.a | 1 | |
|
| 2 | MATERIALIZED | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+---------------------------------+
|
3 rows in set (0.03 sec)
|
|
MariaDB [test]> SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+-----+--------------------+
|
| a | group_concat(t1.b) |
|
+-----+--------------------+
|
| 273 | 3 |
|
| 96 | 2 |
|
| 58 | 1 |
|
+-----+--------------------+
|
3 rows in set (0.00 sec)
|
The plan here use temp table for filesort. So here the fix was to disable using filesort on the first table if the first table was a SJM scan table.
This fixed the wrong results as we are using a different execution path now.
MariaDB [test]> EXPLAIN SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 3 | Using filesort |
|
| 1 | PRIMARY | t1 | eq_ref | PRIMARY | PRIMARY | 4 | test.t2.a | 1 | |
|
| 2 | MATERIALIZED | t2 | ALL | NULL | NULL | NULL | NULL | 3 | Using where |
|
+------+--------------+-------------+--------+---------------+---------+---------+-----------+------+----------------+
|
3 rows in set (0.002 sec)
|
|
MariaDB [test]> SELECT t1.a, group_concat(t1.b) FROM t1 WHERE t1.a IN (SELECT a FROM t2 WHERE b=3) GROUP BY t1.a DESC;
|
+-----+--------------------+
|
| a | group_concat(t1.b) |
|
+-----+--------------------+
|
| 273 | 3 |
|
| 96 | 2 |
|
| 58 | 1 |
|
+-----+--------------------+
|
3 rows in set (0.003 sec)
|
On 10.6 the plan uses filesort on the SJM scan table and also gives the correct result.
This would ensure performance gains if the ORDER BY clause is resolved by the first table.