From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Alexander Farber <alexander.farber@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: You might be able to move the set-returning function into a LATERAL FROM item.
Date: Tue, 20 Mar 2018 11:34:16 +0100
Message-ID: <1521542056.2506.6.camel@cybertec.at> (raw)
In-Reply-To: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com>
References: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com>
Alexander Farber wrote:
> for a word game in PostgreSQL 10.3 I have a table with jsonb column "tiles".
>
> The column holds either a JSON array of objects (word tiles played) or a string (of swapped letters).
>
> I am trying to fetch a history/protocol of a game with:
>
>
> CREATE OR REPLACE FUNCTION words_get_moves(
> [...] AS
> $func$
> [...]
> CASE WHEN JSONB_TYPEOF(tiles) = 'array' THEN JSONB_ARRAY_ELEMENTS(tiles) ELSE NULL END AS x
> [...]
> $func$ LANGUAGE sql;
>
> However calling this stored function gives the error:
>
> ERROR: 0A000: set-returning functions are not allowed in CASE
> LINE 18: ... CASE WHEN JSONB_TYPEOF(tiles) = 'array' THEN JSONB_ARRA...
> ^
> HINT: You might be able to move the set-returning function into a LATERAL FROM item.
>
> I have read that PostgreSQL 10 handles SRF more strictly, but what does it want me to do here, to add 1 more table to the LEFT JOIN?
The problem is that "jsonb_array_elements" returns several rows, which does not
make sense in this context. Which of the rows do you want?
If you know that it will always return at most one row, you could use:
... THEN (SELECT jae FROM jsonb_array_elements(tiles) jae LIMIT 1)
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.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: laurenz.albe@cybertec.at, alexander.farber@gmail.com
Subject: Re: You might be able to move the set-returning function into a LATERAL FROM item.
In-Reply-To: <1521542056.2506.6.camel@cybertec.at>
References: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com> <1521542056.2506.6.camel@cybertec.at>
* 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