SQL/JSON: Fix default ON ERROR behavior for JSON_TABLE
authorAmit Langote <[email protected]>
Fri, 6 Sep 2024 01:13:03 +0000 (10:13 +0900)
committerAmit Langote <[email protected]>
Fri, 6 Sep 2024 01:14:01 +0000 (10:14 +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
Backpatch-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 ebfde38a056810a002ea619d4488ef6fa3a33343..5c7aaa6159db5e15baf37defd2dd6cba91efbc52 100644 (file)
@@ -1150,3 +1150,28 @@ EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ER
    Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$') ERROR ON ERROR)
 (3 rows)
 
+-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
+ Table Function Scan on "json_table"  (cost=0.01..1.00 rows=100 width=32)
+   Output: a
+   Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
+(3 rows)
+
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
+ Table Function Scan on "json_table"  (cost=0.01..1.00 rows=100 width=32)
+   Output: a
+   Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
+(3 rows)
+
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);
+                                             QUERY PLAN                                              
+-----------------------------------------------------------------------------------------------------
+ Table Function Scan on "json_table"  (cost=0.01..1.00 rows=100 width=32)
+   Output: a
+   Table Function Call: JSON_TABLE('"a"'::jsonb, '$' AS json_table_path_0 COLUMNS (a text PATH '$'))
+(3 rows)
+
index c94088789260fe11f97cf04c6285e59317476f3e..31bc9c9ea0c30175c6b2ccbd8c82e16939217bb5 100644 (file)
@@ -547,3 +547,8 @@ SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on er
 -- behavior
 EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
 EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') ERROR ON ERROR);
+
+-- Test JSON_TABLE() deparsing -- don't emit default ON ERROR behavior
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$'));
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ON ERROR);
+EXPLAIN VERBOSE SELECT * from JSON_TABLE('"a"', '$' COLUMNS (a text PATH '$') EMPTY ARRAY ON ERROR);