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

Commit1b4cc49

Browse files
committed
Preserve AND/OR flatness while extracting restriction OR clauses.
The code I added in commitf343a88 wascareless about preserving AND/OR flatness: it could create a structure withan OR node directly underneath another one. That breaks an assumptionthat's fairly important for planning efficiency, not to mention triggeringvarious Asserts (as reported by Benjamin Smith). Add a trifle more logicto handle the case properly.
1 parent07c8651 commit1b4cc49

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

‎src/backend/optimizer/util/orclauses.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ extract_or_clause(RestrictInfo *or_rinfo, RelOptInfo *rel)
178178
{
179179
Node*orarg= (Node*)lfirst(lc);
180180
List*subclauses=NIL;
181+
Node*subclause;
181182

182183
/* OR arguments should be ANDs or sub-RestrictInfos */
183184
if (and_clause(orarg))
@@ -226,9 +227,16 @@ extract_or_clause(RestrictInfo *or_rinfo, RelOptInfo *rel)
226227

227228
/*
228229
* OK, add subclause(s) to the result OR. If we found more than one,
229-
* we need an AND node.
230+
* we need an AND node. But if we found only one, and it is itself an
231+
* OR node, add its subclauses to the result instead; this is needed
232+
* to preserve AND/OR flatness (ie, no OR directly underneath OR).
230233
*/
231-
clauselist=lappend(clauselist,make_ands_explicit(subclauses));
234+
subclause= (Node*)make_ands_explicit(subclauses);
235+
if (or_clause(subclause))
236+
clauselist=list_concat(clauselist,
237+
list_copy(((BoolExpr*)subclause)->args));
238+
else
239+
clauselist=lappend(clauselist,subclause);
232240
}
233241

234242
/*

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2827,6 +2827,33 @@ select * from tenk1 a join tenk1 b on
28272827
Index Cond: (unique2 = 3)
28282828
(12 rows)
28292829

2830+
explain (costs off)
2831+
select * from tenk1 a join tenk1 b on
2832+
(a.unique1 = 1 and b.unique1 = 2) or
2833+
((a.unique2 = 3 or a.unique2 = 7) and b.hundred = 4);
2834+
QUERY PLAN
2835+
----------------------------------------------------------------------------------------------------------------------
2836+
Nested Loop
2837+
Join Filter: (((a.unique1 = 1) AND (b.unique1 = 2)) OR (((a.unique2 = 3) OR (a.unique2 = 7)) AND (b.hundred = 4)))
2838+
-> Bitmap Heap Scan on tenk1 b
2839+
Recheck Cond: ((unique1 = 2) OR (hundred = 4))
2840+
-> BitmapOr
2841+
-> Bitmap Index Scan on tenk1_unique1
2842+
Index Cond: (unique1 = 2)
2843+
-> Bitmap Index Scan on tenk1_hundred
2844+
Index Cond: (hundred = 4)
2845+
-> Materialize
2846+
-> Bitmap Heap Scan on tenk1 a
2847+
Recheck Cond: ((unique1 = 1) OR (unique2 = 3) OR (unique2 = 7))
2848+
-> BitmapOr
2849+
-> Bitmap Index Scan on tenk1_unique1
2850+
Index Cond: (unique1 = 1)
2851+
-> Bitmap Index Scan on tenk1_unique2
2852+
Index Cond: (unique2 = 3)
2853+
-> Bitmap Index Scan on tenk1_unique2
2854+
Index Cond: (unique2 = 7)
2855+
(19 rows)
2856+
28302857
--
28312858
-- test placement of movable quals in a parameterized join tree
28322859
--

‎src/test/regress/sql/join.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,10 @@ select * from tenk1 a join tenk1 b on
774774
explain (costs off)
775775
select*from tenk1 ajoin tenk1 bon
776776
(a.unique1=1andb.unique1=2)or (a.unique2=3andb.ten=4);
777+
explain (costs off)
778+
select*from tenk1 ajoin tenk1 bon
779+
(a.unique1=1andb.unique1=2)or
780+
((a.unique2=3ora.unique2=7)andb.hundred=4);
777781

778782
--
779783
-- test placement of movable quals in a parameterized join tree

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp