Bug #121868 Dump/copy utilities lose rows when the chunking key has a FLOAT column: chunk bounds use the 6-digit FLOAT text

Submitted: 22 Nov 22:51
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:51] Ke Han
Description:
util.dumpInstance(), util.dumpSchemas(), util.dumpTables() and the copy
utilities (util.copyInstance(), util.copySchemas(), util.copyTables()) leave
out rows of a table whose chunking index (the primary key, or the unique key
chosen instead) contains a FLOAT column. The dump and the load succeed,
"0 warnings were reported during the load", and "Rows written" shows the
smaller count. 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 t_tiny has 2 of its 3 rows.

The chunker reads the first and last row of each chunk as text and pastes that
text into the chunk's WHERE clause. The text of a FLOAT value has 6
significant digits: the stored value 3.1415927410125732 becomes 3.14159, and
the dump query of the 3-row table below (from the general log) is
  SELECT `f`,`label` FROM `lab`.`t_tiny` WHERE ((`f`>=1.25)AND(`f`<=3.14159)) ORDER BY `f`
The server compares the FLOAT column with 3.14159 as a double, and
3.1415927410125732 <= 3.14159 is false, so the row that ends the chunk is not
dumped. In general the row that ends a chunk is lost when its text rounds
down, and the row that starts a chunk when its text rounds up.

With the default bytesPerChunk (64M) a small table is one chunk, bounded by the
table's first and last key, so the 3-row table loses its last row with default
options. With more chunks (tables larger than bytesPerChunk, or a smaller
bytesPerChunk) rows are lost at the chunk boundaries: 37 of 3000 rows in the
second table, whose key is (sensor VARCHAR, reading FLOAT), for example
  chunk 0: ... AND(`sensor`<'s0' OR(`sensor`='s0' AND(`reading`<=1.243))))
  chunk 1: ((`sensor`>'s0' OR(`sensor`='s0' AND(`reading`>=1.246))) ...
leave out the row ('s0', 1.243), stored as 1.2430000305175781.
util.copySchemas(['lab'], <second server>) with default options also arrives
without the 'pi' row.

The same values in a DOUBLE key are dumped completely (the text of a DOUBLE
round-trips), and so is the FLOAT table with chunking: false. This is not
about the precision of the dumped FLOAT values (they are written with 6
significant digits, as by mysqldump); whole rows are missing from the dump.

How to repeat:
-- mysql client, as root
CREATE DATABASE lab;
CREATE TABLE lab.t_tiny (f FLOAT NOT NULL PRIMARY KEY, label VARCHAR(20));
INSERT INTO lab.t_tiny VALUES (1.25, 'quarter'), (2.7182817, 'e'), (3.1415927, 'pi');
-- the same values in a DOUBLE key
CREATE TABLE lab.t_double (f DOUBLE NOT NULL PRIMARY KEY, label VARCHAR(20));
INSERT INTO lab.t_double SELECT f, label FROM lab.t_tiny;
-- 3000 sensor readings keyed by (sensor, reading), about 1 KB per row
SET SESSION cte_max_recursion_depth = 100000;
CREATE TABLE lab.t_readings (sensor VARCHAR(8) NOT NULL, reading FLOAT NOT NULL, note VARCHAR(1000),
  PRIMARY KEY (sensor, reading)) STATS_PERSISTENT=1 STATS_SAMPLE_PAGES=10000;
INSERT INTO lab.t_readings WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n + 1 FROM s WHERE n < 3000)
  SELECT CONCAT('s', n % 3), 1 + n / 1000.0 + n / 7777777.0, REPEAT('x', 1000) FROM s;
ANALYZE TABLE lab.t_tiny, lab.t_double, lab.t_readings;
SET GLOBAL local_infile = 1;   -- needed by util.loadDump
SELECT f, CAST(f AS DOUBLE), label FROM lab.t_tiny ORDER BY f;
SELECT COUNT(*) FROM lab.t_tiny WHERE f >= 1.25 AND f <= 3.14159;

// mysqlsh --js, connected to the same server as root
util.dumpTables('lab', ['t_tiny', 't_double'], '/tmp/d1', {showProgress: false})
util.loadDump('/tmp/d1', {schema: 'copy1', showProgress: false})
util.dumpTables('lab', ['t_readings'], '/tmp/d2', {bytesPerChunk: '128k', showProgress: false})
util.loadDump('/tmp/d2', {schema: 'copy2', showProgress: false})
util.dumpTables('lab', ['t_tiny', 't_readings'], '/tmp/d3', {chunking: false, showProgress: false})
util.loadDump('/tmp/d3', {schema: 'copy3', showProgress: false})

-- mysql client
SELECT 'lab.t_tiny' AS t, COUNT(*) FROM lab.t_tiny UNION ALL SELECT 'copy1.t_tiny', COUNT(*) FROM copy1.t_tiny
UNION ALL SELECT 'lab.t_double', COUNT(*) FROM lab.t_double UNION ALL SELECT 'copy1.t_double', COUNT(*) FROM copy1.t_double
UNION ALL SELECT 'lab.t_readings', COUNT(*) FROM lab.t_readings UNION ALL SELECT 'copy2.t_readings', COUNT(*) FROM copy2.t_readings
UNION ALL SELECT 'copy3.t_tiny', COUNT(*) FROM copy3.t_tiny UNION ALL SELECT 'copy3.t_readings', COUNT(*) FROM copy3.t_readings;
SELECT f, label FROM copy1.t_tiny ORDER BY f;

Result (MySQL Shell 26.10.0, server 26.10.0):
  SELECT f, CAST(f AS DOUBLE), label:  1.25 1.25 quarter | 2.71828 2.7182817459106445 e | 3.14159 3.1415927410125732 pi
  COUNT(*) WHERE f >= 1.25 AND f <= 3.14159:  2
  dump d1:  Rows written: 5      (t_tiny 2 + t_double 3)
  load d1:  0 warnings were reported during the load.
  dump d2:  Rows written: 2963
  load d2:  37 chunks (2.96K rows, 3.00 MB) for 1 tables ... 0 warnings were reported during the load.
  dump d3 (chunking: false):  Rows written: 3003
  counts:   lab.t_tiny 3, copy1.t_tiny 2, lab.t_double 3, copy1.t_double 3,
            lab.t_readings 3000, copy2.t_readings 2963, copy3.t_tiny 3, copy3.t_readings 3000
  copy1.t_tiny: (1.25, quarter), (2.71828, e)       <- the 'pi' row is missing

With MySQL Shell 8.4.10 and server 8.4.13: copy1.t_tiny 2 rows, copy2.t_readings
2961 rows, copy3 complete.

Expected: every copy holds all source rows (3, 3 and 3000), as with
chunking: false.

Suggested fix:
Code at mysql-shell 1161d07bad16a57cc728a9bbdb8575f3577483b4 (= 26.10.0):
- fetch_row() reads the boundary rows with row->get_as_string(i), the server's
  text of the value:
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  For a FLOAT that is the 6-digit text (Field_float::val_str(), my_gcvt with
  MY_GCVT_ARG_FLOAT):
  https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
- quote_value() pastes non-string, non-DECIMAL values unchanged, and compare(),
  ge() and between() build each chunk as col >= <first row> AND col <= <last row>:
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
- chunk_column() takes the table's first and last key, and
  chunk_non_integer_column() (used when the first key column is not an integer
  or DECIMAL) takes the boundary rows of each chunk, both through fetch_row():
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...
- select_index() excludes only indexes with ENUM columns (BUG#35180061, the fix
  of Bug #110352), so FLOAT keys are used for chunking:
  https://github.com/mysql/mysql-shell/blob/1161d07bad16a57cc728a9bbdb8575f3577483b4/modules...

Either read FLOAT key columns in a form that round-trips when fetching the
boundary rows (for example CAST(col AS DOUBLE): every FLOAT value is exactly
representable as a DOUBLE, and DOUBLE keys are not affected), or exclude
indexes with FLOAT columns from chunking, as is done for ENUM.

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

Page generated in 0.040 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.