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

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

Tom Lane wrote:
> Markus Bertheau =?UTF-8?Q?=E2=98=AD?= <twanger(at)bluetwanger(dot)de> writes:
>
>>By analogy, array_upper('{}'::TEXT[], 1) should return 0 instead of
>>NULL.
>
> No, that doesn't follow ... we've traditionally considered '{}' to
> denote a zero-dimensional array. A 1-D array of no elements is
> '[1:0]={}', just as Joe shows ... or at least it would be except
> for an overenthusiastic error check:
>
> regression=# select '[1:0]={}' :: int[];
> ERROR: upper bound cannot be less than lower bound
>
> I think this should be a legal boundary case. In general, it should be
> possible to form zero-size arrays of any number of dimensions.
>

I've been playing with exactly this over the weekend. Of course, as
usual, the devil is in the details. For instance, using the above
notation, how would I specify a zero-element 1D array starting at a
lower bound index of 0? The answer following the above pattern would be:

select '[0:-1]={}'::int[];

You could not use '[0:0]={}'::int[], because that would be a one-element
array. I propose the following instead:

regression=# select '[1:]={}' :: int[];
int4
------
{}
(1 row)

regression=# select array_dims('[1:]={}' :: int[]);
array_dims
------------
[1:]
(1 row)

In other words, an missing upper bound indicates zero elements.

Now the next question; what does a 2D zero-element array look like? I
think this makes sense:

regression=# select '[1:2][1:]={{},{}}'::int[];
int4
------
{}
(1 row)

Except (I think) array_out() should probably output something closer to
the input literal. Any thoughts on this?

Joe

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Joe Conway 2005-06-06 15:50:13 Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Previous Message Tom Lane 2005-06-06 15:39:27 Re: regexp_replace

Browse pgsql-patches by date

  From Date Subject
Next Message Joe Conway 2005-06-06 15:50:13 Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Previous Message Tom Lane 2005-06-06 15:39:27 Re: regexp_replace

Browse pgsql-sql by date

  From Date Subject
Next Message Joe Conway 2005-06-06 15:50:13 Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}
Previous Message Markus Bertheau ☭ 2005-06-06 14:54:59 Re: [SQL] ARRAY() returning NULL instead of ARRAY[] resp. {}