Skip to content

Commit 4257442

Browse files
author
Alexander Barkov
committed
MDEV-8699 Wrong result for SELECT..WHERE HEX(date_column)!='323030312D30312D3031' AND date_column='2001-01-01x'
1 parent 2029163 commit 4257442

12 files changed

+832
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
# Trailing garbage in string literals
3+
--eval CREATE TABLE t1 (a $TYPE);
4+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
5+
SELECT * FROM t1 WHERE a='2001-01-01 00:00:00x';
6+
SELECT * FROM t1 WHERE LENGTH(a) != 20;
7+
SELECT * FROM t1 WHERE LENGTH(a) != 20 AND a='2001-01-01 00:00:00x';
8+
EXPLAIN EXTENDED
9+
SELECT * FROM t1 WHERE LENGTH(a) != 20 AND a='2001-01-01 00:00:00x';
10+
EXPLAIN EXTENDED
11+
SELECT * FROM t1 WHERE LENGTH(a)!=30+RAND() AND a='2001-01-01 00:00:00x';
12+
DROP TABLE t1;
13+
14+
# Leading spaces in string literals
15+
--eval CREATE TABLE t1 (a $TYPE);
16+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
17+
SELECT * FROM t1 WHERE LENGTH(a)=19;
18+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=' 2001-01-01 00:00:00';
19+
EXPLAIN EXTENDED
20+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=' 2001-01-01 00:00:00';
21+
EXPLAIN EXTENDED
22+
SELECT * FROM t1 WHERE LENGTH(a)=19+RAND() AND a=' 2001-01-01 00:00:00';
23+
# This should not propagate
24+
EXPLAIN EXTENDED
25+
SELECT * FROM t1 WHERE LENGTH(a)=30+RAND() AND a=' garbage ';
26+
DROP TABLE t1;
27+
28+
# Trailing fractional digits in temporal literals
29+
--eval CREATE TABLE t1 (a $TYPE);
30+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
31+
SELECT * FROM t1 WHERE a=TIMESTAMP'2001-01-01 00:00:00.000000';
32+
SELECT * FROM t1 WHERE LENGTH(a)=19;
33+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
34+
EXPLAIN EXTENDED
35+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
36+
EXPLAIN EXTENDED
37+
SELECT * FROM t1 WHERE LENGTH(a)=30+RAND() AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
38+
DROP TABLE t1;
39+
40+
# Trailing fractional digits in temporal literals, the same precision
41+
--eval CREATE TABLE t1 (a $TYPE(6));
42+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00.000000'),('2001-01-01 00:00:01.000000');
43+
SELECT * FROM t1 WHERE a=TIMESTAMP'2001-01-01 00:00:00.000000';
44+
SELECT * FROM t1 WHERE LENGTH(a)=26;
45+
SELECT * FROM t1 WHERE LENGTH(a)=26 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
46+
EXPLAIN EXTENDED
47+
SELECT * FROM t1 WHERE LENGTH(a)=26 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
48+
EXPLAIN EXTENDED
49+
SELECT * FROM t1 WHERE LENGTH(a)=40+RAND() AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
50+
DROP TABLE t1;
51+
52+
# DATETIME/TIMESTAMP column vs TIME literal
53+
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30');
54+
--eval CREATE TABLE t1 (a $TYPE);
55+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
56+
SELECT * FROM t1 WHERE a=TIME'00:00:00';
57+
SELECT * FROM t1 WHERE LENGTH(a)=19;
58+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIME'00:00:00';
59+
EXPLAIN EXTENDED
60+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIME'00:00:00';
61+
EXPLAIN EXTENDED
62+
SELECT * FROM t1 WHERE LENGTH(a)=40+RAND() AND a=TIME'00:00:00';
63+
DROP TABLE t1;
64+

mysql-test/r/type_date.result

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,3 +513,86 @@ DROP TABLE t1;
513513
#
514514
# End of 10.1 tests
515515
#
516+
#
517+
# Start of 10.1 tests
518+
#
519+
#
520+
# MDEV-8699 Wrong result for SELECT..WHERE HEX(date_column)!='323030312D30312D3031' AND date_column='2001-01-01x'
521+
#
522+
CREATE TABLE t1 (a DATE);
523+
INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-02');
524+
SELECT * FROM t1 WHERE a='2001-01-01x';
525+
a
526+
2001-01-01
527+
Warnings:
528+
Warning 1292 Truncated incorrect date value: '2001-01-01x'
529+
SELECT * FROM t1 WHERE HEX(a)!='323030312D30312D3031';
530+
a
531+
2001-01-02
532+
SELECT * FROM t1 WHERE HEX(a)!='323030312D30312D3031' AND a='2001-01-01x';
533+
a
534+
Warnings:
535+
Warning 1292 Truncated incorrect date value: '2001-01-01x'
536+
EXPLAIN EXTENDED
537+
SELECT * FROM t1 WHERE HEX(a)!='323030312D30312D3031' AND a='2001-01-01x';
538+
id select_type table type possible_keys key key_len ref rows filtered Extra
539+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
540+
Warnings:
541+
Warning 1292 Truncated incorrect date value: '2001-01-01x'
542+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
543+
EXPLAIN EXTENDED
544+
SELECT * FROM t1 WHERE HEX(a)!=CONCAT('xx',RAND()) AND a='2001-01-01x';
545+
id select_type table type possible_keys key key_len ref rows filtered Extra
546+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
547+
Warnings:
548+
Warning 1292 Truncated incorrect date value: '2001-01-01x'
549+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = '2001-01-01x') and (<cache>(hex(DATE'2001-01-01')) <> concat('xx',rand())))
550+
DROP TABLE t1;
551+
CREATE TABLE t1 (a DATE);
552+
INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-02');
553+
SELECT * FROM t1 WHERE LENGTH(a)=11;
554+
a
555+
SELECT * FROM t1 WHERE LENGTH(a)=11 AND a=' 2001-01-01';
556+
a
557+
EXPLAIN EXTENDED
558+
SELECT * FROM t1 WHERE LENGTH(a)=11 AND a=' 2001-01-01';
559+
id select_type table type possible_keys key key_len ref rows filtered Extra
560+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
561+
Warnings:
562+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
563+
EXPLAIN EXTENDED
564+
SELECT * FROM t1 WHERE LENGTH(a)=11+RAND() AND a=' 2001-01-01';
565+
id select_type table type possible_keys key key_len ref rows filtered Extra
566+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
567+
Warnings:
568+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = ' 2001-01-01') and (<cache>(length(DATE'2001-01-01')) = (11 + rand())))
569+
EXPLAIN EXTENDED
570+
SELECT * FROM t1 WHERE LENGTH(a)=11+RAND() AND a=' garbage ';
571+
id select_type table type possible_keys key key_len ref rows filtered Extra
572+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
573+
Warnings:
574+
Warning 1292 Incorrect datetime value: ' garbage '
575+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = ' garbage ') and (length(`test`.`t1`.`a`) = (11 + rand())))
576+
DROP TABLE t1;
577+
CREATE TABLE t1 (a DATE);
578+
INSERT INTO t1 VALUES ('2001-01-01'),('2001-01-01');
579+
SELECT * FROM t1 WHERE LENGTH(a)=8;
580+
a
581+
SELECT * FROM t1 WHERE LENGTH(a)=8 AND a='20010101';
582+
a
583+
EXPLAIN EXTENDED
584+
SELECT * FROM t1 WHERE LENGTH(a)=8 AND a='20010101';
585+
id select_type table type possible_keys key key_len ref rows filtered Extra
586+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
587+
Warnings:
588+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
589+
EXPLAIN EXTENDED
590+
SELECT * FROM t1 WHERE LENGTH(a)=8+RAND() AND a='20010101';
591+
id select_type table type possible_keys key key_len ref rows filtered Extra
592+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
593+
Warnings:
594+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = '20010101') and (<cache>(length(DATE'2001-01-01')) = (8 + rand())))
595+
DROP TABLE t1;
596+
#
597+
# End of 10.1 tests
598+
#

mysql-test/r/type_datetime.result

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,5 +945,145 @@ CAST(CAST(TIMESTAMP'0000-00-00 00:00:00.000001' AS TIME(6)) AS DATETIME(6))
945945
SET old_mode=DEFAULT;
946946
SET sql_mode=DEFAULT;
947947
#
948+
# MDEV-8699 Wrong result for SELECT..WHERE HEX(date_column)!='323030312D30312D3031' AND date_column='2001-01-01x'
949+
#
950+
CREATE TABLE t1 (a DATETIME);;
951+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
952+
SELECT * FROM t1 WHERE a='2001-01-01 00:00:00x';
953+
a
954+
2001-01-01 00:00:00
955+
Warnings:
956+
Warning 1292 Truncated incorrect datetime value: '2001-01-01 00:00:00x'
957+
SELECT * FROM t1 WHERE LENGTH(a) != 20;
958+
a
959+
2001-01-01 00:00:00
960+
2001-01-01 00:00:01
961+
SELECT * FROM t1 WHERE LENGTH(a) != 20 AND a='2001-01-01 00:00:00x';
962+
a
963+
2001-01-01 00:00:00
964+
Warnings:
965+
Warning 1292 Truncated incorrect datetime value: '2001-01-01 00:00:00x'
966+
Warning 1292 Truncated incorrect datetime value: '2001-01-01 00:00:00x'
967+
EXPLAIN EXTENDED
968+
SELECT * FROM t1 WHERE LENGTH(a) != 20 AND a='2001-01-01 00:00:00x';
969+
id select_type table type possible_keys key key_len ref rows filtered Extra
970+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
971+
Warnings:
972+
Warning 1292 Truncated incorrect datetime value: '2001-01-01 00:00:00x'
973+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = '2001-01-01 00:00:00x')
974+
EXPLAIN EXTENDED
975+
SELECT * FROM t1 WHERE LENGTH(a)!=30+RAND() AND a='2001-01-01 00:00:00x';
976+
id select_type table type possible_keys key key_len ref rows filtered Extra
977+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
978+
Warnings:
979+
Warning 1292 Truncated incorrect datetime value: '2001-01-01 00:00:00x'
980+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = '2001-01-01 00:00:00x') and (<cache>(length(TIMESTAMP'2001-01-01 00:00:00')) <> (30 + rand())))
981+
DROP TABLE t1;
982+
CREATE TABLE t1 (a DATETIME);;
983+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
984+
SELECT * FROM t1 WHERE LENGTH(a)=19;
985+
a
986+
2001-01-01 00:00:00
987+
2001-01-01 00:00:01
988+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=' 2001-01-01 00:00:00';
989+
a
990+
2001-01-01 00:00:00
991+
EXPLAIN EXTENDED
992+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=' 2001-01-01 00:00:00';
993+
id select_type table type possible_keys key key_len ref rows filtered Extra
994+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
995+
Warnings:
996+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = ' 2001-01-01 00:00:00')
997+
EXPLAIN EXTENDED
998+
SELECT * FROM t1 WHERE LENGTH(a)=19+RAND() AND a=' 2001-01-01 00:00:00';
999+
id select_type table type possible_keys key key_len ref rows filtered Extra
1000+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1001+
Warnings:
1002+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = ' 2001-01-01 00:00:00') and (<cache>(length(TIMESTAMP'2001-01-01 00:00:00')) = (19 + rand())))
1003+
EXPLAIN EXTENDED
1004+
SELECT * FROM t1 WHERE LENGTH(a)=30+RAND() AND a=' garbage ';
1005+
id select_type table type possible_keys key key_len ref rows filtered Extra
1006+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1007+
Warnings:
1008+
Warning 1292 Incorrect datetime value: ' garbage '
1009+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = ' garbage ') and (length(`test`.`t1`.`a`) = (30 + rand())))
1010+
DROP TABLE t1;
1011+
CREATE TABLE t1 (a DATETIME);;
1012+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
1013+
SELECT * FROM t1 WHERE a=TIMESTAMP'2001-01-01 00:00:00.000000';
1014+
a
1015+
2001-01-01 00:00:00
1016+
SELECT * FROM t1 WHERE LENGTH(a)=19;
1017+
a
1018+
2001-01-01 00:00:00
1019+
2001-01-01 00:00:01
1020+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1021+
a
1022+
2001-01-01 00:00:00
1023+
EXPLAIN EXTENDED
1024+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1025+
id select_type table type possible_keys key key_len ref rows filtered Extra
1026+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1027+
Warnings:
1028+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = TIMESTAMP'2001-01-01 00:00:00.000000')
1029+
EXPLAIN EXTENDED
1030+
SELECT * FROM t1 WHERE LENGTH(a)=30+RAND() AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1031+
id select_type table type possible_keys key key_len ref rows filtered Extra
1032+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1033+
Warnings:
1034+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = TIMESTAMP'2001-01-01 00:00:00.000000') and (<cache>(length(TIMESTAMP'2001-01-01 00:00:00')) = (30 + rand())))
1035+
DROP TABLE t1;
1036+
CREATE TABLE t1 (a DATETIME(6));;
1037+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00.000000'),('2001-01-01 00:00:01.000000');
1038+
SELECT * FROM t1 WHERE a=TIMESTAMP'2001-01-01 00:00:00.000000';
1039+
a
1040+
2001-01-01 00:00:00.000000
1041+
SELECT * FROM t1 WHERE LENGTH(a)=26;
1042+
a
1043+
2001-01-01 00:00:00.000000
1044+
2001-01-01 00:00:01.000000
1045+
SELECT * FROM t1 WHERE LENGTH(a)=26 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1046+
a
1047+
2001-01-01 00:00:00.000000
1048+
EXPLAIN EXTENDED
1049+
SELECT * FROM t1 WHERE LENGTH(a)=26 AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1050+
id select_type table type possible_keys key key_len ref rows filtered Extra
1051+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1052+
Warnings:
1053+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = TIMESTAMP'2001-01-01 00:00:00.000000')
1054+
EXPLAIN EXTENDED
1055+
SELECT * FROM t1 WHERE LENGTH(a)=40+RAND() AND a=TIMESTAMP'2001-01-01 00:00:00.000000';
1056+
id select_type table type possible_keys key key_len ref rows filtered Extra
1057+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1058+
Warnings:
1059+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = TIMESTAMP'2001-01-01 00:00:00.000000') and (<cache>(length(TIMESTAMP'2001-01-01 00:00:00.000000')) = (40 + rand())))
1060+
DROP TABLE t1;
1061+
SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30');
1062+
CREATE TABLE t1 (a DATETIME);;
1063+
INSERT INTO t1 VALUES ('2001-01-01 00:00:00'),('2001-01-01 00:00:01');
1064+
SELECT * FROM t1 WHERE a=TIME'00:00:00';
1065+
a
1066+
2001-01-01 00:00:00
1067+
SELECT * FROM t1 WHERE LENGTH(a)=19;
1068+
a
1069+
2001-01-01 00:00:00
1070+
2001-01-01 00:00:01
1071+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIME'00:00:00';
1072+
a
1073+
2001-01-01 00:00:00
1074+
EXPLAIN EXTENDED
1075+
SELECT * FROM t1 WHERE LENGTH(a)=19 AND a=TIME'00:00:00';
1076+
id select_type table type possible_keys key key_len ref rows filtered Extra
1077+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1078+
Warnings:
1079+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = TIME'00:00:00')
1080+
EXPLAIN EXTENDED
1081+
SELECT * FROM t1 WHERE LENGTH(a)=40+RAND() AND a=TIME'00:00:00';
1082+
id select_type table type possible_keys key key_len ref rows filtered Extra
1083+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
1084+
Warnings:
1085+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = TIME'00:00:00') and (<cache>(length(TIMESTAMP'2001-01-01 00:00:00')) = (40 + rand())))
1086+
DROP TABLE t1;
1087+
#
9481088
# End of 10.1 tests
9491089
#

0 commit comments

Comments
 (0)