]> granicus.if.org Git - postgresql/commitdiff
Fix dumping of FUNCTION RTEs that contain non-function-call expressions.
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Jul 2017 23:24:44 +0000 (19:24 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 13 Jul 2017 23:24:44 +0000 (19:24 -0400)
The grammar will only accept something syntactically similar to a function
call in a function-in-FROM expression.  However, there are various ways
to input something that ruleutils.c won't deparse that way, potentially
leading to a view or rule that fails dump/reload.  Fix by inserting a
dummy CAST around anything that isn't going to deparse as a function
(which is one of the ways to get something like that in there in the
first place).

In HEAD, also make use of the infrastructure added by this to avoid
emitting unnecessary parentheses in CREATE INDEX deparsing.  I did
not change that in back branches, thinking that people might find it
to be unexpected/unnecessary behavioral change.

In HEAD, also fix incorrect logic for when to add extra parens to
partition key expressions.  Somebody apparently thought they could
get away with simpler logic than pg_get_indexdef_worker has, but
they were wrong --- a counterexample is PARTITION BY LIST ((a[1])).
Ignoring the prettyprint flag for partition expressions isn't exactly
a nice solution anyway.

This has been broken all along, so back-patch to all supported branches.

Discussion: https://postgr.es/m/10477.1499970459@sss.pgh.pa.us

src/backend/utils/adt/ruleutils.c
src/test/regress/expected/create_view.out
src/test/regress/sql/create_view.sql

index 6a58b7e1ff45a5648e4ca1297197da8880cf4721..a364602b221a97e5ff898ba88391e0229dadda1c 100644 (file)
@@ -409,6 +409,9 @@ static void get_rule_expr(Node *node, deparse_context *context,
                          bool showimplicit);
 static void get_rule_expr_toplevel(Node *node, deparse_context *context,
                                           bool showimplicit);
+static void get_rule_expr_funccall(Node *node, deparse_context *context,
+                                          bool showimplicit);
+static bool looks_like_function(Node *node);
 static void get_oper_expr(OpExpr *expr, deparse_context *context);
 static void get_func_expr(FuncExpr *expr, deparse_context *context,
                          bool showimplicit);
@@ -8259,6 +8262,63 @@ get_rule_expr_toplevel(Node *node, deparse_context *context,
                get_rule_expr(node, context, showimplicit);
 }
 
+/*
+ * get_rule_expr_funccall              - Parse back a function-call expression
+ *
+ * Same as get_rule_expr(), except that we guarantee that the output will
+ * look like a function call, or like one of the things the grammar treats as
+ * equivalent to a function call (see the func_expr_windowless production).
+ * This is needed in places where the grammar uses func_expr_windowless and
+ * you can't substitute a parenthesized a_expr.  If what we have isn't going
+ * to look like a function call, wrap it in a dummy CAST() expression, which
+ * will satisfy the grammar --- and, indeed, is likely what the user wrote to
+ * produce such a thing.
+ */
+static void
+get_rule_expr_funccall(Node *node, deparse_context *context,
+                                          bool showimplicit)
+{
+       if (looks_like_function(node))
+               get_rule_expr(node, context, showimplicit);
+       else
+       {
+               StringInfo      buf = context->buf;
+
+               appendStringInfoString(buf, "CAST(");
+               /* no point in showing any top-level implicit cast */
+               get_rule_expr(node, context, false);
+               appendStringInfo(buf, " AS %s)",
+                                                format_type_with_typemod(exprType(node),
+                                                                                                 exprTypmod(node)));
+       }
+}
+
+/*
+ * Helper function to identify node types that satisfy func_expr_windowless.
+ * If in doubt, "false" is always a safe answer.
+ */
+static bool
+looks_like_function(Node *node)
+{
+       if (node == NULL)
+               return false;                   /* probably shouldn't happen */
+       switch (nodeTag(node))
+       {
+               case T_FuncExpr:
+                       /* OK, unless it's going to deparse as a cast */
+                       return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL);
+               case T_NullIfExpr:
+               case T_CoalesceExpr:
+               case T_MinMaxExpr:
+               case T_XmlExpr:
+                       /* these are all accepted by func_expr_common_subexpr */
+                       return true;
+               default:
+                       break;
+       }
+       return false;
+}
+
 
 /*
  * get_oper_expr                       - Parse back an OpExpr node
@@ -9120,7 +9180,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
                                if (list_length(rte->functions) == 1 &&
                                        (rtfunc1->funccolnames == NIL || !rte->funcordinality))
                                {
-                                       get_rule_expr(rtfunc1->funcexpr, context, true);
+                                       get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
                                        /* we'll print the coldeflist below, if it has one */
                                }
                                else
@@ -9183,7 +9243,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
 
                                                        if (funcno > 0)
                                                                appendStringInfoString(buf, ", ");
-                                                       get_rule_expr(rtfunc->funcexpr, context, true);
+                                                       get_rule_expr_funccall(rtfunc->funcexpr, context, true);
                                                        if (rtfunc->funccolnames != NIL)
                                                        {
                                                                /* Reconstruct the column definition list */
index b1c3cff9315a7d1e98f533ea9f5885bbdffbd180..049fe0d778c04bc69305bb9d788b6bb6ecaef3dc 100644 (file)
@@ -1567,6 +1567,29 @@ select pg_get_viewdef('tt19v', true);
      'foo'::text = ANY ((( SELECT ARRAY['abc'::text, 'def'::text, 'foo'::text] AS "array"))::text[]) AS c2;
 (1 row)
 
+-- check display of assorted RTE_FUNCTION expressions
+create view tt20v as
+select * from
+  coalesce(1,2) as c,
+  collation for ('x'::text) col,
+  current_date as d,
+  cast(1+2 as int4) as i4,
+  cast(1+2 as int8) as i8;
+select pg_get_viewdef('tt20v', true);
+               pg_get_viewdef                
+---------------------------------------------
+  SELECT c.c,                               +
+     col.col,                               +
+     d.d,                                   +
+     i4.i4,                                 +
+     i8.i8                                  +
+    FROM COALESCE(1, 2) c(c),               +
+     pg_collation_for('x'::text) col(col),  +
+     CAST('now'::text::date AS date) d(d),  +
+     CAST(1 + 2 AS integer) i4(i4),         +
+     CAST((1 + 2)::bigint AS bigint) i8(i8);
+(1 row)
+
 -- clean up all the random objects we made above
 set client_min_messages = warning;
 DROP SCHEMA temp_view_test CASCADE;
index 5fe8b94aae0079d91c9d24cf8224a9a3effbd415..d7b461f8d4bbf6f80f885102093c0679095680ef 100644 (file)
@@ -520,6 +520,17 @@ select 'foo'::text = any(array['abc','def','foo']::text[]) c1,
        'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2;
 select pg_get_viewdef('tt19v', true);
 
+-- check display of assorted RTE_FUNCTION expressions
+
+create view tt20v as
+select * from
+  coalesce(1,2) as c,
+  collation for ('x'::text) col,
+  current_date as d,
+  cast(1+2 as int4) as i4,
+  cast(1+2 as int8) as i8;
+select pg_get_viewdef('tt20v', true);
+
 -- clean up all the random objects we made above
 set client_min_messages = warning;
 DROP SCHEMA temp_view_test CASCADE;