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

Commit53630f1

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 parent3f94dfc commit53630f1

File tree

4 files changed

+119
-20
lines changed

4 files changed

+119
-20
lines changed

‎src/backend/parser/parse_target.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,8 @@ ExpandRowReference(ParseState *pstate, Node *expr,
14991499
* drill down to find the ultimate defining expression and attempt to infer
15001500
* the tupdesc from it. We ereport if we can't determine the tupdesc.
15011501
*
1502-
* levelsup is an extra offset to interpret the Var's varlevelsup correctly.
1502+
* levelsup is an extra offset to interpret the Var's varlevelsup correctly
1503+
* when recursing. Outside callers should pass zero.
15031504
*/
15041505
TupleDesc
15051506
expandRecordVariable(ParseState*pstate,Var*var,intlevelsup)
@@ -1587,10 +1588,17 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
15871588
/*
15881589
* Recurse into the sub-select to see what its Var refers
15891590
* to. We have to build an additional level of ParseState
1590-
* to keep in step with varlevelsup in the subselect.
1591+
* to keep in step with varlevelsup in the subselect;
1592+
* furthermore, the subquery RTE might be from an outer
1593+
* query level, in which case the ParseState for the
1594+
* subselect must have that outer level as parent.
15911595
*/
15921596
ParseStatemypstate= {0};
1597+
Indexlevelsup;
15931598

1599+
/* this loop must work, since GetRTEByRangeTablePosn did */
1600+
for (levelsup=0;levelsup<netlevelsup;levelsup++)
1601+
pstate=pstate->parentParseState;
15941602
mypstate.parentParseState=pstate;
15951603
mypstate.p_rtable=rte->subquery->rtable;
15961604
/* don't bother filling the rest of the fake pstate */
@@ -1641,12 +1649,11 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
16411649
* Recurse into the CTE to see what its Var refers to. We
16421650
* have to build an additional level of ParseState to keep
16431651
* in step with varlevelsup in the CTE; furthermore it
1644-
* could be an outer CTE.
1652+
* could be an outer CTE (compare SUBQUERY case above).
16451653
*/
1646-
ParseStatemypstate;
1654+
ParseStatemypstate= {0};
16471655
Indexlevelsup;
16481656

1649-
MemSet(&mypstate,0,sizeof(mypstate));
16501657
/* this loop must work, since GetCTEForRTE did */
16511658
for (levelsup=0;
16521659
levelsup<rte->ctelevelsup+netlevelsup;

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7806,22 +7806,28 @@ get_name_for_var_field(Var *var, int fieldno,
78067806
* Recurse into the sub-select to see what its Var
78077807
* refers to. We have to build an additional level of
78087808
* namespace to keep in step with varlevelsup in the
7809-
* subselect.
7809+
* subselect; furthermore, the subquery RTE might be
7810+
* from an outer query level, in which case the
7811+
* namespace for the subselect must have that outer
7812+
* level as parent namespace.
78107813
*/
7814+
List*save_nslist=context->namespaces;
7815+
List*parent_namespaces;
78117816
deparse_namespacemydpns;
78127817
constchar*result;
78137818

7819+
parent_namespaces=list_copy_tail(context->namespaces,
7820+
netlevelsup);
7821+
78147822
set_deparse_for_query(&mydpns,rte->subquery,
7815-
context->namespaces);
7823+
parent_namespaces);
78167824

7817-
context->namespaces=lcons(&mydpns,
7818-
context->namespaces);
7825+
context->namespaces=lcons(&mydpns,parent_namespaces);
78197826

78207827
result=get_name_for_var_field((Var*)expr,fieldno,
78217828
0,context);
78227829

7823-
context->namespaces=
7824-
list_delete_first(context->namespaces);
7830+
context->namespaces=save_nslist;
78257831

78267832
returnresult;
78277833
}
@@ -7913,29 +7919,30 @@ get_name_for_var_field(Var *var, int fieldno,
79137919
attnum);
79147920

79157921
if (ste==NULL||ste->resjunk)
7916-
elog(ERROR,"subquery %s does not have attribute %d",
7922+
elog(ERROR,"CTE %s does not have attribute %d",
79177923
rte->eref->aliasname,attnum);
79187924
expr= (Node*)ste->expr;
79197925
if (IsA(expr,Var))
79207926
{
79217927
/*
79227928
* Recurse into the CTE to see what its Var refers to.
79237929
* We have to build an additional level of namespace
7924-
* to keep in step with varlevelsup in the CTE.
7925-
*Furthermore it could be an outer CTE, so we may
7926-
*have to delete some levels of namespace.
7930+
* to keep in step with varlevelsup in the CTE;
7931+
*furthermore it could be an outer CTE (compare
7932+
*SUBQUERY case above).
79277933
*/
79287934
List*save_nslist=context->namespaces;
7929-
List*new_nslist;
7935+
List*parent_namespaces;
79307936
deparse_namespacemydpns;
79317937
constchar*result;
79327938

7939+
parent_namespaces=list_copy_tail(context->namespaces,
7940+
ctelevelsup);
7941+
79337942
set_deparse_for_query(&mydpns,ctequery,
7934-
context->namespaces);
7943+
parent_namespaces);
79357944

7936-
new_nslist=list_copy_tail(context->namespaces,
7937-
ctelevelsup);
7938-
context->namespaces=lcons(&mydpns,new_nslist);
7945+
context->namespaces=lcons(&mydpns,parent_namespaces);
79397946

79407947
result=get_name_for_var_field((Var*)expr,fieldno,
79417948
0,context);

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

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

1243+
--
1244+
-- Check parsing of indirect references to composite values (bug #18077)
1245+
--
1246+
explain (verbose, costs off)
1247+
with cte(c) as materialized (select row(1, 2)),
1248+
cte2(c) as (select * from cte)
1249+
select * from cte2 as t
1250+
where (select * from (select c as c1) s
1251+
where (select (c1).f1 > 0)) is not null;
1252+
QUERY PLAN
1253+
--------------------------------------------
1254+
CTE Scan on cte
1255+
Output: cte.c
1256+
Filter: ((SubPlan 3) IS NOT NULL)
1257+
CTE cte
1258+
-> Result
1259+
Output: '(1,2)'::record
1260+
SubPlan 3
1261+
-> Result
1262+
Output: cte.c
1263+
One-Time Filter: $2
1264+
InitPlan 2 (returns $2)
1265+
-> Result
1266+
Output: ((cte.c).f1 > 0)
1267+
(13 rows)
1268+
1269+
with cte(c) as materialized (select row(1, 2)),
1270+
cte2(c) as (select * from cte)
1271+
select * from cte2 as t
1272+
where (select * from (select c as c1) s
1273+
where (select (c1).f1 > 0)) is not null;
1274+
c
1275+
-------
1276+
(1,2)
1277+
(1 row)
1278+
1279+
-- Also check deparsing of such cases
1280+
create view composite_v as
1281+
with cte(c) as materialized (select row(1, 2)),
1282+
cte2(c) as (select * from cte)
1283+
select 1 as one from cte2 as t
1284+
where (select * from (select c as c1) s
1285+
where (select (c1).f1 > 0)) is not null;
1286+
select pg_get_viewdef('composite_v', true);
1287+
pg_get_viewdef
1288+
--------------------------------------------------------
1289+
WITH cte(c) AS MATERIALIZED ( +
1290+
SELECT ROW(1, 2) AS "row" +
1291+
), cte2(c) AS ( +
1292+
SELECT cte.c +
1293+
FROM cte +
1294+
) +
1295+
SELECT 1 AS one +
1296+
FROM cte2 t +
1297+
WHERE (( SELECT s.c1 +
1298+
FROM ( SELECT t.c AS c1) s +
1299+
WHERE ( SELECT (s.c1).f1 > 0))) IS NOT NULL;
1300+
(1 row)
1301+
1302+
drop view composite_v;
12431303
--
12441304
-- Tests for component access / FieldSelect
12451305
--

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

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

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

498523
--
499524
-- Tests for component access / FieldSelect

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp