Fix qual-clause-misplacement issues with pulled-up LATERAL subqueries.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Aug 2013 17:19:25 +0000 (13:19 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 19 Aug 2013 17:19:41 +0000 (13:19 -0400)
commitc64de21e9625acad57e2caf8f22435e1617fb1ce
tree0304ca222f842e04c1c72c598d103feeae4de5e9
parent78e1220104227c86b4b49d0fc123db7fa596d43d
Fix qual-clause-misplacement issues with pulled-up LATERAL subqueries.

In an example such as
SELECT * FROM
  i LEFT JOIN LATERAL (SELECT * FROM j WHERE i.n = j.n) j ON true;
it is safe to pull up the LATERAL subquery into its parent, but we must
then treat the "i.n = j.n" clause as a qual clause of the LEFT JOIN.  The
previous coding in deconstruct_recurse mistakenly labeled the clause as
"is_pushed_down", resulting in wrong semantics if the clause were applied
at the join node, as per an example submitted awhile ago by Jeremy Evans.
To fix, postpone processing of such clauses until we return back up to
the appropriate recursion depth in deconstruct_recurse.

In addition, tighten the is-safe-to-pull-up checks in is_simple_subquery;
we previously missed the possibility that the LATERAL subquery might itself
contain an outer join that makes lateral references in lower quals unsafe.

A regression test case equivalent to Jeremy's example was already in my
commit of yesterday, but was giving the wrong results because of this
bug.  This patch fixes the expected output for that, and also adds a
test case for the second problem.
src/backend/optimizer/README
src/backend/optimizer/plan/initsplan.c
src/backend/optimizer/prep/prepjointree.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql