Bug #121871 Index/partition pruning on YEAR: upper bound in 100..1900 or above 2155 gives an impossible range, rows missed

Submitted: 22 Nov 22:53
Reporter: Ke Han Email Updates:
Status: Open Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:26.10.0 (trunk 06bcb0eb0ad), 8.4.13 OS:Any
Assigned to: CPU Architecture:Any

[22 Nov 22:53] Ke Han
Description:
The range optimizer treats an upper bound outside YEAR's domain
({0000} and 1901..2155) as an impossible range, in two cases:

1. A bound in 100..1900 (the hole between 0000 and 1901): with an index on a
   YEAR column, y < 1900, y <= 1500 and y BETWEEN 0 AND 1000 are planned as
   "Zero rows (no matching row in const table)" (the condition itself is not
   rewritten; SHOW WARNINGS after EXPLAIN shows it unchanged), so rows holding
   YEAR 0000 (a valid value, often used for "unknown") are not found. Without
   the index the same statements return them (0000 compares as 0).
2. A bound above 2155: with an index, y BETWEEN 1990 AND 2200 returns nothing;
   with partitioning by the YEAR column (RANGE or HASH), yr < 2200 and
   yr <= 2156 prune every partition (EXPLAIN: partitions NULL), so nothing is
   returned. Partition pruning also drops the 0000 row for the hole bound
   yr <= 1900.

The same plans are used by DELETE, UPDATE and CREATE TABLE ... SELECT, which
then act on no rows: DELETE ... WHERE birth_year < 1900 deletes 0 rows (1
matches), UPDATE ... WHERE birth_year BETWEEN 1990 AND 2200 changes 0 (2
match), CREATE TABLE ... SELECT ... WHERE yr < 2200 copies 0 of 5 rows. There
is no error or warning; default sql_mode and optimizer settings.

It also makes MySQL Shell's chunked dumps of a table with a YEAR primary key
leave out the 0000 row: util.dumpTables('lab', ['yearly'], '/dumps/y1',
{bytesPerChunk: '128k'}) of a 255-row table (y YEAR NOT NULL PRIMARY KEY, years
0000 and 1902..2155) writes 254 rows; its first chunk is
  SELECT `y`,`report` FROM `lab`.`yearly` WHERE (`y` BETWEEN 0 AND 1431) ORDER BY `y`
which returns the 0000 row with IGNORE INDEX and nothing through the index.

Bug #90100 reported yr < 2156 on an indexed YEAR column returning nothing; it
was closed as fixed in 8.0.16 by WL#11935 ("Add folding of constants when
compared to fields"). That folding rewrites a simple comparison before range
analysis (here yr < 2200 on an index becomes yr IS NOT NULL and is correct),
but BETWEEN is not folded, partition pruning still prunes everything (although
the rewritten condition is `sales`.`yr` is not null), and bounds in the hole
are not folded at all: the folding code deliberately treats them as inside the
range "since a year field may contain 0", while the range optimizer treats them
as below the minimum. Bug #121431 (Verified) is about the same branch of
save_value_and_handle_conversion() for a DOUBLE constant against a BIGINT
column; the cause here is a different one (see Suggested fix).

How to repeat:
CREATE DATABASE m219; USE m219;
-- birth_year 0000 = "unknown" (a valid YEAR value)
CREATE TABLE people (id INT PRIMARY KEY, name VARCHAR(20), birth_year YEAR, KEY kby (birth_year));
INSERT INTO people VALUES (1, 'unknown', 0), (2, 'ada', 1915), (3, 'bob', 1999), (4, 'cyd', 2004), (5, 'dee', NULL);

-- 1. YEAR index, upper bound between 100 and 1900: the 0000 row is not found
SELECT id FROM people WHERE birth_year < 1900;
SELECT id FROM people IGNORE INDEX (kby) WHERE birth_year < 1900;
SELECT id FROM people WHERE birth_year BETWEEN 0 AND 1000;
SELECT id FROM people IGNORE INDEX (kby) WHERE birth_year BETWEEN 0 AND 1000;
EXPLAIN FORMAT=TREE SELECT id FROM people WHERE birth_year < 1900;
SHOW WARNINGS;

-- 2. YEAR index, upper bound above 2155: nothing is found
SELECT id FROM people WHERE birth_year BETWEEN 1990 AND 2200;
SELECT id FROM people IGNORE INDEX (kby) WHERE birth_year BETWEEN 1990 AND 2200;

-- 3. partitioning by the YEAR column: every partition is pruned
CREATE TABLE sales (id INT, yr YEAR, amount INT) PARTITION BY RANGE (yr)
  (PARTITION p_old VALUES LESS THAN (2000), PARTITION p_2000s VALUES LESS THAN (2050),
   PARTITION p_late VALUES LESS THAN MAXVALUE);
CREATE TABLE sales_h (id INT, yr YEAR, amount INT) PARTITION BY HASH (yr) PARTITIONS 4;
INSERT INTO sales VALUES (1, 0, 10), (2, 1950, 20), (3, 1999, 30), (4, 2024, 40), (5, 2155, 50);
INSERT INTO sales_h SELECT * FROM sales;
CREATE TABLE sales_plain (id INT, yr YEAR, amount INT);   -- the same rows, not partitioned
INSERT INTO sales_plain SELECT * FROM sales;
SELECT id FROM sales WHERE yr < 2200 ORDER BY id;
SELECT id FROM sales_h WHERE yr <= 2156 ORDER BY id;
SELECT id FROM sales_plain WHERE yr < 2200 ORDER BY id;
SELECT id FROM sales WHERE yr <= 1900 ORDER BY id;
SELECT id FROM sales_plain WHERE yr <= 1900 ORDER BY id;
EXPLAIN FORMAT=TRADITIONAL SELECT id FROM sales WHERE yr < 2200;
SHOW WARNINGS;

-- 4. the same plans in DML
DELETE FROM people WHERE birth_year < 1900;                             -- 1 row expected
UPDATE people SET name = UPPER(name) WHERE birth_year BETWEEN 1990 AND 2200;   -- 2 rows expected
SELECT * FROM people ORDER BY id;
CREATE TABLE sales_copy AS SELECT * FROM sales WHERE yr < 2200;        -- 5 rows expected
SELECT COUNT(*) FROM sales_copy;
DELETE FROM sales WHERE yr <= 1900;                                     -- 1 row expected
SELECT COUNT(*) FROM sales;

Result on 26.10.0 (8.4.13 returns the same rows; there the first SHOW WARNINGS
is empty because EXPLAIN FORMAT=TREE adds no note):
1. SELECT ... WHERE birth_year < 1900:                    Empty set
   ... IGNORE INDEX (kby) WHERE birth_year < 1900:        1
   SELECT ... WHERE birth_year BETWEEN 0 AND 1000:        Empty set
   ... IGNORE INDEX (kby) ... BETWEEN 0 AND 1000:         1
   EXPLAIN: -> Zero rows (no matching row in const table)  (cost=0..0 rows=0)
   SHOW WARNINGS: Note 1003 ... where (`m219`.`people`.`birth_year` < 1900)
2. SELECT ... WHERE birth_year BETWEEN 1990 AND 2200:     Empty set
   ... IGNORE INDEX (kby) ... BETWEEN 1990 AND 2200:      3, 4
3. sales   WHERE yr < 2200:   Empty set
   sales_h WHERE yr <= 2156:  Empty set
   sales_plain WHERE yr < 2200:  1, 2, 3, 4, 5
   sales   WHERE yr <= 1900:  Empty set
   sales_plain WHERE yr <= 1900: 1
   EXPLAIN: | 1 | SIMPLE | sales | NULL | ALL | ... | Using where |   (partitions NULL)
   SHOW WARNINGS: Note 1003 ... where (`m219`.`sales`.`yr` is not null)
4. DELETE ... birth_year < 1900:              Query OK, 0 rows affected
   UPDATE ... BETWEEN 1990 AND 2200:          Rows matched: 0  Changed: 0  Warnings: 0
   people: unchanged, 5 rows, (1, unknown, 0000) still there
   CREATE TABLE sales_copy AS SELECT ...:     Records: 0;  COUNT(*) = 0
   DELETE FROM sales WHERE yr <= 1900:        Query OK, 0 rows affected;  COUNT(*) = 5

Expected: the same rows with and without the index and partitioning: 1 for
the first four queries of part 1, (3, 4) in part 2, all 5 rows for yr < 2200
and yr <= 2156, row 1 for yr <= 1900, and the DML acting on those rows.

Suggested fix:
save_value_and_handle_conversion() stores the constant into the field to
build the range. For TYPE_WARN_OUT_OF_RANGE it assumes that the field's max or
min value has been stored, and it decides between "above the maximum" and
"below the minimum" from the stored value (field->val_int() > 0); below the
minimum, < and <= are an impossible range:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/ra...

Field_year::store(longlong) does not store the max or min: for nr < 0,
100 <= nr < 1901 and nr > 2155 it stores 0 (YEAR 0000) and returns
TYPE_WARN_OUT_OF_RANGE:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
So every out-of-range YEAR constant is read as "below the minimum":
- a bound above 2155 is really above the maximum (< and <= are always true),
  but becomes an impossible range;
- a bound in 100..1900 is above 0000, which is a valid stored value, so
  < and <= must still match the 0000 rows, but they become an impossible
  range.
The same range analysis is used for partition pruning
(JOIN::prune_table_partitions()):
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...

The constant folding of WL#11935 handles the hole explicitly
(RP_INSIDE_YEAR_HOLE: "For other operations, we treat the constants as if it
is inside range, since a year field may contain 0"):
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...

The decision in save_value_and_handle_conversion() should be made from the
constant rather than from the value stored in the field (or YEAR should be
special-cased there like in the constant folding): above 2155, < and <= are
always true; in 100..1900, < and <= should give the range up to 0000.

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

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