Fix JSON_ARRAY(query) empty set handling and view deparsing
According to the SQL/JSON standard, JSON_ARRAY(query) must return an
empty JSON array ('[]') when the subquery returns zero rows.
Previously, the parser rewrote JSON_ARRAY(query) into a JSON_ARRAYAGG
aggregate function. Because this aggregate evaluates to NULL over an
empty set without a GROUP BY clause, the constructor erroneously
returned NULL. Additionally, this premature rewrite baked physical
implementation details into the catalog, preventing ruleutils.c from
deparsing the original syntax for views.
This patch resolves both issues by introducing a new
JSCTOR_JSON_ARRAY_QUERY constructor type. The parser builds the
executable form --- a COALESCE-wrapped JSON_ARRAYAGG subquery --- from
raw parse nodes via transformExprRecurse, and stores it in the func
field. The original transformed Query is kept in a new orig_query
field so that ruleutils.c can deparse the original syntax for views.
During planning, eval_const_expressions replaces the node with the
pre-built func expression.
The deparsing issue was reported by Tom Lane.
Bump catalog version.
Bug: #19418
Reported-by: Lukas Eder <[email protected]>
Author: Richard Guo <[email protected]>
Reviewed-by: Amit Langote <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/8d829f5a0203ed4395e2d4d3931cd225a00c1b14
Modified Files
--------------
doc/src/sgml/func/func-json.sgml | 3 +-
src/backend/nodes/nodeFuncs.c | 11 ++++
src/backend/optimizer/util/clauses.c | 15 ++++-
src/backend/parser/parse_expr.c | 111 ++++++++++++++++++++++++++++-----
src/backend/utils/adt/ruleutils.c | 15 +++++
src/include/catalog/catversion.h | 2 +-
src/include/nodes/primnodes.h | 26 +++++---
src/test/regress/expected/sqljson.out | 113 ++++++++++++++++++++++++++++++++--
src/test/regress/sql/sqljson.sql | 49 +++++++++++++++
9 files changed, 316 insertions(+), 29 deletions(-)