From: glyn Date: Wed, 28 Jan 2015 10:17:26 +0000 (+0000) Subject: Add missing comparison sql functions file X-Git-Url: https://git.8kb.co.uk/?p=postgresql%2Fpg_jsonb_opx;a=commitdiff_plain;h=4994ca2c88fd138dfa83f850a3a958047a62f858 Add missing comparison sql functions file --- diff --git a/jsonb_opx_sql_comparison.sql b/jsonb_opx_sql_comparison.sql new file mode 100755 index 0000000..4bfebf6 --- /dev/null +++ b/jsonb_opx_sql_comparison.sql @@ -0,0 +1,59 @@ +-- The functions in this script are SQL versions of the C ones for comparison +-- of performance between the two. + +CREATE OR REPLACE FUNCTION jsonb_delete_left(a jsonb, b text) +RETURNS jsonb AS +$BODY$ +SELECT COALESCE( +( +SELECT ('{' || string_agg(to_json(key) || ':' || value, ',') || '}') +FROM jsonb_each(a) +WHERE key <> b +) +, '{}')::jsonb; +$BODY$ +LANGUAGE sql IMMUTABLE STRICT; + +-- + +CREATE OR REPLACE FUNCTION jsonb_delete_left(a jsonb, b text[]) +RETURNS jsonb AS +$BODY$ +SELECT COALESCE( +( +SELECT ('{' || string_agg(to_json(key) || ':' || value, ',') || '}') +FROM jsonb_each(a) +WHERE key <> ALL(b) +) +, '{}')::jsonb; +$BODY$ +LANGUAGE sql IMMUTABLE STRICT; + +-- + +CREATE OR REPLACE FUNCTION jsonb_delete_left(a jsonb, b jsonb) +RETURNS jsonb AS +$BODY$ +SELECT COALESCE( +( +SELECT ('{' || string_agg(to_json(key) || ':' || value, ',') || '}') +FROM jsonb_each(a) +WHERE NOT ('{' || to_json(key) || ':' || value || '}')::jsonb <@ b +) +, '{}')::jsonb; +$BODY$ +LANGUAGE sql IMMUTABLE STRICT; + +-- + +CREATE OR REPLACE FUNCTION jsonb_concat_left (a jsonb, b jsonb) +RETURNS jsonb AS +$BODY$ +SELECT json_object_agg(key, value)::jsonb FROM +( + SELECT * FROM jsonb_each(a) + UNION ALL + SELECT * FROM jsonb_each(b) +) a; +$BODY$ +LANGUAGE sql IMMUTABLE STRICT;