pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Additional functions and operators for jsonb
10+ messages / 6 participants
[nested] [flat]

* pgsql: Additional functions and operators for jsonb
@ 2015-05-12 19:55 Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:11 ` Re: pgsql: Additional functions and operators for jsonb Pavel Stehule <pavel.stehule@gmail.com>
  2015-05-12 20:37 ` Re: pgsql: Additional functions and operators for jsonb Tom Lane <tgl@sss.pgh.pa.us>
  2015-05-14 00:21 ` Re: pgsql: Additional functions and operators for jsonb Peter Geoghegan <pg@heroku.com>
  0 siblings, 3 replies; 10+ messages in thread

From: Andrew Dunstan @ 2015-05-12 19:55 UTC (permalink / raw)
  To: pgsql-committers

Additional functions and operators for jsonb

jsonb_pretty(jsonb) produces nicely indented json output.
jsonb || jsonb concatenates two jsonb values.
jsonb - text removes a key and its associated value from the json
jsonb - int removes the designated array element
jsonb - text[] removes a key and associated value or array element at
the designated path
jsonb_replace(jsonb,text[],jsonb) replaces the array element designated
by the path or the value associated with the key designated by the path
with the given value.

Original work by Dmitry Dolgov, adapted and reworked for PostgreSQL core
by Andrew Dunstan, reviewed and tidied up by Petr Jelinek.

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/c6947010ceb42143d9f047c65c1eac2b38928ab7

Modified Files
--------------
doc/src/sgml/func.sgml                |   62 +++
src/backend/utils/adt/jsonb.c         |   81 +++-
src/backend/utils/adt/jsonfuncs.c     |  717 +++++++++++++++++++++++++++++++++
src/include/catalog/pg_operator.h     |    8 +
src/include/catalog/pg_proc.h         |    9 +-
src/include/utils/jsonb.h             |   19 +-
src/test/regress/expected/jsonb.out   |  424 ++++++++++++++++++-
src/test/regress/expected/jsonb_1.out |  424 ++++++++++++++++++-
src/test/regress/sql/jsonb.sql        |   85 +++-
9 files changed, 1813 insertions(+), 16 deletions(-)


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers


^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
@ 2015-05-12 20:11 ` Pavel Stehule <pavel.stehule@gmail.com>
  2015-05-12 20:45   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-13 14:34   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2 siblings, 2 replies; 10+ messages in thread

From: Pavel Stehule @ 2015-05-12 20:11 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: pgsql-committers

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)


Regards

Pavel

2015-05-12 21:55 GMT+02:00 Andrew Dunstan <andrew@dunslane.net>:

> Additional functions and operators for jsonb
>
> jsonb_pretty(jsonb) produces nicely indented json output.
> jsonb || jsonb concatenates two jsonb values.
> jsonb - text removes a key and its associated value from the json
> jsonb - int removes the designated array element
> jsonb - text[] removes a key and associated value or array element at
> the designated path
> jsonb_replace(jsonb,text[],jsonb) replaces the array element designated
> by the path or the value associated with the key designated by the path
> with the given value.
>
> Original work by Dmitry Dolgov, adapted and reworked for PostgreSQL core
> by Andrew Dunstan, reviewed and tidied up by Petr Jelinek.
>
> Branch
> ------
> master
>
> Details
> -------
>
> http://git.postgresql.org/pg/commitdiff/c6947010ceb42143d9f047c65c1eac2b38928ab7
>
> Modified Files
> --------------
> doc/src/sgml/func.sgml                |   62 +++
> src/backend/utils/adt/jsonb.c         |   81 +++-
> src/backend/utils/adt/jsonfuncs.c     |  717
> +++++++++++++++++++++++++++++++++
> src/include/catalog/pg_operator.h     |    8 +
> src/include/catalog/pg_proc.h         |    9 +-
> src/include/utils/jsonb.h             |   19 +-
> src/test/regress/expected/jsonb.out   |  424 ++++++++++++++++++-
> src/test/regress/expected/jsonb_1.out |  424 ++++++++++++++++++-
> src/test/regress/sql/jsonb.sql        |   85 +++-
> 9 files changed, 1813 insertions(+), 16 deletions(-)
>
>
> --
> Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-committers
>

^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:11 ` Re: pgsql: Additional functions and operators for jsonb Pavel Stehule <pavel.stehule@gmail.com>
@ 2015-05-12 20:45   ` Andrew Dunstan <andrew@dunslane.net>
  1 sibling, 0 replies; 10+ messages in thread

From: Andrew Dunstan @ 2015-05-12 20:45 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-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)
>
>
> Regards
>
> Pavel
>

Yes. that's a bug.

will check.

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



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:11 ` Re: pgsql: Additional functions and operators for jsonb Pavel Stehule <pavel.stehule@gmail.com>
@ 2015-05-13 14:34   ` Andrew Dunstan <andrew@dunslane.net>
  2015-05-13 16:28     ` Re: pgsql: Additional functions and operators for jsonb Petr Jelinek <petr@2ndquadrant.com>
  2015-05-13 16:30     ` Re: pgsql: Additional functions and operators for jsonb Dmitry Dolgov <9erthalion6@gmail.com>
  1 sibling, 2 replies; 10+ messages in thread

From: Andrew Dunstan @ 2015-05-13 14:34 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-committers; Petr Jelinek <petr@2ndquadrant.com>; Dmitry Dolgov <9erthalion6@gmail.com>


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)); }
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:11 ` Re: pgsql: Additional functions and operators for jsonb Pavel Stehule <pavel.stehule@gmail.com>
  2015-05-13 14:34   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
@ 2015-05-13 16:28     ` Petr Jelinek <petr@2ndquadrant.com>
  1 sibling, 0 replies; 10+ messages in thread

From: Petr Jelinek @ 2015-05-13 16:28 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; Pavel Stehule <pavel.stehule@gmail.com>; +Cc: pgsql-committers; Dmitry Dolgov <9erthalion6@gmail.com>

On 13/05/15 16:34, Andrew Dunstan wrote:
>
> 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.
>

I think it's ok like this, except that the "cannot get delete from 
scalar" error message should probably be "cannot delete from scalar" in 
both places.


-- 
  Petr Jelinek                  http://www.2ndQuadrant.com/
  PostgreSQL Development, 24x7 Support, Training & Services


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:11 ` Re: pgsql: Additional functions and operators for jsonb Pavel Stehule <pavel.stehule@gmail.com>
  2015-05-13 14:34   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
@ 2015-05-13 16:30     ` Dmitry Dolgov <9erthalion6@gmail.com>
  1 sibling, 0 replies; 10+ messages in thread

From: Dmitry Dolgov @ 2015-05-13 16:30 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: Pavel Stehule <pavel.stehule@gmail.com>; pgsql-committers; Petr Jelinek <petr@2ndquadrant.com>

Yes, looks like everything is ok - i just would like to add few tests for
this issues.

On 13 May 2015 at 21:34, Andrew Dunstan <andrew@dunslane.net> wrote:

>
> 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:

jsonbxtrafix2.patch (application/octet-stream, 8.0K) 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)); } 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]');
^ permalink raw reply [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
@ 2015-05-12 20:37 ` Tom Lane <tgl@sss.pgh.pa.us>
  2015-05-12 20:44   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2 siblings, 1 reply; 10+ messages in thread

From: Tom Lane @ 2015-05-12 20:37 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: pgsql-committers

Andrew Dunstan <andrew@dunslane.net> writes:
> Additional functions and operators for jsonb

Buildfarm doesn't like this at all.

Also, you should have bumped catversion, no?

			regards, tom lane


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-12 20:37 ` Re: pgsql: Additional functions and operators for jsonb Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-05-12 20:44   ` Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 0 replies; 10+ messages in thread

From: Andrew Dunstan @ 2015-05-12 20:44 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-committers


On 05/12/2015 04:37 PM, Tom Lane wrote:
> Andrew Dunstan <andrew@dunslane.net> writes:
>> Additional functions and operators for jsonb
> Buildfarm doesn't like this at all.
>
> Also, you should have bumped catversion, no?
>
> 			


Yes, I should.

OK. I'll look at what's wrong.


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



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
@ 2015-05-14 00:21 ` Peter Geoghegan <pg@heroku.com>
  2015-05-14 03:02   ` Re: pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2 siblings, 1 reply; 10+ messages in thread

From: Peter Geoghegan @ 2015-05-14 00:21 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: pgsql-committers

On Tue, May 12, 2015 at 12:55 PM, Andrew Dunstan <andrew@dunslane.net> wrote:
> Additional functions and operators for jsonb

I think that there should be examples of usage of some of these new
operators within "8.14. JSON Types". In particular, I'd like to see an
example of use of the concatenate operator to perform "field
assignment" for JSON objects. The inability to do that is a complaint
that people have with jsonb, and this ought to be positioned as the
solution to that problem.

-- 
Peter Geoghegan


-- 
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers



^ permalink  raw  reply  [nested|flat] 10+ messages in thread

* Re: pgsql: Additional functions and operators for jsonb
  2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
  2015-05-14 00:21 ` Re: pgsql: Additional functions and operators for jsonb Peter Geoghegan <pg@heroku.com>
@ 2015-05-14 03:02   ` Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 0 replies; 10+ messages in thread

From: Andrew Dunstan @ 2015-05-14 03:02 UTC (permalink / raw)
  To: Peter Geoghegan <pg@heroku.com>; +Cc: pgsql-committers


On 05/13/2015 08:21 PM, Peter Geoghegan wrote:
> On Tue, May 12, 2015 at 12:55 PM, Andrew Dunstan <andrew@dunslane.net> wrote:
>> Additional functions and operators for jsonb
> I think that there should be examples of usage of some of these new
> operators within "8.14. JSON Types". In particular, I'd like to see an
> example of use of the concatenate operator to perform "field
> assignment" for JSON objects. The inability to do that is a complaint
> that people have with jsonb, and this ought to be positioned as the
> solution to that problem.
>


I'll be happy to look at docs patches. They don't have to be in by 
feature freeze, thank goodness.

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



^ permalink  raw  reply  [nested|flat] 10+ messages in thread


end of thread, other threads:[~2015-05-14 03:02 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-05-12 19:55 pgsql: Additional functions and operators for jsonb Andrew Dunstan <andrew@dunslane.net>
2015-05-12 20:11 ` Pavel Stehule <pavel.stehule@gmail.com>
2015-05-12 20:45   ` Andrew Dunstan <andrew@dunslane.net>
2015-05-13 14:34   ` Andrew Dunstan <andrew@dunslane.net>
2015-05-13 16:28     ` Petr Jelinek <petr@2ndquadrant.com>
2015-05-13 16:30     ` Dmitry Dolgov <9erthalion6@gmail.com>
2015-05-12 20:37 ` Tom Lane <tgl@sss.pgh.pa.us>
2015-05-12 20:44   ` Andrew Dunstan <andrew@dunslane.net>
2015-05-14 00:21 ` Peter Geoghegan <pg@heroku.com>
2015-05-14 03:02   ` Andrew Dunstan <andrew@dunslane.net>

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