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

Commit2679a10

Browse files
committed
Track nesting depth correctly when drilling down into RECORD Vars.
expandRecordVariable() failed to adjust the parse nesting structurecorrectly when recursing to inspect an outer-level Var. This couldresult in assertion failures or core dumps in corner cases.Likewise, get_name_for_var_field() failed to adjust the deparsenamespace stack correctly when recursing to inspect an outer-levelVar. In this case the likely result was a "bogus varno" errorwhile deparsing a view.Per bug #18077 from Jingzhou Fu. Back-patch to all supportedbranches.Richard Guo, with some adjustments by meDiscussion:https://postgr.es/m/18077-b9db97c6e0ab45d8@postgresql.org
1 parentfe60434 commit2679a10

File tree

4 files changed

+120
-22
lines changed

4 files changed

+120
-22
lines changed

‎src/backend/parser/parse_target.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,8 @@ ExpandRowReference(ParseState *pstate, Node *expr,
15041504
* drill down to find the ultimate defining expression and attempt to infer
15051505
* the tupdesc from it. We ereport if we can't determine the tupdesc.
15061506
*
1507-
* levelsup is an extra offset to interpret the Var's varlevelsup correctly.
1507+
* levelsup is an extra offset to interpret the Var's varlevelsup correctly
1508+
* when recursing. Outside callers should pass zero.
15081509
*/
15091510
TupleDesc
15101511
expandRecordVariable(ParseState*pstate,Var*var,intlevelsup)
@@ -1592,11 +1593,17 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
15921593
/*
15931594
* Recurse into the sub-select to see what its Var refers
15941595
* to. We have to build an additional level of ParseState
1595-
* to keep in step with varlevelsup in the subselect.
1596+
* to keep in step with varlevelsup in the subselect;
1597+
* furthermore, the subquery RTE might be from an outer
1598+
* query level, in which case the ParseState for the
1599+
* subselect must have that outer level as parent.
15961600
*/
1597-
ParseStatemypstate;
1601+
ParseStatemypstate= {0};
1602+
Indexlevelsup;
15981603

1599-
MemSet(&mypstate,0,sizeof(mypstate));
1604+
/* this loop must work, since GetRTEByRangeTablePosn did */
1605+
for (levelsup=0;levelsup<netlevelsup;levelsup++)
1606+
pstate=pstate->parentParseState;
16001607
mypstate.parentParseState=pstate;
16011608
mypstate.p_rtable=rte->subquery->rtable;
16021609
/* don't bother filling the rest of the fake pstate */
@@ -1647,12 +1654,11 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
16471654
* Recurse into the CTE to see what its Var refers to. We
16481655
* have to build an additional level of ParseState to keep
16491656
* in step with varlevelsup in the CTE; furthermore it
1650-
* could be an outer CTE.
1657+
* could be an outer CTE (compare SUBQUERY case above).
16511658
*/
1652-
ParseStatemypstate;
1659+
ParseStatemypstate= {0};
16531660
Indexlevelsup;
16541661

1655-
MemSet(&mypstate,0,sizeof(mypstate));
16561662
/* this loop must work, since GetCTEForRTE did */
16571663
for (levelsup=0;
16581664
levelsup<rte->ctelevelsup+netlevelsup;

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7845,22 +7845,28 @@ get_name_for_var_field(Var *var, int fieldno,
78457845
* Recurse into the sub-select to see what its Var
78467846
* refers to. We have to build an additional level of
78477847
* namespace to keep in step with varlevelsup in the
7848-
* subselect.
7848+
* subselect; furthermore, the subquery RTE might be
7849+
* from an outer query level, in which case the
7850+
* namespace for the subselect must have that outer
7851+
* level as parent namespace.
78497852
*/
7853+
List*save_nslist=context->namespaces;
7854+
List*parent_namespaces;
78507855
deparse_namespacemydpns;
78517856
constchar*result;
78527857

7858+
parent_namespaces=list_copy_tail(context->namespaces,
7859+
netlevelsup);
7860+
78537861
set_deparse_for_query(&mydpns,rte->subquery,
7854-
context->namespaces);
7862+
parent_namespaces);
78557863

7856-
context->namespaces=lcons(&mydpns,
7857-
context->namespaces);
7864+
context->namespaces=lcons(&mydpns,parent_namespaces);
78587865

78597866
result=get_name_for_var_field((Var*)expr,fieldno,
78607867
0,context);
78617868

7862-
context->namespaces=
7863-
list_delete_first(context->namespaces);
7869+
context->namespaces=save_nslist;
78647870

78657871
returnresult;
78667872
}
@@ -7952,29 +7958,30 @@ get_name_for_var_field(Var *var, int fieldno,
79527958
attnum);
79537959

79547960
if (ste==NULL||ste->resjunk)
7955-
elog(ERROR,"subquery %s does not have attribute %d",
7961+
elog(ERROR,"CTE %s does not have attribute %d",
79567962
rte->eref->aliasname,attnum);
79577963
expr= (Node*)ste->expr;
79587964
if (IsA(expr,Var))
79597965
{
79607966
/*
79617967
* Recurse into the CTE to see what its Var refers to.
79627968
* We have to build an additional level of namespace
7963-
* to keep in step with varlevelsup in the CTE.
7964-
*Furthermore it could be an outer CTE, so we may
7965-
*have to delete some levels of namespace.
7969+
* to keep in step with varlevelsup in the CTE;
7970+
*furthermore it could be an outer CTE (compare
7971+
*SUBQUERY case above).
79667972
*/
79677973
List*save_nslist=context->namespaces;
7968-
List*new_nslist;
7974+
List*parent_namespaces;
79697975
deparse_namespacemydpns;
79707976
constchar*result;
79717977

7978+
parent_namespaces=list_copy_tail(context->namespaces,
7979+
ctelevelsup);
7980+
79727981
set_deparse_for_query(&mydpns,ctequery,
7973-
context->namespaces);
7982+
parent_namespaces);
79747983

7975-
new_nslist=list_copy_tail(context->namespaces,
7976-
ctelevelsup);
7977-
context->namespaces=lcons(&mydpns,new_nslist);
7984+
context->namespaces=lcons(&mydpns,parent_namespaces);
79787985

79797986
result=get_name_for_var_field((Var*)expr,fieldno,
79807987
0,context);

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,66 @@ select r, r is null as isnull, r is not null as isnotnull from r;
12091209
(,) | t | f
12101210
(6 rows)
12111211

1212+
--
1213+
-- Check parsing of indirect references to composite values (bug #18077)
1214+
--
1215+
explain (verbose, costs off)
1216+
with cte(c) as materialized (select row(1, 2)),
1217+
cte2(c) as (select * from cte)
1218+
select * from cte2 as t
1219+
where (select * from (select c as c1) s
1220+
where (select (c1).f1 > 0)) is not null;
1221+
QUERY PLAN
1222+
--------------------------------------------
1223+
CTE Scan on cte
1224+
Output: cte.c
1225+
Filter: ((SubPlan 3) IS NOT NULL)
1226+
CTE cte
1227+
-> Result
1228+
Output: '(1,2)'::record
1229+
SubPlan 3
1230+
-> Result
1231+
Output: cte.c
1232+
One-Time Filter: $2
1233+
InitPlan 2 (returns $2)
1234+
-> Result
1235+
Output: ((cte.c).f1 > 0)
1236+
(13 rows)
1237+
1238+
with cte(c) as materialized (select row(1, 2)),
1239+
cte2(c) as (select * from cte)
1240+
select * from cte2 as t
1241+
where (select * from (select c as c1) s
1242+
where (select (c1).f1 > 0)) is not null;
1243+
c
1244+
-------
1245+
(1,2)
1246+
(1 row)
1247+
1248+
-- Also check deparsing of such cases
1249+
create view composite_v as
1250+
with cte(c) as materialized (select row(1, 2)),
1251+
cte2(c) as (select * from cte)
1252+
select 1 as one from cte2 as t
1253+
where (select * from (select c as c1) s
1254+
where (select (c1).f1 > 0)) is not null;
1255+
select pg_get_viewdef('composite_v', true);
1256+
pg_get_viewdef
1257+
--------------------------------------------------------
1258+
WITH cte(c) AS MATERIALIZED ( +
1259+
SELECT ROW(1, 2) AS "row" +
1260+
), cte2(c) AS ( +
1261+
SELECT cte.c +
1262+
FROM cte +
1263+
) +
1264+
SELECT 1 AS one +
1265+
FROM cte2 t +
1266+
WHERE (( SELECT s.c1 +
1267+
FROM ( SELECT t.c AS c1) s +
1268+
WHERE ( SELECT (s.c1).f1 > 0))) IS NOT NULL;
1269+
(1 row)
1270+
1271+
drop view composite_v;
12121272
--
12131273
-- Tests for component access / FieldSelect
12141274
--

‎src/test/regress/sql/rowtypes.sql

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,31 @@ with r(a,b) as materialized
487487
(null,row(1,2)), (null,row(null,null)), (null,null) )
488488
select r, r isnullas isnull, ris not nullas isnotnullfrom r;
489489

490+
--
491+
-- Check parsing of indirect references to composite values (bug #18077)
492+
--
493+
explain (verbose, costs off)
494+
with cte(c)as materialized (select row(1,2)),
495+
cte2(c)as (select*from cte)
496+
select*from cte2as t
497+
where (select*from (select cas c1) s
498+
where (select (c1).f1>0))is not null;
499+
500+
with cte(c)as materialized (select row(1,2)),
501+
cte2(c)as (select*from cte)
502+
select*from cte2as t
503+
where (select*from (select cas c1) s
504+
where (select (c1).f1>0))is not null;
505+
506+
-- Also check deparsing of such cases
507+
createviewcomposite_vas
508+
with cte(c)as materialized (select row(1,2)),
509+
cte2(c)as (select*from cte)
510+
select1as onefrom cte2as t
511+
where (select*from (select cas c1) s
512+
where (select (c1).f1>0))is not null;
513+
select pg_get_viewdef('composite_v', true);
514+
dropview composite_v;
490515

491516
--
492517
-- Tests for component access / FieldSelect

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp