Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YsXjt-0002QU-GI for pgsql-committers@arkaria.postgresql.org; Wed, 13 May 2015 14:34:25 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YsXjt-0005Wj-1D for pgsql-committers@arkaria.postgresql.org; Wed, 13 May 2015 14:34:25 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YsXjr-0005Wb-Ol for pgsql-committers@postgresql.org; Wed, 13 May 2015 14:34:23 +0000 Received: from relay3-d.mail.gandi.net ([2001:4b98:c:538::195]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YsXjn-0001HF-5z for pgsql-committers@postgresql.org; Wed, 13 May 2015 14:34:21 +0000 Received: from mfilter5-d.gandi.net (mfilter5-d.gandi.net [217.70.178.132]) by relay3-d.mail.gandi.net (Postfix) with ESMTP id 40C3DA80F0; Wed, 13 May 2015 16:34:15 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at mfilter5-d.gandi.net Received: from relay3-d.mail.gandi.net ([217.70.183.195]) by mfilter5-d.gandi.net (mfilter5-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id 0Dm6EJO1DhzM; Wed, 13 May 2015 16:34:10 +0200 (CEST) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-98-27-58-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 9F604A80E9; Wed, 13 May 2015 16:34:09 +0200 (CEST) Message-ID: <555360E0.2040004@dunslane.net> Date: Wed, 13 May 2015 10:34:08 -0400 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: Pavel Stehule CC: "pgsql-committers@postgresql.org" , Petr Jelinek , Dmitry Dolgov <9erthalion6@gmail.com> Subject: Re: pgsql: Additional functions and operators for jsonb References: In-Reply-To: Content-Type: multipart/mixed; boundary="------------060500030304050706020004" X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-committers Precedence: bulk Sender: pgsql-committers-owner@postgresql.org This is a multi-part message in MIME format. --------------060500030304050706020004 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable On 05/12/2015 04:11 PM, Pavel Stehule wrote: > Hi > > I did some tests, and I am not sure if this is not bug: > > postgres=3D# select '{"x":20}'::jsonb - 'x'::text; > ERROR: unknown type of jsonb container --->>> it should be empty=20 > jsonb, not error > Time: 0.971 ms > postgres=3D# select '{"x":20, "y":30}'::jsonb - 'x'::text; > =E2=94=8C=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=90 > =E2=94=82 ?column? =E2=94=82 > =E2=95=9E=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=A1 > =E2=94=82 {"y": 30} =E2=94=82 > =E2=94=94=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=98 > (1 row) > > > Some of this logic needs tightening. The attached patch should do that.=20 Among other things, it errors out if we attempt to delete or replace on=20 a scalar, just returns the input argument if there are no changes=20 instead of cloning it, checks via an Assert that the constructed=20 JsonbValue is not null, and otherwise returns it unconditionally. The=20 result is actually simpler code, I think. Before I apply it I'd like to=20 have comments from Dmitry and Petr, just to make sure I haven't=20 inadvertently slipped my moorings. cheers andrew --------------060500030304050706020004 Content-Type: text/x-patch; name="jsonbxtrafix.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="jsonbxtrafix.patch" 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)); } --------------060500030304050706020004 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-committers --------------060500030304050706020004--