commit d83bdef5428279be0126d20c7f75187b9f4847b0 Author: Joel Jakobsson Date: Fri Jun 16 17:14:49 2023 +0200 Implement || operator to append an int to an int4hashset To avoid need for another C-function, the new SQL helper function int4_add_int4hashset() swaps the arguments of the || so that hashset_add() can be used for both scenarios. Also setseed() in the benchmark.sql for reproducibility. diff --git a/hashset--0.0.1.sql b/hashset--0.0.1.sql index 20d019d..2c2f9e8 100644 --- a/hashset--0.0.1.sql +++ b/hashset--0.0.1.sql @@ -82,6 +82,12 @@ RETURNS bigint AS 'hashset', 'int4hashset_collisions' LANGUAGE C IMMUTABLE; +CREATE OR REPLACE FUNCTION int4_add_int4hashset(int4, int4hashset) +RETURNS int4hashset +AS $$SELECT $2 || $1$$ +LANGUAGE SQL +IMMUTABLE PARALLEL SAFE STRICT COST 1; + /* * Aggregation Functions */ @@ -165,6 +171,20 @@ CREATE OPERATOR <> ( HASHES ); +CREATE OPERATOR || ( + leftarg = int4hashset, + rightarg = int4, + function = hashset_add, + commutator = || +); + +CREATE OPERATOR || ( + leftarg = int4, + rightarg = int4hashset, + function = int4_add_int4hashset, + commutator = || +); + /* * Hashset Hash Operators */ diff --git a/test/expected/basic.out b/test/expected/basic.out index 65be2a6..5690ba4 100644 --- a/test/expected/basic.out +++ b/test/expected/basic.out @@ -220,6 +220,18 @@ SELECT '{1,2,3}'::int4hashset <> '{1,2,3,4}'::int4hashset; -- true t (1 row) +SELECT '{1,2,3}'::int4hashset || 4; + ?column? +----------- + {1,3,2,4} +(1 row) + +SELECT 4 || '{1,2,3}'::int4hashset; + ?column? +----------- + {1,3,2,4} +(1 row) + /* * Hashset Hash Operators */ diff --git a/test/sql/basic.sql b/test/sql/basic.sql index 4882895..464884e 100644 --- a/test/sql/basic.sql +++ b/test/sql/basic.sql @@ -66,6 +66,9 @@ SELECT '{1,2,3}'::int4hashset <> '{4,5,6}'::int4hashset; -- true SELECT '{1,2,3}'::int4hashset <> '{1,2}'::int4hashset; -- true SELECT '{1,2,3}'::int4hashset <> '{1,2,3,4}'::int4hashset; -- true +SELECT '{1,2,3}'::int4hashset || 4; +SELECT 4 || '{1,2,3}'::int4hashset; + /* * Hashset Hash Operators */ diff --git a/test/sql/benchmark.sql b/test/sql/benchmark.sql index 6f825dc..1697451 100644 --- a/test/sql/benchmark.sql +++ b/test/sql/benchmark.sql @@ -58,6 +58,7 @@ $$ LANGUAGE plpgsql; \echo *** Testing 100000 random ints +SELECT setseed(0.12345); \echo - Testing default hash function (Jenkins/lookup3) DO @@ -75,6 +76,7 @@ BEGIN END $$ LANGUAGE plpgsql; +SELECT setseed(0.12345); \echo - Testing Murmurhash32 DO @@ -92,6 +94,7 @@ BEGIN END $$ LANGUAGE plpgsql; +SELECT setseed(0.12345); \echo - Testing naive hash function DO