summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2020-06-10 13:55:55 +0400
committerAlexander Barkov <bar@mariadb.com>2020-06-10 13:55:55 +0400
commitbf2a244406c36cd12bc53f9a30c8a2cab191f235 (patch)
tree50309b72c7365c3db451aba16751e8bee1b3826c
parent86c50a255a8319a4c89399b9c2d4f416b4973815 (diff)
downloadmariadb-git-bf2a244406c36cd12bc53f9a30c8a2cab191f235.tar.gz
MDEV-22854 Garbage returned with SELECT CASE..DEFAULT(timestamp_field_with_now_as_default)
Item_default_value did not override val_native(), so the inherited Item_field::val_native() was called. As a result Item_default_value::calculate() was not called and Item_field::val_native() was called on a Field with a non-initialized ptr. Implementing Item_default_value::val_native() properly.
-rw-r--r--mysql-test/main/type_timestamp.result12
-rw-r--r--mysql-test/main/type_timestamp.test12
-rw-r--r--sql/item.cc6
-rw-r--r--sql/item.h1
4 files changed, 31 insertions, 0 deletions
diff --git a/mysql-test/main/type_timestamp.result b/mysql-test/main/type_timestamp.result
index 11d27e77a3d..e6fa9756b1d 100644
--- a/mysql-test/main/type_timestamp.result
+++ b/mysql-test/main/type_timestamp.result
@@ -1308,5 +1308,17 @@ Warnings:
Warning 1292 Incorrect datetime value: '2010-00-01 00:00:00'
DROP TABLE t1;
#
+# MDEV-22854 Garbage returned with SELECT CASE..DEFAULT(timestamp_field_with_now_as_default)
+#
+SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.456789');
+CREATE TABLE t1 (a TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP);
+INSERT INTO t1 VALUES ('2019-02-23 11:31:04'),('2023-02-09 00:00:00');
+SELECT CASE WHEN a THEN DEFAULT(a) END FROM t1;
+CASE WHEN a THEN DEFAULT(a) END
+2001-01-01 10:20:30.456
+2001-01-01 10:20:30.456
+DROP TABLE t1;
+SET timestamp=DEFAULT;
+#
# End of 10.4 tests
#
diff --git a/mysql-test/main/type_timestamp.test b/mysql-test/main/type_timestamp.test
index be3803618e1..9c5b57b8885 100644
--- a/mysql-test/main/type_timestamp.test
+++ b/mysql-test/main/type_timestamp.test
@@ -864,5 +864,17 @@ DROP TABLE t1;
--echo #
+--echo # MDEV-22854 Garbage returned with SELECT CASE..DEFAULT(timestamp_field_with_now_as_default)
+--echo #
+
+SET timestamp=UNIX_TIMESTAMP('2001-01-01 10:20:30.456789');
+CREATE TABLE t1 (a TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP);
+INSERT INTO t1 VALUES ('2019-02-23 11:31:04'),('2023-02-09 00:00:00');
+SELECT CASE WHEN a THEN DEFAULT(a) END FROM t1;
+DROP TABLE t1;
+SET timestamp=DEFAULT;
+
+
+--echo #
--echo # End of 10.4 tests
--echo #
diff --git a/sql/item.cc b/sql/item.cc
index 84e2c369bac..4b64d76905a 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -9338,6 +9338,12 @@ void Item_default_value::calculate()
DEBUG_SYNC(field->table->in_use, "after_Item_default_value_calculate");
}
+bool Item_default_value::val_native(THD *thd, Native *to)
+{
+ calculate();
+ return Item_field::val_native(thd, to);
+}
+
String *Item_default_value::val_str(String *str)
{
calculate();
diff --git a/sql/item.h b/sql/item.h
index 98b61e15c11..1fa80066f9d 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -6320,6 +6320,7 @@ public:
longlong val_int();
my_decimal *val_decimal(my_decimal *decimal_value);
bool get_date(THD *thd, MYSQL_TIME *ltime,date_mode_t fuzzydate);
+ bool val_native(THD *thd, Native *to);
bool send(Protocol *protocol, st_value *buffer);
int save_in_field(Field *field_arg, bool no_conversions);
bool save_in_param(THD *thd, Item_param *param)