Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVkj4-0008E0-Sf for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Apr 2021 00:46:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lVkj2-0004G2-MI for pgsql-bugs@arkaria.postgresql.org; Mon, 12 Apr 2021 00:46:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVkj2-0004Fu-GM for pgsql-bugs@lists.postgresql.org; Mon, 12 Apr 2021 00:46:48 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lVkiv-00082V-7x for pgsql-bugs@lists.postgresql.org; Mon, 12 Apr 2021 00:46:47 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 13C0kbAk2734127; Sun, 11 Apr 2021 20:46:37 -0400 From: Tom Lane To: posuch@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16959: Unnesting null from string_to_array silently removes whole rows from result In-reply-to: <16959-4c11c2e812e2b52c@postgresql.org> References: <16959-4c11c2e812e2b52c@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 11 Apr 2021 23:50:01 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2734125.1618188397.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 11 Apr 2021 20:46:37 -0400 Message-ID: <2734126.1618188397@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > Sample data: > create table test_rows as = > SELECT * FROM (VALUES (1, null), (2, 'second')) AS t (num,letter); > Query with the unexpected result (I expected 2 rows): > select num, unnest(string_to_array(letter, ',')) from test_rows; > num | unnest = > ----+-------- > 2 | second > (1 row) Well, you could perhaps argue that string_to_array with NULL input should produce an empty array rather than a NULL. But UNNEST() would produce zero rows in either case, and I fail to see why you find that surprising, much less buggy. It would be a bug if it manufactured a value out of nothing. Having said that, you could inject the value you prefer using COALESCE, say # select num, unnest(coalesce(string_to_array(letter, ','), '{""}')) from = test_rows; num | unnest = -----+-------- 1 | = 2 | second (2 rows) Alternatively, perhaps you'd consider a lateral left join to be less-surprising behavior: # select num, u from test_rows left join lateral unnest(string_to_array(le= tter, ',')) u on true; num | u = -----+-------- 1 | = 2 | second (2 rows) The behavior you're getting from SRF-in-the-targetlist is basically equivalent to a lateral plain join, rather than left join. See https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS= -RETURNING-SET regards, tom lane