1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

MDEV-27911: Implement range notation for json path

Range can be thought about in similar manner as wildcard (*) where
more than one elements are processed. To implement range notation, extended
json parser to parse the 'to' keyword and added JSON_PATH_ARRAY_RANGE for
path type. If there is 'to' keyword then use JSON_PATH_ARRAY range for
path type along with existing type.
This new integer to store the end index of range is n_item_end.
When there is 'to' keyword, store the integer in n_item_end else store in
n_item.
This commit is contained in:
Rucha Deodhar
2022-03-05 01:03:49 +05:30
parent abe9712194
commit c781cefd8a
8 changed files with 480 additions and 88 deletions

View File

@@ -402,7 +402,7 @@ select json_length( 'true', '$.*' );
json_length( 'true', '$.*' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_length'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_length'
# wildcards not allowed in path expressions for this function
error ER_INVALID_JSON_PATH_WILDCARD
@@ -410,7 +410,7 @@ select json_length( 'true', '$.foo**.bar' );
json_length( 'true', '$.foo**.bar' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_length'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_length'
select json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[0]' );
json_length( '[ 1, [ 2, 3, 4 ], 5 ]', '$[0]' )
1
@@ -691,25 +691,25 @@ select json_remove( '[ 1, 2, 3 ]', '$[*]' );
json_remove( '[ 1, 2, 3 ]', '$[*]' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_remove'
error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$**[2]' );
json_remove( '[ 1, 2, 3 ]', '$**[2]' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_remove'
error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$[2]', '$[*]' );
json_remove( '[ 1, 2, 3 ]', '$[2]', '$[*]' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 3 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 3 to function 'json_remove'
error ER_INVALID_JSON_PATH_WILDCARD
select json_remove( '[ 1, 2, 3 ]', '$[2]', '$**[2]' );
json_remove( '[ 1, 2, 3 ]', '$[2]', '$**[2]' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 3 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 3 to function 'json_remove'
select json_remove( '[ 1, 2, 3 ]', '$[0]' );
json_remove( '[ 1, 2, 3 ]', '$[0]' )
[2, 3]
@@ -730,7 +730,7 @@ select json_remove( '[ { "a": { "a": true } } ]', '$**.a' );
json_remove( '[ { "a": { "a": true } } ]', '$**.a' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_remove'
select json_remove( '[ { "a": true }, { "b": false }, { "c": null }, { "a": null } ]', '$[0].a', '$[2].c' );
json_remove( '[ { "a": true }, { "b": false }, { "c": null }, { "a": null } ]', '$[0].a', '$[2].c' )
[{}, {"b": false}, {}, {"a": null}]
@@ -739,7 +739,7 @@ select json_remove( '[ { "a": true }, { "b": [ { "c": { "a": true } } ] }, { "c
json_remove( '[ { "a": true }, { "b": [ { "c": { "a": true } } ] }, { "c": null }, { "a": null } ]', '$**.a' )
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_remove'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_remove'
select json_remove( '{"id": 123, "name": "systemQA", "array": [1, 2, 3]}', '$[0]' );
json_remove( '{"id": 123, "name": "systemQA", "array": [1, 2, 3]}', '$[0]' )
{"id": 123, "name": "systemQA", "array": [1, 2, 3]}
@@ -1210,7 +1210,7 @@ select json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.*.b');
json_keys('{"a": 1, "b": {"e": "foo", "b": 3}}', '$.*.b')
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_keys'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_keys'
# returns [ "a", "b" ]
SELECT JSON_KEYS('{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }');
JSON_KEYS('{ "a" : "foo", "b" : [ true, { "c" : "123" } ] }')
@@ -1637,7 +1637,7 @@ select json_array_append(json_compact('{"a": {"b": [3]}}'), '$**[0]', 6);
json_array_append(json_compact('{"a": {"b": [3]}}'), '$**[0]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_array_append'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_array_append'
# Auto-wrapping, since because the paths identify scalars.
# should return {"a": "foo", "b": ["bar", 4], "c": ["wibble", "grape"]}
SELECT JSON_ARRAY_APPEND('{"a": "foo", "b": "bar", "c": "wibble"}',
@@ -1668,13 +1668,13 @@ select json_array_append( '[[], [], []]', '$[*]', 3, '$[*]', 4);
json_array_append( '[[], [], []]', '$[*]', 3, '$[*]', 4)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_array_append'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_array_append'
error ER_INVALID_JSON_PATH_WILDCARD
select json_array_append( '[[], "not array", []]', '$[*]', 3, '$[*]', 4);
json_array_append( '[[], "not array", []]', '$[*]', 3, '$[*]', 4)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_array_append'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_array_append'
SELECT JSON_ARRAY_APPEND
(
'{ "a" : "foo", "b" : "bar", "c" : "wibble" }',
@@ -1775,73 +1775,73 @@ select json_insert(json_compact('{"a": [1], "b": 2}'), '$.*[1]', 6);
json_insert(json_compact('{"a": [1], "b": 2}'), '$.*[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": 1, "b": 2}'), '$.*[1]', 6);
json_insert(json_compact('{"a": 1, "b": 2}'), '$.*[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": 3}}'), '$.a.*[1]', 6);
json_insert(json_compact('{"a": {"b": 3}}'), '$.a.*[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": [3]}}'), '$.a.*[1]', 6);
json_insert(json_compact('{"a": {"b": [3]}}'), '$.a.*[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": 3}}'), '$**[1]', 6);
json_insert(json_compact('{"a": {"b": 3}}'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
json_insert(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1]'), '$[*][1]', 6);
json_insert(json_compact('[1]'), '$[*][1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1]'), '$**[1]', 6);
json_insert(json_compact('[1]'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1, [2], 3]'), '$[*][1]', 6);
json_insert(json_compact('[1, [2], 3]'), '$[*][1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[1, [2], 3]'), '$**[1]', 6);
json_insert(json_compact('[1, [2], 3]'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[[1]]'), '$[*][1]', 6);
json_insert(json_compact('[[1]]'), '$[*][1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(json_compact('[[1]]'), '$**[1]', 6);
json_insert(json_compact('[[1]]'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
select json_insert(json_compact('{"a": 3}'), '$[1]', 6);
json_insert(json_compact('{"a": 3}'), '$[1]', 6)
[{"a": 3}, 6]
@@ -2053,19 +2053,19 @@ select json_insert('[]', '$.a.*[1]', 6);
json_insert('[]', '$.a.*[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert('[]', '$**[1]', 6);
json_insert('[]', '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
error ER_INVALID_JSON_PATH_WILDCARD
select json_insert('[]', '$[*][1]', 6);
json_insert('[]', '$[*][1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_insert'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_insert'
select json_array_insert( '[ 1, 2, 3 ]', '$[1]', true, '$[1]', false );
json_array_insert( '[ 1, 2, 3 ]', '$[1]', true, '$[1]', false )
[1, false, true, 2, 3]
@@ -2154,7 +2154,7 @@ select json_set(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
json_set(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6)
NULL
Warnings:
Warning 4044 Wildcards in JSON path not allowed in argument 2 to function 'json_set'
Warning 4044 Wildcards or range in JSON path not allowed in argument 2 to function 'json_set'
SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a',
JSON_OBJECT());