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

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: Joe Conway <mail(at)joeconway(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Markus Bertheau <twanger(at)bluetwanger(dot)de>, pgsql-sql(at)postgresql(dot)org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: 2005-06-04 22:52:31
Message-ID: 200506042252.j54MqVu14936@candle.pha.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches pgsql-sql

Joe Conway wrote:
> Bruce Momjian wrote:
> > Joe Conway wrote:
> >>
> >>Any thoughts on how this should be handled for an empty 1D array?
> >
> > No one responed to this email, so I will try. Is this the one
> > dimmentional array you were talking about?
> >
> > test=> select array_dims('{}'::integer[]);
> > array_dims
> > ------------
> >
> > (1 row)
>
> In this case, what you get is actually a dimensionless array. Literally,
> you get this:
>
> if (nitems == 0)
> {
> /* Return empty array */
> retval = (ArrayType *) palloc0(sizeof(ArrayType));
> retval->size = sizeof(ArrayType);
> retval->elemtype = element_type;
> PG_RETURN_ARRAYTYPE_P(retval);
> }
>
> I.e. the array structure is allocated, the size is set (which is
> required since arrays are varlena), and the element type is initialized.
> There is no initialization of ndim, ARR_DIMS(), or ARR_LBOUND().
>
> In this case, since there are no dimensions, array_dims() probably does
> the right thing by returning NULL.
>
> > Why is [1:0] wrong to return?
> >
>
> I'm not sure it is wrong -- it just seems a bit strange. The difference
> is that in order to return an empty *one-dimensional* array, ndim,
> ARR_DIMS(), and ARR_LBOUND() are all appropriately set (by the patched
> code). Basically, ndim == 1, ARR_DIMS() is a single element int array (C
> array that is) indicating 0 elements for dimension 1, and ARR_LBOUND()
> is a single element int array indicating a lower bound of 1. This leads
> to the array_dims() return value of [1:0]. The value 1 is unquestionably
> correct for the lower bound index, but what should be reported for the
> upper bound? We can't return [1:1], because that would indicate that we
> have one element.

OK, so '[1:0]' seems correct. How would to specify such an array
manually in a string?

--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2005-06-04 23:01:30 Re: regexp_replace
Previous Message Joe Conway 2005-06-04 21:01:18 Re: ARRAY() returning NULL instead of ARRAY[] resp. {}

Browse pgsql-patches by date

  From Date Subject
Next Message Bruce Momjian 2005-06-04 22:55:30 Re: return_next for plperl (was Re: call for help)
Previous Message Andrew Dunstan 2005-06-04 22:27:10 Re: return_next for plperl (was Re: call for help)

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Fuhr 2005-06-05 00:52:24 Re: Multiple SRF parameters from query
Previous Message Joe Conway 2005-06-04 21:01:18 Re: ARRAY() returning NULL instead of ARRAY[] resp. {}