Skip to content

Commit 0ecc407

Browse files
committed
Fix dumping of FUNCTION RTEs that contain non-function-call expressions.
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
1 parent 5c5af32 commit 0ecc407

File tree

3 files changed

+95
-1
lines changed

3 files changed

+95
-1
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ static void get_rule_expr(Node *node, deparse_context *context,
389389
bool showimplicit);
390390
static void get_rule_expr_toplevel(Node *node, deparse_context *context,
391391
bool showimplicit);
392+
static void get_rule_expr_funccall(Node *node, deparse_context *context,
393+
bool showimplicit);
394+
static bool looks_like_function(Node *node);
392395
static void get_oper_expr(OpExpr *expr, deparse_context *context);
393396
static void get_func_expr(FuncExpr *expr, deparse_context *context,
394397
bool showimplicit);
@@ -7571,6 +7574,63 @@ get_rule_expr_toplevel(Node *node, deparse_context *context,
75717574
get_rule_expr(node, context, showimplicit);
75727575
}
75737576

7577+
/*
7578+
* get_rule_expr_funccall - Parse back a function-call expression
7579+
*
7580+
* Same as get_rule_expr(), except that we guarantee that the output will
7581+
* look like a function call, or like one of the things the grammar treats as
7582+
* equivalent to a function call (see the func_expr_windowless production).
7583+
* This is needed in places where the grammar uses func_expr_windowless and
7584+
* you can't substitute a parenthesized a_expr. If what we have isn't going
7585+
* to look like a function call, wrap it in a dummy CAST() expression, which
7586+
* will satisfy the grammar --- and, indeed, is likely what the user wrote to
7587+
* produce such a thing.
7588+
*/
7589+
static void
7590+
get_rule_expr_funccall(Node *node, deparse_context *context,
7591+
bool showimplicit)
7592+
{
7593+
if (looks_like_function(node))
7594+
get_rule_expr(node, context, showimplicit);
7595+
else
7596+
{
7597+
StringInfo buf = context->buf;
7598+
7599+
appendStringInfoString(buf, "CAST(");
7600+
/* no point in showing any top-level implicit cast */
7601+
get_rule_expr(node, context, false);
7602+
appendStringInfo(buf, " AS %s)",
7603+
format_type_with_typemod(exprType(node),
7604+
exprTypmod(node)));
7605+
}
7606+
}
7607+
7608+
/*
7609+
* Helper function to identify node types that satisfy func_expr_windowless.
7610+
* If in doubt, "false" is always a safe answer.
7611+
*/
7612+
static bool
7613+
looks_like_function(Node *node)
7614+
{
7615+
if (node == NULL)
7616+
return false; /* probably shouldn't happen */
7617+
switch (nodeTag(node))
7618+
{
7619+
case T_FuncExpr:
7620+
/* OK, unless it's going to deparse as a cast */
7621+
return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL);
7622+
case T_NullIfExpr:
7623+
case T_CoalesceExpr:
7624+
case T_MinMaxExpr:
7625+
case T_XmlExpr:
7626+
/* these are all accepted by func_expr_common_subexpr */
7627+
return true;
7628+
default:
7629+
break;
7630+
}
7631+
return false;
7632+
}
7633+
75747634

75757635
/*
75767636
* get_oper_expr - Parse back an OpExpr node
@@ -8325,7 +8385,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
83258385
break;
83268386
case RTE_FUNCTION:
83278387
/* Function RTE */
8328-
get_rule_expr(rte->funcexpr, context, true);
8388+
get_rule_expr_funccall(rte->funcexpr, context, true);
83298389
break;
83308390
case RTE_VALUES:
83318391
/* Values list RTE */

src/test/regress/expected/create_view.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,29 @@ select pg_get_viewdef('tt19v', true);
15671567
'foo'::text = ANY ((( SELECT ARRAY['abc'::text, 'def'::text, 'foo'::text] AS "array"))::text[]) AS c2;
15681568
(1 row)
15691569

1570+
-- check display of assorted RTE_FUNCTION expressions
1571+
create view tt20v as
1572+
select * from
1573+
coalesce(1,2) as c,
1574+
collation for ('x'::text) col,
1575+
current_date as d,
1576+
cast(1+2 as int4) as i4,
1577+
cast(1+2 as int8) as i8;
1578+
select pg_get_viewdef('tt20v', true);
1579+
pg_get_viewdef
1580+
---------------------------------------------
1581+
SELECT c.c, +
1582+
col.col, +
1583+
d.d, +
1584+
i4.i4, +
1585+
i8.i8 +
1586+
FROM COALESCE(1, 2) c(c), +
1587+
pg_collation_for('x'::text) col(col), +
1588+
CAST('now'::text::date AS date) d(d), +
1589+
CAST(1 + 2 AS integer) i4(i4), +
1590+
CAST((1 + 2)::bigint AS bigint) i8(i8);
1591+
(1 row)
1592+
15701593
-- clean up all the random objects we made above
15711594
set client_min_messages = warning;
15721595
DROP SCHEMA temp_view_test CASCADE;

src/test/regress/sql/create_view.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,17 @@ select 'foo'::text = any(array['abc','def','foo']::text[]) c1,
520520
'foo'::text = any((select array['abc','def','foo']::text[])::text[]) c2;
521521
select pg_get_viewdef('tt19v', true);
522522

523+
-- check display of assorted RTE_FUNCTION expressions
524+
525+
create view tt20v as
526+
select * from
527+
coalesce(1,2) as c,
528+
collation for ('x'::text) col,
529+
current_date as d,
530+
cast(1+2 as int4) as i4,
531+
cast(1+2 as int8) as i8;
532+
select pg_get_viewdef('tt20v', true);
533+
523534
-- clean up all the random objects we made above
524535
set client_min_messages = warning;
525536
DROP SCHEMA temp_view_test CASCADE;

0 commit comments

Comments
 (0)