is_simple_subquery() refuses to pull up a LATERAL subquery whose
targetlist or quals reference rels outside the lowest outer join above
it.  But pull_up_simple_subquery() rechecks this before flattening
join alias Vars in the subquery, so a lateral reference hidden in a
join Var goes unnoticed.  Both queries below hit Assert("sjinfo ==
NULL") in distribute_qual_to_rels():

create table t (a int);

-- hidden in the subquery's targetlist
select 1 from t t1,
  lateral (select (j is null)::int
           from ((select t1.a) s left join (select 1) v on false) j) ss(x)
  left join t t2 on ss.x = t2.a;

-- hidden in the subquery's quals
select 1 from t t1,
  t t2 left join
  lateral (select 1 from ((select t1.a) s left join (select 1) v on false) j
           where length(j::text) > 3) ss on true;

Without asserts, the second fails with "wrong phnullingrels".

I think we should flatten join alias Vars in the subquery's targetlist
and quals before the recheck.  Attached is a WIP patch doing that.

- Richard

Attachment: v1-0001-Flatten-join-alias-Vars-before-rechecking-subquer.patch
Description: Binary data

Reply via email to