Skip to content

Commit 100f0c9

Browse files
committed
MDEV-23388 Assertion `args[0]->decimals == 0' failed in Item_func_round::fix_arg_int
Type_handler_temporal_result::Item_func_min_max_fix_attributes() in an expression GREATEST(string,date), e.g: SELECT GREATEST('1', CAST('2020-12-12' AS DATE)); incorrectly evaluated decimals as 6 (like for DATETIME). Adding a separate virtual implementation: Type_handler_date_common::Item_func_min_max_fix_attributes() This makes the code simpler.
1 parent 6a2ee9c commit 100f0c9

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

mysql-test/main/type_date.result

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,5 +1103,35 @@ t2 CREATE TABLE `t2` (
11031103
) ENGINE=MyISAM DEFAULT CHARSET=latin1
11041104
DROP TABLE t2,t1;
11051105
#
1106+
# MDEV-23388 Assertion `args[0]->decimals == 0' failed in Item_func_round::fix_arg_int
1107+
#
1108+
SELECT ROUND(GREATEST('1', CAST('2020-12-12' AS DATE)));
1109+
ROUND(GREATEST('1', CAST('2020-12-12' AS DATE)))
1110+
20201212
1111+
Warnings:
1112+
Warning 1292 Truncated incorrect datetime value: '1'
1113+
SELECT GREATEST('1', CAST('2020-12-12' AS DATE));
1114+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
1115+
def GREATEST('1', CAST('2020-12-12' AS DATE)) 10 10 10 Y 128 0 63
1116+
GREATEST('1', CAST('2020-12-12' AS DATE))
1117+
2020-12-12
1118+
Warnings:
1119+
Warning 1292 Truncated incorrect datetime value: '1'
1120+
CREATE TABLE t1 (c_date DATE NOT NULL, c_int INT NOT NULL);
1121+
CREATE TABLE t2 AS SELECT
1122+
GREATEST(c_date,c_date),
1123+
GREATEST(c_date,c_int),
1124+
GREATEST(c_int,c_date)
1125+
FROM t1;
1126+
SHOW CREATE TABLE t2;
1127+
Table Create Table
1128+
t2 CREATE TABLE `t2` (
1129+
`GREATEST(c_date,c_date)` date NOT NULL,
1130+
`GREATEST(c_date,c_int)` date DEFAULT NULL,
1131+
`GREATEST(c_int,c_date)` date DEFAULT NULL
1132+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1133+
DROP TABLE t2;
1134+
DROP TABLE t1;
1135+
#
11061136
# End of 10.4 tests
11071137
#

mysql-test/main/type_date.test

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,28 @@ CREATE TABLE t2 AS SELECT FLOOR(a), CEIL(a),ROUND(a),TRUNCATE(a,0) FROM t1;
749749
SHOW CREATE TABLE t2;
750750
DROP TABLE t2,t1;
751751

752+
--echo #
753+
--echo # MDEV-23388 Assertion `args[0]->decimals == 0' failed in Item_func_round::fix_arg_int
754+
--echo #
755+
756+
SELECT ROUND(GREATEST('1', CAST('2020-12-12' AS DATE)));
757+
758+
--disable_ps_protocol
759+
--enable_metadata
760+
SELECT GREATEST('1', CAST('2020-12-12' AS DATE));
761+
--disable_metadata
762+
--enable_ps_protocol
763+
764+
CREATE TABLE t1 (c_date DATE NOT NULL, c_int INT NOT NULL);
765+
CREATE TABLE t2 AS SELECT
766+
GREATEST(c_date,c_date),
767+
GREATEST(c_date,c_int),
768+
GREATEST(c_int,c_date)
769+
FROM t1;
770+
SHOW CREATE TABLE t2;
771+
DROP TABLE t2;
772+
DROP TABLE t1;
773+
752774
--echo #
753775
--echo # End of 10.4 tests
754776
--echo #

sql/sql_type.cc

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,6 +4120,7 @@ bool Type_handler_temporal_result::
41204120
Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
41214121
Item **items, uint nitems) const
41224122
{
4123+
DBUG_ASSERT(func->field_type() != MYSQL_TYPE_DATE);
41234124
bool rc= Type_handler::Item_func_min_max_fix_attributes(thd, func,
41244125
items, nitems);
41254126
bool is_time= func->field_type() == MYSQL_TYPE_TIME;
@@ -4174,7 +4175,6 @@ bool Type_handler_temporal_result::
41744175
DATETIME DATETIME no conversion
41754176
DATETIME TIMESTAMP safe conversion
41764177
DATETIME DATE safe conversion
4177-
DATE DATE no conversion
41784178
TIME TIME no conversion
41794179
41804180
Note, a function cannot return TIMESTAMP if it has non-TIMESTAMP
@@ -4191,9 +4191,6 @@ bool Type_handler_temporal_result::
41914191
-------------------- ------------- -------
41924192
TIMESTAMP TIME Not possible
41934193
DATETIME TIME depends on OLD_MODE_ZERO_DATE_TIME_CAST
4194-
DATE TIMESTAMP Not possible
4195-
DATE DATETIME Not possible
4196-
DATE TIME Not possible
41974194
TIME TIMESTAMP Not possible
41984195
TIME DATETIME Not possible
41994196
TIME DATE Not possible
@@ -4216,6 +4213,30 @@ bool Type_handler_temporal_result::
42164213
}
42174214

42184215

4216+
bool Type_handler_date_common::
4217+
Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
4218+
Item **items, uint nitems) const
4219+
{
4220+
func->fix_attributes_date();
4221+
if (func->maybe_null)
4222+
return false;
4223+
/*
4224+
We cannot trust the generic maybe_null value calculated during fix_fields().
4225+
If a conversion from non-temoral types to DATE happens,
4226+
then the result can be NULL (even if all arguments are not NULL).
4227+
*/
4228+
for (uint i= 0; i < nitems; i++)
4229+
{
4230+
if (items[i]->type_handler()->cmp_type() != TIME_RESULT)
4231+
{
4232+
func->maybe_null= true;
4233+
break;
4234+
}
4235+
}
4236+
return false;
4237+
}
4238+
4239+
42194240
bool Type_handler_real_result::
42204241
Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
42214242
Item **items, uint nitems) const

sql/sql_type.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5508,6 +5508,8 @@ class Type_handler_date_common: public Type_handler_temporal_with_date
55085508
Type_handler_hybrid_field_type *,
55095509
Type_all_attributes *atrr,
55105510
Item **items, uint nitems) const;
5511+
bool Item_func_min_max_fix_attributes(THD *thd, Item_func_min_max *func,
5512+
Item **items, uint nitems) const;
55115513
void Item_param_set_param_func(Item_param *param,
55125514
uchar **pos, ulong len) const;
55135515
};

0 commit comments

Comments
 (0)