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 982325289B for ; Sat, 4 Jun 2005 18:01:19 -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 55336-10 for ; Sat, 4 Jun 2005 21:01:18 +0000 (GMT) Received: from joeconway.com (wsip-24-249-201-67.sd.sd.cox.net [24.249.201.67]) by svr1.postgresql.org (Postfix) with ESMTP id 271CE5292D for ; Sat, 4 Jun 2005 18:01:16 -0300 (ADT) Received: from [192.168.4.1] (account jconway [192.168.4.1] verified) by joeconway.com (CommuniGate Pro SMTP 4.1.8) with ESMTP-TLS id 2884130; Sat, 04 Jun 2005 14:58:42 -0700 Message-ID: <42A2169E.9020700@joeconway.com> Date: Sat, 04 Jun 2005 14:01:18 -0700 From: Joe Conway User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.6) Gecko/20050328 Fedora/1.7.6-1.2.5 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Bruce Momjian Cc: Tom Lane , Markus Bertheau , pgsql-sql@postgresql.org Subject: Re: ARRAY() returning NULL instead of ARRAY[] resp. {} References: <200506041752.j54HqGZ12891@candle.pha.pa.us> In-Reply-To: <200506041752.j54HqGZ12891@candle.pha.pa.us> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.054 tagged_above=0 required=5 tests=AWL, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200506/60 X-Sequence-Number: 21661 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