Re: IN query operator and NULL values

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Denis Gasparin <denis(at)edistar(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: IN query operator and NULL values
Date: 2008-05-16 16:54:51
Message-ID: D82A2F0A-7041-4BCA-A94D-64678B453165@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On May 16, 2008, at 5:40 PM, Denis Gasparin wrote:

> Hi all.
>
> I have a problem with the IN operator in PostgreSQL 8.2.7. Here it
> is an example that reproduce the problem:
>
> test=# select * from test where b in(1,null);
> a | b
> ---+---
> 1 | 1
>
> In the last resultset, i was expecting two records the one with b =
> 1 and the one with b = null.
> PostgreSQL instead returns only the value with not null values.

Yes, of course it does. NULL means "unknown". Comparing it to
anything results in NULL, as the result is "unknown" again. What
happens is this:

development=> select b, coalesce( (b in (1, null))::text, 'NULL')
from test;
b | coalesce
---+----------
1 | true
2 | NULL
| NULL
(3 rows)

The where clause can only handle true or false (as per the SQL spec),
so it assumes "unknown" means the record wasn't a match.

> I tested the example also in PostgreSQL 8.1 and it works correctly
> (two records).

That looks like a bug in 8.1.

> So the question is: what has changed from 8.1 to 8.2?

I think a bug was fixed ;)

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,482dbc5e927668957138674!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alban Hertroys 2008-05-16 16:58:24 Re: IN query operator and NULL values
Previous Message Robert Fitzpatrick 2008-05-16 16:47:25 Re: Installing debugger