Skip to content

Commit 5067c23

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 c88ce38 commit 5067c23

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/backend/parser/parse_expr.c

+2-2
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

+1-1
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

+25
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

+5
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)