Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e105df2

Browse files
committed
Fix incorrect matching of subexpressions in outer-join plan nodes.
Previously we would re-use input subexpressions in all expression trees attached to a Join plan node. However, if it's an outer join and the subexpression appears in the nullable-side input, this is potentially incorrect for apparently-matching subexpressions that came from above the outer join (ie, targetlist and qpqual expressions), because the executor will treat the subexpression value as NULL when maybe it should not be. The case is fairly hard to hit because (a) you need a non-strict subexpression (else NULL is correct), and (b) we don't usually compute expressions in the outputs of non-toplevel plan nodes. But we might do so if the expressions are sort keys for a mergejoin, for example. Probably in the long run we should make a more explicit distinction between Vars appearing above and below an outer join, but that will be a major planner redesign and not at all back-patchable. For the moment, just hack set_join_references so that it will not match any non-Var expressions coming from nullable inputs to expressions that came from above the join. (This is somewhat overkill, in that a strict expression could still be matched, but it doesn't seem worth the effort to check that.) Per report from Qingqing Zhou. The added regression test case is based on his example. This has been broken for a very long time, so back-patch to all active branches.
1 parent cbccaf2 commit e105df2

File tree

3 files changed

+121
-14
lines changed

3 files changed

+121
-14
lines changed

src/backend/optimizer/plan/setrefs.c

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,19 +1231,13 @@ set_join_references(PlannerInfo *root, Join *join, int rtoffset)
12311231
outer_itlist = build_tlist_index(outer_plan->targetlist);
12321232
inner_itlist = build_tlist_index(inner_plan->targetlist);
12331233

1234-
/* All join plans have tlist, qual, and joinqual */
1235-
join->plan.targetlist = fix_join_expr(root,
1236-
join->plan.targetlist,
1237-
outer_itlist,
1238-
inner_itlist,
1239-
(Index) 0,
1240-
rtoffset);
1241-
join->plan.qual = fix_join_expr(root,
1242-
join->plan.qual,
1243-
outer_itlist,
1244-
inner_itlist,
1245-
(Index) 0,
1246-
rtoffset);
1234+
/*
1235+
* First process the joinquals (including merge or hash clauses). These
1236+
* are logically below the join so they can always use all values
1237+
* available from the input tlists. It's okay to also handle
1238+
* NestLoopParams now, because those couldn't refer to nullable
1239+
* subexpressions.
1240+
*/
12471241
join->joinqual = fix_join_expr(root,
12481242
join->joinqual,
12491243
outer_itlist,
@@ -1295,6 +1289,49 @@ set_join_references(PlannerInfo *root, Join *join, int rtoffset)
12951289
rtoffset);
12961290
}
12971291

1292+
/*
1293+
* Now we need to fix up the targetlist and qpqual, which are logically
1294+
* above the join. This means they should not re-use any input expression
1295+
* that was computed in the nullable side of an outer join. Vars and
1296+
* PlaceHolderVars are fine, so we can implement this restriction just by
1297+
* clearing has_non_vars in the indexed_tlist structs.
1298+
*
1299+
* XXX This is a grotty workaround for the fact that we don't clearly
1300+
* distinguish between a Var appearing below an outer join and the "same"
1301+
* Var appearing above it. If we did, we'd not need to hack the matching
1302+
* rules this way.
1303+
*/
1304+
switch (join->jointype)
1305+
{
1306+
case JOIN_LEFT:
1307+
case JOIN_SEMI:
1308+
case JOIN_ANTI:
1309+
inner_itlist->has_non_vars = false;
1310+
break;
1311+
case JOIN_RIGHT:
1312+
outer_itlist->has_non_vars = false;
1313+
break;
1314+
case JOIN_FULL:
1315+
outer_itlist->has_non_vars = false;
1316+
inner_itlist->has_non_vars = false;
1317+
break;
1318+
default:
1319+
break;
1320+
}
1321+
1322+
join->plan.targetlist = fix_join_expr(root,
1323+
join->plan.targetlist,
1324+
outer_itlist,
1325+
inner_itlist,
1326+
(Index) 0,
1327+
rtoffset);
1328+
join->plan.qual = fix_join_expr(root,
1329+
join->plan.qual,
1330+
outer_itlist,
1331+
inner_itlist,
1332+
(Index) 0,
1333+
rtoffset);
1334+
12981335
pfree(outer_itlist);
12991336
pfree(inner_itlist);
13001337
}
@@ -1573,7 +1610,9 @@ search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
15731610
* If no match, return NULL.
15741611
*
15751612
* NOTE: it is a waste of time to call this unless itlist->has_ph_vars or
1576-
* itlist->has_non_vars
1613+
* itlist->has_non_vars. Furthermore, set_join_references() relies on being
1614+
* able to prevent matching of non-Vars by clearing itlist->has_non_vars,
1615+
* so there's a correctness reason not to call it unless that's set.
15771616
*/
15781617
static Var *
15791618
search_indexed_tlist_for_non_var(Node *node,

src/test/regress/expected/join.out

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3096,6 +3096,54 @@ explain (costs off)
30963096
Index Cond: (unique2 = 42)
30973097
(6 rows)
30983098

3099+
--
3100+
-- test that quals attached to an outer join have correct semantics,
3101+
-- specifically that they don't re-use expressions computed below the join;
3102+
-- we force a mergejoin so that coalesce(b.q1, 1) appears as a join input
3103+
--
3104+
set enable_hashjoin to off;
3105+
set enable_nestloop to off;
3106+
explain (verbose, costs off)
3107+
select a.q2, b.q1
3108+
from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
3109+
where coalesce(b.q1, 1) > 0;
3110+
QUERY PLAN
3111+
-------------------------------------------------------
3112+
Merge Left Join
3113+
Output: a.q2, b.q1
3114+
Merge Cond: (a.q2 = (COALESCE(b.q1, 1::bigint)))
3115+
Filter: (COALESCE(b.q1, 1::bigint) > 0)
3116+
-> Sort
3117+
Output: a.q2
3118+
Sort Key: a.q2
3119+
-> Seq Scan on public.int8_tbl a
3120+
Output: a.q2
3121+
-> Sort
3122+
Output: b.q1, (COALESCE(b.q1, 1::bigint))
3123+
Sort Key: (COALESCE(b.q1, 1::bigint))
3124+
-> Seq Scan on public.int8_tbl b
3125+
Output: b.q1, COALESCE(b.q1, 1::bigint)
3126+
(14 rows)
3127+
3128+
select a.q2, b.q1
3129+
from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
3130+
where coalesce(b.q1, 1) > 0;
3131+
q2 | q1
3132+
-------------------+------------------
3133+
-4567890123456789 |
3134+
123 | 123
3135+
123 | 123
3136+
456 |
3137+
4567890123456789 | 4567890123456789
3138+
4567890123456789 | 4567890123456789
3139+
4567890123456789 | 4567890123456789
3140+
4567890123456789 | 4567890123456789
3141+
4567890123456789 | 4567890123456789
3142+
4567890123456789 | 4567890123456789
3143+
(10 rows)
3144+
3145+
reset enable_hashjoin;
3146+
reset enable_nestloop;
30993147
--
31003148
-- test join removal
31013149
--

src/test/regress/sql/join.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,26 @@ explain (costs off)
907907
explain (costs off)
908908
select * from tenk1 a full join tenk1 b using(unique2) where unique2 = 42;
909909

910+
--
911+
-- test that quals attached to an outer join have correct semantics,
912+
-- specifically that they don't re-use expressions computed below the join;
913+
-- we force a mergejoin so that coalesce(b.q1, 1) appears as a join input
914+
--
915+
916+
set enable_hashjoin to off;
917+
set enable_nestloop to off;
918+
919+
explain (verbose, costs off)
920+
select a.q2, b.q1
921+
from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
922+
where coalesce(b.q1, 1) > 0;
923+
select a.q2, b.q1
924+
from int8_tbl a left join int8_tbl b on a.q2 = coalesce(b.q1, 1)
925+
where coalesce(b.q1, 1) > 0;
926+
927+
reset enable_hashjoin;
928+
reset enable_nestloop;
929+
910930
--
911931
-- test join removal
912932
--

0 commit comments

Comments
 (0)