Re: NULL safe equality operator

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
Cc: Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: NULL safe equality operator
Date: 2005-11-25 07:55:37
Message-ID: FFEDF611-C5D0-433D-B2AD-ACE44FC2C5EB@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Nov 25, 2005, at 16:43 , Christopher Kings-Lynne wrote:

>> Yeah, I saw your commit. Nice shortcut. Also didn't know you
>> could define operators using SQL functions. Tom's suggestion of
>> NOT (a DISTINCT FROM b) is really cool. Much cleaner in my
>> opinion. I learn a lot from these lists :)
>
> Needs to return 0 or 1 though.

CREATE OR REPLACE FUNCTION null_safe_cmp (ANYELEMENT, ANYELEMENT)
RETURNS INTEGER IMMUTABLE
LANGUAGE SQL AS $$
SELECT CASE
WHEN NOT ($1 IS DISTINCT FROM $2) THEN 1
ELSE 0
END;
$$;

select null_safe_cmp (1,1) as "(1,1)"
, null_safe_cmp (1,0) as "(1,0)"
, null_safe_cmp (1,NULL) as "(1,NULL)"
, null_safe_cmp (NULL,1) as "(NULL,1)"
, null_safe_cmp (NULL::integer,NULL::integer) as "(NULL,NULL)";
(1,1) | (1,0) | (1,NULL) | (NULL,1) | (NULL,NULL)
-------+-------+----------+----------+-------------
1 | 0 | 0 | 0 | 1
(1 row)

test=# select null_safe_cmp (NULL,NULL);
ERROR: could not determine anyarray/anyelement type because input
has type "unknown"
test=# select null_safe_cmp (NULL::integer,NULL::integer);
null_safe_cmp
---------------
1
(1 row)

Same casting problem due to anyelement, of course.

Michael Glaesemann
grzm myrealbox com

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Christopher Kings-Lynne 2005-11-25 08:06:46 Re: NULL safe equality operator
Previous Message Christopher Kings-Lynne 2005-11-25 07:43:44 Re: NULL safe equality operator