File tree Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Expand file tree Collapse file tree 3 files changed +41
-2
lines changed Original file line number Diff line number Diff 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 /*
Original file line number Diff line number Diff 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--
Original file line number Diff line number Diff line change @@ -774,6 +774,10 @@ select * from tenk1 a join tenk1 b on
774774explain (costs off)
775775select * from tenk1 a join tenk1 b on
776776 (a .unique1 = 1 and b .unique1 = 2 ) or (a .unique2 = 3 and b .ten = 4 );
777+ explain (costs off)
778+ select * from tenk1 a join tenk1 b on
779+ (a .unique1 = 1 and b .unique1 = 2 ) or
780+ ((a .unique2 = 3 or a .unique2 = 7 ) and b .hundred = 4 );
777781
778782--
779783-- test placement of movable quals in a parameterized join tree
You can’t perform that action at this time.
0 commit comments