Created
June 11, 2015 10:30
-
-
Save rafaelveloso/67b4d86e7cc799cf359d to your computer and use it in GitHub Desktop.
JSONB Equality Operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION jsonb_equality(left_arg jsonb, right_arg jsonb) | |
RETURNS bool AS | |
$BODY$ | |
SELECT left_arg @> right_arg AND left_arg <@ right_arg; | |
$BODY$ | |
LANGUAGE SQL IMMUTABLE STRICT; | |
CREATE OPERATOR = ( | |
LEFTARG = jsonb, | |
RIGHTARG = jsonb, | |
PROCEDURE = jsonb_equality, | |
NEGATOR = <> | |
); | |
SELECT '{}'::jsonb = '{}'::jsonb | |
$ TRUE | |
SELECT '{"a": "string", "b":2, "c": true}'::jsonb <> '{"c": false, "a": "string", "b":2}'::jsonb | |
$ TRUE | |
SELECT '{"a": "string", "b":2, "c": false}'::jsonb = '{"c": false, "a": "string", "b":2}'::jsonb | |
$ TRUE | |
SELECT '{"c":{"d": true}, "a":2}'::jsonb = '{"a":2, "c":{"d": true}}'::jsonb | |
$ TRUE | |
SELECT '{"c":{"d": true}, "a":2}'::jsonb <> '{"a":2, "c":{"d": true}}'::jsonb | |
$ FALSE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment