Skip site navigation (1) Skip section navigation (2)

Re: [GENERAL] Empty arrays with ARRAY[]

From: "Brendan Jurd" <direvus(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Martijn van Oosterhout" <kleptog(at)svana(dot)org>, pgsql-hackers(at)postgresql(dot)org, pgsql-patches(at)postgresql(dot)org
Subject: Re: [GENERAL] Empty arrays with ARRAY[]
Date: 2007-11-30 09:14:14
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-generalpgsql-hackerspgsql-patches
As discussed on -hackers, this patch allows the construction of an
empty array if an explicit cast to an array type is given (as in,

postgres=# select array[]::int[];

postgres=# select array[];
ERROR:  no target type for empty array
HINT:  Empty arrays must be explictly cast to the desired array type,
e.g. ARRAY[]::int[]

A few notes on the implementation:

 * The syntax now allows an ARRAY constructor with an empty expression
list (array_expr_list may be empty).

 * I've added a new parsenode for arrays, A_ArrayExpr (previously the
parser would create ArrayExpr primnodes).

 * transformArrayExpr() now takes two extra arguments, a type oid and
a typmod.  When transforming a typecast which casts an A_ArrayExpr to
an array type, transformExpr passes these type details down to
transformArrayExpr, and skips the typecast.

 * transformArrayExpr() behaves slightly differently when passed type
information.  The overall type of the array is set to the given type,
and all elements are explictly coerced to the equivalent element type.
 If it was not passed a type, then the behaviour is as previous; the
function looks for a common type among the elements, and coerces them
to that type.  The overall type of the array is derived from the
common element type.

The patch is very invasive (at least compared to any of my previous
patches), but so far I haven't managed to find any broken behaviour.
All regression tests pass, and the regression tests for arrays seem to
be quite comprehensive.  I did add a couple of new tests for the empty
array behaviours, but the rest I've left alone.

I look forward to your comments -- although given the length of the
8.4 patch review queue, that will probably be an exercise in extreme

Major thanks go out to Tom for all his guidance on -hackers while I
developed the patch.


Attachment: empty-array_0.diff
Description: text/plain (15.5 KB)

In response to


pgsql-hackers by date

Next:From: Simon RiggsDate: 2007-11-30 09:17:21
Subject: Re: Release Note Changes
Previous:From: NikhilSDate: 2007-11-30 07:44:53
Subject: Re: BUG #3774: create table like including index doesn't update pg_constraints with primary key

pgsql-patches by date

Next:From: Simon RiggsDate: 2007-11-30 09:54:19
Subject: Re: [PATCHES] Partition: use triggers instead of rules
Previous:From: Jorgen Austvik - Sun NorwayDate: 2007-11-30 08:43:30
Subject: Re: pg_regress: paths in largeobject test

pgsql-general by date

Next:From: Usama DarDate: 2007-11-30 09:26:57
Subject: Re: can't createdb with my new user
Previous:From: Martijn van OosterhoutDate: 2007-11-30 08:50:18
Subject: Re: Record variable not behaving as expected (bug?)

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group