Skip to content

Commit

Permalink
MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mariadb-RuchaDeodhar committed Nov 2, 2023
1 parent be24e75 commit 9cc179c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
21 changes: 21 additions & 0 deletions mysql-test/main/func_json.result
Original file line number Diff line number Diff line change
Expand Up @@ -2608,3 +2608,24 @@ SET @@collation_connection= @save_collation_connection;
#
# End of 10.9 Test
#
#
# MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
# as first character in key
#
CREATE TEMPORARY TABLE IF NOT EXISTS jsonTest AS
SELECT '{ "-1234" : "something",
"12-34" : "else",
"1234-" : "and",
"1234" : "match" }' AS 'message';
SELECT JSON_SEARCH(message, 'one', 'something') AS t1_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'something'))) AS t1_result,
JSON_SEARCH(message, 'one', 'else') AS t2_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'else'))) AS t2_result,
JSON_SEARCH(message, 'one', 'and') AS t3_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'and'))) AS t3_result,
JSON_SEARCH(message, 'one', 'match') AS t4_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'match'))) AS t4_result
FROM jsonTest;
t1_path t1_result t2_path t2_result t3_path t3_result t4_path t4_result
"$.-1234" something "$.12-34" else "$.1234-" and "$.1234" match
# End of 11.0 test
23 changes: 23 additions & 0 deletions mysql-test/main/func_json.test
Original file line number Diff line number Diff line change
Expand Up @@ -1809,3 +1809,26 @@ SET @@collation_connection= @save_collation_connection;
--echo #
--echo # End of 10.9 Test
--echo #

--echo #
--echo # MDEV-32007: JSON_VALUE and JSON_EXTRACT doesn't handle dash (-)
--echo # as first character in key
--echo #

CREATE TEMPORARY TABLE IF NOT EXISTS jsonTest AS
SELECT '{ "-1234" : "something",
"12-34" : "else",
"1234-" : "and",
"1234" : "match" }' AS 'message';

SELECT JSON_SEARCH(message, 'one', 'something') AS t1_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'something'))) AS t1_result,
JSON_SEARCH(message, 'one', 'else') AS t2_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'else'))) AS t2_result,
JSON_SEARCH(message, 'one', 'and') AS t3_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'and'))) AS t3_result,
JSON_SEARCH(message, 'one', 'match') AS t4_path,
JSON_VALUE(message, JSON_UNQUOTE(JSON_SEARCH(message, 'one', 'match'))) AS t4_result
FROM jsonTest;

--echo # End of 11.0 test
2 changes: 1 addition & 1 deletion strings/json_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1093,7 +1093,7 @@ static int json_path_transitions[N_PATH_STATES][N_PATH_CLASSES]=
/* AS */ { JE_EOS, JE_SYN, JE_SYN, JE_SYN, PS_T, PS_PT, JE_SYN, PS_NEG,
PS_Z, PS_INT, PS_LAST, PS_AS, JE_SYN, JE_SYN, JE_SYN,
JE_NOT_JSON_CHR, JE_BAD_CHR},
/* KEY */ { JE_EOS, PS_KNM, PS_KWD, JE_SYN, PS_KNM, PS_KNM, JE_SYN, JE_SYN,
/* KEY */ { JE_EOS, PS_KNM, PS_KWD, JE_SYN, PS_KNM, PS_KNM, JE_SYN, PS_KNM,
PS_KNM, PS_KNM, PS_KNM, PS_KNM, PS_KNM, JE_SYN, PS_KEYX, PS_KNM,
JE_NOT_JSON_CHR, JE_BAD_CHR},
/* KNM */ { PS_KOK, PS_KNM, PS_AST, PS_EAR, PS_KNM, PS_KNM, PS_EKY, PS_KNM,
Expand Down

0 comments on commit 9cc179c

Please sign in to comment.