Search Postgresql Archives

Re: You might be able to move the set-returning function into a LATERAL FROM item.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Alexander Farber <alexander.farber@xxxxxxxxx> 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




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux