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

Commitbf2efc5

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 parent815bd57 commitbf2efc5

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
@@ -5370,19 +5370,20 @@ get_select_query_def(Query *query, deparse_context *context,
53705370
}
53715371

53725372
/*
5373-
* Detect whether query looks like SELECT ... FROM VALUES();
5374-
* if so, return the VALUES RTE. Otherwise return NULL.
5373+
* Detect whether query looks like SELECT ... FROM VALUES(),
5374+
* with no need to rename the output columns of the VALUES RTE.
5375+
* If so, return the VALUES RTE. Otherwise return NULL.
53755376
*/
53765377
staticRangeTblEntry*
5377-
get_simple_values_rte(Query*query)
5378+
get_simple_values_rte(Query*query,TupleDescresultDesc)
53785379
{
53795380
RangeTblEntry*result=NULL;
53805381
ListCell*lc;
53815382

53825383
/*
5383-
* We want toreturn trueeven if the Query also contains OLD or NEW rule
5384-
* RTEs. So the idea is to scan the rtable and see if there is only one
5385-
* inFromCl RTE that is a VALUES RTE.
5384+
* We want todetect a matcheven if the Query also contains OLD or NEW
5385+
*ruleRTEs. So the idea is to scan the rtable and see if there is only
5386+
*oneinFromCl RTE that is a VALUES RTE.
53865387
*/
53875388
foreach(lc,query->rtable)
53885389
{
@@ -5405,23 +5406,36 @@ get_simple_values_rte(Query *query)
54055406
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
54065407
* appear inside auto-generated sub-queries with very restricted
54075408
* structure. However, DefineView might have modified the tlist by
5408-
* injecting new column aliases; so compare tlist resnames against the
5409-
* RTE's names to detect that.
5409+
* injecting new column aliases, or we might have some other column
5410+
* aliases forced by a resultDesc. We can only simplify if the RTE's
5411+
* column names match the names that get_target_list() would select.
54105412
*/
54115413
if (result)
54125414
{
54135415
ListCell*lcn;
5416+
intcolno;
54145417

54155418
if (list_length(query->targetList)!=list_length(result->eref->colnames))
54165419
returnNULL;/* this probably cannot happen */
5420+
colno=0;
54175421
forboth(lc,query->targetList,lcn,result->eref->colnames)
54185422
{
54195423
TargetEntry*tle= (TargetEntry*)lfirst(lc);
54205424
char*cname=strVal(lfirst(lcn));
5425+
char*colname;
54215426

54225427
if (tle->resjunk)
54235428
returnNULL;/* this probably cannot happen */
5424-
if (tle->resname==NULL||strcmp(tle->resname,cname)!=0)
5429+
5430+
/* compute name that get_target_list would use for column */
5431+
colno++;
5432+
if (resultDesc&&colno <=resultDesc->natts)
5433+
colname=NameStr(TupleDescAttr(resultDesc,colno-1)->attname);
5434+
else
5435+
colname=tle->resname;
5436+
5437+
/* does it match the VALUES RTE? */
5438+
if (colname==NULL||strcmp(colname,cname)!=0)
54255439
returnNULL;/* column name has been changed */
54265440
}
54275441
}
@@ -5449,7 +5463,7 @@ get_basic_select_query(Query *query, deparse_context *context,
54495463
* VALUES part. This reverses what transformValuesClause() did at parse
54505464
* time.
54515465
*/
5452-
values_rte=get_simple_values_rte(query);
5466+
values_rte=get_simple_values_rte(query,resultDesc);
54535467
if (values_rte)
54545468
{
54555469
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
@@ -3025,6 +3025,18 @@ create view rule_v1 as values(1,2);
30253025
View definition:
30263026
VALUES (1,2);
30273027

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