pgsql: Additional functions and operators for jsonb

Started by Andrew Dunstanabout 11 years ago10 messagescomitters
Jump to latest
#1Andrew 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

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Andrew Dunstan (#1)
Re: pgsql: Additional functions and operators for jsonb

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

Show quoted text

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

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrew Dunstan (#1)
Re: pgsql: Additional functions and operators for jsonb

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

#4Andrew Dunstan
andrew@dunslane.net
In reply to: Tom Lane (#3)
Re: pgsql: Additional functions and operators for jsonb

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

#5Andrew Dunstan
andrew@dunslane.net
In reply to: Pavel Stehule (#2)
Re: pgsql: Additional functions and operators for jsonb

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

#6Andrew Dunstan
andrew@dunslane.net
In reply to: Pavel Stehule (#2)
Re: pgsql: Additional functions and operators for jsonb

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

Attachments:

jsonbxtrafix.patchtext/x-patch; name=jsonbxtrafix.patchDownload+35-53
#7Petr Jelinek
petr@2ndquadrant.com
In reply to: Andrew Dunstan (#6)
Re: pgsql: Additional functions and operators for jsonb

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

#8Dmitry Dolgov
9erthalion6@gmail.com
In reply to: Andrew Dunstan (#6)
Re: pgsql: Additional functions and operators for jsonb

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:

Show quoted text

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

Attachments:

jsonbxtrafix2.patchapplication/octet-stream; name=jsonbxtrafix2.patchDownload+58-53
In reply to: Andrew Dunstan (#1)
Re: pgsql: Additional functions and operators for jsonb

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

#10Andrew Dunstan
andrew@dunslane.net
In reply to: Peter Geoghegan (#9)
Re: pgsql: Additional functions and operators for jsonb

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