[MDEV-21044] Wrong result when using a smaller size for sort buffer Created: 2019-11-13  Updated: 2019-11-19  Resolved: 2019-11-18

Status: Closed
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.1.44, 10.2.30, 10.3.21, 10.4.11

Type: Bug Priority: Major
Reporter: Varun Gupta (Inactive) Assignee: Varun Gupta (Inactive)
Resolution: Fixed Votes: 0
Labels: None


 Description   

DATASET

create table t1(a varchar(255), b varchar(255), c varchar(255),d int);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);
insert into t1 values ("jkl","mno", "pqr",1),("abc","def","ghi",2);

MariaDB [test]> select * from t1 order  by d;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
+------+------+------+------+
16 rows in set (0.01 sec)
 
MariaDB [test]> set sort_buffer_size=1024;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [test]> select * from t1 order  by d;
+------+------+------+------+
| a    | b    | c    | d    |
+------+------+------+------+
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| jkl  | mno  | pqr  |    1 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
| abc  | def  | ghi  |    2 |
+------+------+------+------+
14 rows in set (0.00 sec)

With smaller sort_buffer_size we get incorrect results, actual expected rows is 16



 Comments   
Comment by Varun Gupta (Inactive) [ 2019-11-15 ]

Investigation
Inside the function filesort:

(gdb) p param.max_keys_per_buffer
$7 = 1

    /*
      Use also the space previously used by string pointers in sort_buffer
      for temporary key storage.
    */
    param.max_keys_per_buffer=((param.max_keys_per_buffer *
                                (param.rec_length + sizeof(char*))) /
                               param.rec_length - 1);

In the example this sets the param.max_keys_per_buffer to 0.

(gdb) p param.max_keys_per_buffer
$8 = 0

The problem with this is later this causes the merge buffers to not read anything as there is no key and one merge buffer is skipped and not written to the output file. Hence we get wrong results.

Comment by Varun Gupta (Inactive) [ 2019-11-15 ]

Patch
http://lists.askmonty.org/pipermail/commits/2019-November/014050.html

Comment by Sergei Petrunia [ 2019-11-17 ]

Ok to push.

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