pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
!= ANY(array) does not behave as expected
5+ messages / 4 participants
[nested] [flat]

* != ANY(array) does not behave as expected
@ 2018-12-07 12:39  Chris Wilson <chris.wilson@cantabcapital.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Chris Wilson @ 2018-12-07 12:39 UTC (permalink / raw)
  To: pgsql-general

Dear Postgres users,

I like using ANY(array) instead of IN (...), as we can pass the array as binary data, avoiding the need to render its contents (which might be integers) into a SQL string, for Postgres to parse them back into integers again, and it also works with an empty list<https://stackoverflow.com/questions/23523147/sqlalchemy-and-empty-in-clause/45345041#45345041;. For example:

create table foo (id integer);
insert into foo (id) values (1), (2), (3);
select * from foo where id IN (1, 2); /* returns rows 1 and 2 */
select * from foo where id = ANY (ARRAY[1, 2]); /* returns rows 1 and 2 */

However, if we try to invert it by using the != operator, then we get unexpected results:

select * from foo where id NOT IN (1, 2); /* returns row 3 only, as expected */
select * from foo where id != ANY (ARRAY[1, 2]); /* returns all rows, unexpected */

I don't really understand why this is the case. I guess that perhaps an ANY-object has an equality operator that tests for membership of the array, but its inequality operator does something different. I don't understand what it's doing at all, or how it might be useful. Could anyone enlighten me?

I did find a workaround that may be useful to others (perhaps something to add to the documentation?):

select * from foo where NOT(id = ANY (ARRAY[1, 2])); /* returns row 3 only, as expected */

In a search for a solution or workaround, to pass arrays of IDs to exclude into queries, I noted that the manual says<https://www.postgresql.org/docs/current/functions-subquery.html#FUNCTIONS-SUBQUERY-NOTIN;:

expression NOT IN (subquery)
The right-hand side is a parenthesized subquery, which must return exactly one column.
I tried to pass an expression that returns one column, but that failed:

select * from foo where id NOT IN (unnest(ARRAY[1, 2])); /* fails with "set-returning functions are not allowed in WHERE" */

But if I use a real subquery then it succeeds:

select * from foo where id NOT IN (SELECT * FROM unnest(ARRAY[1, 2])) /* returns row 3 only */

If the current behaviour of != ANY (ARRAY...) is not useful, then is there any support for (or opposition to) fixing it? And is it a bug that one can't use unnest in a NOT IN expression in the WHERE clause?

Thanks, Chris.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: != ANY(array) does not behave as expected
@ 2018-12-07 12:48  Thomas Kellerer <spam_eater@gmx.net>
  parent: Chris Wilson <chris.wilson@cantabcapital.com>
  1 sibling, 1 reply; 5+ messages in thread

From: Thomas Kellerer @ 2018-12-07 12:48 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Chris Wilson schrieb am 07.12.2018 um 13:39:
> However, if we try to invert it by using the != operator, then we get unexpected results:
> 
> select * from foo where id NOT IN (1, 2); /* returns row 3 only, as expected */
> select * from foo where id != ANY (ARRAY[1, 2]); /* returns all rows, unexpected */

id <> ANY (...) means: return "true" if at least one of the elements is not equal to the value on the left side. 

What you are looking for is the ALL operator

  select * 
  from foo 
  where id <> ALL (ARRAY[1, 2]); 

That is essentially the equivalent to NOT IN





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: != ANY(array) does not behave as expected
@ 2018-12-07 13:20  Thomas Kellerer <spam_eater@gmx.net>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 5+ messages in thread

From: Thomas Kellerer @ 2018-12-07 13:20 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

Thomas Kellerer schrieb am 07.12.2018 um 13:48:
> Chris Wilson schrieb am 07.12.2018 um 13:39:
>> However, if we try to invert it by using the != operator, then we get unexpected results:
>>
>> select * from foo where id NOT IN (1, 2); /* returns row 3 only, as expected */
>> select * from foo where id != ANY (ARRAY[1, 2]); /* returns all rows, unexpected */
> 
> id <> ANY (...) means: return "true" if at least one of the elements is not equal to the value on the left side. 
> 
> What you are looking for is the ALL operator
> 
>   select * 
>   from foo 
>   where id <> ALL (ARRAY[1, 2]); 
> 
> That is essentially the equivalent to NOT IN

See here for a longer explanation:

   https://stackoverflow.com/a/10675636






^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: != ANY(array) does not behave as expected
@ 2018-12-07 18:40  Ken Tanzer <ken.tanzer@gmail.com>
  parent: Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 0 replies; 5+ messages in thread

From: Ken Tanzer @ 2018-12-07 18:40 UTC (permalink / raw)
  To: spam_eater@gmx.net; +Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>

On Fri, Dec 7, 2018 at 5:21 AM Thomas Kellerer <spam_eater@gmx.net> wrote:

> Thomas Kellerer schrieb am 07.12.2018 um 13:48:
> > Chris Wilson schrieb am 07.12.2018 um 13:39:
> >> However, if we try to invert it by using the != operator, then we get
> unexpected results:
> >>
> >> select * from foo where id NOT IN (1, 2); /* returns row 3 only, as
> expected */
> >> select * from foo where id != ANY (ARRAY[1, 2]); /* returns all rows,
> unexpected */
> >

> id <> ANY (...) means: return "true" if at least one of the elements is
> not equal to the value on the left side.
> >
> > What you are looking for is the ALL operator
> >
> >   select *
> >   from foo
> >   where id <> ALL (ARRAY[1, 2]);
> >
> > That is essentially the equivalent to NOT IN
>
 See here for a longer explanation:

>
>    https://stackoverflow.com/a/10675636
>
>
>
You can also just do this:

select * from foo where NOT id = ANY (ARRAY[1, 2]);

Cheers,
Ken
-- 
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken.tanzer@agency-software.org
(253) 245-3801

Subscribe to the mailing list
<agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: != ANY(array) does not behave as expected
@ 2018-12-07 18:49  Andrew Gierth <andrew@tao11.riddles.org.uk>
  parent: Chris Wilson <chris.wilson@cantabcapital.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Andrew Gierth @ 2018-12-07 18:49 UTC (permalink / raw)
  To: Chris Wilson <chris.wilson@cantabcapital.com>; +Cc: pgsql-general

>>>>> "Chris" == Chris Wilson <chris.wilson@cantabcapital.com> writes:

 Chris> However, if we try to invert it by using the != operator, then
 Chris> we get unexpected results:

Mr. De Morgan would like a word.
https://en.wikipedia.org/wiki/De_Morgan%27s_laws

In short, if you have a condition of the form (a OR b) and you want to
negate it, then you find that:

    NOT (a OR b)  is equivalent to  (NOT a) AND (NOT b)

Since  x = ANY (array[1,2])  is equivalent to (x = 1) OR (x = 2), then
the negation would be (x != 1) AND (x != 2), not OR.

Which can be conveniently expressed as  x != ALL (array[1,2]).

So just as you interchange AND and OR when inverting the sense of a
condition, you also interchange ALL and ANY for exactly the same
reasons.

 Chris> expression NOT IN (subquery)

https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_NOT_IN

 Chris> And is it a bug that one can't use unnest in a NOT IN expression
 Chris> in the WHERE clause?

No.

-- 
Andrew (irc:RhodiumToad)




^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2018-12-07 18:49 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-07 12:39 != ANY(array) does not behave as expected Chris Wilson <chris.wilson@cantabcapital.com>
2018-12-07 12:48 ` Thomas Kellerer <spam_eater@gmx.net>
2018-12-07 13:20   ` Thomas Kellerer <spam_eater@gmx.net>
2018-12-07 18:40     ` Ken Tanzer <ken.tanzer@gmail.com>
2018-12-07 18:49 ` Andrew Gierth <andrew@tao11.riddles.org.uk>

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