Re: handling of COUNT(record) vs IS NULL

From: Gregory Stark <stark(at)enterprisedb(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Sam Mason" <sam(at)samason(dot)me(dot)uk>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: handling of COUNT(record) vs IS NULL
Date: 2008-01-28 21:56:27
Message-ID: 87odb5fwc4.fsf@oxford.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> writes:

> Sam Mason <sam(at)samason(dot)me(dot)uk> writes:
>> I've just noticed that the handling of COUNT(record) and (record IS
>> NULL) aren't consistent with my understanding of them. If I run the
>> following query:
>
>> SELECT
>> NULL IS NULL, COUNT( NULL ),
>> (NULL,NULL) IS NULL, COUNT((NULL,NULL));
>
>> The IS NULL checks both return TRUE as I'd expect them to, but the
>> second count doesn't return 0.
>
> THe fourth of those isn't really valid SQL. According to SQL99,
> IS NULL takes a <row value expression> as argument, so it's valid
> to do (NULL,NULL) IS NULL, but COUNT takes a <value expression>.
>
> I don't see anything in the spec suggesting that we are supposed
> to drill down into a rowtype value to see whether all its fields
> are null, in any context other than the IS [NOT] NULL predicate.

Well it's not just in the predicate, we handle it for other strict operators
and functions:

postgres=# select (ROW(null,null)=row(1,2)) IS NULL;
?column?
----------
t
(1 row)

It does seem a bit inconsistent:

postgres=# select count(ROW(null,null)=row(1,2));
count
-------
0
(1 row)

postgres=# select count(ROW(null,null));
count
-------
1
(1 row)

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's PostGIS support!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2008-01-28 21:59:59 Re: handling of COUNT(record) vs IS NULL
Previous Message Vlad 2008-01-28 21:56:20 8.3RC2 vs 8.2.6 testing results