Обсуждение: pgsql: Additional functions and operators for jsonb

Поиск
Список
Период
Сортировка

pgsql: Additional functions and operators for jsonb

От
Andrew Dunstan
Дата:
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(-)


Re: pgsql: Additional functions and operators for jsonb

От
Pavel Stehule
Дата:
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

Re: pgsql: Additional functions and operators for jsonb

От
Tom Lane
Дата:
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


Re: pgsql: Additional functions and operators for jsonb

От
Andrew Dunstan
Дата:
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


Re: pgsql: Additional functions and operators for jsonb

От
Andrew Dunstan
Дата:
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


Re: pgsql: Additional functions and operators for jsonb

От
Andrew Dunstan
Дата:
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




Вложения

Re: pgsql: Additional functions and operators for jsonb

От
Petr Jelinek
Дата:
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


Re: pgsql: Additional functions and operators for jsonb

От
Dmitry Dolgov
Дата:
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




Вложения

Re: pgsql: Additional functions and operators for jsonb

От
Peter Geoghegan
Дата:
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


Re: pgsql: Additional functions and operators for jsonb

От
Andrew Dunstan
Дата:
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