Skip to content

Commit 8afe96f

Browse files
author
Alexander Barkov
committed
MDEV-8910 Wrong metadata or field type for MAX(COALESCE(string_field))
1 parent bed4e84 commit 8afe96f

File tree

7 files changed

+387
-18
lines changed

7 files changed

+387
-18
lines changed

mysql-test/r/func_hybrid_type.result

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,5 +3059,177 @@ DROP TABLE t2;
30593059
DROP TABLE t1;
30603060
SET timestamp=DEFAULT;
30613061
#
3062+
# MDEV-8910 Wrong metadata or field type for MAX(COALESCE(string_field))
3063+
#
3064+
CREATE TABLE t1 (c1 TINYBLOB, c2 MEDIUMBLOB, c3 BLOB, c4 LONGBLOB);
3065+
CREATE TABLE t2 AS
3066+
SELECT
3067+
MAX(COALESCE(c1)) AS c1,
3068+
MAX(COALESCE(c2)) AS c2,
3069+
MAX(COALESCE(c3)) AS c3,
3070+
MAX(COALESCE(c4)) AS c4
3071+
FROM t1;
3072+
SHOW CREATE TABLE t2;
3073+
Table Create Table
3074+
t2 CREATE TABLE `t2` (
3075+
`c1` varbinary(255) DEFAULT NULL,
3076+
`c2` mediumblob,
3077+
`c3` blob,
3078+
`c4` longblob
3079+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3080+
SELECT
3081+
MAX(COALESCE(c1)) AS c1,
3082+
MAX(COALESCE(c2)) AS c2,
3083+
MAX(COALESCE(c3)) AS c3,
3084+
MAX(COALESCE(c4)) AS c4
3085+
FROM t1;
3086+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3087+
def c1 253 255 0 Y 128 31 63
3088+
def c2 250 16777215 0 Y 128 31 63
3089+
def c3 252 65535 0 Y 128 31 63
3090+
def c4 251 4294967295 0 Y 128 31 63
3091+
c1 c2 c3 c4
3092+
NULL NULL NULL NULL
3093+
DROP TABLE t2;
3094+
DROP TABLE t1;
3095+
CREATE TABLE t1 (c1 CHAR(1), c2 CHAR(255)) CHARACTER SET latin1;
3096+
CREATE TABLE t2 AS
3097+
SELECT
3098+
MAX(COALESCE(c1)) AS c1,
3099+
MAX(COALESCE(c2)) AS c2
3100+
FROM t1;
3101+
SHOW CREATE TABLE t2;
3102+
Table Create Table
3103+
t2 CREATE TABLE `t2` (
3104+
`c1` varchar(1) DEFAULT NULL,
3105+
`c2` varchar(255) DEFAULT NULL
3106+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3107+
SELECT
3108+
MAX(COALESCE(c1)) AS c1,
3109+
MAX(COALESCE(c2)) AS c2
3110+
FROM t1;
3111+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3112+
def c1 253 1 0 Y 0 31 8
3113+
def c2 253 255 0 Y 0 31 8
3114+
c1 c2
3115+
NULL NULL
3116+
DROP TABLE t2;
3117+
DROP TABLE t1;
3118+
CREATE TABLE t1 (c1 CHAR(1), c2 CHAR(255)) CHARACTER SET utf8;
3119+
CREATE TABLE t2 AS
3120+
SELECT
3121+
MAX(COALESCE(c1)) AS c1,
3122+
MAX(COALESCE(c2)) AS c2
3123+
FROM t1;
3124+
SHOW CREATE TABLE t2;
3125+
Table Create Table
3126+
t2 CREATE TABLE `t2` (
3127+
`c1` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
3128+
`c2` varchar(255) CHARACTER SET utf8 DEFAULT NULL
3129+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3130+
SELECT
3131+
MAX(COALESCE(c1)) AS c1,
3132+
MAX(COALESCE(c2)) AS c2
3133+
FROM t1;
3134+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3135+
def c1 253 1 0 Y 0 31 8
3136+
def c2 253 255 0 Y 0 31 8
3137+
c1 c2
3138+
NULL NULL
3139+
DROP TABLE t2;
3140+
DROP TABLE t1;
3141+
CREATE TABLE t1 (c1 VARCHAR(1), c2 VARCHAR(255), c3 VARCHAR(20000)) CHARACTER SET latin1;
3142+
CREATE TABLE t2 AS
3143+
SELECT
3144+
MAX(COALESCE(c1)) AS c1,
3145+
MAX(COALESCE(c2)) AS c2,
3146+
MAX(COALESCE(c3)) AS c3
3147+
FROM t1;
3148+
SHOW CREATE TABLE t2;
3149+
Table Create Table
3150+
t2 CREATE TABLE `t2` (
3151+
`c1` varchar(1) DEFAULT NULL,
3152+
`c2` varchar(255) DEFAULT NULL,
3153+
`c3` text
3154+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3155+
SELECT
3156+
MAX(COALESCE(c1)) AS c1,
3157+
MAX(COALESCE(c2)) AS c2,
3158+
MAX(COALESCE(c3)) AS c3
3159+
FROM t1;
3160+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3161+
def c1 253 1 0 Y 0 31 8
3162+
def c2 253 255 0 Y 0 31 8
3163+
def c3 252 20000 0 Y 0 31 8
3164+
c1 c2 c3
3165+
NULL NULL NULL
3166+
DROP TABLE t2;
3167+
DROP TABLE t1;
3168+
CREATE TABLE t1 (c1 VARCHAR(1), c2 VARCHAR(255), c3 VARCHAR(20000)) CHARACTER SET utf8;
3169+
CREATE TABLE t2 AS
3170+
SELECT
3171+
MAX(COALESCE(c1)) AS c1,
3172+
MAX(COALESCE(c2)) AS c2,
3173+
MAX(COALESCE(c3)) AS c3
3174+
FROM t1;
3175+
SHOW CREATE TABLE t2;
3176+
Table Create Table
3177+
t2 CREATE TABLE `t2` (
3178+
`c1` varchar(1) CHARACTER SET utf8 DEFAULT NULL,
3179+
`c2` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
3180+
`c3` text CHARACTER SET utf8
3181+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3182+
SELECT
3183+
MAX(COALESCE(c1)) AS c1,
3184+
MAX(COALESCE(c2)) AS c2,
3185+
MAX(COALESCE(c3)) AS c3
3186+
FROM t1;
3187+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3188+
def c1 253 1 0 Y 0 31 8
3189+
def c2 253 255 0 Y 0 31 8
3190+
def c3 252 60000 0 Y 0 31 8
3191+
c1 c2 c3
3192+
NULL NULL NULL
3193+
DROP TABLE t2;
3194+
DROP TABLE t1;
3195+
CREATE TABLE t1 (c1 ENUM('a')) CHARACTER SET latin1;
3196+
CREATE TABLE t2 AS
3197+
SELECT
3198+
MAX(COALESCE(c1)) AS c1
3199+
FROM t1;
3200+
SHOW CREATE TABLE t2;
3201+
Table Create Table
3202+
t2 CREATE TABLE `t2` (
3203+
`c1` varchar(1) DEFAULT NULL
3204+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3205+
SELECT
3206+
MAX(COALESCE(c1)) AS c1
3207+
FROM t1;
3208+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3209+
def c1 253 1 0 Y 0 31 8
3210+
c1
3211+
NULL
3212+
DROP TABLE t2;
3213+
DROP TABLE t1;
3214+
CREATE TABLE t1 (c1 ENUM('a')) CHARACTER SET utf8;
3215+
CREATE TABLE t2 AS
3216+
SELECT
3217+
MAX(COALESCE(c1)) AS c1
3218+
FROM t1;
3219+
SHOW CREATE TABLE t2;
3220+
Table Create Table
3221+
t2 CREATE TABLE `t2` (
3222+
`c1` varchar(1) CHARACTER SET utf8 DEFAULT NULL
3223+
) ENGINE=MyISAM DEFAULT CHARSET=latin1
3224+
SELECT
3225+
MAX(COALESCE(c1)) AS c1
3226+
FROM t1;
3227+
Catalog Database Table Table_alias Column Column_alias Type Length Max length Is_null Flags Decimals Charsetnr
3228+
def c1 253 1 0 Y 0 31 8
3229+
c1
3230+
NULL
3231+
DROP TABLE t2;
3232+
DROP TABLE t1;
3233+
#
30623234
# End of 10.1 tests
30633235
#

mysql-test/t/func_hybrid_type.test

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,140 @@ DROP TABLE t2;
165165
DROP TABLE t1;
166166
SET timestamp=DEFAULT;
167167

168+
--echo #
169+
--echo # MDEV-8910 Wrong metadata or field type for MAX(COALESCE(string_field))
170+
--echo #
171+
CREATE TABLE t1 (c1 TINYBLOB, c2 MEDIUMBLOB, c3 BLOB, c4 LONGBLOB);
172+
CREATE TABLE t2 AS
173+
SELECT
174+
MAX(COALESCE(c1)) AS c1,
175+
MAX(COALESCE(c2)) AS c2,
176+
MAX(COALESCE(c3)) AS c3,
177+
MAX(COALESCE(c4)) AS c4
178+
FROM t1;
179+
SHOW CREATE TABLE t2;
180+
--disable_ps_protocol
181+
--enable_metadata
182+
SELECT
183+
MAX(COALESCE(c1)) AS c1,
184+
MAX(COALESCE(c2)) AS c2,
185+
MAX(COALESCE(c3)) AS c3,
186+
MAX(COALESCE(c4)) AS c4
187+
FROM t1;
188+
--disable_metadata
189+
--enable_ps_protocol
190+
DROP TABLE t2;
191+
DROP TABLE t1;
192+
193+
CREATE TABLE t1 (c1 CHAR(1), c2 CHAR(255)) CHARACTER SET latin1;
194+
CREATE TABLE t2 AS
195+
SELECT
196+
MAX(COALESCE(c1)) AS c1,
197+
MAX(COALESCE(c2)) AS c2
198+
FROM t1;
199+
SHOW CREATE TABLE t2;
200+
--disable_ps_protocol
201+
--enable_metadata
202+
SELECT
203+
MAX(COALESCE(c1)) AS c1,
204+
MAX(COALESCE(c2)) AS c2
205+
FROM t1;
206+
--disable_metadata
207+
--enable_ps_protocol
208+
DROP TABLE t2;
209+
DROP TABLE t1;
210+
211+
CREATE TABLE t1 (c1 CHAR(1), c2 CHAR(255)) CHARACTER SET utf8;
212+
CREATE TABLE t2 AS
213+
SELECT
214+
MAX(COALESCE(c1)) AS c1,
215+
MAX(COALESCE(c2)) AS c2
216+
FROM t1;
217+
SHOW CREATE TABLE t2;
218+
--disable_ps_protocol
219+
--enable_metadata
220+
SELECT
221+
MAX(COALESCE(c1)) AS c1,
222+
MAX(COALESCE(c2)) AS c2
223+
FROM t1;
224+
--disable_metadata
225+
--enable_ps_protocol
226+
DROP TABLE t2;
227+
DROP TABLE t1;
228+
229+
CREATE TABLE t1 (c1 VARCHAR(1), c2 VARCHAR(255), c3 VARCHAR(20000)) CHARACTER SET latin1;
230+
CREATE TABLE t2 AS
231+
SELECT
232+
MAX(COALESCE(c1)) AS c1,
233+
MAX(COALESCE(c2)) AS c2,
234+
MAX(COALESCE(c3)) AS c3
235+
FROM t1;
236+
SHOW CREATE TABLE t2;
237+
--disable_ps_protocol
238+
--enable_metadata
239+
SELECT
240+
MAX(COALESCE(c1)) AS c1,
241+
MAX(COALESCE(c2)) AS c2,
242+
MAX(COALESCE(c3)) AS c3
243+
FROM t1;
244+
--disable_metadata
245+
--enable_ps_protocol
246+
DROP TABLE t2;
247+
DROP TABLE t1;
248+
249+
CREATE TABLE t1 (c1 VARCHAR(1), c2 VARCHAR(255), c3 VARCHAR(20000)) CHARACTER SET utf8;
250+
CREATE TABLE t2 AS
251+
SELECT
252+
MAX(COALESCE(c1)) AS c1,
253+
MAX(COALESCE(c2)) AS c2,
254+
MAX(COALESCE(c3)) AS c3
255+
FROM t1;
256+
SHOW CREATE TABLE t2;
257+
--disable_ps_protocol
258+
--enable_metadata
259+
SELECT
260+
MAX(COALESCE(c1)) AS c1,
261+
MAX(COALESCE(c2)) AS c2,
262+
MAX(COALESCE(c3)) AS c3
263+
FROM t1;
264+
--disable_metadata
265+
--enable_ps_protocol
266+
DROP TABLE t2;
267+
DROP TABLE t1;
268+
269+
CREATE TABLE t1 (c1 ENUM('a')) CHARACTER SET latin1;
270+
CREATE TABLE t2 AS
271+
SELECT
272+
MAX(COALESCE(c1)) AS c1
273+
FROM t1;
274+
SHOW CREATE TABLE t2;
275+
--disable_ps_protocol
276+
--enable_metadata
277+
SELECT
278+
MAX(COALESCE(c1)) AS c1
279+
FROM t1;
280+
--disable_metadata
281+
--enable_ps_protocol
282+
DROP TABLE t2;
283+
DROP TABLE t1;
284+
285+
CREATE TABLE t1 (c1 ENUM('a')) CHARACTER SET utf8;
286+
CREATE TABLE t2 AS
287+
SELECT
288+
MAX(COALESCE(c1)) AS c1
289+
FROM t1;
290+
SHOW CREATE TABLE t2;
291+
--disable_ps_protocol
292+
--enable_metadata
293+
SELECT
294+
MAX(COALESCE(c1)) AS c1
295+
FROM t1;
296+
--disable_metadata
297+
--enable_ps_protocol
298+
DROP TABLE t2;
299+
DROP TABLE t1;
300+
301+
168302
--echo #
169303
--echo # End of 10.1 tests
170304
--echo #

sql/item.cc

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5362,17 +5362,6 @@ void Item::make_field(Send_field *tmp_field)
53625362
}
53635363

53645364

5365-
enum_field_types Item::string_field_type() const
5366-
{
5367-
enum_field_types f_type= MYSQL_TYPE_VAR_STRING;
5368-
if (max_length >= 16777216)
5369-
f_type= MYSQL_TYPE_LONG_BLOB;
5370-
else if (max_length >= 65536)
5371-
f_type= MYSQL_TYPE_MEDIUM_BLOB;
5372-
return f_type;
5373-
}
5374-
5375-
53765365
void Item_empty_string::make_field(Send_field *tmp_field)
53775366
{
53785367
init_make_field(tmp_field, string_field_type());

sql/item.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,10 @@ class Item: public Value_source,
761761
/* ... while cmp_type() specifies how it should be compared */
762762
Item_result cmp_type() const;
763763
virtual Item_result cast_to_int_type() const { return cmp_type(); }
764-
virtual enum_field_types string_field_type() const;
764+
enum_field_types string_field_type() const
765+
{
766+
return Type_handler::string_type_handler(max_length)->field_type();
767+
}
765768
enum_field_types field_type() const;
766769
virtual enum Type type() const =0;
767770
/*

sql/item_sum.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
11871187
if ((!item->fixed && item->fix_fields(thd, args)) ||
11881188
(item= args[0])->check_cols(1))
11891189
return TRUE;
1190-
decimals=item->decimals;
1190+
Type_std_attributes::set(args[0]);
11911191
with_subselect= args[0]->with_subselect;
11921192

11931193
Item *item2= item->real_item();
@@ -1196,13 +1196,13 @@ Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
11961196
else if (item->cmp_type() == TIME_RESULT)
11971197
set_handler_by_field_type(item2->field_type());
11981198
else
1199-
set_handler_by_result_type(item2->result_type());
1199+
set_handler_by_result_type(item2->result_type(),
1200+
max_length, collation.collation);
12001201

12011202
switch (Item_sum_hybrid::result_type()) {
12021203
case INT_RESULT:
12031204
case DECIMAL_RESULT:
12041205
case STRING_RESULT:
1205-
max_length= item->max_length;
12061206
break;
12071207
case REAL_RESULT:
12081208
max_length= float_length(decimals);
@@ -1214,7 +1214,6 @@ Item_sum_hybrid::fix_fields(THD *thd, Item **ref)
12141214
setup_hybrid(thd, args[0], NULL);
12151215
/* MIN/MAX can return NULL for empty set indepedent of the used column */
12161216
maybe_null= 1;
1217-
unsigned_flag=item->unsigned_flag;
12181217
result_field=0;
12191218
null_value=1;
12201219
fix_length_and_dec();

0 commit comments

Comments
 (0)