From 3f5bca9889c977887ded185df0a533ab0d03082f Mon Sep 17 00:00:00 2001 From: YANGGMM Date: Thu, 19 Sep 2024 18:14:25 +0800 Subject: [PATCH] fix json extract function(#1.2 hotfix) (#18849) fix json extract function Approved by: @aressu1985, @m-schen, @sukki37, @heni02 --- pkg/sql/plan/function/func_builtin_json.go | 8 ++-- .../cases/function/func_json_extract.result | 42 +++++++++++++------ .../cases/function/func_json_extract.test | 11 +++++ 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/pkg/sql/plan/function/func_builtin_json.go b/pkg/sql/plan/function/func_builtin_json.go index 76406caca0651..645694430da71 100644 --- a/pkg/sql/plan/function/func_builtin_json.go +++ b/pkg/sql/plan/function/func_builtin_json.go @@ -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 @@ -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 { diff --git a/test/distributed/cases/function/func_json_extract.result b/test/distributed/cases/function/func_json_extract.result index 8527a7e49717b..12cf854faacbd 100644 --- a/test/distributed/cases/function/func_json_extract.result +++ b/test/distributed/cases/function/func_json_extract.result @@ -253,7 +253,7 @@ 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 @@ -261,11 +261,11 @@ 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 @@ -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'); @@ -311,7 +311,7 @@ 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 @@ -319,11 +319,11 @@ 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 @@ -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'); @@ -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; diff --git a/test/distributed/cases/function/func_json_extract.test b/test/distributed/cases/function/func_json_extract.test index 0da3b7631f46e..4140b8e1723fb 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;