pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Bruce Momjian <pgman@candle.pha.pa.us>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Markus Bertheau <twanger@bluetwanger.de>
Cc: pgsql-sql@postgresql.org
Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
Date: Sat, 04 Jun 2005 14:01:18 -0700
Message-ID: <42A2169E.9020700@joeconway.com> (raw)
In-Reply-To: <200506041752.j54HqGZ12891@candle.pha.pa.us>
References: <200506041752.j54HqGZ12891@candle.pha.pa.us>

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.

Joe



view thread (22+ messages)  latest in thread

Message-ID: <42A2169E.9020700@joeconway.com>
Permalink:  ../42A2169E.9020700@joeconway.com/
Also on:    postgresql.org/message-id/42A2169E.9020700@joeconway.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mail@joeconway.com, pgman@candle.pha.pa.us, tgl@sss.pgh.pa.us, twanger@bluetwanger.de
  Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {}
  In-Reply-To: <42A2169E.9020700@joeconway.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox