Skip to content

Commit 79d8de6

Browse files
author
Gleb Shchepa
committed
Bug #45360: wrong results
Propagation of a large unsigned numeric constant in the WHERE expression led to wrong result. For example, "WHERE a = CAST(0xFFFFFFFFFFFFFFFF AS USIGNED) AND FOO(a)", where a is an UNSIGNED BIGINT, and FOO() accepts strings, was transformed to "... AND FOO('-1')". That has been fixed. Also EXPLAIN EXTENDED printed incorrect numeric constants in transformed WHERE expressions like above. That has been fixed too.
1 parent 3dc1646 commit 79d8de6

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

mysql-test/r/bigint.result

+34
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,37 @@ describe t1;
404404
Field Type Null Key Default Extra
405405
bi decimal(19,0) NO 0
406406
drop table t1;
407+
#
408+
# Bug #45360: wrong results
409+
#
410+
CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY,
411+
a BIGINT(20) UNSIGNED,
412+
b VARCHAR(20));
413+
INSERT INTO t1 (a) VALUES
414+
(0),
415+
(CAST(0x7FFFFFFFFFFFFFFF AS UNSIGNED)),
416+
(CAST(0x8000000000000000 AS UNSIGNED)),
417+
(CAST(0xFFFFFFFFFFFFFFFF AS UNSIGNED));
418+
UPDATE t1 SET b = a;
419+
# FFFFFFFFFFFFFFFF
420+
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE a = 18446744073709551615 AND TRIM(a) = b;
421+
SHOW WARNINGS;
422+
Level Code Message
423+
Note 1003 select 1 AS `1` from `test`.`t1` where ((`test`.`t1`.`a` = 18446744073709551615) and ('18446744073709551615' = `test`.`t1`.`b`))
424+
# 8000000000000000
425+
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE a = 9223372036854775808 AND TRIM(a) = b;
426+
SHOW WARNINGS;
427+
Level Code Message
428+
Note 1003 select 1 AS `1` from `test`.`t1` where ((`test`.`t1`.`a` = 9223372036854775808) and ('9223372036854775808' = `test`.`t1`.`b`))
429+
# 7FFFFFFFFFFFFFFF
430+
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE a = 9223372036854775807 AND TRIM(a) = b;
431+
SHOW WARNINGS;
432+
Level Code Message
433+
Note 1003 select 1 AS `1` from `test`.`t1` where ((`test`.`t1`.`a` = 9223372036854775807) and ('9223372036854775807' = `test`.`t1`.`b`))
434+
# 0
435+
EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE a = 0 AND TRIM(a) = b;
436+
SHOW WARNINGS;
437+
Level Code Message
438+
Note 1003 select 1 AS `1` from `test`.`t1` where ((`test`.`t1`.`a` = 0) and ('0' = `test`.`t1`.`b`))
439+
DROP TABLE t1;
440+
# End of 5.1 tests

mysql-test/t/bigint.test

+35
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,38 @@ drop table t1;
327327
create table t1 select -9223372036854775809 bi;
328328
describe t1;
329329
drop table t1;
330+
331+
--echo #
332+
--echo # Bug #45360: wrong results
333+
--echo #
334+
335+
CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY,
336+
a BIGINT(20) UNSIGNED,
337+
b VARCHAR(20));
338+
339+
INSERT INTO t1 (a) VALUES
340+
(0),
341+
(CAST(0x7FFFFFFFFFFFFFFF AS UNSIGNED)),
342+
(CAST(0x8000000000000000 AS UNSIGNED)),
343+
(CAST(0xFFFFFFFFFFFFFFFF AS UNSIGNED));
344+
345+
UPDATE t1 SET b = a;
346+
347+
let $n = `SELECT MAX(id) FROM t1`;
348+
while($n) {
349+
let $x = `SELECT a FROM t1 WHERE id = $n`;
350+
dec $n;
351+
let $hex = `SELECT HEX($x)`;
352+
echo # $hex;
353+
354+
--disable_result_log
355+
eval EXPLAIN EXTENDED SELECT 1 FROM t1 WHERE a = $x AND TRIM(a) = b;
356+
--enable_result_log
357+
SHOW WARNINGS;
358+
}
359+
360+
DROP TABLE t1;
361+
362+
--echo # End of 5.1 tests
363+
364+

sql/item.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -2209,14 +2209,14 @@ String *Item_int::val_str(String *str)
22092209
{
22102210
// following assert is redundant, because fixed=1 assigned in constructor
22112211
DBUG_ASSERT(fixed == 1);
2212-
str->set(value, &my_charset_bin);
2212+
str->set_int(value, unsigned_flag, &my_charset_bin);
22132213
return str;
22142214
}
22152215

22162216
void Item_int::print(String *str, enum_query_type query_type)
22172217
{
22182218
// my_charset_bin is good enough for numbers
2219-
str_value.set(value, &my_charset_bin);
2219+
str_value.set_int(value, unsigned_flag, &my_charset_bin);
22202220
str->append(str_value);
22212221
}
22222222

0 commit comments

Comments
 (0)