diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 2d41455..31e149f 100644 --- a/src/backend/utils/adt/jsonfuncs.c +++ b/src/backend/utils/adt/jsonfuncs.c @@ -3332,7 +3332,7 @@ jsonb_concat(PG_FUNCTION_ARGS) out = JsonbValueToJsonb(res); } - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(out); } @@ -3349,7 +3349,6 @@ jsonb_delete(PG_FUNCTION_ARGS) text *key = PG_GETARG_TEXT_PP(1); char *keyptr = VARDATA_ANY(key); int keylen = VARSIZE_ANY_EXHDR(key); - Jsonb *out = palloc(VARSIZE(in)); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r; @@ -3357,10 +3356,13 @@ jsonb_delete(PG_FUNCTION_ARGS) *res = NULL; bool skipNested = false; - SET_VARSIZE(out, VARSIZE(in)); + if (JB_ROOT_IS_SCALAR(in)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot get delete from scalar"))); if (JB_ROOT_COUNT(in) == 0) - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); @@ -3382,13 +3384,9 @@ jsonb_delete(PG_FUNCTION_ARGS) res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } - if (res == NULL || (res->type == jbvArray && res->val.array.nElems == 0) || - (res->type == jbvObject && res->val.object.nPairs == 0)) - SET_VARSIZE(out, VARHDRSZ); - else - out = JsonbValueToJsonb(res); + Assert(res != NULL); - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(JsonbValueToJsonb(res)); } /* @@ -3403,7 +3401,6 @@ jsonb_delete_idx(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); int idx = PG_GETARG_INT32(1); - Jsonb *out = palloc(VARSIZE(in)); JsonbParseState *state = NULL; JsonbIterator *it; uint32 r, @@ -3412,11 +3409,13 @@ jsonb_delete_idx(PG_FUNCTION_ARGS) JsonbValue v, *res = NULL; + if (JB_ROOT_IS_SCALAR(in)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot get delete from scalar"))); + if (JB_ROOT_COUNT(in) == 0) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); @@ -3435,10 +3434,7 @@ jsonb_delete_idx(PG_FUNCTION_ARGS) } if (idx >= n) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); @@ -3457,13 +3453,9 @@ jsonb_delete_idx(PG_FUNCTION_ARGS) res = pushJsonbValue(&state, r, r < WJB_BEGIN_ARRAY ? &v : NULL); } - if (res == NULL || (res->type == jbvArray && res->val.array.nElems == 0) || - (res->type == jbvObject && res->val.object.nPairs == 0)) - SET_VARSIZE(out, VARHDRSZ); - else - out = JsonbValueToJsonb(res); + Assert (res != NULL); - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(JsonbValueToJsonb(res)); } /* @@ -3475,7 +3467,6 @@ jsonb_replace(PG_FUNCTION_ARGS) Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); Jsonb *newval = PG_GETARG_JSONB(2); - Jsonb *out = palloc(VARSIZE(in) + VARSIZE(newval)); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; @@ -3488,31 +3479,27 @@ jsonb_replace(PG_FUNCTION_ARGS) (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); + if (JB_ROOT_IS_SCALAR(in)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot replace path in scalar"))); + if (JB_ROOT_COUNT(in) == 0) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = replacePath(&it, path_elems, path_nulls, path_len, &st, 0, newval); - if (res == NULL) - SET_VARSIZE(out, VARHDRSZ); - else - out = JsonbValueToJsonb(res); + Assert (res != NULL); - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(JsonbValueToJsonb(res)); } @@ -3524,7 +3511,6 @@ jsonb_delete_path(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); ArrayType *path = PG_GETARG_ARRAYTYPE_P(1); - Jsonb *out = palloc(VARSIZE(in)); JsonbValue *res = NULL; Datum *path_elems; bool *path_nulls; @@ -3537,31 +3523,27 @@ jsonb_delete_path(PG_FUNCTION_ARGS) (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR), errmsg("wrong number of array subscripts"))); + if (JB_ROOT_IS_SCALAR(in)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("cannot delete path in scalar"))); + if (JB_ROOT_COUNT(in) == 0) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); deconstruct_array(path, TEXTOID, -1, false, 'i', &path_elems, &path_nulls, &path_len); if (path_len == 0) - { - memcpy(out, in, VARSIZE(in)); - PG_RETURN_POINTER(out); - } + PG_RETURN_JSONB(in); it = JsonbIteratorInit(&in->root); res = replacePath(&it, path_elems, path_nulls, path_len, &st, 0, NULL); - if (res == NULL) - SET_VARSIZE(out, VARHDRSZ); - else - out = JsonbValueToJsonb(res); + Assert (res != NULL); - PG_RETURN_POINTER(out); + PG_RETURN_JSONB(JsonbValueToJsonb(res)); } diff --git a/src/test/regress/expected/jsonb.out b/src/test/regress/expected/jsonb.out index 83201fb..49b0dd8 100644 --- a/src/test/regress/expected/jsonb.out +++ b/src/test/regress/expected/jsonb.out @@ -2923,6 +2923,14 @@ select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a'); {"b": 2, "c": 3} (1 row) +select jsonb_delete('{"a":1}'::jsonb, 'a'); + jsonb_delete +-------------- + {} +(1 row) + +select jsonb_delete('"a"'::jsonb, 'a'); +ERROR: cannot get delete from scalar select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a'); jsonb_delete ------------------ @@ -2947,6 +2955,14 @@ select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd'); {"a": 1, "b": 2, "c": 3} (1 row) +select '{"a":1}'::jsonb - 'a'::text; + ?column? +---------- + {} +(1 row) + +select '"a"'::jsonb - 'a'::text; +ERROR: cannot get delete from scalar select '{"a":1 , "b":2, "c":3}'::jsonb - 'a'::text; ?column? ------------------ @@ -3079,6 +3095,8 @@ select '{"a":1, "b":2, "c":3}'::jsonb - -4; {"a": 1, "b": 2, "c": 3} (1 row) +select jsonb_replace('"a"'::jsonb, '{a}', '[1,2,3]'); +ERROR: cannot replace path in scalar select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); jsonb_replace -------------------------------------------------------------------------- diff --git a/src/test/regress/sql/jsonb.sql b/src/test/regress/sql/jsonb.sql index 808da9c..86b24c5 100644 --- a/src/test/regress/sql/jsonb.sql +++ b/src/test/regress/sql/jsonb.sql @@ -718,10 +718,14 @@ select pg_column_size('{"aa":1, "b":2}'::jsonb || '{}'::jsonb) = pg_column_size( select pg_column_size('{}'::jsonb || '{"aa":1, "b":2}'::jsonb) = pg_column_size('{"aa":1, "b":2}'::jsonb); select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'a'); +select jsonb_delete('{"a":1}'::jsonb, 'a'); +select jsonb_delete('"a"'::jsonb, 'a'); select jsonb_delete('{"a":null , "b":2, "c":3}'::jsonb, 'a'); select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'b'); select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'c'); select jsonb_delete('{"a":1 , "b":2, "c":3}'::jsonb, 'd'); +select '{"a":1}'::jsonb - 'a'::text; +select '"a"'::jsonb - 'a'::text; select '{"a":1 , "b":2, "c":3}'::jsonb - 'a'::text; select '{"a":null , "b":2, "c":3}'::jsonb - 'a'::text; select '{"a":1 , "b":2, "c":3}'::jsonb - 'b'::text; @@ -747,6 +751,7 @@ select '{"a":1, "b":2, "c":3}'::jsonb - -2; select '{"a":1, "b":2, "c":3}'::jsonb - -3; select '{"a":1, "b":2, "c":3}'::jsonb - -4; +select jsonb_replace('"a"'::jsonb, '{a}', '[1,2,3]'); select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{n}', '[1,2,3]'); select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{b,-1}', '[1,2,3]'); select jsonb_replace('{"n":null, "a":1, "b":[1,2], "c":{"1":2}, "d":{"1":[2,3]}}'::jsonb, '{d,1,0}', '[1,2,3]');