Re: Testing for null value in arrays - Mailing list pgsql-general

From Tom Lane
Subject Re: Testing for null value in arrays
Date
Msg-id 15152.978496435@sss.pgh.pa.us
Whole thread Raw
In response to Testing for null value in arrays  (Michael Dunn <michael@2cactus.com>)
List pgsql-general
Michael Dunn <michael@2cactus.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

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: RE: RE: Re: MySQL and PostgreSQL speed compare
Next
From: "Gordan Bobic"
Date:
Subject: Re: How passwords can be crypted in postgres?