Skip to content

Commit

Permalink
MDEV-22854 Garbage returned with SELECT CASE..DEFAULT(timestamp_field…
Browse files Browse the repository at this point in the history
…_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.
  • Loading branch information
abarkov committed Jun 10, 2020
1 parent 86c50a2 commit bf2a244
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mysql-test/main/type_timestamp.result
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
12 changes: 12 additions & 0 deletions mysql-test/main/type_timestamp.test
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,18 @@ SELECT * FROM t1 WHERE c='2010-00-01 00:00:00';
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 #
6 changes: 6 additions & 0 deletions sql/item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -6320,6 +6320,7 @@ class Item_default_value : public Item_field
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)
Expand Down

0 comments on commit bf2a244

Please sign in to comment.