Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ee75a03

Browse files
committed
SQL/JSON: Fix JSON_TABLE() column deparsing
The deparsing code in get_json_expr_options() unnecessarily emitted the default column-specific ON ERROR / EMPTY behavior when the top-level ON ERROR behavior in JSON_TABLE was set to ERROR. Fix that by not overriding the column-specific default, determined based on the column's JsonExprOp in get_json_table_columns(), with JSON_BEHAVIOR_ERROR when that is the top-level ON ERROR behavior. Note that this only removes redundancy; the current deparsing output is not incorrect, just redundant. Reviewed-by: Jian He <jian.universality@gmail.com> Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com Backpatch-through: 17
1 parent 4d7e24e commit ee75a03

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11719,7 +11719,6 @@ get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan,
1171911719
bool showimplicit)
1172011720
{
1172111721
StringInfo buf = context->buf;
11722-
JsonExpr *jexpr = castNode(JsonExpr, tf->docexpr);
1172311722
ListCell *lc_colname;
1172411723
ListCell *lc_coltype;
1172511724
ListCell *lc_coltypmod;
@@ -11772,6 +11771,10 @@ get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan,
1177211771
if (ordinality)
1177311772
continue;
1177411773

11774+
/*
11775+
* Set default_behavior to guide get_json_expr_options() on whether to
11776+
* to emit the ON ERROR / EMPTY clauses.
11777+
*/
1177511778
if (colexpr->op == JSON_EXISTS_OP)
1177611779
{
1177711780
appendStringInfoString(buf, " EXISTS");
@@ -11795,9 +11798,6 @@ get_json_table_columns(TableFunc *tf, JsonTablePathScan *scan,
1179511798
default_behavior = JSON_BEHAVIOR_NULL;
1179611799
}
1179711800

11798-
if (jexpr->on_error->btype == JSON_BEHAVIOR_ERROR)
11799-
default_behavior = JSON_BEHAVIOR_ERROR;
11800-
1180111801
appendStringInfoString(buf, " PATH ");
1180211802

1180311803
get_json_path_spec(colexpr->path_spec, context, showimplicit);

src/test/regress/expected/sqljson_jsontable.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,3 +1132,26 @@ ERROR: invalid ON ERROR behavior for column "a"
11321132
LINE 1: ...M JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty obje...
11331133
^
11341134
DETAIL: Only ERROR, TRUE, FALSE, or UNKNOWN is allowed in ON ERROR for EXISTS columns.
1135+
-- Test JSON_TABLE() column deparsing -- don't emit default ON ERROR / EMPTY
1136+
-- behavior
1137+
CREATE VIEW json_table_view8 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
1138+
\sv json_table_view8;
1139+
CREATE OR REPLACE VIEW public.json_table_view8 AS
1140+
SELECT a
1141+
FROM JSON_TABLE(
1142+
'"a"'::text, '$' AS json_table_path_0
1143+
COLUMNS (
1144+
a text PATH '$'
1145+
)
1146+
)
1147+
CREATE VIEW json_table_view9 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ERROR ON ERROR);
1148+
\sv json_table_view9;
1149+
CREATE OR REPLACE VIEW public.json_table_view9 AS
1150+
SELECT a
1151+
FROM JSON_TABLE(
1152+
'"a"'::text, '$' AS json_table_path_0
1153+
COLUMNS (
1154+
a text PATH '$'
1155+
) ERROR ON ERROR
1156+
)
1157+
DROP VIEW json_table_view8, json_table_view9;

src/test/regress/sql/sqljson_jsontable.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,3 +542,13 @@ SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int) NULL ON ERROR);
542542
SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int true on empty));
543543
SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int omit quotes true on error));
544544
SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on error));
545+
546+
-- Test JSON_TABLE() column deparsing -- don't emit default ON ERROR / EMPTY
547+
-- behavior
548+
CREATE VIEW json_table_view8 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
549+
\sv json_table_view8;
550+
551+
CREATE VIEW json_table_view9 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ERROR ON ERROR);
552+
\sv json_table_view9;
553+
554+
DROP VIEW json_table_view8, json_table_view9;

0 commit comments

Comments
 (0)