pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: 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 10:27:57 -0400
Message-ID: <11515.1521556077@sss.pgh.pa.us> (raw)
In-Reply-To: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com>
References: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com>

Alexander Farber <alexander.farber@gmail.com> writes:
> I am trying to fetch a history/protocol of a game with:

>                 SELECT
>                     CASE WHEN JSONB_TYPEOF(tiles) = 'array' THEN
> JSONB_ARRAY_ELEMENTS(tiles) ELSE NULL END AS x

I think you could push the conditionality into a plpgsql function,
something like (untested)

create function jsonb_elements_if_array(j jsonb) returns setof jsonb as $$
begin
  if jsonb_typeof(j) = 'array' then
    return query select jsonb_array_elements(j);
  end if;
end$$
strict immutable language plpgsql;

Note that this gives *no* elements, rather than a single NULL value,
if the input isn't an array --- but that seems to me to make more sense
than your existing code anyhow.  If you disagree, add "else return next
null::jsonb".

			regards, tom lane




view thread (4+ messages)  latest in thread

Message-ID: <11515.1521556077@sss.pgh.pa.us>
Permalink:  ../11515.1521556077@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/11515.1521556077@sss.pgh.pa.us

 · 

reply

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: tgl@sss.pgh.pa.us, alexander.farber@gmail.com
  Subject: Re: You might be able to move the set-returning function into a LATERAL FROM item.
  In-Reply-To: <11515.1521556077@sss.pgh.pa.us>
  References: <CAADeyWh41KfV6g1dFQHHNvV6O5z90QXSxQQoSW9yj0jD-r8H9A@mail.gmail.com> <11515.1521556077@sss.pgh.pa.us>

* 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