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

Commit f864fe0

Browse files
committed
Fix dumping of views that are just VALUES(...) but have column aliases.
The "simple" path for printing VALUES clauses doesn't work if we need to attach nondefault column aliases, because there's noplace to do that in the minimal VALUES() syntax. So modify get_simple_values_rte() to detect nondefault aliases and treat that as a non-simple case. This further exposes that the "non-simple" path never actually worked; it didn't produce valid syntax. Fix that too. Per bug #12789 from Curtis McEnroe, and analysis by Andrew Gierth. Back-patch to all supported branches. Before 9.3, this also requires back-patching the part of commit 092d7de that created get_simple_values_rte() to begin with; inserting the extra test into the old factorization of that logic would've been too messy.
1 parent a6ddff8 commit f864fe0

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4360,10 +4360,7 @@ get_simple_values_rte(Query *query)
43604360
/*
43614361
* We want to return TRUE even if the Query also contains OLD or NEW rule
43624362
* RTEs. So the idea is to scan the rtable and see if there is only one
4363-
* inFromCl RTE that is a VALUES RTE. We don't look at the targetlist at
4364-
* all. This is okay because parser/analyze.c will never generate a
4365-
* "bare" VALUES RTE --- they only appear inside auto-generated
4366-
* sub-queries with very restricted structure.
4363+
* inFromCl RTE that is a VALUES RTE.
43674364
*/
43684365
foreach(lc, query->rtable)
43694366
{
@@ -4380,6 +4377,33 @@ get_simple_values_rte(Query *query)
43804377
else
43814378
return NULL; /* something else -> not simple VALUES */
43824379
}
4380+
4381+
/*
4382+
* We don't need to check the targetlist in any great detail, because
4383+
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
4384+
* appear inside auto-generated sub-queries with very restricted
4385+
* structure. However, DefineView might have modified the tlist by
4386+
* injecting new column aliases; so compare tlist resnames against the
4387+
* RTE's names to detect that.
4388+
*/
4389+
if (result)
4390+
{
4391+
ListCell *lcn;
4392+
4393+
if (list_length(query->targetList) != list_length(result->eref->colnames))
4394+
return NULL; /* this probably cannot happen */
4395+
forboth(lc, query->targetList, lcn, result->eref->colnames)
4396+
{
4397+
TargetEntry *tle = (TargetEntry *) lfirst(lc);
4398+
char *cname = strVal(lfirst(lcn));
4399+
4400+
if (tle->resjunk)
4401+
return NULL; /* this probably cannot happen */
4402+
if (tle->resname == NULL || strcmp(tle->resname, cname) != 0)
4403+
return NULL; /* column name has been changed */
4404+
}
4405+
}
4406+
43834407
return result;
43844408
}
43854409

@@ -8157,7 +8181,9 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
81578181
break;
81588182
case RTE_VALUES:
81598183
/* Values list RTE */
8184+
appendStringInfoChar(buf, '(');
81608185
get_values_def(rte->values_lists, context);
8186+
appendStringInfoChar(buf, ')');
81618187
break;
81628188
case RTE_CTE:
81638189
appendStringInfoString(buf, quote_identifier(rte->ctename));
@@ -8199,6 +8225,11 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
81998225
*/
82008226
printalias = true;
82018227
}
8228+
else if (rte->rtekind == RTE_VALUES)
8229+
{
8230+
/* Alias is syntactically required for VALUES */
8231+
printalias = true;
8232+
}
82028233
else if (rte->rtekind == RTE_CTE)
82038234
{
82048235
/*

src/test/regress/expected/rules.out

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,3 +2631,56 @@ ALTER RULE "_RETURN" ON rule_v1 RENAME TO abc; -- ON SELECT rule cannot be renam
26312631
ERROR: renaming an ON SELECT rule is not allowed
26322632
DROP VIEW rule_v1;
26332633
DROP TABLE rule_t1;
2634+
--
2635+
-- check display of VALUES in view definitions
2636+
--
2637+
create view rule_v1 as values(1,2);
2638+
\d+ rule_v1
2639+
View "public.rule_v1"
2640+
Column | Type | Modifiers | Storage | Description
2641+
---------+---------+-----------+---------+-------------
2642+
column1 | integer | | plain |
2643+
column2 | integer | | plain |
2644+
View definition:
2645+
VALUES (1,2);
2646+
2647+
drop view rule_v1;
2648+
create view rule_v1(x) as values(1,2);
2649+
\d+ rule_v1
2650+
View "public.rule_v1"
2651+
Column | Type | Modifiers | Storage | Description
2652+
---------+---------+-----------+---------+-------------
2653+
x | integer | | plain |
2654+
column2 | integer | | plain |
2655+
View definition:
2656+
SELECT "*VALUES*".column1 AS x,
2657+
"*VALUES*".column2
2658+
FROM (VALUES (1,2)) "*VALUES*";
2659+
2660+
drop view rule_v1;
2661+
create view rule_v1(x) as select * from (values(1,2)) v;
2662+
\d+ rule_v1
2663+
View "public.rule_v1"
2664+
Column | Type | Modifiers | Storage | Description
2665+
---------+---------+-----------+---------+-------------
2666+
x | integer | | plain |
2667+
column2 | integer | | plain |
2668+
View definition:
2669+
SELECT v.column1 AS x,
2670+
v.column2
2671+
FROM ( VALUES (1,2)) v;
2672+
2673+
drop view rule_v1;
2674+
create view rule_v1(x) as select * from (values(1,2)) v(q,w);
2675+
\d+ rule_v1
2676+
View "public.rule_v1"
2677+
Column | Type | Modifiers | Storage | Description
2678+
--------+---------+-----------+---------+-------------
2679+
x | integer | | plain |
2680+
w | integer | | plain |
2681+
View definition:
2682+
SELECT v.q AS x,
2683+
v.w
2684+
FROM ( VALUES (1,2)) v(q, w);
2685+
2686+
drop view rule_v1;

src/test/regress/sql/rules.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,19 @@ ALTER RULE "_RETURN" ON rule_v1 RENAME TO abc; -- ON SELECT rule cannot be renam
10001000

10011001
DROP VIEW rule_v1;
10021002
DROP TABLE rule_t1;
1003+
1004+
--
1005+
-- check display of VALUES in view definitions
1006+
--
1007+
create view rule_v1 as values(1,2);
1008+
\d+ rule_v1
1009+
drop view rule_v1;
1010+
create view rule_v1(x) as values(1,2);
1011+
\d+ rule_v1
1012+
drop view rule_v1;
1013+
create view rule_v1(x) as select * from (values(1,2)) v;
1014+
\d+ rule_v1
1015+
drop view rule_v1;
1016+
create view rule_v1(x) as select * from (values(1,2)) v(q,w);
1017+
\d+ rule_v1
1018+
drop view rule_v1;

0 commit comments

Comments
 (0)