|
| 1 | +/* |
| 2 | + If you receive errors about "could not identify an equality operator |
| 3 | + for type json" and you cannot or do not want to convert the column type |
| 4 | + from json to jsonb, you can try applying this file. It adds equality |
| 5 | + operator support for the json type. |
| 6 | +
|
| 7 | + See also https://github.com/mla/pg_sample/issues/20 |
| 8 | +*/ |
| 9 | + |
| 10 | +create function json_lt(json, json) |
| 11 | +returns boolean language sql immutable as $$ |
| 12 | + select $1::jsonb < $2::jsonb |
| 13 | +$$; |
| 14 | + |
| 15 | +create function json_lte(json, json) |
| 16 | +returns boolean language sql immutable as $$ |
| 17 | + select $1::jsonb < $2::jsonb OR $1::jsonb = $2::jsonb |
| 18 | +$$; |
| 19 | + |
| 20 | +create function json_gt(json, json) |
| 21 | +returns boolean language sql immutable as $$ |
| 22 | + select $1::jsonb > $2::jsonb |
| 23 | +$$; |
| 24 | + |
| 25 | +create function json_gte(json, json) |
| 26 | +returns boolean language sql immutable as $$ |
| 27 | + select $1::jsonb > $2::jsonb OR $1::jsonb = $2::jsonb |
| 28 | +$$; |
| 29 | + |
| 30 | +create function json_eq(json, json) |
| 31 | +returns boolean language sql immutable as $$ |
| 32 | + select $1::jsonb = $2::jsonb |
| 33 | +$$; |
| 34 | + |
| 35 | + |
| 36 | +create operator < (leftarg = json, rightarg = json, procedure = json_lt); |
| 37 | +create operator > (leftarg = json, rightarg = json, procedure = json_gt); |
| 38 | +create operator >= (leftarg = json, rightarg = json, procedure = json_gte); |
| 39 | +create operator <= (leftarg = json, rightarg = json, procedure = json_lte); |
| 40 | +create operator = (leftarg = json, rightarg = json, procedure = json_eq); |
| 41 | + |
| 42 | +create function jsoncmp(json, json) |
| 43 | +returns integer language sql immutable as $$ |
| 44 | + select case |
| 45 | + when $1 = $2 then 0 |
| 46 | + when $1 < $2 then -1 |
| 47 | + else 1 |
| 48 | + end |
| 49 | +$$; |
| 50 | + |
| 51 | +create operator class json_ops |
| 52 | + default for type json using btree AS |
| 53 | + operator 1 <, |
| 54 | + operator 2 <=, |
| 55 | + operator 3 =, |
| 56 | + operator 4 >=, |
| 57 | + operator 5 >, |
| 58 | + function 1 jsoncmp(json, json) |
| 59 | +; |
0 commit comments