Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHIL1-0005ky-7K for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Sep 2013 19:01:59 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VHIL0-0004Mt-Ns for pgsql-bugs@arkaria.postgresql.org; Wed, 04 Sep 2013 19:01:58 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHIL0-0004Mn-0E for pgsql-bugs@postgresql.org; Wed, 04 Sep 2013 19:01:58 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VHIKt-0007SM-D2 for pgsql-bugs@postgresql.org; Wed, 04 Sep 2013 19:01:57 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1VHIKs-0000hr-GX; Wed, 04 Sep 2013 15:01:50 -0400 Date: Wed, 4 Sep 2013 15:01:50 -0400 From: Bruce Momjian To: elein@varlena.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7730: intarray representation of empty arrays Message-ID: <20130904190150.GQ21874@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="+g7M9IMkV8truYOl" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -4.3 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --+g7M9IMkV8truYOl Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Dec 5, 2012 at 12:44:39AM +0000, elein@varlena.com wrote: > The following bug has been logged on the website: > > Bug reference: 7730 > Logged by: elein > Email address: elein@varlena.com > PostgreSQL version: 9.2.1 > Operating system: Linux > Description: > > select NULLIF('{1,2,3}'::integer[] - '{3,2,1}'::integer[], '{}'::integer[]); > This returns an empty array. It should return NULL. > > Per RhodiumToad: the core code represents '{}' as an array with 0 > dimensions, whereas intarray represents it as an array with 1 dimension but > 0 elements > > intarray should use the same standards as the core code if possible. I > peered at the code and don't see anything untoward but did not have time to > spend on it. I just got time to look at this, and it is certainly easier to see when you use array_dims(): SELECT '{1,2,3}'::integer[] - '{3,2,1}'::integer[]; ?column? ---------- {} SELECT array_dims('{1,2,3}'::integer[] - '{3,2,1}'::integer[]); array_dims ------------ [1:0] SELECT array_dims('{}'::integer[]); array_dims ------------ (null) This is part of the larger TODO item of how to handle empty >=1-dimensional empty arrays vs. zero-dimensional empty arrays, which is discussed here: https://wiki.postgresql.org/wiki/Todo#Arrays Improve handling of empty arrays In that thread, no one could find a way to create a 1-dimensional empty array at the SQL level, but thanks to intarray, you found a way. It is natural that intarray, being mostly used for one-dimensional arrays, would return a 1-dimensional empty array. However, besides being inconsistent, as you mentioned, there is also no way to dump/restore one-dimensional empty arrays, which is a larger concern. I have developed the attached patch to force empty intarray results to be zero-dimensional empty arrays, rather than 1-dimensional empty arrays. With this patch, a zero-dimensional empty array is returned: SELECT array_dims('{1,2,3}'::integer[] - '{3,2,1}'::integer[]); array_dims ------------ (null) -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + It's impossible for everything to be true. + --+g7M9IMkV8truYOl Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="array.diff" diff --git a/contrib/intarray/_int_tool.c b/contrib/intarray/_int_tool.c new file mode 100644 index 8635576..fc462b2 *** a/contrib/intarray/_int_tool.c --- b/contrib/intarray/_int_tool.c *************** resize_intArrayType(ArrayType *a, int nu *** 246,251 **** --- 246,258 ---- int nbytes = ARR_DATA_OFFSET(a) + sizeof(int) * num; int i; + /* if no elements, return a zero-dimensional array */ + if (num == 0) + { + ARR_NDIM(a) = 0; + return a; + } + if (num == ARRNELEMS(a)) return a; --+g7M9IMkV8truYOl Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --+g7M9IMkV8truYOl--