Skip to content

Commit

Permalink
fix json extract function(#1.2 hotfix) (#18849)
Browse files Browse the repository at this point in the history
fix json extract function

Approved by: @aressu1985, @m-schen, @sukki37, @heni02
  • Loading branch information
YANGGMM authored Sep 19, 2024
1 parent 5cd387f commit 3f5bca9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
8 changes: 4 additions & 4 deletions pkg/sql/plan/function/func_builtin_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ func (op *opBuiltInJsonExtract) jsonExtractString(parameters []*vector.Vector, r
return err
}

if !op.simple {
return moerr.NewInvalidInput(proc.Ctx, "json_extract_value should use a path that retrives a single value")
if !op.simple || (op.simple && len(op.paths) > 1) {
return moerr.NewInvalidInput(proc.Ctx, "json_extract_string should use a path that retrives a single value")
}
if jsonVec.GetType().Oid == types.T_json {
fn = computeJsonSimple
Expand Down Expand Up @@ -332,8 +332,8 @@ func (op *opBuiltInJsonExtract) jsonExtractFloat64(parameters []*vector.Vector,
if err = op.buildPath(parameters, length); err != nil {
return err
}
if !op.simple {
return moerr.NewInvalidInput(proc.Ctx, "json_extract_value should use a path that retrives a single value")
if !op.simple || (op.simple && len(op.paths) > 1) {
return moerr.NewInvalidInput(proc.Ctx, "json_extract_float64 should use a path that retrives a single value")
}

if jsonVec.GetType().Oid == types.T_json {
Expand Down
42 changes: 30 additions & 12 deletions test/distributed/cases/function/func_json_extract.result
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,19 @@ select json_extract_string('{"a":{"q":[{"a":1},{"a":2},{"a":3}]}}','$.a.q');
json_extract_string({"a":{"q":[{"a":1},{"a":2},{"a":3}]}}, $.a.q)
null
select json_extract_string('[1,2,3]','$[*]');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a":[1,2,3,{"b":4}]}','$.a[3].b');
json_extract_string({"a":[1,2,3,{"b":4}]}, $.a[3].b)
null
select json_extract_string('{"a":[1,2,3,{"b":4}]}','$.a[3].c');
json_extract_string({"a":[1,2,3,{"b":4}]}, $.a[3].c)
null
select json_extract_string('{"a":[1,2,3,{"b":4}],"c":5}','$.*');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a":[1,2,3,{"a":4}]}','$**.a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a":[1,2,3,{"a":4}]}','$.a[*].a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a":1}','$[0]');
json_extract_string({"a":1}, $[0])
null
Expand All @@ -279,9 +279,9 @@ select json_extract_string('{"a":1}','$[1]');
json_extract_string({"a":1}, $[1])
null
select json_extract_string('{"af": [1, "2", {"aaf": "bb"}],"eab":"888"}','$**.f');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a": [1, "2", {"a": "bb"}]}','$**.a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_string should use a path that retrives a single value
select json_extract_string('{"a":"a1","b":"b1"}','$.**');
invalid input: invalid json path '$.**'
select json_extract_string('{"a":"a1","b":"b1"}','$**.1');
Expand Down Expand Up @@ -311,19 +311,19 @@ select json_extract_float64('{"a":{"q":[{"a":1},{"a":2},{"a":3}]}}','$.a.q');
json_extract_float64({"a":{"q":[{"a":1},{"a":2},{"a":3}]}}, $.a.q)
null
select json_extract_float64('[1,2,3]','$[*]');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a":[1,2,3,{"b":4}]}','$.a[3].b');
json_extract_float64({"a":[1,2,3,{"b":4}]}, $.a[3].b)
4.0
select json_extract_float64('{"a":[1,2,3,{"b":4}]}','$.a[3].c');
json_extract_float64({"a":[1,2,3,{"b":4}]}, $.a[3].c)
null
select json_extract_float64('{"a":[1,2,3,{"b":4}],"c":5}','$.*');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a":[1,2,3,{"a":4}]}','$**.a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a":[1,2,3,{"a":4}]}','$.a[*].a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a":1}','$[0]');
json_extract_float64({"a":1}, $[0])
null
Expand All @@ -337,9 +337,9 @@ select json_extract_float64('{"a":1}','$[1]');
json_extract_float64({"a":1}, $[1])
null
select json_extract_float64('{"af": [1, "2", {"aaf": "bb"}],"eab":"888"}','$**.f');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a": [1, "2", {"a": "bb"}]}','$**.a');
invalid input: json_extract_value should use a path that retrives a single value
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_float64('{"a":"a1","b":"b1"}','$.**');
invalid input: invalid json path '$.**'
select json_extract_float64('{"a":"a1","b":"b1"}','$**.1');
Expand Down Expand Up @@ -575,3 +575,21 @@ id extracted_name extracted_age extracted_math_score extracted_scien
8 Hank 25.0 80.5 88.0
9 Ivan 25.0 null null
drop database test;
create database test;
use test;
create table test_123(c1 json);
insert into test_123 values ('{"a1":10, "a2":20}');
insert into test_123 values ('{"a1":"test", "a2":"test2"}');
select * from test_123;
c1
{"a1": 10, "a2": 20}
{"a1": "test", "a2": "test2"}
select json_extract(c1, '$.a1', '$.a2') from test_123;
json_extract(c1, $.a1, $.a2)
[10, 20]
["test", "test2"]
select json_extract_float64(c1, '$.a1', '$.a2') from test_123;
invalid input: json_extract_float64 should use a path that retrives a single value
select json_extract_string(c1, '$.a1', '$.a2') from test_123;
invalid input: json_extract_string should use a path that retrives a single value
drop database test;
11 changes: 11 additions & 0 deletions test/distributed/cases/function/func_json_extract.test
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,14 @@ SELECT
FROM test_json_simplified;

drop database test;

create database test;
use test;
create table test_123(c1 json);
insert into test_123 values ('{"a1":10, "a2":20}');
insert into test_123 values ('{"a1":"test", "a2":"test2"}');
select * from test_123;
select json_extract(c1, '$.a1', '$.a2') from test_123;
select json_extract_float64(c1, '$.a1', '$.a2') from test_123;
select json_extract_string(c1, '$.a1', '$.a2') from test_123;
drop database test;

0 comments on commit 3f5bca9

Please sign in to comment.