Re: COMMUTATOR doesn't seem to work

D'Arcy Cain <darcy@druid.net>

From: D'Arcy Cain <darcy@druid.net>
To: Florian Pflug <fgp@phlo.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>, pgsql-hackers@postgresql.org
Date: 2012-06-22T15:12:50Z
Lists: pgsql-hackers
On 12-06-22 07:11 AM, Florian Pflug wrote:
> On Jun22, 2012, at 06:32 , D'Arcy Cain wrote:
>> So I have my type working now but I had to create a new C function
>> that take the opposite argument order.  Seems redundant but I could
>> not see a better way.
>
> There isn't. Postgres itself contains a huge number of such functions,
> e.g. for every *lt() (less-than) there's a *gt() (greater-than).

Right but that's not the same thing.  Assuming you meant lt/gte and
lte/gt those still are not self-commutating (SC).  For example, '='
on two ints is SC.  The issue here is that the operator is SC but
the args are different types.  It would be nice if there was a way
to automatically generate code that reverses arguments.  Maybe such
a thing belongs in the CREATE FUNCTION command.

Or, I guess this works and keeps from creating a second C function:

CREATE OR REPLACE FUNCTION eq(chkpass, text)
     RETURNS bool
     STRICT
     AS 'chkpass.so', 'chkpass_eq'
     LANGUAGE 'c'
     RETURNS NULL ON NULL INPUT;

CREATE OR REPLACE FUNCTION eq(text, chkpass)
     RETURNS bool
     AS 'select eq($2, $1);'
     LANGUAGE SQL
     RETURNS NULL ON NULL INPUT;


-- 
D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
IM: darcy@Vex.Net