Re: Custom Operators Cannot be Found for Composite Type Values

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "David E(dot) Wheeler" <david(at)justatheory(dot)com>
Cc: pgsql-hackers Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Custom Operators Cannot be Found for Composite Type Values
Date: 2012-03-08 04:23:45
Message-ID: 740.1331180625@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

"David E. Wheeler" <david(at)justatheory(dot)com> writes:
> Im doing some development with the new JSON type (actually, Andrews backport to 9.1) and needed to do some very basic equivalence testing. So I created a custom operator:

> CREATE OR REPLACE FUNCTION json_eq(
> json,
> json
> ) RETURNS BOOLEAN LANGUAGE SQL STRICT IMMUTABLE AS $$
> SELECT $1::text = $2::text;
> $$;

> CREATE OPERATOR = (
> LEFTARG = json,
> RIGHTARG = json,
> PROCEDURE = json_eq
> );

> With this in place, these work:

> SELECT '{}'::json = '{}'::json;
> SELECT ROW('{}'::json) = ROW('{}'::json);

> However this does not:

> create type ajson AS (a json);
> SELECT ROW('{}'::json)::ajson = ROW('{}'::json)::ajson;

> That last line emits an error:

> ERROR: could not identify an equality operator for type json

> To which my response was: WTF?

You have not told the system that your operator is equality for the
datatype. It's just a random operator that happens to be named "=".
We try to avoid depending on operator names as cues to semantics.

You need to incorporate it into a default hash or btree opclass before
the composite-type logic will accept it as the thing to use for
comparing that column.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2012-03-08 04:39:21 Re: pg_upgrade --logfile option documentation
Previous Message Tom Lane 2012-03-08 04:07:19 Re: Patch: improve selectivity estimation for IN/NOT IN