Thread: Empty arrays with ARRAY[]

Empty arrays with ARRAY[]

From
"Brendan Jurd"
Date:
Hi all,

I noticed in the 8.3 release notes that ARRAY(SELECT ...) now returns
an empty array if there are no rows returned by the subquery.

Until reading this, I didn't even realise that empty arrays were
possible, since ARRAY[] causes a syntax error.  I played around with
the array input syntax and found that you can create an empty array
with literal input (e.g., with '{}'::int[]), but it's not possible
with ARRAY[].  Apparently the array constructor syntax requires

"a left square bracket [, one or more expressions (separated by
commas) for the array element values, and finally a right square
bracket ]" [1].

My question is, if it's possible to create an empty array, why is the
array constructor designed to only construct arrays with at least one
element?

I'm thinking it's because the constructor needs to decide what type to
make the array.  Is that correct, and if so, would the following TODO
item make ARRAY[] possible?

"Delay resolution of array expression's data type so assignment
coercion can be performed on empty array expressions"

Regards,
BJ

[1] http://www.postgresql.org/docs/8.3/static/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS

Re: Empty arrays with ARRAY[]

From
Martijn van Oosterhout
Date:
On Mon, Nov 26, 2007 at 03:51:37AM +1100, Brendan Jurd wrote:
> Hi all,
>
> I noticed in the 8.3 release notes that ARRAY(SELECT ...) now returns
> an empty array if there are no rows returned by the subquery.

This has come up before, Tom had an idea about how to fix it:

http://groups.google.com/group/pgsql.general/browse_thread/thread/911791e145a17daa/6b035035aeaac399
http://www.mail-archive.com/pgsql-general@postgresql.org/msg90681.html

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Those who make peaceful revolution impossible will make violent revolution inevitable.
>  -- John F Kennedy

Attachment