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

Commit3b0c2db

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 parentcb02cbc commit3b0c2db

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

‎src/backend/utils/adt/ruleutils.c

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,9 @@ static void get_rule_expr(Node *node, deparse_context *context,
409409
boolshowimplicit);
410410
staticvoidget_rule_expr_toplevel(Node*node,deparse_context*context,
411411
boolshowimplicit);
412+
staticvoidget_rule_expr_funccall(Node*node,deparse_context*context,
413+
boolshowimplicit);
414+
staticboollooks_like_function(Node*node);
412415
staticvoidget_oper_expr(OpExpr*expr,deparse_context*context);
413416
staticvoidget_func_expr(FuncExpr*expr,deparse_context*context,
414417
boolshowimplicit);
@@ -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+
staticvoid
8278+
get_rule_expr_funccall(Node*node,deparse_context*context,
8279+
boolshowimplicit)
8280+
{
8281+
if (looks_like_function(node))
8282+
get_rule_expr(node,context,showimplicit);
8283+
else
8284+
{
8285+
StringInfobuf=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+
staticbool
8301+
looks_like_function(Node*node)
8302+
{
8303+
if (node==NULL)
8304+
return false;/* probably shouldn't happen */
8305+
switch (nodeTag(node))
8306+
{
8307+
caseT_FuncExpr:
8308+
/* OK, unless it's going to deparse as a cast */
8309+
return (((FuncExpr*)node)->funcformat==COERCE_EXPLICIT_CALL);
8310+
caseT_NullIfExpr:
8311+
caseT_CoalesceExpr:
8312+
caseT_MinMaxExpr:
8313+
caseT_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

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