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

Commita1b2cf0

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 parent0b0f281 commita1b2cf0

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
@@ -5191,19 +5191,20 @@ get_select_query_def(Query *query, deparse_context *context,
51915191
}
51925192

51935193
/*
5194-
* Detect whether query looks like SELECT ... FROM VALUES();
5195-
* if so, return the VALUES RTE. Otherwise return NULL.
5194+
* Detect whether query looks like SELECT ... FROM VALUES(),
5195+
* with no need to rename the output columns of the VALUES RTE.
5196+
* If so, return the VALUES RTE. Otherwise return NULL.
51965197
*/
51975198
staticRangeTblEntry*
5198-
get_simple_values_rte(Query*query)
5199+
get_simple_values_rte(Query*query,TupleDescresultDesc)
51995200
{
52005201
RangeTblEntry*result=NULL;
52015202
ListCell*lc;
52025203

52035204
/*
5204-
* We want toreturn TRUEeven if the Query also contains OLD or NEW rule
5205-
* RTEs. So the idea is to scan the rtable and see if there is only one
5206-
* inFromCl RTE that is a VALUES RTE.
5205+
* We want todetect a matcheven if the Query also contains OLD or NEW
5206+
*ruleRTEs. So the idea is to scan the rtable and see if there is only
5207+
*oneinFromCl RTE that is a VALUES RTE.
52075208
*/
52085209
foreach(lc,query->rtable)
52095210
{
@@ -5226,23 +5227,36 @@ get_simple_values_rte(Query *query)
52265227
* parser/analyze.c will never generate a "bare" VALUES RTE --- they only
52275228
* appear inside auto-generated sub-queries with very restricted
52285229
* structure. However, DefineView might have modified the tlist by
5229-
* injecting new column aliases; so compare tlist resnames against the
5230-
* RTE's names to detect that.
5230+
* injecting new column aliases, or we might have some other column
5231+
* aliases forced by a resultDesc. We can only simplify if the RTE's
5232+
* column names match the names that get_target_list() would select.
52315233
*/
52325234
if (result)
52335235
{
52345236
ListCell*lcn;
5237+
intcolno;
52355238

52365239
if (list_length(query->targetList)!=list_length(result->eref->colnames))
52375240
returnNULL;/* this probably cannot happen */
5241+
colno=0;
52385242
forboth(lc,query->targetList,lcn,result->eref->colnames)
52395243
{
52405244
TargetEntry*tle= (TargetEntry*)lfirst(lc);
52415245
char*cname=strVal(lfirst(lcn));
5246+
char*colname;
52425247

52435248
if (tle->resjunk)
52445249
returnNULL;/* this probably cannot happen */
5245-
if (tle->resname==NULL||strcmp(tle->resname,cname)!=0)
5250+
5251+
/* compute name that get_target_list would use for column */
5252+
colno++;
5253+
if (resultDesc&&colno <=resultDesc->natts)
5254+
colname=NameStr(TupleDescAttr(resultDesc,colno-1)->attname);
5255+
else
5256+
colname=tle->resname;
5257+
5258+
/* does it match the VALUES RTE? */
5259+
if (colname==NULL||strcmp(colname,cname)!=0)
52465260
returnNULL;/* column name has been changed */
52475261
}
52485262
}
@@ -5270,7 +5284,7 @@ get_basic_select_query(Query *query, deparse_context *context,
52705284
* VALUES part. This reverses what transformValuesClause() did at parse
52715285
* time.
52725286
*/
5273-
values_rte=get_simple_values_rte(query);
5287+
values_rte=get_simple_values_rte(query,resultDesc);
52745288
if (values_rte)
52755289
{
52765290
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
@@ -2903,6 +2903,18 @@ create view rule_v1 as values(1,2);
29032903
View definition:
29042904
VALUES (1,2);
29052905

2906+
alter table rule_v1 rename column column2 to q2;
2907+
\d+ rule_v1
2908+
View "public.rule_v1"
2909+
Column | Type | Collation | Nullable | Default | Storage | Description
2910+
---------+---------+-----------+----------+---------+---------+-------------
2911+
column1 | integer | | | | plain |
2912+
q2 | integer | | | | plain |
2913+
View definition:
2914+
SELECT "*VALUES*".column1,
2915+
"*VALUES*".column2 AS q2
2916+
FROM (VALUES (1,2)) "*VALUES*";
2917+
29062918
drop view rule_v1;
29072919
create view rule_v1(x) as values(1,2);
29082920
\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