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: 37ed240d0711300114w5dfa408m1bfa2cc408cc98e1@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers pgsql-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,
ARRAY[]::int[]).

postgres=# select array[]::int[];
array
-------
{}

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
patience!

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

Regards,
BJ

Attachment Content-Type Size
empty-array_0.diff text/plain 15.5 KB

In response to

Responses

Browse pgsql-general by date

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

Browse pgsql-hackers by date

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

Browse pgsql-patches by date

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