From: Paul Jungwirth <pj@illuminatedcomputing.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Using Lateral
Date: Tue, 27 Mar 2018 08:37:26 -0700
Message-ID: <c8210475-950f-daa8-6d8c-cbd9f6d790ad@illuminatedcomputing.com> (raw)
In-Reply-To: <CAGZ55DR4DOTr+=hdUeM3C5_sqN1ARdgYbfX7W3PbkvBO+UfvCg@mail.gmail.com>
References: <CAGZ55DR4DOTr+=hdUeM3C5_sqN1ARdgYbfX7W3PbkvBO+UfvCg@mail.gmail.com>
On 03/27/2018 03:22 AM, Johann Spies wrote:
> In the past I could use this in a query:
>
> SELECT
> DISTINCT ut,
> CASE
> WHEN xpath_exists ('//t:address_spec/t:country/text()',
> q.address_spec,
> p.ns)
> THEN unnest (xpath ('//t:address_spec/t:country/text()',
> q.address_spec,
> p.ns))::citext
> ELSE NULL
> END country,
>
> No longer. The error message suggests I should use a lateral query.
> But I could not figure out in the documentation how to get the same
> result using a "lateral" construct.
>
> Just selecting "unnest(...)" gives the wrong result because if the
> xpath does not exist all the other parts of the query (like 'ut' in
> this case) also are empty.
It is hard to suggest something without seeing your whole query (e.g.
how are you joining q & p?). But it sounds like you basically want a
left join to the unnested xpath result. It could be a lateral join or not.
It is common to use UNNEST with an implicit lateral join, like this:
SELECT ...
FROM q, UNNEST(xpath('...', q.address_spec))
But that gives you an inner join. To get an outer join you need to be
more explicit. Something like this:
SELECT ...
FROM q, p
LEFT OUTER JOIN LATERAL (
SELECT *
FROM unnest(xpath('//t:address_spec/t:country/text()',
q.address_spec, p.ns))::citext
) x(country)
ON true
(Presumably you would do something more restrictive to connect q & p
though.)
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: pj@illuminatedcomputing.com
Subject: Re: Using Lateral
In-Reply-To: <c8210475-950f-daa8-6d8c-cbd9f6d790ad@illuminatedcomputing.com>
References: <CAGZ55DR4DOTr+=hdUeM3C5_sqN1ARdgYbfX7W3PbkvBO+UfvCg@mail.gmail.com> <c8210475-950f-daa8-6d8c-cbd9f6d790ad@illuminatedcomputing.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox