Thread: pgsql: SQL/JSON query functions

pgsql: SQL/JSON query functions

From
Andrew Dunstan
Date:
SQL/JSON query functions

This introduces the SQL/JSON functions for querying JSON data using
jsonpath expressions. The functions are:

JSON_EXISTS()
JSON_QUERY()
JSON_VALUE()

All of these functions only operate on jsonb. The workaround for now is
to cast the argument to jsonb.

JSON_EXISTS() tests if the jsonpath expression applied to the jsonb
value yields any values. JSON_VALUE() must return a single value, and an
error occurs if it tries to return multiple values. JSON_QUERY() must
return a json object or array, and there are various WRAPPER options for
handling scalar or multi-value results. Both these functions have
options for handling EMPTY and ERROR conditions.

Nikita Glukhov

Reviewers have included (in no particular order) Andres Freund, Alexander
Korotkov, Pavel Stehule, Andrew Alsup, Erik Rijkers, Zihong Yu,
Himanshu Upadhyaya, Daniel Gustafsson, Justin Pryzby.

Discussion: https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886deac4b@postgrespro.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1a36bc9dba8eae90963a586d37b6457b32b2fed4

Modified Files
--------------
src/backend/executor/execExpr.c             |  206 ++++--
src/backend/executor/execExprInterp.c       |  543 ++++++++++++++
src/backend/jit/llvm/llvmjit_expr.c         |    6 +
src/backend/jit/llvm/llvmjit_types.c        |    1 +
src/backend/nodes/copyfuncs.c               |  150 ++++
src/backend/nodes/equalfuncs.c              |  127 ++++
src/backend/nodes/makefuncs.c               |   15 +
src/backend/nodes/nodeFuncs.c               |  186 ++++-
src/backend/nodes/outfuncs.c                |   70 ++
src/backend/nodes/readfuncs.c               |   86 +++
src/backend/optimizer/path/costsize.c       |    3 +-
src/backend/optimizer/util/clauses.c        |   32 +
src/backend/parser/gram.y                   |  333 ++++++++-
src/backend/parser/parse_collate.c          |    4 +
src/backend/parser/parse_expr.c             |  490 ++++++++++++-
src/backend/parser/parse_target.c           |   15 +
src/backend/utils/adt/formatting.c          |   45 +-
src/backend/utils/adt/jsonb.c               |   62 ++
src/backend/utils/adt/jsonfuncs.c           |   50 +-
src/backend/utils/adt/jsonpath.c            |  257 +++++++
src/backend/utils/adt/jsonpath_exec.c       |  350 +++++++--
src/backend/utils/adt/ruleutils.c           |  135 ++++
src/backend/utils/misc/queryjumble.c        |   21 +
src/include/executor/execExpr.h             |   54 ++
src/include/executor/executor.h             |    2 +
src/include/nodes/makefuncs.h               |    1 +
src/include/nodes/nodes.h                   |    7 +
src/include/nodes/parsenodes.h              |   59 ++
src/include/nodes/primnodes.h               |  109 +++
src/include/parser/kwlist.h                 |   11 +
src/include/utils/formatting.h              |    4 +
src/include/utils/jsonb.h                   |    3 +
src/include/utils/jsonfuncs.h               |    4 +
src/include/utils/jsonpath.h                |   33 +
src/test/regress/expected/json_sqljson.out  |   15 +
src/test/regress/expected/jsonb_sqljson.out | 1018 +++++++++++++++++++++++++++
src/test/regress/parallel_schedule          |    2 +-
src/test/regress/sql/json_sqljson.sql       |   11 +
src/test/regress/sql/jsonb_sqljson.sql      |  317 +++++++++
39 files changed, 4716 insertions(+), 121 deletions(-)


Re: pgsql: SQL/JSON query functions

From
Michael Paquier
Date:
Hi Andrew,

On Tue, Mar 29, 2022 at 08:57:37PM +0000, Andrew Dunstan wrote:
> SQL/JSON query functions
>
> This introduces the SQL/JSON functions for querying JSON data using
> jsonpath expressions. The functions are:
>
> JSON_EXISTS()
> JSON_QUERY()
> JSON_VALUE()
>
> All of these functions only operate on jsonb. The workaround for now is
> to cast the argument to jsonb.
>
> JSON_EXISTS() tests if the jsonpath expression applied to the jsonb
> value yields any values. JSON_VALUE() must return a single value, and an
> error occurs if it tries to return multiple values. JSON_QUERY() must
> return a json object or array, and there are various WRAPPER options for
> handling scalar or multi-value results. Both these functions have
> options for handling EMPTY and ERROR conditions.

Some of the tests introduced in this commit are a bit unstable when
run through a primary/standby setup.  conchuela has showed one failure
today:
https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=conchuela&dt=2022-04-02%2002%3A36%3A03

Thanks,
--
Michael

Attachment

Re: pgsql: SQL/JSON query functions

From
Tom Lane
Date:
Michael Paquier <michael@paquier.xyz> writes:
> On Tue, Mar 29, 2022 at 08:57:37PM +0000, Andrew Dunstan wrote:
>> SQL/JSON query functions

> Some of the tests introduced in this commit are a bit unstable when
> run through a primary/standby setup.

Yeah, looks like some of these queries need an ORDER BY.

            regards, tom lane



Re: pgsql: SQL/JSON query functions

From
Andrew Dunstan
Date:
On 4/2/22 01:20, Tom Lane wrote:
> Michael Paquier <michael@paquier.xyz> writes:
>> On Tue, Mar 29, 2022 at 08:57:37PM +0000, Andrew Dunstan wrote:
>>> SQL/JSON query functions
>> Some of the tests introduced in this commit are a bit unstable when
>> run through a primary/standby setup.
> Yeah, looks like some of these queries need an ORDER BY.
>
>             


Done.


cheers


andrew


--
Andrew Dunstan
EDB: https://www.enterprisedb.com