Skip to content

Commit bf7a2bb

Browse files
author
Alexander Barkov
committed
MDEV-8704 Wrong result for SELECT..WHERE LENGTH(double_column)!=6 AND double_column=100e0
1 parent 5448e0a commit bf7a2bb

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

mysql-test/r/type_float.result

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,98 @@ DROP TABLE t1,t2;
532532
#
533533
# End of 10.0 tests
534534
#
535+
#
536+
# MDEV-8704 Wrong result for SELECT..WHERE LENGTH(double_column)!=6 AND double_column=100e0
537+
#
538+
CREATE TABLE t1 (a DOUBLE(9,2));
539+
INSERT INTO t1 VALUES (100),(110);
540+
SELECT * FROM t1 WHERE LENGTH(a)!=6;
541+
a
542+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
543+
a
544+
DROP TABLE t1;
545+
CREATE TABLE t1 (a DOUBLE);
546+
INSERT INTO t1 VALUES (100),(110);
547+
SELECT * FROM t1 WHERE LENGTH(a)!=6;
548+
a
549+
100
550+
110
551+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
552+
a
553+
100
554+
EXPLAIN EXTENDED
555+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
556+
id select_type table type possible_keys key key_len ref rows filtered Extra
557+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
558+
Warnings:
559+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` = 100e0)
560+
EXPLAIN EXTENDED
561+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=100e0;
562+
id select_type table type possible_keys key key_len ref rows filtered Extra
563+
1 SIMPLE t1 ALL NULL NULL NULL NULL 2 100.00 Using where
564+
Warnings:
565+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 100e0) and (<cache>(length(100)) <> rand()))
566+
DROP TABLE t1;
567+
CREATE TABLE t1 (a DOUBLE(10,1));
568+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
569+
SELECT * FROM t1 WHERE LENGTH(a)!=3;
570+
a
571+
SELECT * FROM t1 WHERE LENGTH(a)!=3 AND a=1.10e0;
572+
a
573+
EXPLAIN EXTENDED
574+
SELECT * FROM t1 WHERE LENGTH(a)!=3 AND a=1.10e0;
575+
id select_type table type possible_keys key key_len ref rows filtered Extra
576+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
577+
Warnings:
578+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
579+
# Notice 1.1 instead of 1.10 in the final WHERE condition
580+
EXPLAIN EXTENDED
581+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
582+
id select_type table type possible_keys key key_len ref rows filtered Extra
583+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
584+
Warnings:
585+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 1.10e0) and (<cache>(length(1.1)) <> rand()))
586+
DROP TABLE t1;
587+
CREATE TABLE t1 (a DOUBLE(10,2));
588+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
589+
SELECT * FROM t1 WHERE LENGTH(a)!=4;
590+
a
591+
SELECT * FROM t1 WHERE LENGTH(a)!=4 AND a=1.10e0;
592+
a
593+
EXPLAIN EXTENDED
594+
SELECT * FROM t1 WHERE LENGTH(a)!=4 AND a=1.10e0;
595+
id select_type table type possible_keys key key_len ref rows filtered Extra
596+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
597+
Warnings:
598+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
599+
# Notice 1.10 in the final WHERE condition
600+
EXPLAIN EXTENDED
601+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
602+
id select_type table type possible_keys key key_len ref rows filtered Extra
603+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
604+
Warnings:
605+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 1.10e0) and (<cache>(length(1.10)) <> rand()))
606+
DROP TABLE t1;
607+
CREATE TABLE t1 (a DOUBLE(10,3));
608+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
609+
SELECT * FROM t1 WHERE LENGTH(a)!=5;
610+
a
611+
SELECT * FROM t1 WHERE LENGTH(a)!=5 AND a=1.10e0;
612+
a
613+
EXPLAIN EXTENDED
614+
SELECT * FROM t1 WHERE LENGTH(a)!=5 AND a=1.10e0;
615+
id select_type table type possible_keys key key_len ref rows filtered Extra
616+
1 SIMPLE NULL NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
617+
Warnings:
618+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where 0
619+
# Notice 1.100 rather than 1.10 in the final WHERE condition
620+
EXPLAIN EXTENDED
621+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
622+
id select_type table type possible_keys key key_len ref rows filtered Extra
623+
1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where
624+
Warnings:
625+
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where ((`test`.`t1`.`a` = 1.10e0) and (<cache>(length(1.100)) <> rand()))
626+
DROP TABLE t1;
627+
#
628+
# End of 10.1 tests
629+
#

mysql-test/t/type_float.test

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,67 @@ DROP TABLE t1,t2;
385385
--echo #
386386
--echo # End of 10.0 tests
387387
--echo #
388+
389+
390+
--echo #
391+
--echo # MDEV-8704 Wrong result for SELECT..WHERE LENGTH(double_column)!=6 AND double_column=100e0
392+
--echo #
393+
394+
# The original test case from the bug report
395+
CREATE TABLE t1 (a DOUBLE(9,2));
396+
INSERT INTO t1 VALUES (100),(110);
397+
SELECT * FROM t1 WHERE LENGTH(a)!=6;
398+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
399+
DROP TABLE t1;
400+
401+
# DOUBLE with no specific precision
402+
CREATE TABLE t1 (a DOUBLE);
403+
INSERT INTO t1 VALUES (100),(110);
404+
SELECT * FROM t1 WHERE LENGTH(a)!=6;
405+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
406+
EXPLAIN EXTENDED
407+
SELECT * FROM t1 WHERE LENGTH(a)!=6 AND a=100e0;
408+
EXPLAIN EXTENDED
409+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=100e0;
410+
DROP TABLE t1;
411+
412+
# The constant scale is bigger than the field scale
413+
CREATE TABLE t1 (a DOUBLE(10,1));
414+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
415+
SELECT * FROM t1 WHERE LENGTH(a)!=3;
416+
SELECT * FROM t1 WHERE LENGTH(a)!=3 AND a=1.10e0;
417+
EXPLAIN EXTENDED
418+
SELECT * FROM t1 WHERE LENGTH(a)!=3 AND a=1.10e0;
419+
--echo # Notice 1.1 instead of 1.10 in the final WHERE condition
420+
EXPLAIN EXTENDED
421+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
422+
DROP TABLE t1;
423+
424+
# The constant scale is equal to the field scale
425+
CREATE TABLE t1 (a DOUBLE(10,2));
426+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
427+
SELECT * FROM t1 WHERE LENGTH(a)!=4;
428+
SELECT * FROM t1 WHERE LENGTH(a)!=4 AND a=1.10e0;
429+
EXPLAIN EXTENDED
430+
SELECT * FROM t1 WHERE LENGTH(a)!=4 AND a=1.10e0;
431+
--echo # Notice 1.10 in the final WHERE condition
432+
EXPLAIN EXTENDED
433+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
434+
DROP TABLE t1;
435+
436+
# The constant scale is smaller than the field scale
437+
CREATE TABLE t1 (a DOUBLE(10,3));
438+
INSERT INTO t1 VALUES (1.1),(1.2),(1.3);
439+
SELECT * FROM t1 WHERE LENGTH(a)!=5;
440+
SELECT * FROM t1 WHERE LENGTH(a)!=5 AND a=1.10e0;
441+
EXPLAIN EXTENDED
442+
SELECT * FROM t1 WHERE LENGTH(a)!=5 AND a=1.10e0;
443+
--echo # Notice 1.100 rather than 1.10 in the final WHERE condition
444+
EXPLAIN EXTENDED
445+
SELECT * FROM t1 WHERE LENGTH(a)!=RAND() AND a=1.10e0;
446+
DROP TABLE t1;
447+
448+
449+
--echo #
450+
--echo # End of 10.1 tests
451+
--echo #

sql/field.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4639,6 +4639,28 @@ bool Field_real::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate)
46394639
}
46404640

46414641

4642+
Item *Field_real::get_equal_const_item(THD *thd, const Context &ctx,
4643+
Item_field *field_item,
4644+
Item *const_item)
4645+
{
4646+
if (flags & ZEROFILL_FLAG)
4647+
return Field_num::get_equal_zerofill_const_item(thd, ctx,
4648+
field_item, const_item);
4649+
switch (ctx.subst_constraint()) {
4650+
case IDENTITY_SUBST:
4651+
if (const_item->decimal_scale() != Field_real::decimals())
4652+
{
4653+
double val= const_item->val_real();
4654+
return new (thd->mem_root) Item_float(thd, val, Field_real::decimals());
4655+
}
4656+
break;
4657+
case ANY_SUBST:
4658+
break;
4659+
}
4660+
return const_item;
4661+
}
4662+
4663+
46424664
String *Field_double::val_str(String *val_buffer,
46434665
String *val_ptr __attribute__((unused)))
46444666
{

sql/field.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,8 @@ class Field_real :public Field_num {
13631363
my_decimal *val_decimal(my_decimal *);
13641364
uint32 max_display_length() { return field_length; }
13651365
uint size_of() const { return sizeof(*this); }
1366+
Item *get_equal_const_item(THD *thd, const Context &ctx,
1367+
Item_field *field_item, Item *const_item);
13661368
};
13671369

13681370

0 commit comments

Comments
 (0)