Skip to content

Commit

Permalink
MDEV-22224: Support JSON Path negative index
Browse files Browse the repository at this point in the history
This patch can be viewed as combination of two parts:
1) Enabling '-' in the path so that the parser does not give out a warning.
2) Setting the negative index to a correct value and returning the
   appropriate value.

1) To enable using the negative index in the path:
To make the parser not return warning when negative index is used in path
'-' needs to be allowed in json path characters. P_NEG is added
to enable this and is made recognizable by setting the 45th index of
json_path_chr_map[] to P_NEG (instead of previous P_ETC)
because 45 corresponds to '-' in unicode.
When the path is being parsed and '-' is encountered, the parser should
recognize it as parsing '-' sign, so a new json state PS_NEG is required.
When the state is PS_NEG, it means that a negative integer is
going to be parsed so set is_negative_index of current step to 1 and
n_item is set accordingly when integer is encountered after '-'.
Next proceed with parsing rest of the path and get the correct path.
Next thing is parsing the json and returning correct value.

2) Setting the negative index to a correct value and returning the value:
While parsing json if we encounter array and the path step for the array
is a negative index (n_item < 0), then we can count the number of elements
in the array and set n_item to correct corresponding value. This is done in
json_skip_array_and_count.
  • Loading branch information
mariadb-RuchaDeodhar committed Apr 13, 2022
1 parent e98013c commit dfcbb30
Show file tree
Hide file tree
Showing 7 changed files with 682 additions and 76 deletions.
18 changes: 8 additions & 10 deletions include/json_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ enum json_path_step_types
JSON_PATH_KEY_WILD= 1+4,
JSON_PATH_KEY_DOUBLEWILD= 1+8,
JSON_PATH_ARRAY_WILD= 2+4,
JSON_PATH_ARRAY_DOUBLEWILD= 2+8
JSON_PATH_ARRAY_DOUBLEWILD= 2+8,
JSON_PATH_NEGATIVE_INDEX= 16
};


Expand All @@ -93,7 +94,7 @@ typedef struct st_json_path_step_t
/* see json_path_step_types */
const uchar *key; /* Pointer to the beginning of the key. */
const uchar *key_end; /* Pointer to the end of the key. */
uint n_item; /* Item number in an array. No meaning for the key step. */
int n_item; /* Item number in an array. No meaning for the key step. */
} json_path_step_t;


Expand Down Expand Up @@ -356,7 +357,7 @@ int json_skip_level_and_count(json_engine_t *j, int *n_items_skipped);
*/
int json_find_path(json_engine_t *je,
json_path_t *p, json_path_step_t **p_cur_step,
uint *array_counters);
int *array_counters);


typedef struct st_json_find_paths_t
Expand All @@ -365,7 +366,7 @@ typedef struct st_json_find_paths_t
json_path_t *paths;
uint cur_depth;
uint *path_depths;
uint array_counters[JSON_DEPTH_LIMIT];
int array_counters[JSON_DEPTH_LIMIT];
} json_find_paths_t;


Expand Down Expand Up @@ -425,13 +426,8 @@ int json_get_path_start(json_engine_t *je, CHARSET_INFO *i_cs,

int json_get_path_next(json_engine_t *je, json_path_t *p);


int json_path_parts_compare(
const json_path_step_t *a, const json_path_step_t *a_end,
const json_path_step_t *b, const json_path_step_t *b_end,
enum json_value_types vt);
int json_path_compare(const json_path_t *a, const json_path_t *b,
enum json_value_types vt);
enum json_value_types vt, const int* array_size_counter);

int json_valid(const char *js, size_t js_len, CHARSET_INFO *cs);

Expand All @@ -443,6 +439,8 @@ int json_locate_key(const char *js, const char *js_end,
int json_normalize(DYNAMIC_STRING *result,
const char *s, size_t size, CHARSET_INFO *cs);

int json_skip_array_and_count(json_engine_t *j, int* n_item);

#ifdef __cplusplus
}
#endif
Expand Down
262 changes: 262 additions & 0 deletions mysql-test/main/func_json.result
Original file line number Diff line number Diff line change
Expand Up @@ -1753,3 +1753,265 @@ Warning 4037 Unexpected end of JSON text in argument 2 to function 'json_overlap
#
# End of 10.9 test
#
#
# Beginning of 10.9 Test
#
# MDEV-22224: Support JSON Path negative index
#
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_APPEND(@json, '$.A[-2][-1]', 5);
JSON_ARRAY_APPEND(@json, '$.A[-2][-1]', 5)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, 13, {"key1": "value1"}, [15, 5]], true], "B": {"C": 1}, "D": 2}
SELECT JSON_ARRAY_APPEND(@json, '$.A[last-1][last]', 5);
JSON_ARRAY_APPEND(@json, '$.A[last-1][last]', 5)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, 13, {"key1": "value1"}, [15, 5]], true], "B": {"C": 1}, "D": 2}
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_ARRAY_INSERT(@json, '$.A[-2][-2]', 5);
JSON_ARRAY_INSERT(@json, '$.A[-2][-2]', 5)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, 13, {"key1": "value1"}, 5, [15]], true], "B": {"C": 1}, "D": 2}
SELECT JSON_ARRAY_INSERT(@json, '$.A[last-1][last-1]', 5);
JSON_ARRAY_INSERT(@json, '$.A[last-1][last-1]', 5)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, 13, {"key1": "value1"}, 5, [15]], true], "B": {"C": 1}, "D": 2}
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_CONTAINS(@json, '15', '$.A[-2][-1]');
JSON_CONTAINS(@json, '15', '$.A[-2][-1]')
1
SELECT JSON_CONTAINS(@json, '15', '$.A[last-1][last]');
JSON_CONTAINS(@json, '15', '$.A[last-1][last]')
1
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_CONTAINS_PATH(@json, 'one', '$.A[-2]');
JSON_CONTAINS_PATH(@json, 'one', '$.A[-2]')
1
SELECT JSON_CONTAINS_PATH(@json, 'one', '$.A[last-1]');
JSON_CONTAINS_PATH(@json, 'one', '$.A[last-1]')
1
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, 13, {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_EXISTS(@json, '$.A[-2][-1]');
JSON_EXISTS(@json, '$.A[-2][-1]')
1
SELECT JSON_EXISTS(@json, '$.A[last-1][last]');
JSON_EXISTS(@json, '$.A[last-1][last]')
1
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_EXTRACT(@json, '$.A[-8][1]');
JSON_EXTRACT(@json, '$.A[-8][1]')
5
SELECT JSON_EXTRACT(@json, '$.A[last-7][1]');
JSON_EXTRACT(@json, '$.A[last-7][1]')
5
SET @json= '[{"A": 1, "B": 2, "C": {"D": 3}},{"A": 1, "B": 2, "C": {"D": 3}}]';
SELECT JSON_KEYS(@json, '$[-1].C');
JSON_KEYS(@json, '$[-1].C')
["D"]
SELECT JSON_KEYS(@json, '$[last].C');
JSON_KEYS(@json, '$[last].C')
["D"]
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_LENGTH(@json, '$.A[-2][-3]');
JSON_LENGTH(@json, '$.A[-2][-3]')
2
SELECT JSON_LENGTH(@json, '$.A[last-1][last-2]');
JSON_LENGTH(@json, '$.A[last-1][last-2]')
2
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_QUERY(@json, '$.A[-8]');
JSON_QUERY(@json, '$.A[-8]')
[4, 5, 6]
SELECT JSON_QUERY(@json, '$.A[last-7]');
JSON_QUERY(@json, '$.A[last-7]')
[4, 5, 6]
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_REMOVE(@json, '$.A[-10]');
JSON_REMOVE(@json, '$.A[-10]')
{"A": [[1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], true], "B": {"C": 1}, "D": 2}
SELECT JSON_REMOVE(@json, '$.A[last-9]');
JSON_REMOVE(@json, '$.A[last-9]')
{"A": [[1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], true], "B": {"C": 1}, "D": 2}
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_REPLACE(@json, '$.A[-1]', 4);
JSON_REPLACE(@json, '$.A[-1]', 4)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], 4], "B": {"C": 1}, "D": 2}
SELECT JSON_REPLACE(@json, '$.A[last]', 4);
JSON_REPLACE(@json, '$.A[last]', 4)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, false, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], 4], "B": {"C": 1}, "D": 2}
SET @json = '["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]';
SELECT JSON_SEARCH(@json, 'all', 'abc', NULL, '$[-2]');
JSON_SEARCH(@json, 'all', 'abc', NULL, '$[-2]')
"$[2].x"
SELECT JSON_SEARCH(@json, 'all', 'abc', NULL, '$[last-1]');
JSON_SEARCH(@json, 'all', 'abc', NULL, '$[last-1]')
"$[2].x"
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":"value1"},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_SET(@json, '$.A[-4]', 100);
JSON_SET(@json, '$.A[-4]', 100)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, 100, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], true], "B": {"C": 1}, "D": 2}
SELECT JSON_SET(@json, '$.A[last-3]', 100);
JSON_SET(@json, '$.A[last-3]', 100)
{"A": [0, [1, 2, 3], [4, 5, 6], "seven", 0.8, true, 100, "eleven", [12, [13, 14], {"key1": "value1"}, [15]], true], "B": {"C": 1}, "D": 2}
SET @json='{
"A": [0,
[1, 2, 3],
[4, 5, 6],
"seven",
0.8,
true,
false,
"eleven",
[12, [13, 14], {"key1":123},[15]],
true],
"B": {"C": 1},
"D": 2
}';
SELECT JSON_VALUE(@json, '$.A[-2][-2].key1');
JSON_VALUE(@json, '$.A[-2][-2].key1')
123
SELECT JSON_VALUE(@json, '$.A[last-1][last-1].key1');
JSON_VALUE(@json, '$.A[last-1][last-1].key1')
123
#
# End of 10.9 Test
#

0 comments on commit dfcbb30

Please sign in to comment.