Re: BUG #7730: intarray representation of empty arrays

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: elein(at)varlena(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #7730: intarray representation of empty arrays
Date: 2013-09-04 19:01:50
Message-ID: 20130904190150.GQ21874@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Wed, Dec 5, 2012 at 12:44:39AM +0000, elein(at)varlena(dot)com wrote:
> The following bug has been logged on the website:
>
> Bug reference: 7730
> Logged by: elein
> Email address: elein(at)varlena(dot)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 <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

Attachment Content-Type Size
array.diff text/x-diff 518 bytes

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2013-09-04 20:45:23 Re: BUG #8436: Heisenbug: random: relation "XXX" does not exist on 3 tables/views
Previous Message Tom Lane 2013-09-04 17:03:29 Re: BUG #8435: PGAdmin backup: obect list missing objects