Skip to content

Commit

Permalink
fix json_extract_string, json_extract_float64 with multi path param (m…
Browse files Browse the repository at this point in the history
…atrixorigin#18770)

fix json_extract_string, json_extract_float64 with multi path param

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

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

Expand Down
18 changes: 18 additions & 0 deletions test/distributed/cases/function/func_json_extract.result
Original file line number Diff line number Diff line change
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_value should use a path that retrives a single value
select json_extract_string(c1, '$.a1', '$.a2') from test_123;
invalid input: json_extract_value 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 951d582

Please sign in to comment.