Skip to content

Commit d7df63e

Browse files
MDEV-19487: JSON_TYPE doesnt detect the type of String Values
(returns NULL) and for Date/DateTime returns "INTEGER" Analysis: When the first character of json is scanned it is number. Based on that integer is returned. Fix: Scan rest of the json before returning the final result to ensure json is valid in the first place in order to have a valid type.
1 parent c6e3fe2 commit d7df63e

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

mysql-test/main/func_json.result

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,5 +1697,13 @@ INSERT INTO t (doc) VALUES ('{ "_id" : { "$oid" : "0ca0b0f0" },"a" : [ { "a" : [
16971697
ERROR 22001: Data too long for column 'id' at row 1
16981698
DROP TABLE t;
16991699
#
1700+
# MDEV-19487: JSON_TYPE doesnt detect the type of String Values (returns NULL) and for Date/DateTime returns "INTEGER"
1701+
#
1702+
SELECT JSON_TYPE(json_value(JSON_OBJECT("id", 1, "name", 'Monty', "date", Cast('2019-01-01' as Date) ), '$.date'));
1703+
JSON_TYPE(json_value(JSON_OBJECT("id", 1, "name", 'Monty', "date", Cast('2019-01-01' as Date) ), '$.date'))
1704+
NULL
1705+
Warnings:
1706+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 5
1707+
#
17001708
# End of 10.5 tests
17011709
#

mysql-test/main/func_json.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,13 @@ INSERT INTO t (doc) VALUES ('{ "_id" : { "$oid" : "0ca0b0f0" },"a" : [ { "a" : [
11251125

11261126
DROP TABLE t;
11271127

1128+
--echo #
1129+
--echo # MDEV-19487: JSON_TYPE doesnt detect the type of String Values (returns NULL) and for Date/DateTime returns "INTEGER"
1130+
--echo #
1131+
1132+
SELECT JSON_TYPE(json_value(JSON_OBJECT("id", 1, "name", 'Monty', "date", Cast('2019-01-01' as Date) ), '$.date'));
1133+
1134+
11281135
--echo #
11291136
--echo # End of 10.5 tests
11301137
--echo #

mysql-test/suite/json/r/json_no_table.result

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,19 +1002,27 @@ DOUBLE
10021002
error ER_INVALID_JSON_TEXT_IN_PARAM
10031003
select json_type(CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8'));
10041004
json_type(CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8'))
1005-
INTEGER
1005+
NULL
1006+
Warnings:
1007+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 5
10061008
# ----------------------------------------------------------------------
10071009
# Test of json_compact(literal)
10081010
# ----------------------------------------------------------------------
10091011
select json_type(json_compact(cast('2014-11-25 18:00' as datetime)));
10101012
json_type(json_compact(cast('2014-11-25 18:00' as datetime)))
1011-
INTEGER
1013+
NULL
1014+
Warnings:
1015+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 5
10121016
select json_type(json_compact(cast('2014-11-25' as date)));
10131017
json_type(json_compact(cast('2014-11-25' as date)))
1014-
INTEGER
1018+
NULL
1019+
Warnings:
1020+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 5
10151021
select json_type(json_compact(cast('18:00:59' as time)));
10161022
json_type(json_compact(cast('18:00:59' as time)))
1017-
INTEGER
1023+
NULL
1024+
Warnings:
1025+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 3
10181026
select json_type(json_compact(127));
10191027
json_type(json_compact(127))
10201028
INTEGER
@@ -1064,7 +1072,9 @@ json_type(json_compact(3.14E30))
10641072
DOUBLE
10651073
select json_type(json_compact(cast('10101abcde' as binary)));
10661074
json_type(json_compact(cast('10101abcde' as binary)))
1067-
INTEGER
1075+
NULL
1076+
Warnings:
1077+
Warning 4038 Syntax error in JSON text in argument 1 to function 'json_type' at position 6
10681078
select json_type(json_compact(ST_GeomFromText('POINT(1 1)')));
10691079
json_type(json_compact(ST_GeomFromText('POINT(1 1)')))
10701080
NULL

sql/item_jsonfunc.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,6 +2971,12 @@ String *Item_func_json_type::val_str(String *str)
29712971
break;
29722972
}
29732973

2974+
/* ensure the json is at least valid. */
2975+
while(json_scan_next(&je) == 0) {}
2976+
2977+
if (je.s.error)
2978+
goto error;
2979+
29742980
str->set(type, strlen(type), &my_charset_utf8mb3_general_ci);
29752981
return str;
29762982

0 commit comments

Comments
 (0)