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

Commitd898edf

Browse files
committed
Further fix dumping of views that contain just VALUES(...).
It turns out that commite9f1c01 missed a case: we must print aVALUES clause in long format if get_query_def is given a resultDescthat would require the query's output column name(s) to be differentfrom what the bare VALUES clause would produce.This applies in case an ALTER ... RENAME COLUMN has been done toa view that formerly could be printed in simple format, as shownin the added regression test case. It also explains bug #16119from Dmitry Telpt, because it turns out that (unlike CREATE VIEW)CREATE MATERIALIZED VIEW fails to apply any column aliases it'sgiven to the stored ON SELECT rule. So to get them to be printed,we have to account for the resultDesc renaming. It might be worthchanging the matview code so that it creates the ON SELECT rulewith the correct aliases; but we'd still need these messy checks inget_simple_values_rte to handle the case of a subsequent columnrename, so any such change would be just neatnik-ism not a bug fix.Like the previous patch, back-patch to all supported branches.Discussion:https://postgr.es/m/16119-e64823f30a45a754@postgresql.org
1 parent25a9ff6 commitd898edf

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5301,19 +5301,20 @@ get_select_query_def(Query *query, deparse_context *context,
53015301
}
53025302

53035303
/*
5304-
* Detect whether query looks like SELECT ... FROM VALUES();
5305-
* if so, return the VALUES RTE. Otherwise return NULL.
5304+
* Detect whether query looks like SELECT ... FROM VALUES(),
5305+
* with no need to rename the output columns of the VALUES RTE.
5306+
* If so, return the VALUES RTE. Otherwise return NULL.
53065307
*/
53075308
staticRangeTblEntry*
5308-
get_simple_values_rte(Query*query)
5309+
get_simple_values_rte(Query*query,TupleDescresultDesc)
53095310
{
53105311
RangeTblEntry*result=NULL;
53115312
ListCell*lc;
53125313

53135314
/*
5314-
* We want toreturn trueeven if the Query also contains OLD or NEW rule
5315-
* RTEs. So the idea is to scan the rtable and see if there is only one
5316-
* inFromCl RTE that is a VALUES RTE.
5315+
* We want todetect a matcheven if the Query also contains OLD or NEW
5316+
*ruleRTEs. So the idea is to scan the rtable and see if there is only
5317+
*oneinFromCl RTE that is a VALUES RTE.
53175318
*/
53185319
foreach(lc,query->rtable)
53195320
{
@@ -5336,23 +5337,36 @@ get_simple_values_rte(Query *query)
53365337
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
53375338
* appear inside auto-generated sub-queries with very restricted
53385339
* structure. However, DefineView might have modified the tlist by
5339-
* injecting new column aliases; so compare tlist resnames against the
5340-
* RTE's names to detect that.
5340+
* injecting new column aliases, or we might have some other column
5341+
* aliases forced by a resultDesc. We can only simplify if the RTE's
5342+
* column names match the names that get_target_list() would select.
53415343
*/
53425344
if (result)
53435345
{
53445346
ListCell*lcn;
5347+
intcolno;
53455348

53465349
if (list_length(query->targetList)!=list_length(result->eref->colnames))
53475350
returnNULL;/* this probably cannot happen */
5351+
colno=0;
53485352
forboth(lc,query->targetList,lcn,result->eref->colnames)
53495353
{
53505354
TargetEntry*tle= (TargetEntry*)lfirst(lc);
53515355
char*cname=strVal(lfirst(lcn));
5356+
char*colname;
53525357

53535358
if (tle->resjunk)
53545359
returnNULL;/* this probably cannot happen */
5355-
if (tle->resname==NULL||strcmp(tle->resname,cname)!=0)
5360+
5361+
/* compute name that get_target_list would use for column */
5362+
colno++;
5363+
if (resultDesc&&colno <=resultDesc->natts)
5364+
colname=NameStr(TupleDescAttr(resultDesc,colno-1)->attname);
5365+
else
5366+
colname=tle->resname;
5367+
5368+
/* does it match the VALUES RTE? */
5369+
if (colname==NULL||strcmp(colname,cname)!=0)
53565370
returnNULL;/* column name has been changed */
53575371
}
53585372
}
@@ -5380,7 +5394,7 @@ get_basic_select_query(Query *query, deparse_context *context,
53805394
* VALUES part. This reverses what transformValuesClause() did at parse
53815395
* time.
53825396
*/
5383-
values_rte=get_simple_values_rte(query);
5397+
values_rte=get_simple_values_rte(query,resultDesc);
53845398
if (values_rte)
53855399
{
53865400
get_values_def(values_rte->values_lists,context);

‎src/test/regress/expected/rules.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2907,6 +2907,18 @@ create view rule_v1 as values(1,2);
29072907
View definition:
29082908
VALUES (1,2);
29092909

2910+
alter table rule_v1 rename column column2 to q2;
2911+
\d+ rule_v1
2912+
View "public.rule_v1"
2913+
Column | Type | Collation | Nullable | Default | Storage | Description
2914+
---------+---------+-----------+----------+---------+---------+-------------
2915+
column1 | integer | | | | plain |
2916+
q2 | integer | | | | plain |
2917+
View definition:
2918+
SELECT "*VALUES*".column1,
2919+
"*VALUES*".column2 AS q2
2920+
FROM (VALUES (1,2)) "*VALUES*";
2921+
29102922
drop view rule_v1;
29112923
create view rule_v1(x) as values(1,2);
29122924
\d+ rule_v1

‎src/test/regress/sql/rules.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,8 @@ DROP TABLE rule_t1;
10461046
--
10471047
createviewrule_v1asvalues(1,2);
10481048
\d+ rule_v1
1049+
altertable rule_v1 rename column column2 to q2;
1050+
\d+ rule_v1
10491051
dropview rule_v1;
10501052
createviewrule_v1(x)asvalues(1,2);
10511053
\d+ rule_v1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp