Description:
When an integer column is compared with a DOUBLE constant, the constant
folding converts the double to DECIMAL with double2decimal(), which prints
the double with my_gcvt() (the shortest decimal string that reads back as the
same double, at most 17 significant digits) and parses that string. That
string is not the double's value once the double is above about 10^17. For
example the double 2^60 is exactly 1152921504606846976, but the folded
constant is 1152921504606847000, and the double -2^63 (exactly BIGINT's
minimum) becomes -9223372036854776000, which is below the BIGINT range, so
the comparison is folded to FALSE ("Impossible WHERE"). Rows equal to the
double's exact value are then missed by "=", and "<" and ">" count the wrong
rows. There is no warning.
The per-row comparison of the same values is right (c = @k is 1 for that
row), so the answer depends on the route:
- With a prepared statement (PREPARE/EXECUTE ... USING, and a stored
procedure with a DOUBLE parameter), the folding runs at execution with the
parameter's value: c = ? with 2^60 or -2^63 finds no row; c > ? with -2^63
is folded to c IS NOT NULL and counts the row -2^63 too; on BIGINT UNSIGNED,
c = ? with 9223372036854775808E0 (2^63) finds no row. The same literals in
the query text, and WHERE c = @k, find the row.
- With some literals the plain query is wrong too: WHERE c =
9223372036854774784E0 (the largest double below 2^63, an exact integer) is
folded to c = 9223372036854775000 and misses the row 9223372036854774784.
- A DECIMAL column is folded the same way: WHERE d = 1152921504606846976E0 on
a DECIMAL(25,0) column is folded to d = 1152921504606847000 and misses the
row 1152921504606846976 (which the select-list comparison d =
1152921504606846976E0 calls equal).
The manual (Constant-Folding Optimization) says that for a DOUBLE constant
against an integer column the constant is rounded to an integer, then range
checked; -2^63 and 2^60 are integers and in range, so nothing should change.
Code (trunk 4573da3722):
sql/sql_const_folding.cc, analyze_int_field_constant(), case REAL_RESULT
(the STRING_RESULT case falls through to it):
255 const double v = (*const_val)->val_real();
256 int err = double2decimal(v, &dec);
sql/sql_const_folding.cc, analyze_decimal_field_constant(), case REAL_RESULT:
510 const double v = (*const_val)->val_real();
511 err = double2decimal(v, &val_dec);
mysys/decimal.cc, double2decimal():
1107 const char *end = buff + my_gcvt(from, MY_GCVT_ARG_DOUBLE,
1108 (int)sizeof(buff) - 1, buff, nullptr);
SELECT CAST(1152921504606846976E0 AS DECIMAL(30,0)) shows the same
conversion: 1152921504606847000.
How to repeat:
CREATE DATABASE x;
USE x;
CREATE TABLE t (id INT PRIMARY KEY, c BIGINT, KEY (c));
INSERT INTO t VALUES (1, -9223372036854775808), (2, 1152921504606846976),
(3, 9223372036854774784), (4, 0), (5, 7);
-- the per-row comparison is right:
SET @k = 1152921504606846976E0;
SELECT id, c = @k FROM t WHERE id = 2; -- 2 1
SELECT COUNT(*) FROM t WHERE c = @k; -- 1
-- a prepared statement misses the row:
PREPARE s FROM 'SELECT COUNT(*) FROM t WHERE c = ?';
EXECUTE s USING @k; -- 0 (expected 1)
SET @k = -9223372036854775808E0;
SELECT COUNT(*) FROM t WHERE c = -9223372036854775808E0; -- 1
EXECUTE s USING @k; -- 0 (expected 1)
PREPARE g FROM 'SELECT COUNT(*) FROM t WHERE c > ?';
EXECUTE g USING @k; -- 5 (expected 4)
PREPARE e FROM 'EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM t WHERE c = ?';
EXECUTE e USING @k;
-- -> Zero input rows (Impossible WHERE), aggregated into one output row
SET @k = 1152921504606846976E0;
EXECUTE e USING @k;
-- -> Covering index lookup on t using c (c = 1152921504606847000)
-- a stored procedure with a DOUBLE parameter:
CREATE PROCEDURE p (k DOUBLE) SELECT COUNT(*) FROM t WHERE c = k;
CALL p(1152921504606846976E0); -- 0 (expected 1)
-- a literal that is wrong without a prepared statement:
SELECT COUNT(*) FROM t WHERE c = 9223372036854774784E0; -- 0 (expected 1)
SELECT c = 9223372036854774784E0 FROM t WHERE id = 3; -- 1
EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM t WHERE c = 9223372036854774784E0;
-- -> Covering index lookup on t using c (c = 9223372036854775000)
-- BIGINT UNSIGNED:
CREATE TABLE u (id INT PRIMARY KEY, c BIGINT UNSIGNED, KEY (c));
INSERT INTO u VALUES (1, 9223372036854775808), (2, 0);
PREPARE us FROM 'SELECT COUNT(*) FROM u WHERE c = ?';
SET @k = 9223372036854775808E0;
EXECUTE us USING @k; -- 0 (expected 1)
-- DECIMAL:
CREATE TABLE d (id INT PRIMARY KEY, c DECIMAL(25,0));
INSERT INTO d VALUES (1, 1152921504606846976), (2, 0);
SELECT COUNT(*) FROM d WHERE c = 1152921504606846976E0; -- 0 (expected 1)
SELECT c = 1152921504606846976E0 FROM d WHERE id = 1; -- 1
EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM d WHERE c = 1152921504606846976E0;
-- -> Filter: (d.c = 1152921504606847000)
All answers above are from 26.10.0.1 (trunk 4573da3722); 9.7.4 and 8.4.13
give the same.
Suggested fix:
Convert the double to DECIMAL exactly in the folding, not through the
shortest round-trip string. Every double of magnitude 2^53 or more is an
integer, so for those values the exact integer can be used directly (for
example longlong2decimal() for -2^63 <= v < 2^63 and ulonglong2decimal() for
2^63 <= v < 2^64, or printing the double with all of its digits). The same
applies to the REAL_RESULT case of analyze_decimal_field_constant().
Description: When an integer column is compared with a DOUBLE constant, the constant folding converts the double to DECIMAL with double2decimal(), which prints the double with my_gcvt() (the shortest decimal string that reads back as the same double, at most 17 significant digits) and parses that string. That string is not the double's value once the double is above about 10^17. For example the double 2^60 is exactly 1152921504606846976, but the folded constant is 1152921504606847000, and the double -2^63 (exactly BIGINT's minimum) becomes -9223372036854776000, which is below the BIGINT range, so the comparison is folded to FALSE ("Impossible WHERE"). Rows equal to the double's exact value are then missed by "=", and "<" and ">" count the wrong rows. There is no warning. The per-row comparison of the same values is right (c = @k is 1 for that row), so the answer depends on the route: - With a prepared statement (PREPARE/EXECUTE ... USING, and a stored procedure with a DOUBLE parameter), the folding runs at execution with the parameter's value: c = ? with 2^60 or -2^63 finds no row; c > ? with -2^63 is folded to c IS NOT NULL and counts the row -2^63 too; on BIGINT UNSIGNED, c = ? with 9223372036854775808E0 (2^63) finds no row. The same literals in the query text, and WHERE c = @k, find the row. - With some literals the plain query is wrong too: WHERE c = 9223372036854774784E0 (the largest double below 2^63, an exact integer) is folded to c = 9223372036854775000 and misses the row 9223372036854774784. - A DECIMAL column is folded the same way: WHERE d = 1152921504606846976E0 on a DECIMAL(25,0) column is folded to d = 1152921504606847000 and misses the row 1152921504606846976 (which the select-list comparison d = 1152921504606846976E0 calls equal). The manual (Constant-Folding Optimization) says that for a DOUBLE constant against an integer column the constant is rounded to an integer, then range checked; -2^63 and 2^60 are integers and in range, so nothing should change. Code (trunk 4573da3722): sql/sql_const_folding.cc, analyze_int_field_constant(), case REAL_RESULT (the STRING_RESULT case falls through to it): 255 const double v = (*const_val)->val_real(); 256 int err = double2decimal(v, &dec); sql/sql_const_folding.cc, analyze_decimal_field_constant(), case REAL_RESULT: 510 const double v = (*const_val)->val_real(); 511 err = double2decimal(v, &val_dec); mysys/decimal.cc, double2decimal(): 1107 const char *end = buff + my_gcvt(from, MY_GCVT_ARG_DOUBLE, 1108 (int)sizeof(buff) - 1, buff, nullptr); SELECT CAST(1152921504606846976E0 AS DECIMAL(30,0)) shows the same conversion: 1152921504606847000. How to repeat: CREATE DATABASE x; USE x; CREATE TABLE t (id INT PRIMARY KEY, c BIGINT, KEY (c)); INSERT INTO t VALUES (1, -9223372036854775808), (2, 1152921504606846976), (3, 9223372036854774784), (4, 0), (5, 7); -- the per-row comparison is right: SET @k = 1152921504606846976E0; SELECT id, c = @k FROM t WHERE id = 2; -- 2 1 SELECT COUNT(*) FROM t WHERE c = @k; -- 1 -- a prepared statement misses the row: PREPARE s FROM 'SELECT COUNT(*) FROM t WHERE c = ?'; EXECUTE s USING @k; -- 0 (expected 1) SET @k = -9223372036854775808E0; SELECT COUNT(*) FROM t WHERE c = -9223372036854775808E0; -- 1 EXECUTE s USING @k; -- 0 (expected 1) PREPARE g FROM 'SELECT COUNT(*) FROM t WHERE c > ?'; EXECUTE g USING @k; -- 5 (expected 4) PREPARE e FROM 'EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM t WHERE c = ?'; EXECUTE e USING @k; -- -> Zero input rows (Impossible WHERE), aggregated into one output row SET @k = 1152921504606846976E0; EXECUTE e USING @k; -- -> Covering index lookup on t using c (c = 1152921504606847000) -- a stored procedure with a DOUBLE parameter: CREATE PROCEDURE p (k DOUBLE) SELECT COUNT(*) FROM t WHERE c = k; CALL p(1152921504606846976E0); -- 0 (expected 1) -- a literal that is wrong without a prepared statement: SELECT COUNT(*) FROM t WHERE c = 9223372036854774784E0; -- 0 (expected 1) SELECT c = 9223372036854774784E0 FROM t WHERE id = 3; -- 1 EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM t WHERE c = 9223372036854774784E0; -- -> Covering index lookup on t using c (c = 9223372036854775000) -- BIGINT UNSIGNED: CREATE TABLE u (id INT PRIMARY KEY, c BIGINT UNSIGNED, KEY (c)); INSERT INTO u VALUES (1, 9223372036854775808), (2, 0); PREPARE us FROM 'SELECT COUNT(*) FROM u WHERE c = ?'; SET @k = 9223372036854775808E0; EXECUTE us USING @k; -- 0 (expected 1) -- DECIMAL: CREATE TABLE d (id INT PRIMARY KEY, c DECIMAL(25,0)); INSERT INTO d VALUES (1, 1152921504606846976), (2, 0); SELECT COUNT(*) FROM d WHERE c = 1152921504606846976E0; -- 0 (expected 1) SELECT c = 1152921504606846976E0 FROM d WHERE id = 1; -- 1 EXPLAIN FORMAT=TREE SELECT COUNT(*) FROM d WHERE c = 1152921504606846976E0; -- -> Filter: (d.c = 1152921504606847000) All answers above are from 26.10.0.1 (trunk 4573da3722); 9.7.4 and 8.4.13 give the same. Suggested fix: Convert the double to DECIMAL exactly in the folding, not through the shortest round-trip string. Every double of magnitude 2^53 or more is an integer, so for those values the exact integer can be used directly (for example longlong2decimal() for -2^63 <= v < 2^63 and ulonglong2decimal() for 2^63 <= v < 2^64, or printing the double with all of its digits). The same applies to the REAL_RESULT case of analyze_decimal_field_constant().