[MDEV-25441] WITH TIES is not respected with SQL_BUFFER_RESULT and constant in ORDER BY Created: 2021-04-17  Updated: 2021-05-08  Resolved: 2021-05-08

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: N/A
Fix Version/s: 10.6.0

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Vicențiu Ciorbaru
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-23908 Implement SELECT ... OFFSET ... FETCH... Closed

 Description   

Maybe the query can be converted into something more meaningful, I just kept it minimal to demonstrate the problem. Or maybe it really only affects such useless queries, in this case feel free to decrease the priority.

CREATE TABLE t1 (a INT);
INSERT INTO t1 WITH RECURSIVE cte AS (SELECT 1 AS f UNION ALL SELECT f+1 FROM cte WHERE f<10) SELECT * FROM cte;
 
SELECT SQL_BUFFER_RESULT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;
SELECT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;

The base query returns all 10 rows as expected. The one with SQL_BUFFER_RESULT returns 2 rows.

bb-10.6-refactor-limit-review 4bd13ff8

MariaDB [test]> SELECT SQL_BUFFER_RESULT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;
+---+
| f |
+---+
| 1 |
| 1 |
+---+
2 rows in set (0.001 sec)
 
 
MariaDB [test]> SELECT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;
+---+
| f |
+---+
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
| 1 |
+---+
10 rows in set (0.002 sec)

Plans:

with SQL_BUFFER_RESULT

MariaDB [test]> explain extended SELECT SQL_BUFFER_RESULT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra           |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------+
|    1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 10   |   100.00 | Using temporary |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-----------------+
1 row in set, 1 warning (0.001 sec)
 
MariaDB [test]> show warnings;
+-------+------+---------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                     |
+-------+------+---------------------------------------------------------------------------------------------+
| Note  | 1003 | select sql_buffer_result 1 AS `f` from `test`.`t1` order by '' fetch first 2 rows with ties |
+-------+------+---------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)

Base query

MariaDB [test]> explain extended SELECT 1 AS f FROM t1 ORDER BY f FETCH NEXT 2 ROW WITH TIES;
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------+
| id   | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------+
|    1 | SIMPLE      | t1    | ALL  | NULL          | NULL | NULL    | NULL | 10   |   100.00 |       |
+------+-------------+-------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.001 sec)
 
MariaDB [test]> show warnings;
+-------+------+---------------------------------------------------------------------------+
| Level | Code | Message                                                                   |
+-------+------+---------------------------------------------------------------------------+
| Note  | 1003 | select 1 AS `f` from `test`.`t1` order by '' fetch first 2 rows with ties |
+-------+------+---------------------------------------------------------------------------+
1 row in set (0.000 sec)



 Comments   
Comment by Vicențiu Ciorbaru [ 2021-04-18 ]

elenst This was hard to track, but easy to fix. Fix pushed to https://github.com/MariaDB/server/commit/f439c32a2eaa01b8b6e5496eb9e4bf1dcc080db4,
part of bb-10.6-refactor-limit-review

Leaving it open until we push the whole feature.

Generated at Thu Feb 08 09:37:43 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.