Skip to content

Commit 565caaa

Browse files
committed
SQL/JSON: Fix default ON ERROR behavior for JSON_TABLE
Use EMPTY ARRAY instead of EMPTY. This change does not affect the runtime behavior of JSON_TABLE(), which continues to return an empty relation ON ERROR. It only alters whether the default ON ERROR behavior is shown in the deparsed output. Reported-by: Jian He <jian.universality@gmail.com> Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com Backpatch-through: 17
1 parent 6822285 commit 565caaa

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/backend/parser/parse_expr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4603,13 +4603,13 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
46034603
}
46044604

46054605
/*
4606-
* Assume EMPTY ON ERROR when ON ERROR is not specified.
4606+
* Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
46074607
*
46084608
* ON EMPTY cannot be specified at the top level but it can be for
46094609
* the individual columns.
46104610
*/
46114611
jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
4612-
JSON_BEHAVIOR_EMPTY,
4612+
JSON_BEHAVIOR_EMPTY_ARRAY,
46134613
jsexpr->returning);
46144614
break;
46154615

src/backend/utils/adt/ruleutils.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11875,7 +11875,7 @@ get_json_table(TableFunc *tf, deparse_context *context, bool showimplicit)
1187511875
get_json_table_columns(tf, castNode(JsonTablePathScan, tf->plan), context,
1187611876
showimplicit);
1187711877

11878-
if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY)
11878+
if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
1187911879
get_json_behavior(jexpr->on_error, context, "ERROR");
1188011880

1188111881
if (PRETTY_INDENT(context))

src/test/regress/expected/sqljson_jsontable.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,3 +1150,28 @@ EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ER
11501150
Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$') ERROR ON ERROR)
11511151
(3 rows)
11521152

1153+
-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
1154+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
1155+
QUERY PLAN
1156+
-----------------------------------------------------------------------------------------------------
1157+
Table Function Scan on "json_table" (cost=0.01..1.00 rows=100 width=32)
1158+
Output: a
1159+
Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
1160+
(3 rows)
1161+
1162+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
1163+
QUERY PLAN
1164+
-----------------------------------------------------------------------------------------------------
1165+
Table Function Scan on "json_table" (cost=0.01..1.00 rows=100 width=32)
1166+
Output: a
1167+
Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
1168+
(3 rows)
1169+
1170+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);
1171+
QUERY PLAN
1172+
-----------------------------------------------------------------------------------------------------
1173+
Table Function Scan on "json_table" (cost=0.01..1.00 rows=100 width=32)
1174+
Output: a
1175+
Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
1176+
(3 rows)
1177+

src/test/regress/sql/sqljson_jsontable.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,3 +547,8 @@ SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on er
547547
-- behavior
548548
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
549549
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ERROR ON ERROR);
550+
551+
-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
552+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
553+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
554+
EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy