Re: Testing for null value in arrays

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Michael Dunn <michael(at)2cactus(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Testing for null value in arrays
Date: 2001-01-03 04:33:55
Message-ID: 15152.978496435@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Michael Dunn <michael(at)2cactus(dot)com> writes:
> IF argv_vector = \'{}\'
> THEN
> This evaluates successfully and without errors... but, obviously I am
> testing for not null.

Um ... no ... you are testing for not-empty-array. A zero-element array
is not the same thing as an SQL NULL. Not sure if the distinction is
important for your purposes or not, but there *is* a difference.

> So, by reversing the evaluation

> IF argv_vector != \'{}\'
> the function when executed fails with the following error:
> SELECT sb_event_insert ('{}');
> ERROR: Unable to identify an operator '<>' for types '_text' and
> 'unknown'. You will have to retype this query using an explicit cast.

A moment's poking into pg_operator shows that there is an '=' operator
for text[], but not a '<>' operator:

regression=# select * from pg_operator where oprname = '=' and
regression-# oprleft = (select oid from pg_type where typname = '_text');
oprname | oprowner | oprprec | oprkind | oprisleft | oprcanhash | oprleft | oprright | oprresult | oprcom | oprnegate | oprlsortop | oprrsortop | oprcode | oprrest | oprjoin
---------+----------+---------+---------+-----------+------------+---------+----------+-----------+--------+-----------+------------+------------+----------+---------+-----------
= | 256 | 0 | b | t | f | 1009 | 1009 | 16 | 381 | 0 | 0 | 0 | array_eq | eqsel | eqjoinsel
(1 row)

regression=# select * from pg_operator where oprname = '<>' and
regression-# oprleft = (select oid from pg_type where typname = '_text');
oprname | oprowner | oprprec | oprkind | oprisleft | oprcanhash | oprleft | oprright | oprresult | oprcom | oprnegate | oprlsortop | oprrsortop | oprcode | oprrest | oprjoin
---------+----------+---------+---------+-----------+------------+---------+----------+-----------+--------+-----------+------------+------------+---------+---------+---------
(0 rows)

This is clearly an omission that should be rectified (feel free to
submit a patch!), but it's not exactly catastrophic. Write
"NOT (argv_vector = \'{}\')" if you feel the need to cast your IFs
in that direction.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gordan Bobic 2001-01-03 09:17:35 Re: How passwords can be crypted in postgres?
Previous Message Tom Lane 2001-01-03 04:23:02 Re: RE: RE: Re: MySQL and PostgreSQL speed compare