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

Commitfcaf29d

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 parentbbaa38e commitfcaf29d

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
@@ -5374,19 +5374,20 @@ get_select_query_def(Query *query, deparse_context *context,
53745374
}
53755375

53765376
/*
5377-
* Detect whether query looks like SELECT ... FROM VALUES();
5378-
* if so, return the VALUES RTE. Otherwise return NULL.
5377+
* Detect whether query looks like SELECT ... FROM VALUES(),
5378+
* with no need to rename the output columns of the VALUES RTE.
5379+
* If so, return the VALUES RTE. Otherwise return NULL.
53795380
*/
53805381
staticRangeTblEntry*
5381-
get_simple_values_rte(Query*query)
5382+
get_simple_values_rte(Query*query,TupleDescresultDesc)
53825383
{
53835384
RangeTblEntry*result=NULL;
53845385
ListCell*lc;
53855386

53865387
/*
5387-
* We want toreturn trueeven if the Query also contains OLD or NEW rule
5388-
* RTEs. So the idea is to scan the rtable and see if there is only one
5389-
* inFromCl RTE that is a VALUES RTE.
5388+
* We want todetect a matcheven if the Query also contains OLD or NEW
5389+
*ruleRTEs. So the idea is to scan the rtable and see if there is only
5390+
*oneinFromCl RTE that is a VALUES RTE.
53905391
*/
53915392
foreach(lc,query->rtable)
53925393
{
@@ -5409,23 +5410,36 @@ get_simple_values_rte(Query *query)
54095410
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
54105411
* appear inside auto-generated sub-queries with very restricted
54115412
* structure. However, DefineView might have modified the tlist by
5412-
* injecting new column aliases; so compare tlist resnames against the
5413-
* RTE's names to detect that.
5413+
* injecting new column aliases, or we might have some other column
5414+
* aliases forced by a resultDesc. We can only simplify if the RTE's
5415+
* column names match the names that get_target_list() would select.
54145416
*/
54155417
if (result)
54165418
{
54175419
ListCell*lcn;
5420+
intcolno;
54185421

54195422
if (list_length(query->targetList)!=list_length(result->eref->colnames))
54205423
returnNULL;/* this probably cannot happen */
5424+
colno=0;
54215425
forboth(lc,query->targetList,lcn,result->eref->colnames)
54225426
{
54235427
TargetEntry*tle= (TargetEntry*)lfirst(lc);
54245428
char*cname=strVal(lfirst(lcn));
5429+
char*colname;
54255430

54265431
if (tle->resjunk)
54275432
returnNULL;/* this probably cannot happen */
5428-
if (tle->resname==NULL||strcmp(tle->resname,cname)!=0)
5433+
5434+
/* compute name that get_target_list would use for column */
5435+
colno++;
5436+
if (resultDesc&&colno <=resultDesc->natts)
5437+
colname=NameStr(TupleDescAttr(resultDesc,colno-1)->attname);
5438+
else
5439+
colname=tle->resname;
5440+
5441+
/* does it match the VALUES RTE? */
5442+
if (colname==NULL||strcmp(colname,cname)!=0)
54295443
returnNULL;/* column name has been changed */
54305444
}
54315445
}
@@ -5453,7 +5467,7 @@ get_basic_select_query(Query *query, deparse_context *context,
54535467
* VALUES part. This reverses what transformValuesClause() did at parse
54545468
* time.
54555469
*/
5456-
values_rte=get_simple_values_rte(query);
5470+
values_rte=get_simple_values_rte(query,resultDesc);
54575471
if (values_rte)
54585472
{
54595473
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
@@ -3023,6 +3023,18 @@ create view rule_v1 as values(1,2);
30233023
View definition:
30243024
VALUES (1,2);
30253025

3026+
alter table rule_v1 rename column column2 to q2;
3027+
\d+ rule_v1
3028+
View "public.rule_v1"
3029+
Column | Type | Collation | Nullable | Default | Storage | Description
3030+
---------+---------+-----------+----------+---------+---------+-------------
3031+
column1 | integer | | | | plain |
3032+
q2 | integer | | | | plain |
3033+
View definition:
3034+
SELECT "*VALUES*".column1,
3035+
"*VALUES*".column2 AS q2
3036+
FROM (VALUES (1,2)) "*VALUES*";
3037+
30263038
drop view rule_v1;
30273039
create view rule_v1(x) as values(1,2);
30283040
\d+ rule_v1

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

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp