Empty arrays with ARRAY[]

From: "Brendan Jurd" <direvus(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Empty arrays with ARRAY[]
Date: 2007-11-25 16:51:37
Message-ID: 37ed240d0711250851k6f917ce1v1c014393f8777c94@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-patches

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

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-11-25 16:58:20 Re: Empty arrays with ARRAY[]
Previous Message Никоноров Григорий 2007-11-25 16:39:00 Error while compiling PostgreSQL with Java

Browse pgsql-hackers by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-11-25 16:58:20 Re: Empty arrays with ARRAY[]
Previous Message Gregory Stark 2007-11-25 16:36:14 Re: 8.3devel slower than 8.2 under read-only load

Browse pgsql-patches by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-11-25 16:58:20 Re: Empty arrays with ARRAY[]
Previous Message Tom Lane 2007-11-25 01:40:45 Proposed patch to adjust UNION/CASE/etc type resolution rules