pg.ddx.io pgsql-committers@postgresql.org mailing list archive
help / color / mirror / Atom feed From: Andrew Dunstan <andrew@dunslane.net>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: pgsql-committers@postgresql.org <pgsql-committers@postgresql.org>
Cc: Petr Jelinek <petr@2ndquadrant.com>
Cc: Dmitry Dolgov <9erthalion6@gmail.com>
Subject: Re: pgsql: Additional functions and operators for jsonb
Date: Wed, 13 May 2015 10:34:08 -0400
Message-ID: <555360E0.2040004@dunslane.net> (raw )
In-Reply-To: <CAFj8pRDX5KTc8C8yENzF1Z8APS=3sM3Z4T0gV1o+C8cbbxu3qw@mail.gmail.com >
References: <E1YsGHP-0006De-1z@gemulon.postgresql.org >
<CAFj8pRDX5KTc8C8yENzF1Z8APS=3sM3Z4T0gV1o+C8cbbxu3qw@mail.gmail.com >
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>
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=# select '{"x":20}'::jsonb - 'x'::text;
> ERROR: unknown type of jsonb container --->>> it should be empty
> jsonb, not error
> Time: 0.971 ms
> postgres=# select '{"x":20, "y":30}'::jsonb - 'x'::text;
> ┌───────────┐
> │ ?column? │
> ╞═══════════╡
> │ {"y": 30} │
> └───────────┘
> (1 row)
>
>
>
Some of this logic needs tightening. The attached patch should do that.
Among other things, it errors out if we attempt to delete or replace on
a scalar, just returns the input argument if there are no changes
instead of cloning it, checks via an Assert that the constructed
JsonbValue is not null, and otherwise returns it unconditionally. The
result is actually simpler code, I think. Before I apply it I'd like to
have comments from Dmitry and Petr, just to make sure I haven't
inadvertently slipped my moorings.
cheers
andrew
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
Attachments:
jsonbxtrafix.patch (text/x-patch, 5.1K) download
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));
}
view thread (10+ messages) latest in thread
Message-ID: <555360E0.2040004@dunslane.net>
Permalink: ../555360E0.2040004@dunslane.net/
Also on: postgresql.org/message-id/555360E0.2040004@dunslane.net
copy link · copy postgr.es · copy git am
reply Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: andrew@dunslane.net, pavel.stehule@gmail.com, petr@2ndquadrant.com, 9erthalion6@gmail.com
Subject: Re: pgsql: Additional functions and operators for jsonb
In-Reply-To: <555360E0.2040004@dunslane.net>
References: <E1YsGHP-0006De-1z@gemulon.postgresql.org> <CAFj8pRDX5KTc8C8yENzF1Z8APS=3sM3Z4T0gV1o+C8cbbxu3qw@mail.gmail.com> <555360E0.2040004@dunslane.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox