Skip to content

Commit

Permalink
--echo #
Browse files Browse the repository at this point in the history
--echo # MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
--echo #
  • Loading branch information
abarkov committed Dec 12, 2018
1 parent 4abb821 commit c353b2a
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 1 deletion.
13 changes: 13 additions & 0 deletions mysql-test/main/timezone2.result
Original file line number Diff line number Diff line change
Expand Up @@ -542,5 +542,18 @@ SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));
a b
DROP TABLE t1;
#
# MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
#
SET time_zone='+00:00';
CREATE TABLE t1 (a INT, ts TIMESTAMP) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1, FROM_UNIXTIME(1288481126) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
CREATE TABLE t2 AS SELECT ts, COALESCE(ts) AS cts FROM t1 GROUP BY cts;
SELECT ts, cts, UNIX_TIMESTAMP(ts) AS uts, UNIX_TIMESTAMP(cts) AS ucts FROM t2;
ts cts uts ucts
2010-10-31 02:25:26 2010-10-31 02:25:26 1288481126 1288481126
DROP TABLE t1,t2;
SET time_zone=DEFAULT;
#
# End of 10.4 tests
#
12 changes: 12 additions & 0 deletions mysql-test/main/timezone2.test
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,18 @@ SELECT * FROM t1 WHERE a = (SELECT MIN(b) FROM t1);
SELECT * FROM t1 WHERE a IN ((SELECT MAX(b) FROM t1), (SELECT MIN(b) FROM t1));
DROP TABLE t1;

--echo #
--echo # MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
--echo #

SET time_zone='+00:00';
CREATE TABLE t1 (a INT, ts TIMESTAMP) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1, FROM_UNIXTIME(1288481126) /*winter time in Moscow*/);
SET time_zone='Europe/Moscow';
CREATE TABLE t2 AS SELECT ts, COALESCE(ts) AS cts FROM t1 GROUP BY cts;
SELECT ts, cts, UNIX_TIMESTAMP(ts) AS uts, UNIX_TIMESTAMP(cts) AS ucts FROM t2;
DROP TABLE t1,t2;
SET time_zone=DEFAULT;

--echo #
--echo # End of 10.4 tests
Expand Down
11 changes: 11 additions & 0 deletions mysql-test/main/type_timestamp.result
Original file line number Diff line number Diff line change
Expand Up @@ -1118,5 +1118,16 @@ ERROR 22007: Incorrect datetime value: '0000-00-00 00:00:00' for column 'a' at r
DROP TABLE t1;
SET sql_mode=DEFAULT;
#
# MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
#
CREATE TABLE t1 (a INT, b TIMESTAMP) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,'2018-06-19 00:00:00');
SELECT NULLIF(b, 'N/A') AS f, MAX(a) FROM t1 GROUP BY f;
f MAX(a)
2018-06-19 00:00:00 1
Warnings:
Warning 1292 Truncated incorrect datetime value: 'N/A'
DROP TABLE t1;
#
# End of 10.4 tests
#
8 changes: 8 additions & 0 deletions mysql-test/main/type_timestamp.test
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ UPDATE t1 SET a=COALESCE(b);
DROP TABLE t1;
SET sql_mode=DEFAULT;

--echo #
--echo # MDEV-17979 Assertion `0' failed in Item::val_native upon SELECT with timestamp, NULLIF, GROUP BY
--echo #

CREATE TABLE t1 (a INT, b TIMESTAMP) ENGINE=MyISAM;
INSERT INTO t1 VALUES (1,'2018-06-19 00:00:00');
SELECT NULLIF(b, 'N/A') AS f, MAX(a) FROM t1 GROUP BY f;
DROP TABLE t1;

--echo #
--echo # End of 10.4 tests
Expand Down
56 changes: 56 additions & 0 deletions sql/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -6021,6 +6021,62 @@ class Item_copy_string : public Item_copy
};


/**
We need a separate class Item_copy_timestamp because
TIMESTAMP->string->TIMESTAMP conversion is not round trip safe
near the DST change, e.g. '2010-10-31 02:25:26' can mean:
- my_time_t(1288477526) - summer time in Moscow
- my_time_t(1288481126) - winter time in Moscow, one hour later
*/
class Item_copy_timestamp: public Item_copy
{
Timestamp_or_zero_datetime m_value;
public:
Item_copy_timestamp(THD *thd, Item *arg): Item_copy(thd, arg) { }
const Type_handler *type_handler() const { return &type_handler_timestamp2; }
void copy()
{
Timestamp_or_zero_datetime_native_null tmp(current_thd, item, false);
null_value= tmp.is_null();
m_value= tmp.is_null() ? Timestamp_or_zero_datetime() :
Timestamp_or_zero_datetime(tmp);
}
int save_in_field(Field *field, bool no_conversions)
{
Timestamp_or_zero_datetime_native native(m_value, decimals);
return native.save_in_field(field, decimals);
}
longlong val_int()
{
return m_value.to_datetime(current_thd).to_longlong();
}
double val_real()
{
return m_value.to_datetime(current_thd).to_double();
}
String *val_str(String *to)
{
return m_value.to_datetime(current_thd).to_string(to, decimals);
}
my_decimal *val_decimal(my_decimal *to)
{
return m_value.to_datetime(current_thd).to_decimal(to);
}
bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
{
bool res= m_value.to_TIME(thd, ltime, fuzzydate);
DBUG_ASSERT(!res);
return res;
}
bool val_native(THD *thd, Native *to)
{
return m_value.to_native(to, decimals);
}
Item *get_copy(THD *thd)
{ return get_item_copy<Item_copy_timestamp>(thd, this); }
};


/*
Cached_item_XXX objects are not exactly caches. They do the following:
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23907,7 +23907,7 @@ setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
on how the value is to be used: In some cases this may be an
argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
*/
if (!(pos=new (thd->mem_root) Item_copy_string(thd, pos)))
if (!(pos= pos->type_handler()->create_item_copy(thd, pos)))
goto err;
if (i < border) // HAVING, ORDER and GROUP BY
{
Expand Down
16 changes: 16 additions & 0 deletions sql/sql_type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3764,6 +3764,22 @@ Type_handler_date_common::Item_get_cache(THD *thd, const Item *item) const
return new (thd->mem_root) Item_cache_date(thd);
}


/*************************************************************************/

Item_copy *
Type_handler::create_item_copy(THD *thd, Item *item) const
{
return new (thd->mem_root) Item_copy_string(thd, item);
}


Item_copy *
Type_handler_timestamp_common::create_item_copy(THD *thd, Item *item) const
{
return new (thd->mem_root) Item_copy_timestamp(thd, item);
}

/*************************************************************************/

bool Type_handler_int_result::
Expand Down
8 changes: 8 additions & 0 deletions sql/sql_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Item_const;
class Item_literal;
class Item_param;
class Item_cache;
class Item_copy;
class Item_func_or_sum;
class Item_sum_hybrid;
class Item_sum_sum;
Expand Down Expand Up @@ -3470,6 +3471,7 @@ class Type_handler
DBUG_ASSERT(0);
return NULL;
}
virtual Item_copy *create_item_copy(THD *thd, Item *item) const;
virtual int cmp_native(const Native &a, const Native &b) const
{
DBUG_ASSERT(0);
Expand Down Expand Up @@ -3771,6 +3773,11 @@ class Type_handler_row: public Type_handler
}
Item *make_const_item_for_comparison(THD *, Item *src, const Item *cmp) const;
Item_cache *Item_get_cache(THD *thd, const Item *item) const;
Item_copy *create_item_copy(THD *thd, Item *item) const
{
DBUG_ASSERT(0);
return NULL;
}
bool set_comparator_func(Arg_comparator *cmp) const;
bool Item_hybrid_func_fix_attributes(THD *thd,
const char *name,
Expand Down Expand Up @@ -5411,6 +5418,7 @@ class Type_handler_timestamp_common: public Type_handler_temporal_with_date
int Item_save_in_field(Item *item, Field *field, bool no_conversions) const;
String *print_item_value(THD *thd, Item *item, String *str) const;
Item_cache *Item_get_cache(THD *thd, const Item *item) const;
Item_copy *create_item_copy(THD *thd, Item *item) const;
String *Item_func_min_max_val_str(Item_func_min_max *, String *) const;
double Item_func_min_max_val_real(Item_func_min_max *) const;
longlong Item_func_min_max_val_int(Item_func_min_max *) const;
Expand Down

0 comments on commit c353b2a

Please sign in to comment.