From 29bbf4749ebcfe328d73ca93c7ee5908a339e850 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Tue, 27 Aug 2019 09:13:20 +0400 Subject: [PATCH] MDEV-19699 Server crashes in Item_null_result::field_type upon SELECT with ROLLUP on constant table Also fixes: MDEV-20431 GREATEST(int_col,date_col) returns wrong results in a view --- mysql-test/r/type_date.result | 33 +++++++++++++++++++++++++++++++ mysql-test/r/type_datetime.result | 31 +++++++++++++++++++++++++++++ mysql-test/t/type_date.test | 24 ++++++++++++++++++++++ mysql-test/t/type_datetime.test | 23 +++++++++++++++++++++ sql/item.cc | 32 ++++++++++++++++++++++++++++++ sql/item.h | 4 ++++ 6 files changed, 147 insertions(+) diff --git a/mysql-test/r/type_date.result b/mysql-test/r/type_date.result index e2c3b7f3b28..1a5a1d1c756 100644 --- a/mysql-test/r/type_date.result +++ b/mysql-test/r/type_date.result @@ -863,12 +863,14 @@ SELECT group_concat(d1/(CASE 'b' WHEN 'j' THEN 'c' END)) FROM v1 GROUP BY greatest(pk, 0, d2); group_concat(d1/(CASE 'b' WHEN 'j' THEN 'c' END)) NULL +NULL Warnings: Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 1 Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 +Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 CREATE TABLE t2 AS SELECT greatest(pk, 0, d2) AS c1 FROM t1 LIMIT 0; SHOW CREATE TABLE t2; Table Create Table @@ -879,5 +881,36 @@ DROP TABLE t2; DROP VIEW v1; DROP TABLE t1; # +# MDEV-19699 Server crashes in Item_null_result::field_type upon SELECT with ROLLUP on constant table +# +CREATE TABLE t1 (d DATE) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('1999-11-04'); +SELECT d FROM t1 GROUP BY d WITH ROLLUP HAVING d > '1990-01-01'; +d +1999-11-04 +DROP TABLE t1; +# +# MDEV-20431 GREATEST(int_col,date_col) returns wrong results in a view +# +CREATE TABLE t1 (pk INT NOT NULL, d DATE NOT NULL); +CREATE VIEW v1 AS SELECT * FROM t1; +INSERT INTO t1 VALUES (1,'2018-06-22'),(2,'2018-07-11'); +SELECT GREATEST(pk, d) FROM t1; +GREATEST(pk, d) +2018-06-22 +2018-07-11 +Warnings: +Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 +Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 +SELECT GREATEST(pk, d) FROM v1; +GREATEST(pk, d) +2018-06-22 +2018-07-11 +Warnings: +Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 +Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 +DROP VIEW v1; +DROP TABLE t1; +# # End of 10.1 tests # diff --git a/mysql-test/r/type_datetime.result b/mysql-test/r/type_datetime.result index dcc7411a6f2..591df19a9c6 100644 --- a/mysql-test/r/type_datetime.result +++ b/mysql-test/r/type_datetime.result @@ -1167,5 +1167,36 @@ INSERT INTO t2 SELECT * FROM t1; DROP TABLE t1, t2; SET SQL_MODE=DEFAULT; # +# MDEV-19699 Server crashes in Item_null_result::field_type upon SELECT with ROLLUP on constant table +# +CREATE TABLE t1 (d DATETIME) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('1999-11-04'); +SELECT d FROM t1 GROUP BY d WITH ROLLUP HAVING d > '1990-01-01'; +d +1999-11-04 00:00:00 +DROP TABLE t1; +# +# MDEV-20431 GREATEST(int_col,date_col) returns wrong results in a view +# +CREATE TABLE t1 (pk INT NOT NULL, d DATETIME NOT NULL); +CREATE VIEW v1 AS SELECT * FROM t1; +INSERT INTO t1 VALUES (1,'2018-06-22 00:00:00'),(2,'2018-07-11 00:00:00'); +SELECT GREATEST(pk, d) FROM t1; +GREATEST(pk, d) +2018-06-22 00:00:00 +2018-07-11 00:00:00 +Warnings: +Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 +Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 +SELECT GREATEST(pk, d) FROM v1; +GREATEST(pk, d) +2018-06-22 00:00:00 +2018-07-11 00:00:00 +Warnings: +Warning 1292 Incorrect datetime value: '1' for column 'pk' at row 1 +Warning 1292 Incorrect datetime value: '2' for column 'pk' at row 2 +DROP VIEW v1; +DROP TABLE t1; +# # End of 10.1 tests # diff --git a/mysql-test/t/type_date.test b/mysql-test/t/type_date.test index 554542f9dd3..c86bc730392 100644 --- a/mysql-test/t/type_date.test +++ b/mysql-test/t/type_date.test @@ -600,6 +600,30 @@ DROP TABLE t2; DROP VIEW v1; DROP TABLE t1; + +--echo # +--echo # MDEV-19699 Server crashes in Item_null_result::field_type upon SELECT with ROLLUP on constant table +--echo # + +CREATE TABLE t1 (d DATE) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('1999-11-04'); +SELECT d FROM t1 GROUP BY d WITH ROLLUP HAVING d > '1990-01-01'; +DROP TABLE t1; + + +--echo # +--echo # MDEV-20431 GREATEST(int_col,date_col) returns wrong results in a view +--echo # + +CREATE TABLE t1 (pk INT NOT NULL, d DATE NOT NULL); +CREATE VIEW v1 AS SELECT * FROM t1; +INSERT INTO t1 VALUES (1,'2018-06-22'),(2,'2018-07-11'); +SELECT GREATEST(pk, d) FROM t1; +SELECT GREATEST(pk, d) FROM v1; +DROP VIEW v1; +DROP TABLE t1; + + --echo # --echo # End of 10.1 tests --echo # diff --git a/mysql-test/t/type_datetime.test b/mysql-test/t/type_datetime.test index 3c5d1dbd62c..6df3ab60764 100644 --- a/mysql-test/t/type_datetime.test +++ b/mysql-test/t/type_datetime.test @@ -724,6 +724,29 @@ DROP TABLE t1, t2; SET SQL_MODE=DEFAULT; +--echo # +--echo # MDEV-19699 Server crashes in Item_null_result::field_type upon SELECT with ROLLUP on constant table +--echo # + +CREATE TABLE t1 (d DATETIME) ENGINE=MyISAM; +INSERT INTO t1 VALUES ('1999-11-04'); +SELECT d FROM t1 GROUP BY d WITH ROLLUP HAVING d > '1990-01-01'; +DROP TABLE t1; + + +--echo # +--echo # MDEV-20431 GREATEST(int_col,date_col) returns wrong results in a view +--echo # + +CREATE TABLE t1 (pk INT NOT NULL, d DATETIME NOT NULL); +CREATE VIEW v1 AS SELECT * FROM t1; +INSERT INTO t1 VALUES (1,'2018-06-22 00:00:00'),(2,'2018-07-11 00:00:00'); +SELECT GREATEST(pk, d) FROM t1; +SELECT GREATEST(pk, d) FROM v1; +DROP VIEW v1; +DROP TABLE t1; + + --echo # --echo # End of 10.1 tests --echo # diff --git a/sql/item.cc b/sql/item.cc index de7e6ae65ff..86bd4670714 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3131,6 +3131,20 @@ my_decimal *Item_null::val_decimal(my_decimal *decimal_value) } +longlong Item_null::val_datetime_packed() +{ + null_value= true; + return 0; +} + + +longlong Item_null::val_time_packed() +{ + null_value= true; + return 0; +} + + bool Item_null::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate) { // following assert is redundant, because fixed=1 assigned in constructor @@ -7296,6 +7310,24 @@ bool Item_ref::get_date(MYSQL_TIME *ltime,ulonglong fuzzydate) } +longlong Item_ref::val_datetime_packed() +{ + DBUG_ASSERT(fixed); + longlong tmp= (*ref)->val_datetime_packed(); + null_value= (*ref)->null_value; + return tmp; +} + + +longlong Item_ref::val_time_packed() +{ + DBUG_ASSERT(fixed); + longlong tmp= (*ref)->val_time_packed(); + null_value= (*ref)->null_value; + return tmp; +} + + my_decimal *Item_ref::val_decimal(my_decimal *decimal_value) { my_decimal *val= (*ref)->val_decimal_result(decimal_value); diff --git a/sql/item.h b/sql/item.h index edf9f748d77..a182d99f701 100644 --- a/sql/item.h +++ b/sql/item.h @@ -2627,6 +2627,8 @@ public: String *val_str(String *str); my_decimal *val_decimal(my_decimal *); bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate); + longlong val_datetime_packed(); + longlong val_time_packed(); int save_in_field(Field *field, bool no_conversions); int save_safe_in_field(Field *field); bool send(Protocol *protocol, String *str); @@ -3971,6 +3973,8 @@ public: String *val_str(String* tmp); bool is_null(); bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate); + longlong val_datetime_packed(); + longlong val_time_packed(); double val_result(); longlong val_int_result(); String *str_result(String* tmp);