Re: tuple compare involving NULL

From: Kevin Grittner <kgrittn(at)ymail(dot)com>
To: Tobias Florek <postgres(at)ibotty(dot)net>, "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: tuple compare involving NULL
Date: 2014-08-13 14:12:14
Message-ID: 1407939134.78049.YahooMailNeo@web122306.mail.ne1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Tobias Florek <postgres(at)ibotty(dot)net> wrote:

> =# SELECT (2,NULL, 'a')  > (1, NULL, 'b');  # no 1
> =# SELECT (NULL, 2, 'a') > (NULL, 1, 'b');  # no 2
> =# SELECT (NULL, 1, 'b') > (NULL, 2, 'a');  # no 3
>
> does anyone knows a way to modify the queries to return true for
> number 1, true for 2 and false for 3, i.e. treat NULL in a tuple
> such that it compares smaller than anything not NULL?

I think it might make sense for the first one to return true,
although I would want to closely review the spec on that.
Logically, I think it *should be* equivalent to:

SELECT 2 >= 1 AND (2 > 1 OR (NULL >= NULL AND (NULL > NULL or 'a' > 'b')));

... and *that* returns true.  I don't think there's any hope for
the other two without using COALESCE, although you could do that
within the row value constructors:

SELECT (coalesce(NULL, ''), 2, 'a') > (coalesce(NULL, ''), 1, 'b');

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Albe Laurenz 2014-08-13 14:17:12 Re: tuple compare involving NULL
Previous Message Tobias Florek 2014-08-13 10:40:35 tuple compare involving NULL