Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0ecc407

Browse files
committed
Fix dumping of FUNCTION RTEs that contain non-function-call expressions.
The grammar will only accept something syntactically similar to a functioncall in a function-in-FROM expression. However, there are various waysto input something that ruleutils.c won't deparse that way, potentiallyleading to a view or rule that fails dump/reload. Fix by inserting adummy 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 thefirst place).In HEAD, also make use of the infrastructure added by this to avoidemitting unnecessary parentheses in CREATE INDEX deparsing. I didnot change that in back branches, thinking that people might find itto be unexpected/unnecessary behavioral change.In HEAD, also fix incorrect logic for when to add extra parens topartition key expressions. Somebody apparently thought they couldget away with simpler logic than pg_get_indexdef_worker has, butthey were wrong --- a counterexample is PARTITION BY LIST ((a[1])).Ignoring the prettyprint flag for partition expressions isn't exactlya 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 parent5c5af32 commit0ecc407

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
boolshowimplicit);
390390
staticvoidget_rule_expr_toplevel(Node*node,deparse_context*context,
391391
boolshowimplicit);
392+
staticvoidget_rule_expr_funccall(Node*node,deparse_context*context,
393+
boolshowimplicit);
394+
staticboollooks_like_function(Node*node);
392395
staticvoidget_oper_expr(OpExpr*expr,deparse_context*context);
393396
staticvoidget_func_expr(FuncExpr*expr,deparse_context*context,
394397
boolshowimplicit);
@@ -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+
staticvoid
7590+
get_rule_expr_funccall(Node*node,deparse_context*context,
7591+
boolshowimplicit)
7592+
{
7593+
if (looks_like_function(node))
7594+
get_rule_expr(node,context,showimplicit);
7595+
else
7596+
{
7597+
StringInfobuf=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+
staticbool
7613+
looks_like_function(Node*node)
7614+
{
7615+
if (node==NULL)
7616+
return false;/* probably shouldn't happen */
7617+
switch (nodeTag(node))
7618+
{
7619+
caseT_FuncExpr:
7620+
/* OK, unless it's going to deparse as a cast */
7621+
return (((FuncExpr*)node)->funcformat==COERCE_EXPLICIT_CALL);
7622+
caseT_NullIfExpr:
7623+
caseT_CoalesceExpr:
7624+
caseT_MinMaxExpr:
7625+
caseT_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
caseRTE_FUNCTION:
83278387
/* Function RTE */
8328-
get_rule_expr(rte->funcexpr,context, true);
8388+
get_rule_expr_funccall(rte->funcexpr,context, true);
83298389
break;
83308390
caseRTE_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+
createviewtt20vas
526+
select*from
527+
coalesce(1,2)as c,
528+
collation for ('x'::text) col,
529+
current_dateas d,
530+
cast(1+2as int4)as i4,
531+
cast(1+2as 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
DROPSCHEMA temp_view_test CASCADE;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp