SQL/JSON: Fix default ON ERROR behavior for JSON_TABLE
authorAmit Langote <[email protected]>
Fri, 6 Sep 2024 04:25:14 +0000 (13:25 +0900)
committerAmit Langote <[email protected]>
Fri, 6 Sep 2024 04:25:53 +0000 (13:25 +0900)
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 <[email protected]>
Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com
Back-through: 17

src/backend/parser/parse_expr.c
src/backend/utils/adt/ruleutils.c
src/test/regress/expected/sqljson_jsontable.out
src/test/regress/sql/sqljson_jsontable.sql

index 56e413da9f51f6c802ed40612d091df0cb9b8306..36c1b7a88f2c3684d6d58dac5c5e27a5d885e783 100644 (file)
@@ -4603,13 +4603,13 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
            }
 
            /*
-            * Assume EMPTY ON ERROR when ON ERROR is not specified.
+            * Assume EMPTY ARRAY ON ERROR when ON ERROR is not specified.
             *
             * ON EMPTY cannot be specified at the top level but it can be for
             * the individual columns.
             */
            jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
-                                                    JSON_BEHAVIOR_EMPTY,
+                                                    JSON_BEHAVIOR_EMPTY_ARRAY,
                                                     jsexpr->returning);
            break;
 
index 371b46e7a2d92080f268d92189873ed4be9f79ff..cd9c3eddd1dd0232e90afd9da9cbe142f9e079cb 100644 (file)
@@ -11875,7 +11875,7 @@ get_json_table(TableFunc *tf, deparse_context *context, bool showimplicit)
    get_json_table_columns(tf, castNode(JsonTablePathScan, tf->plan), context,
                           showimplicit);
 
-   if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY)
+   if (jexpr->on_error->btype != JSON_BEHAVIOR_EMPTY_ARRAY)
        get_json_behavior(jexpr->on_error, context, "ERROR");
 
    if (PRETTY_INDENT(context))
index b661b5e2d131e201c544005f3ed6847ac81d208c..7a698934ac93a8cbb0c5e3448abd10c1edaba154 100644 (file)
@@ -1155,3 +1155,25 @@ CREATE OR REPLACE VIEW public.json_table_view9 AS
             ) ERROR ON ERROR
         )
 DROP VIEW json_table_view8, json_table_view9;
+-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
+CREATE VIEW json_table_view8 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
+\sv json_table_view8;
+CREATE OR REPLACE VIEW public.json_table_view8 AS
+ SELECT a
+   FROM JSON_TABLE(
+            '"a"'::text, '$' AS json_table_path_0
+            COLUMNS (
+                a text PATH '$'
+            )
+        )
+CREATE VIEW json_table_view9 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);
+\sv json_table_view9;
+CREATE OR REPLACE VIEW public.json_table_view9 AS
+ SELECT a
+   FROM JSON_TABLE(
+            '"a"'::text, '$' AS json_table_path_0
+            COLUMNS (
+                a text PATH '$'
+            )
+        )
+DROP VIEW json_table_view8, json_table_view9;
index b533abf01ad9f99a4d22eda1094ab911e2d62f19..154eea79c768acf1f82896d84e4c59eb328e8111 100644 (file)
@@ -552,3 +552,12 @@ CREATE VIEW json_table_view9 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a t
 \sv json_table_view9;
 
 DROP VIEW json_table_view8, json_table_view9;
+
+-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
+CREATE VIEW json_table_view8 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
+\sv json_table_view8;
+
+CREATE VIEW json_table_view9 AS SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);
+\sv json_table_view9;
+
+DROP VIEW json_table_view8, json_table_view9;