Skip to content

Commit 0a5668f

Browse files
author
Varun Gupta
committed
MDEV-22556: Incorrect result for window function when using encrypt-tmp-files=ON
The issue here is that end_of_file for encrypted temporary IO_CACHE (used by filesort) is updated using lseek. Encryption adds storage overhead and hides it from the caller by recalculating offsets and lengths. Two different IO_CACHE cannot possibly modify the same file because the encryption key is randomly generated and stored in the IO_CACHE. So when the tempfiles are encrypted DO NOT use lseek to change end_of_file. Further observations about updating end_of_file using lseek 1) The end_of_file update is only used for binlog index files 2) The whole point is to update file length when the file was modified via a different file descriptor. 3) The temporary IO_CACHE files can never be modified via a different file descriptor. 4) For encrypted temporary IO_CACHE, end_of_file should not be updated with lseek
1 parent 66f1e28 commit 0a5668f

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#
2+
# Tests when the temporary files are encrypted
3+
#
4+
select @@encrypt_tmp_files;
5+
@@encrypt_tmp_files
6+
1
7+
#
8+
# MDEV-22556: Incorrect result for window function when using encrypt-tmp-files=ON
9+
#
10+
set @save_sort_buffer_size=@@sort_buffer_size;
11+
set sort_buffer_size= 2000;
12+
create table t1( a DECIMAL(12,0) DEFAULT NULL, b VARCHAR(20) DEFAULT NULL, c DECIMAL(12,0) DEFAULT NULL)engine=INNODB;
13+
insert into t1 select seq, seq, seq from seq_1_to_5000;
14+
select count(*) from (select a, b, c, ROW_NUMBER() OVER (PARTITION BY a) FROM t1)q;
15+
count(*)
16+
5000
17+
set @@sort_buffer_size=@save_sort_buffer_size;
18+
drop table t1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--encrypt-tmp_files=ON
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--echo #
2+
--echo # Tests when the temporary files are encrypted
3+
--echo #
4+
5+
source include/have_file_key_management_plugin.inc;
6+
source include/have_sequence.inc;
7+
source include/have_innodb.inc;
8+
9+
select @@encrypt_tmp_files;
10+
11+
--echo #
12+
--echo # MDEV-22556: Incorrect result for window function when using encrypt-tmp-files=ON
13+
--echo #
14+
15+
set @save_sort_buffer_size=@@sort_buffer_size;
16+
set sort_buffer_size= 2000;
17+
create table t1( a DECIMAL(12,0) DEFAULT NULL, b VARCHAR(20) DEFAULT NULL, c DECIMAL(12,0) DEFAULT NULL)engine=INNODB;
18+
insert into t1 select seq, seq, seq from seq_1_to_5000;
19+
select count(*) from (select a, b, c, ROW_NUMBER() OVER (PARTITION BY a) FROM t1)q;
20+
21+
set @@sort_buffer_size=@save_sort_buffer_size;
22+
23+
drop table t1;

mysys/mf_iocache.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ my_bool reinit_io_cache(IO_CACHE *info, enum cache_type type,
504504
if (info->type == WRITE_CACHE)
505505
info->end_of_file= my_b_tell(info);
506506
else
507-
info->end_of_file= mysql_file_seek(info->file, 0L, MY_SEEK_END,
508-
MYF(0));
507+
{
508+
if (!(info->myflags & MY_ENCRYPT))
509+
info->end_of_file= mysql_file_seek(info->file, 0L,
510+
MY_SEEK_END, MYF(0));
511+
}
509512
}
510513
/* flush cache if we want to reuse it */
511514
if (!clear_cache && my_b_flush_io_cache(info,1))

0 commit comments

Comments
 (0)