Re: ARRAY() returning NULL instead of ARRAY[] resp. {} - Mailing list pgsql-sql

From Tom Lane
Subject Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date
Msg-id 16774.1116907560@sss.pgh.pa.us
Whole thread Raw
In response to Re: ARRAY() returning NULL instead of ARRAY[] resp. {}  (Joe Conway <mail@joeconway.com>)
Responses Re: ARRAY() returning NULL instead of ARRAY[] resp. {}  (Markus Bertheau <twanger@bluetwanger.de>)
Re: ARRAY() returning NULL instead of ARRAY[] resp. {}  (Joe Conway <mail@joeconway.com>)
Re: ARRAY() returning NULL instead of ARRAY[] resp. {}  (Markus Bertheau ☭ <twanger@bluetwanger.de>)
List pgsql-sql
Joe Conway <mail@joeconway.com> writes:
> Markus Bertheau wrote:
>> why does SELECT ARRAY(SELECT 1 WHERE FALSE) return NULL instead of
>> ARRAY[] resp. '{}'?

> Why would you expect an empty array instead of a NULL?

I think he's got a good point, actually.  We document the ARRAY-with-
parens-around-a-SELECT syntax as
The resulting one-dimensional array will have an element foreach row in the subquery result, with an element type
matchingthatof the subquery's output column.
 

To me, that implies that a subquery result of no rows generates a
one-dimensional array of no elements, not a null array.

This is not the same as

SELECT ARRAY[(SELECT 1 WHERE FALSE)];

We define a scalar subquery that returns no rows as returning null, so
this is equivalent to

SELECT ARRAY[NULL];

which *ought* to yield an array containing a single NULL element,
but since we can't yet handle arrays containing nulls we punt and
return a null array value.  That's wrong too ... but it's a different
issue.  The point Markus is complaining about seems like it should
be easily fixable.
        regards, tom lane


pgsql-sql by date:

Previous
From: Joe Conway
Date:
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Next
From:
Date:
Subject: Help: Function for splitting VARCHAR column and migrating its data to 2 new tables