Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyEbR-0004a9-En for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 10:34:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eyEbP-0003NM-ND for pgsql-general@arkaria.postgresql.org; Tue, 20 Mar 2018 10:34:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyEbP-0003N1-F9 for pgsql-general@lists.postgresql.org; Tue, 20 Mar 2018 10:34:47 +0000 Received: from mx1.mailbox.org ([80.241.60.212]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eyEbM-0002GW-VM for pgsql-general@postgresql.org; Tue, 20 Mar 2018 10:34:46 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [80.241.60.241]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.mailbox.org (Postfix) with ESMTPS id 2642942E6F; Tue, 20 Mar 2018 11:34:43 +0100 (CET) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp2.mailbox.org ([80.241.60.241]) by spamfilter02.heinlein-hosting.de (spamfilter02.heinlein-hosting.de [80.241.56.116]) (amavisd-new, port 10030) with ESMTP id 33xIOLYbUAyG; Tue, 20 Mar 2018 11:34:18 +0100 (CET) Message-ID: <1521542056.2506.6.camel@cybertec.at> Subject: Re: You might be able to move the set-returning function into a LATERAL FROM item. From: Laurenz Albe To: Alexander Farber , pgsql-general Date: Tue, 20 Mar 2018 11:34:16 +0100 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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