Re: ARRAY() returning NULL instead of ARRAY[] resp. {}

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-05-24 04:06:00
Message-ID: 16774.1116907560@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway <mail(at)joeconway(dot)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 for
each row in the subquery result, with an element type matching
that of 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2005-05-24 04:23:30 Re: Deadlocks in 7.4.x ...
Previous Message Tom Lane 2005-05-24 03:37:34 Re: Speeding up the Postgres lexer

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2005-05-24 04:16:38 Re: [BUGS] BUG #1609: Bug in interval datatype for 64 Bit timestamps
Previous Message Prospect Response Newsletter 2005-05-24 03:47:13 TODAY Last Day For SuccessPub 50% Off

Browse pgsql-sql by date

  From Date Subject
Next Message Carlo Annunziata 2005-05-24 04:31:45 Вторая жизнь мягкой мебели.
Previous Message Joe Conway 2005-05-23 20:04:50 Re: ARRAY() returning NULL instead of ARRAY[] resp. {}