X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 6484B5283D for ; Sat, 4 Jun 2005 19:53:15 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 57942-06 for ; Sat, 4 Jun 2005 22:53:05 +0000 (GMT) Received: from candle.pha.pa.us (candle.pha.pa.us [64.139.89.126]) by svr1.postgresql.org (Postfix) with ESMTP id B78805282A for ; Sat, 4 Jun 2005 19:53:03 -0300 (ADT) Received: (from pgman@localhost) by candle.pha.pa.us (8.11.6/8.11.6) id j54MqVu14936; Sat, 4 Jun 2005 18:52:31 -0400 (EDT) From: Bruce Momjian Message-Id: <200506042252.j54MqVu14936@candle.pha.pa.us> Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {} In-Reply-To: <42A2169E.9020700@joeconway.com> To: Joe Conway Date: Sat, 4 Jun 2005 18:52:31 -0400 (EDT) Cc: Tom Lane , Markus Bertheau , pgsql-sql@postgresql.org X-Mailer: ELM [version 2.4ME+ PL121 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.009 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/61 X-Sequence-Number: 21662 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@candle.pha.pa.us | (610) 359-1001 + If your life is a hard drive, | 13 Roberts Road + Christ can be your backup. | Newtown Square, Pennsylvania 19073