diff --git a/pkg/sql/plan/function/func_builtin_json.go b/pkg/sql/plan/function/func_builtin_json.go index 76406caca065..367dbd2d10d9 100644 --- a/pkg/sql/plan/function/func_builtin_json.go +++ b/pkg/sql/plan/function/func_builtin_json.go @@ -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 { @@ -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") } diff --git a/test/distributed/cases/function/func_json_extract.result b/test/distributed/cases/function/func_json_extract.result index 8527a7e49717..f2d2b18f0659 100644 --- a/test/distributed/cases/function/func_json_extract.result +++ b/test/distributed/cases/function/func_json_extract.result @@ -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; diff --git a/test/distributed/cases/function/func_json_extract.test b/test/distributed/cases/function/func_json_extract.test index 0da3b7631f46..4140b8e1723f 100644 --- a/test/distributed/cases/function/func_json_extract.test +++ b/test/distributed/cases/function/func_json_extract.test @@ -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;