Bug #121870 Dump/copy utilities lose rows when the chunking key has a SET column: chunks ordered by bitmask, bounded as text

Submitted: 22 Nov 22:52
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:Shell Dump & Load Severity:S2 (Serious)
Version:MySQL Shell 26.10.0 (master 1161d07), se OS:Any
Assigned to: CPU Architecture:Any

[22 Nov 22:52] Ke Han
Description:
util.dumpInstance(), util.dumpSchemas(), util.dumpTables() and the copy
utilities leave out rows, up to the whole table, when the index used for
chunking (the primary key, or the unique key chosen instead) contains a SET
column. The dump and the load succeed, the load reports "0 warnings were
reported during the load", and checksum: true does not detect it: after util.dumpSchemas(['lab'],
'/dumps/c', {checksum: true}) of the schema below and
util.loadDump('/dumps/c', {checksum: true}) into a second, empty server, the
load reports "3 checksums were verified" and acl arrives empty.

The chunker finds the first and last key of the table and of each chunk with
ORDER BY on the index, i.e. in the order of the SET's numeric value, and then
bounds each chunk with col >= 'first' AND col <= 'last', which the server
evaluates as a comparison of the SET's text. For SET('write','read','admin')
the index order is write (1), read (2), write,read (3), admin (4),
read,admin (6), and the dump query of the 5-row table below (general log) is
  SELECT `perms`,`label` FROM `lab`.`acl` WHERE ((`perms`>='write')AND(`perms`<='read,admin')) ORDER BY `perms`
No row satisfies it, since 'write' > 'read,admin' as text, so the table is
dumped empty. In the composite key (username, roles) rows are lost at the
chunk boundaries (19 of 1550 with the default bytesPerChunk, 203 with
bytesPerChunk: '128k', where the chunks also overlap: that dump writes 1686
rows for the 1586 rows of the three tables, the load reports 1.69K rows loaded
and 0 warnings, and the copy holds 1359 rows), and a unique SET key on a table
without a primary key loses 19 of 31 rows. With chunking: false the dump is
complete.

This is the mechanism of Bug #110352: there the key had ENUM columns, whose
index order (member number) also differs from their text order. That was fixed
in MySQL Shell 8.0.34 by ignoring indexes with ENUM columns when choosing the
chunking index (Bug #35180061), and in 8.4.8 for ENUM columns in any position
of the key (Bug #38475999). SET columns have the same property but are not
excluded.

How to repeat:
-- mysql client, as root
CREATE DATABASE lab;
CREATE TABLE lab.acl (perms SET('write','read','admin') NOT NULL PRIMARY KEY, label VARCHAR(20));
INSERT INTO lab.acl VALUES ('write','w'), ('read','r'), ('write,read','rw'), ('admin','a'), ('read,admin','ra');
-- role assignments keyed by (username, roles): 50 users x 31 role sets, about 1 KB per row
CREATE TABLE lab.user_roles (username VARCHAR(16) NOT NULL,
  roles SET('viewer','editor','owner','billing','audit') NOT NULL, note VARCHAR(1000),
  PRIMARY KEY (username, roles)) STATS_PERSISTENT=1 STATS_SAMPLE_PAGES=10000;
INSERT INTO lab.user_roles WITH RECURSIVE u(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM u WHERE n < 50),
  r(m) AS (SELECT 1 UNION ALL SELECT m + 1 FROM r WHERE m < 31)
  SELECT CONCAT('user', LPAD(n, 2, '0')), m, REPEAT('x', 1000) FROM u, r;
-- no primary key: the NOT NULL unique key on the SET column is used for chunking
CREATE TABLE lab.flags (id INT, f SET('viewer','editor','owner','billing','audit') NOT NULL, UNIQUE KEY (f));
INSERT INTO lab.flags WITH RECURSIVE r(m) AS (SELECT 1 UNION ALL SELECT m + 1 FROM r WHERE m < 31) SELECT m, m FROM r;
ANALYZE TABLE lab.acl, lab.user_roles, lab.flags;
SET GLOBAL local_infile = 1;   -- needed by util.loadDump
SELECT perms, perms + 0, label FROM lab.acl ORDER BY perms;
SELECT COUNT(*) FROM lab.acl WHERE perms >= 'write' AND perms <= 'read,admin';

// mysqlsh --js, connected to the same server as root
util.dumpTables('lab', ['acl', 'user_roles', 'flags'], '/tmp/s1', {showProgress: false})
util.loadDump('/tmp/s1', {schema: 'copy1', showProgress: false})
util.dumpTables('lab', ['acl', 'user_roles', 'flags'], '/tmp/s2', {bytesPerChunk: '128k', showProgress: false})
util.loadDump('/tmp/s2', {schema: 'copy2', showProgress: false})
util.dumpTables('lab', ['acl', 'user_roles', 'flags'], '/tmp/s3', {chunking: false, showProgress: false})
util.loadDump('/tmp/s3', {schema: 'copy3', showProgress: false})

-- mysql client
SELECT 'acl' AS t, (SELECT COUNT(*) FROM lab.acl) AS source, (SELECT COUNT(*) FROM copy1.acl) AS copy1,
  (SELECT COUNT(*) FROM copy2.acl) AS copy2, (SELECT COUNT(*) FROM copy3.acl) AS copy3
UNION ALL SELECT 'user_roles', (SELECT COUNT(*) FROM lab.user_roles), (SELECT COUNT(*) FROM copy1.user_roles),
  (SELECT COUNT(*) FROM copy2.user_roles), (SELECT COUNT(*) FROM copy3.user_roles)
UNION ALL SELECT 'flags', (SELECT COUNT(*) FROM lab.flags), (SELECT COUNT(*) FROM copy1.flags),
  (SELECT COUNT(*) FROM copy2.flags), (SELECT COUNT(*) FROM copy3.flags);

Result (MySQL Shell 26.10.0, server 26.10.0):
  SELECT perms, perms + 0, label ... ORDER BY perms:
    write 1 w | read 2 r | write,read 3 rw | admin 4 a | read,admin 6 ra
  COUNT(*) WHERE perms >= 'write' AND perms <= 'read,admin':  0
  dump s1:  Rows written: 1543      load s1: 0 warnings were reported during the load.
  dump s2:  Rows written: 1686      load s2: 0 warnings were reported during the load.
  dump s3 (chunking: false):  Rows written: 1586
+------------+--------+-------+-------+-------+
| t          | source | copy1 | copy2 | copy3 |
+------------+--------+-------+-------+-------+
| acl        |      5 |     0 |     0 |     5 |
| user_roles |   1550 |  1531 |  1347 |  1550 |
| flags      |     31 |    12 |    12 |    31 |
+------------+--------+-------+-------+-------+
With MySQL Shell 8.4.10 and server 8.4.13 the counts are the same.

Expected: every copy holds all source rows (5, 1550 and 31), as with
chunking: false.

Suggested fix:
Code at mysql-shell 1161d07bad16a57cc728a9bbdb8575f3577483b4 (= 26.10.0):
- select_index() drops indexes that contain an ENUM column
  ("BUG#35180061 - do not use indexes on ENUM columns", "BUG#38475999 - check
  all columns") but not SET columns, which dbstring_to_type() maps to
  Type::Set:
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/mysqlsh...
- chunk_column() takes the first and last key with ORDER BY on the index
  (numeric order of the SET), chunk_non_integer_column() the boundary rows of
  each chunk with ... ORDER BY key LIMIT n, 2, and between() turns them into
  col >= 'first' AND col <= 'last' with the value quoted as a string
  (quote_value()), which the server compares as text:
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...

Excluding Type::Set in select_index() as is done for Type::Enum would fix
it (alternatively, the boundaries of ENUM and SET columns could be compared as
numbers, col+0, which matches the index order).

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.041 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.