Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3b0c2db

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 cb02cbc commit 3b0c2db

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

src/backend/utils/adt/ruleutils.c

+62-2
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ static void get_rule_expr(Node *node, deparse_context *context,
409409
bool showimplicit);
410410
static void get_rule_expr_toplevel(Node *node, deparse_context *context,
411411
bool showimplicit);
412+
static void get_rule_expr_funccall(Node *node, deparse_context *context,
413+
bool showimplicit);
414+
static bool looks_like_function(Node *node);
412415
static void get_oper_expr(OpExpr *expr, deparse_context *context);
413416
static void get_func_expr(FuncExpr *expr, deparse_context *context,
414417
bool showimplicit);
@@ -8259,6 +8262,63 @@ get_rule_expr_toplevel(Node *node, deparse_context *context,
82598262
get_rule_expr(node, context, showimplicit);
82608263
}
82618264

8265+
/*
8266+
* get_rule_expr_funccall - Parse back a function-call expression
8267+
*
8268+
* Same as get_rule_expr(), except that we guarantee that the output will
8269+
* look like a function call, or like one of the things the grammar treats as
8270+
* equivalent to a function call (see the func_expr_windowless production).
8271+
* This is needed in places where the grammar uses func_expr_windowless and
8272+
* you can't substitute a parenthesized a_expr. If what we have isn't going
8273+
* to look like a function call, wrap it in a dummy CAST() expression, which
8274+
* will satisfy the grammar --- and, indeed, is likely what the user wrote to
8275+
* produce such a thing.
8276+
*/
8277+
static void
8278+
get_rule_expr_funccall(Node *node, deparse_context *context,
8279+
bool showimplicit)
8280+
{
8281+
if (looks_like_function(node))
8282+
get_rule_expr(node, context, showimplicit);
8283+
else
8284+
{
8285+
StringInfo buf = context->buf;
8286+
8287+
appendStringInfoString(buf, "CAST(");
8288+
/* no point in showing any top-level implicit cast */
8289+
get_rule_expr(node, context, false);
8290+
appendStringInfo(buf, " AS %s)",
8291+
format_type_with_typemod(exprType(node),
8292+
exprTypmod(node)));
8293+
}
8294+
}
8295+
8296+
/*
8297+
* Helper function to identify node types that satisfy func_expr_windowless.
8298+
* If in doubt, "false" is always a safe answer.
8299+
*/
8300+
static bool
8301+
looks_like_function(Node *node)
8302+
{
8303+
if (node == NULL)
8304+
return false; /* probably shouldn't happen */
8305+
switch (nodeTag(node))
8306+
{
8307+
case T_FuncExpr:
8308+
/* OK, unless it's going to deparse as a cast */
8309+
return (((FuncExpr *) node)->funcformat == COERCE_EXPLICIT_CALL);
8310+
case T_NullIfExpr:
8311+
case T_CoalesceExpr:
8312+
case T_MinMaxExpr:
8313+
case T_XmlExpr:
8314+
/* these are all accepted by func_expr_common_subexpr */
8315+
return true;
8316+
default:
8317+
break;
8318+
}
8319+
return false;
8320+
}
8321+
82628322

82638323
/*
82648324
* get_oper_expr - Parse back an OpExpr node
@@ -9120,7 +9180,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
91209180
if (list_length(rte->functions) == 1 &&
91219181
(rtfunc1->funccolnames == NIL || !rte->funcordinality))
91229182
{
9123-
get_rule_expr(rtfunc1->funcexpr, context, true);
9183+
get_rule_expr_funccall(rtfunc1->funcexpr, context, true);
91249184
/* we'll print the coldeflist below, if it has one */
91259185
}
91269186
else
@@ -9183,7 +9243,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
91839243

91849244
if (funcno > 0)
91859245
appendStringInfoString(buf, ", ");
9186-
get_rule_expr(rtfunc->funcexpr, context, true);
9246+
get_rule_expr_funccall(rtfunc->funcexpr, context, true);
91879247
if (rtfunc->funccolnames != NIL)
91889248
{
91899249
/* Reconstruct the column definition list */

src/test/regress/expected/create_view.out

+23
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

+11
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)