Re: "large" IN/NOT IN subqueries result in query returning wrong data

From: elein <elein(at)varlena(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: George Pavlov <gpavlov(at)mynewplace(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: "large" IN/NOT IN subqueries result in query returning wrong data
Date: 2005-12-28 00:12:51
Message-ID: 20051228001251.GB20674@varlena.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Tue, Dec 27, 2005 at 02:44:51PM -0500, Tom Lane wrote:
> "George Pavlov" <gpavlov(at)mynewplace(dot)com> writes:
> > select count(*) from t2 where t2.name not in (
> > select t1.name from t1 limit 261683)
> > --> 13
> > select count(*) from t2 where t2.name not in (
> > select t1.name from t1 limit 261684)
> > --> 0
>
> > What is so magical about 261683?
>
> Most likely, the 261684'th row of t1 has a NULL value of name.
> Many people find the behavior of NOT IN with nulls unintuitive,
> but it's per SQL spec ...
>
> regards, tom lane

In 8.0 we get:

elein=# select 1 in (NULL, 1, 2);
?column?
----------
t
(1 row)

elein=# select 3 not in (NULL, 1, 2);
?column?
----------

(1 row)

For consistency, either both should return NULL or
both return true/false.

For completeness testing, the following are correct.
Select NULL in/not in any list returns NULL.
elein=# select NULL in (1,2);
?column?
----------

(1 row)

elein=# select NULL not in (1,2);
?column?
----------

(1 row)

elein=# select NULL in (NULL, 1,2);
?column?
----------

(1 row)

elein=# select NULL not in (NULL, 1,2);
?column?
----------

(1 row)

elein
--------------------------------------------------------------
elein(at)varlena(dot)com Varlena, LLC www.varlena.com
(510)655-2584(o) (510)543-6079(c)

PostgreSQL Consulting, Support & Training

PostgreSQL General Bits http://www.varlena.com/GeneralBits/
--------------------------------------------------------------
AIM: varlenallc Yahoo: AElein Skype: varlenallc
--------------------------------------------------------------
I have always depended on the [QA] of strangers.

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2005-12-28 00:25:40 Re: "large" IN/NOT IN subqueries result in query returning wrong data
Previous Message George Pavlov 2005-12-27 20:36:21 Re: "large" IN/NOT IN subqueries result in query returning wrong data