Skip to content

Commit 9cc179c

Browse files
MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
as first character in key Analysis: While parsing the path, if '-' is encountered as a part of the key, the state of the parser changes to error. Hence NULL is returned eventually. Fix: If '-' encountered as part of the key, change the state appropriately to continue scanning the key.
1 parent be24e75 commit 9cc179c

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

mysql-test/main/func_json.result

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,3 +2608,24 @@ SET @@collation_connection= @save_collation_connection;
26082608
#
26092609
# End of 10.9 Test
26102610
#
2611+
#
2612+
# MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
2613+
# as first character in key
2614+
#
2615+
CREATE TEMPORARY TABLE IF NOT EXISTS jsonTest AS
2616+
SELECT '{ "-1234" : "something",
2617+
"12-34" : "else",
2618+
"1234-" : "and",
2619+
"1234" : "match" }' AS 'message';
2620+
SELECT JSON_SEARCH(message, 'one', 'something') AS t1_path,
2621+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'something'))) AS t1_result,
2622+
JSON_SEARCH(message, 'one', 'else') AS t2_path,
2623+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'else'))) AS t2_result,
2624+
JSON_SEARCH(message, 'one', 'and') AS t3_path,
2625+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'and'))) AS t3_result,
2626+
JSON_SEARCH(message, 'one', 'match') AS t4_path,
2627+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'match'))) AS t4_result
2628+
FROM jsonTest;
2629+
t1_path t1_result t2_path t2_result t3_path t3_result t4_path t4_result
2630+
"$.-1234" something "$.12-34" else "$.1234-" and "$.1234" match
2631+
# End of 11.0 test

mysql-test/main/func_json.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,3 +1809,26 @@ SET @@collation_connection= @save_collation_connection;
18091809
--echo #
18101810
--echo # End of 10.9 Test
18111811
--echo #
1812+
1813+
--echo #
1814+
--echo # MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
1815+
--echo # as first character in key
1816+
--echo #
1817+
1818+
CREATE TEMPORARY TABLE IF NOT EXISTS jsonTest AS
1819+
SELECT '{ "-1234" : "something",
1820+
"12-34" : "else",
1821+
"1234-" : "and",
1822+
"1234" : "match" }' AS 'message';
1823+
1824+
SELECT JSON_SEARCH(message, 'one', 'something') AS t1_path,
1825+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'something'))) AS t1_result,
1826+
JSON_SEARCH(message, 'one', 'else') AS t2_path,
1827+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'else'))) AS t2_result,
1828+
JSON_SEARCH(message, 'one', 'and') AS t3_path,
1829+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'and'))) AS t3_result,
1830+
JSON_SEARCH(message, 'one', 'match') AS t4_path,
1831+
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'match'))) AS t4_result
1832+
FROM jsonTest;
1833+
1834+
--echo # End of 11.0 test

strings/json_lib.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static int json_path_transitions[N_PATH_STATES][N_PATH_CLASSES]=
10931093
/* AS */ { JE_EOS, JE_SYN, JE_SYN, JE_SYN, PS_T, PS_PT, JE_SYN, PS_NEG,
10941094
PS_Z, PS_INT, PS_LAST, PS_AS, JE_SYN, JE_SYN, JE_SYN,
10951095
JE_NOT_JSON_CHR, JE_BAD_CHR},
1096-
/* KEY */ { JE_EOS, PS_KNM, PS_KWD, JE_SYN, PS_KNM, PS_KNM, JE_SYN, JE_SYN,
1096+
/* KEY */ { JE_EOS, PS_KNM, PS_KWD, JE_SYN, PS_KNM, PS_KNM, JE_SYN, PS_KNM,
10971097
PS_KNM, PS_KNM, PS_KNM, PS_KNM, PS_KNM, JE_SYN, PS_KEYX, PS_KNM,
10981098
JE_NOT_JSON_CHR, JE_BAD_CHR},
10991099
/* KNM */ { PS_KOK, PS_KNM, PS_AST, PS_EAR, PS_KNM, PS_KNM, PS_EKY, PS_KNM,

0 commit comments

Comments
 (0)