Add basic JSON_TABLE() functionality

JSON_TABLE() allows JSON data to be converted into a relational view
and thus used, for example, in a FROM clause, like other tabular
data.  Data to show in the view is selected from a source JSON object
using a JSON path expression to get a sequence of JSON objects that's
called a "row pattern", which becomes the source to compute the
SQL/JSON values that populate the view's output columns.  Column
values themselves are computed using JSON path expressions applied to
each of the JSON objects comprising the "row pattern", for which the
SQL/JSON query functions added in 6185c9737cf4 are used.

To implement JSON_TABLE() as a table function, this augments the
TableFunc and TableFuncScanState nodes that are currently used to
support XMLTABLE() with some JSON_TABLE()-specific fields.

Note that the JSON_TABLE() spec includes NESTED COLUMNS and PLAN
clauses, which are required to provide more flexibility to extract
data out of nested JSON objects, but they are not implemented here
to keep this commit of manageable size.

Author: Nikita Glukhov <n.glu...@postgrespro.ru>
Author: Teodor Sigaev <teo...@sigaev.ru>
Author: Oleg Bartunov <obartu...@gmail.com>
Author: Alexander Korotkov <aekorot...@gmail.com>
Author: Andrew Dunstan <and...@dunslane.net>
Author: Amit Langote <amitlangot...@gmail.com>
Author: Jian He <jian.universal...@gmail.com>

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, Álvaro Herrera, Jian He

Discussion: 
https://postgr.es/m/cd0bb935-0158-78a7-08b5-904886dea...@postgrespro.ru
Discussion: 
https://postgr.es/m/20220616233130.rparivafipt6d...@alap3.anarazel.de
Discussion: 
https://postgr.es/m/abd9b83b-aa66-f230-3d6d-734817f0995d%40postgresql.org
Discussion: 
https://postgr.es/m/CA+HiwqE4XTdfb1nW=ojoy_tqsrhyt-q_kb6i5d4xckyrlc1...@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/de3600452b61d1bc3967e9e37e86db8956c8f577

Modified Files
--------------
doc/src/sgml/func.sgml                             | 334 +++++++++++
src/backend/commands/explain.c                     |  21 +-
src/backend/executor/execExpr.c                    |  11 +-
src/backend/executor/execExprInterp.c              |   2 +
src/backend/executor/nodeTableFuncscan.c           |  41 +-
src/backend/nodes/makefuncs.c                      |  53 ++
src/backend/nodes/nodeFuncs.c                      |  36 ++
src/backend/parser/Makefile                        |   1 +
src/backend/parser/gram.y                          | 173 +++++-
src/backend/parser/meson.build                     |   1 +
src/backend/parser/parse_clause.c                  |  14 +-
src/backend/parser/parse_expr.c                    |  53 +-
src/backend/parser/parse_jsontable.c               | 421 ++++++++++++++
src/backend/parser/parse_relation.c                |   6 +-
src/backend/parser/parse_target.c                  |   1 +
src/backend/utils/adt/jsonpath_exec.c              | 372 ++++++++++++
src/backend/utils/adt/ruleutils.c                  | 185 +++++-
src/include/nodes/execnodes.h                      |   2 +
src/include/nodes/makefuncs.h                      |   5 +
src/include/nodes/parsenodes.h                     |  66 +++
src/include/nodes/primnodes.h                      |  56 +-
src/include/parser/kwlist.h                        |   3 +
src/include/parser/parse_clause.h                  |   3 +
src/include/utils/jsonpath.h                       |   3 +
src/interfaces/ecpg/test/ecpg_schedule             |   1 +
.../ecpg/test/expected/sql-sqljson_jsontable.c     | 143 +++++
.../test/expected/sql-sqljson_jsontable.stderr     |  16 +
.../test/expected/sql-sqljson_jsontable.stdout     |   1 +
src/interfaces/ecpg/test/sql/Makefile              |   1 +
src/interfaces/ecpg/test/sql/meson.build           |   1 +
src/interfaces/ecpg/test/sql/sqljson_jsontable.pgc |  29 +
src/test/regress/expected/sqljson_jsontable.out    | 636 +++++++++++++++++++++
src/test/regress/parallel_schedule                 |   2 +-
src/test/regress/sql/sqljson_jsontable.sql         | 290 ++++++++++
src/tools/pgindent/typedefs.list                   |  12 +
35 files changed, 2943 insertions(+), 52 deletions(-)

Reply via email to