SQL/JSON: Prevent ON EMPTY for EXISTS columns in JSON_TABLE()
authorAmit Langote <[email protected]>
Fri, 28 Jun 2024 04:59:13 +0000 (13:59 +0900)
committerAmit Langote <[email protected]>
Fri, 28 Jun 2024 05:01:43 +0000 (14:01 +0900)
Due to an oversight in de3600452b61, the ON EMPTY clause was
incorrectly allowed in the EXISTS column. Fix the grammar to prevent
this.

Discussion: https://postgr.es/m/CA%2BHiwqHh3YDXTpccgAo4CdfV9Mhy%2Bmg%3Doh6t8rfM5uLW1BJN4g%40mail.gmail.com

src/backend/parser/gram.y
src/test/regress/expected/sqljson_jsontable.out
src/test/regress/sql/sqljson_jsontable.sql

index 4a4b47ca501fd75557064801c6294c36917c8edd..a043fd4c669a8ad13ead2aebf599d78a84ce8392 100644 (file)
@@ -14265,7 +14265,7 @@ json_table_column_definition:
                }
            | ColId Typename
                EXISTS json_table_column_path_clause_opt
-               json_behavior_clause_opt
+               json_on_error_clause_opt
                {
                    JsonTableColumn *n = makeNode(JsonTableColumn);
 
@@ -14276,8 +14276,8 @@ json_table_column_definition:
                    n->wrapper = JSW_NONE;
                    n->quotes = JS_QUOTES_UNSPEC;
                    n->pathspec = (JsonTablePathSpec *) $4;
-                   n->on_empty = (JsonBehavior *) linitial($5);
-                   n->on_error = (JsonBehavior *) lsecond($5);
+                   n->on_empty = NULL;
+                   n->on_error = (JsonBehavior *) $5;
                    n->location = @1;
                    $$ = (Node *) n;
                }
index cee90cead13b6c46a56ddfb879c9322b28e78a85..73039ea65ebf01f28831e755dc460df23167ed28 100644 (file)
@@ -1067,3 +1067,8 @@ CREATE OR REPLACE VIEW public.jsonb_table_view7 AS
         ) sub
 DROP VIEW jsonb_table_view7;
 DROP TABLE s;
+-- Prevent ON EMPTY specification on EXISTS columns
+SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on empty));
+ERROR:  syntax error at or near "empty"
+LINE 1: ...sonb '1', '$' COLUMNS (a int exists empty object on empty));
+                                                               ^
index a1f924146e069f0e66827b11554ff3779c796805..bda579814810471c4f5f9efd17b8f8683dde42c8 100644 (file)
@@ -518,3 +518,6 @@ SELECT sub.* FROM s,
 \sv jsonb_table_view7
 DROP VIEW jsonb_table_view7;
 DROP TABLE s;
+
+-- Prevent ON EMPTY specification on EXISTS columns
+SELECT * FROM JSON_TABLE(jsonb '1', '$' COLUMNS (a int exists empty object on empty));