From e9f0bd96f8fa49f9510660c9bdd8c07ea6a0814f Mon Sep 17 00:00:00 2001 From: wangfeihuo Date: Mon, 9 Dec 2024 09:20:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Djson=5Fextract=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E7=BB=93=E6=9E=9C=E4=B8=8Emysql=E4=B8=8D=E4=B8=80?= =?UTF-8?q?=E8=87=B4=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=AF=94=E8=BE=83=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=E7=BB=93=E6=9E=9C=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contrib/dolphin/expected/json_extract.out | 54 +++++++++++++++++-- contrib/dolphin/expected/json_merge_patch.out | 6 +-- .../dolphin/expected/json_merge_preserve.out | 6 +-- .../dolphin/plugin_utils/adt/jsonfuncs.cpp | 28 ++++++++++ contrib/dolphin/sql/json_extract.sql | 11 ++++ 5 files changed, 96 insertions(+), 9 deletions(-) diff --git a/contrib/dolphin/expected/json_extract.out b/contrib/dolphin/expected/json_extract.out index b11b3bf93..02c8aabc9 100644 --- a/contrib/dolphin/expected/json_extract.out +++ b/contrib/dolphin/expected/json_extract.out @@ -175,9 +175,9 @@ select json_extract(1, null); ERROR: Invalid data type for JSON data in argument 1 to function json_extract CONTEXT: referenced column: json_extract select json_extract('{"a": 341522654875451.12345678901234567890123456789012345678901234567}', '$.a'); - json_extract --------------------- - 341522654875451.12 + json_extract +----------------------------------------------------------------- + 341522654875451.12345678901234567890123456789012345678901234567 (1 row) create temp table test ( @@ -218,5 +218,53 @@ select * from test; {"c": true} (5 rows) +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name'); + json_extract +--------------------- + 9223372036854775805 +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); + json_extract +--------------------- + 9223372036854775806 +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854775807}', '$.name'); + json_extract +--------------------- + 9223372036854775807 +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854776000}', '$.name'); + json_extract +--------------------- + 9223372036854776000 +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854776001}', '$.name'); + json_extract +--------------------- + 9223372036854776001 +(1 row) + +select json_extract('{"id": "7", "name": 9.223372036854776e18}', '$.name'); + json_extract +---------------------- + 9.223372036854776e18 +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name') < json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); + ?column? +---------- + t +(1 row) + +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name') = json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); + ?column? +---------- + f +(1 row) + drop schema test_json_extract cascade; reset current_schema; diff --git a/contrib/dolphin/expected/json_merge_patch.out b/contrib/dolphin/expected/json_merge_patch.out index 995a22c28..520d02486 100644 --- a/contrib/dolphin/expected/json_merge_patch.out +++ b/contrib/dolphin/expected/json_merge_patch.out @@ -316,9 +316,9 @@ select json_merge_patch(1,null,'1'); ERROR: Invalid data type for JSON data in argument 1 to function json_merge_patch CONTEXT: referenced column: json_merge_patch select json_merge_patch('["a",1,{"a":"abc"}]','["b",341522654875451.12345678901234567890123456789012345678901234567,{"b":"bcd"}]'); - json_merge_patch ------------------------------------------ - ["b", 341522654875451.12, {"b": "bcd"}] + json_merge_patch +-------------------------------------------------------------------------------------- + ["b", 341522654875451.12345678901234567890123456789012345678901234567, {"b": "bcd"}] (1 row) create table test ( diff --git a/contrib/dolphin/expected/json_merge_preserve.out b/contrib/dolphin/expected/json_merge_preserve.out index 49cdfbf2a..dcf937603 100644 --- a/contrib/dolphin/expected/json_merge_preserve.out +++ b/contrib/dolphin/expected/json_merge_preserve.out @@ -316,9 +316,9 @@ select json_merge_preserve(1,null,'1'); ERROR: Invalid data type for JSON data in argument 1 to function json_merge_preserve CONTEXT: referenced column: json_merge_preserve select json_merge_preserve('["a",1,{"a":"abc"}]','["b",341522654875451.12345678901234567890123456789012345678901234567,{"b":"bcd"}]'); - json_merge_preserve ---------------------------------------------------------------- - ["a", 1, {"a": "abc"}, "b", 341522654875451.12, {"b": "bcd"}] + json_merge_preserve +------------------------------------------------------------------------------------------------------------ + ["a", 1, {"a": "abc"}, "b", 341522654875451.12345678901234567890123456789012345678901234567, {"b": "bcd"}] (1 row) create table test ( diff --git a/contrib/dolphin/plugin_utils/adt/jsonfuncs.cpp b/contrib/dolphin/plugin_utils/adt/jsonfuncs.cpp index 9549f40d5..7c4345661 100644 --- a/contrib/dolphin/plugin_utils/adt/jsonfuncs.cpp +++ b/contrib/dolphin/plugin_utils/adt/jsonfuncs.cpp @@ -4298,6 +4298,28 @@ Datum json_textcontains(PG_FUNCTION_ARGS) } #ifdef DOLPHIN + + + +void replace_json_double_to_numeric(cJSON *root) +{ + cJSON *item = NULL; + if (root == NULL) { + return; + } + if (root->type == cJSON_Number && root->valuestring != NULL) { + root->type = cJSON_Raw; + } + cJSON_ArrayForEach(item, root) { + const char *key = item->string; + cJSON *value = item; + if (item->type == cJSON_Number && item->valuestring != NULL) { + item->type = cJSON_Raw; + } + } +} + + static cJSON *input_to_cjson(Oid valtype, const char *funcName, int pos, Datum arg) { Oid typOutput; @@ -5482,6 +5504,7 @@ Datum json_extract(PG_FUNCTION_ARGS) valtype = get_fn_expr_argtype(fcinfo->flinfo, 0); arg = PG_GETARG_DATUM(0); root = input_to_cjson(valtype, "json_extract", 1, arg); + replace_json_double_to_numeric(root); cJSON_SortObject(root); deconstruct_array(in_array, TEXTOID, -1, false, 'i', &in_datums, &in_nulls, &in_count); @@ -6398,6 +6421,10 @@ static void json_regular_format(StringInfo result, cJSON *json) escape_json(result, json->valuestring); break; } + case cJSON_Raw :{ + appendStringInfoString(result, json->valuestring); + break; + } default: elog(ERROR, "unexpected json type: %d", (json->type & 0xFF)); } @@ -7680,6 +7707,7 @@ static cJSON *input_to_cjson_cmp(Oid valtype, Datum arg, bool& jsontype) { } else { jsontype = false; } + replace_json_double_to_numeric(root); return root; } diff --git a/contrib/dolphin/sql/json_extract.sql b/contrib/dolphin/sql/json_extract.sql index aeb103bf4..0b0b1d33f 100644 --- a/contrib/dolphin/sql/json_extract.sql +++ b/contrib/dolphin/sql/json_extract.sql @@ -54,5 +54,16 @@ insert into test values (json_extract('{"a": 43, "b": {"c": true}}', '$.b')); select * from test; + +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name'); +select json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); +select json_extract('{"id": "7", "name": 9223372036854775807}', '$.name'); +select json_extract('{"id": "7", "name": 9223372036854776000}', '$.name'); +select json_extract('{"id": "7", "name": 9223372036854776001}', '$.name'); +select json_extract('{"id": "7", "name": 9.223372036854776e18}', '$.name'); + +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name') < json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); +select json_extract('{"id": "7", "name": 9223372036854775805}', '$.name') = json_extract('{"id": "7", "name": 9223372036854775806}', '$.name'); + drop schema test_json_extract cascade; reset current_schema; \ No newline at end of file -- Gitee