pg.ddx.io pgsql-docs@postgresql.org mailing list archive
help / color / mirror / Atom feedDuplicates being removed from intarray on subtraction of another intarray
3+ messages / 3 participants
[nested] [flat]
* Duplicates being removed from intarray on subtraction of another intarray
@ 2024-03-18 08:21 PG Doc comments form <noreply@postgresql.org>
0 siblings, 2 replies; 3+ messages in thread
From: PG Doc comments form @ 2024-03-18 08:21 UTC (permalink / raw)
To: pgsql-docs@lists.postgresql.org; +Cc: tbogusiak@gmail.com
The following documentation comment has been logged on the website:
Page: https://www.postgresql.org/docs/16/intarray.html
Description:
Hi,
I recently ran into an unusual issue with the intarray extension where if
you subtract one array from another the result is *also* sorted and
de-duplicated. The documentation does not seem to imply that this should be
the case, stating only that the operator "removes elements of the right
array from the left array" and not that it also de-duplicates and sorts the
result... It seems to only occur when subtracting an array. Is this the
intended behavior?
SELECT '{3,1,1,2,2,2}'::int[] - 1; --> {3,2,2,2} as you would expect
SELECT '{3,1,1,2,2,2}'::int[] - '{1}'::int[]; --> {2,3} instead of
{3,2,2,2}
I have confirmed that I get the same result when using PostgreSQL 9 through
16 on DBFiddle. See here: https://dbfiddle.uk/i-eXKhFR
I am using the official PostgreSQL 16.2 Docker image for Debian (PostgreSQL
16.2 (Debian 16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc
(Debian 12.2.0-14) 12.2.0, 64-bit).
Cheers
Tom
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Duplicates being removed from intarray on subtraction of another intarray
@ 2024-03-18 16:01 Laurenz Albe <laurenz.albe@cybertec.at>
parent: PG Doc comments form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: Laurenz Albe @ 2024-03-18 16:01 UTC (permalink / raw)
To: tbogusiak@gmail.com; pgsql-docs@lists.postgresql.org
On Mon, 2024-03-18 at 08:21 +0000, PG Doc comments form wrote:
> Page: https://www.postgresql.org/docs/16/intarray.html
>
> Hi,
> I recently ran into an unusual issue with the intarray extension where if
> you subtract one array from another the result is *also* sorted and
> de-duplicated. The documentation does not seem to imply that this should be
> the case, stating only that the operator "removes elements of the right
> array from the left array" and not that it also de-duplicates and sorts the
> result... It seems to only occur when subtracting an array. Is this the
> intended behavior?
>
> SELECT '{3,1,1,2,2,2}'::int[] - 1; --> {3,2,2,2} as you would expect
> SELECT '{3,1,1,2,2,2}'::int[] - '{1}'::int[]; --> {2,3} instead of
> {3,2,2,2}
There is no harm in documenting that; I propose the attached patch.
Yours,
Laurenz Albe
Attachments:
[text/x-patch] v1-0001-Documentation-fix-for-intarray-s-operator.patch (967B, ../../396e990fa8ae32bf5c1e3bf2473778e6e296fbfe.camel@cybertec.at/2-v1-0001-Documentation-fix-for-intarray-s-operator.patch)
download | inline diff:
From 96a7fed7be768c047f8a74a4431716da0beb0e28 Mon Sep 17 00:00:00 2001
From: Laurenz Albe <laurenz.albe@cybertec.at>
Date: Mon, 18 Mar 2024 16:59:43 +0100
Subject: [PATCH v1] Documentation fix for intarray's "-" operator
Document that int[] - int[] removes duplicates as well as
elements of the right array.
Discussion: https://postgr.es/m/171075007381.7104.7931589808177869854%40wrigleys.postgresql.org
---
doc/src/sgml/intarray.sgml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/doc/src/sgml/intarray.sgml b/doc/src/sgml/intarray.sgml
index c72d49b01d..ec1d525042 100644
--- a/doc/src/sgml/intarray.sgml
+++ b/doc/src/sgml/intarray.sgml
@@ -312,7 +312,7 @@
<returnvalue>integer[]</returnvalue>
</para>
<para>
- Removes elements of the right array from the left array.
+ Removes elements of the right array and duplicate entries from the left array.
</para></entry>
</row>
--
2.44.0
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Duplicates being removed from intarray on subtraction of another intarray
@ 2024-03-18 16:25 Erik Wienhold <ewie@ewie.name>
parent: PG Doc comments form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: Erik Wienhold @ 2024-03-18 16:25 UTC (permalink / raw)
To: tbogusiak@gmail.com; pgsql-docs@lists.postgresql.org
On 2024-03-18 09:21 +0100, PG Doc comments form wrote:
> The following documentation comment has been logged on the website:
>
> Page: https://www.postgresql.org/docs/16/intarray.html
> Description:
>
> Hi,
> I recently ran into an unusual issue with the intarray extension where if
> you subtract one array from another the result is *also* sorted and
> de-duplicated. The documentation does not seem to imply that this should be
> the case, stating only that the operator "removes elements of the right
> array from the left array" and not that it also de-duplicates and sorts the
> result... It seems to only occur when subtracting an array. Is this the
> intended behavior?
>
> SELECT '{3,1,1,2,2,2}'::int[] - 1; --> {3,2,2,2} as you would expect
> SELECT '{3,1,1,2,2,2}'::int[] - '{1}'::int[]; --> {2,3} instead of
> {3,2,2,2}
>
> I have confirmed that I get the same result when using PostgreSQL 9 through
> 16 on DBFiddle. See here: https://dbfiddle.uk/i-eXKhFR
> I am using the official PostgreSQL 16.2 Docker image for Debian (PostgreSQL
> 16.2 (Debian 16.2-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc
> (Debian 12.2.0-14) 12.2.0, 64-bit).
I don't know if it's intended behavior but it's implemented that way
since its inception (see intset_subtract in [1]). Also the intersection
and union operators behave similarly (sorted result without duplicates).
The attached patch changes the docs to state that the operator computes
the difference.
Operator integer[] - integer OTOH appears to be more in line with
integer[] + integer[] and integer[] + integer in that it doesn't treat
the arguments as sets.
It's unfortunate that both operations use the same operator and not
something like integer[] / integer[] which would be closer to the usual
notation for set difference.
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=181ca96e7a730ba35e973d3361422e6d8...
--
Erik
Attachments:
[text/plain] v1-0001-Document-intarray-subtraction-as-set-difference.patch (0B, ../../sjpfxdbmudnrlw5jgfsfo2bvnx3eirbbsaztrmbrljl7uxeoo4@lzu7vohpkjwo/2-v1-0001-Document-intarray-subtraction-as-set-difference.patch)
download
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2024-03-18 16:25 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 08:21 Duplicates being removed from intarray on subtraction of another intarray PG Doc comments form <noreply@postgresql.org>
2024-03-18 16:01 ` Laurenz Albe <laurenz.albe@cybertec.at>
2024-03-18 16:25 ` Erik Wienhold <ewie@ewie.name>
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