Re: Do we want a hashset type? - Mailing list pgsql-hackers

From jian he
Subject Re: Do we want a hashset type?
Date
Msg-id CACJufxEqPoic3jSMoBdB5YL7wrQ_Zw8-wrd_WfcK805w+XcsuA@mail.gmail.com
Whole thread Raw
In response to Re: Do we want a hashset type?  ("Joel Jacobson" <joel@compiler.org>)
Responses Re: Do we want a hashset type?
List pgsql-hackers


similar to (int[] || int4) and (int4 || int[])
should we expect
('{1,2}'::int4hashset || 3) == (3 ||  '{1,2}'::int4hashset) == (select hashset_add('{1,2}'::int4hashset,3)); ?

The following is the general idea on how to make it work by looking at similar code....
CREATE OPERATOR || (
    leftarg = int4hashset,
    rightarg = int4,
    function = int4hashset_add,
    commutator = ||
);

CREATE OR REPLACE FUNCTION int4_add_int4hashset(int4, int4hashset)
RETURNS int4hashset
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN $2 || $1;

CREATE OPERATOR || (
    leftarg = int4,
    rightarg = int4hashset,
    function = int4_add_int4hashset,
    commutator = ||
);
while creating an operator. I am not sure how to specify NEGATOR,RESTRICT,JOIN clause.
-----------------------------------------------------------------------------------------------------------------------------
also. I think the following query should return one row only? but now it doesn't.
select hashset_cmp('{1,2}','{2,1}')
union
select hashset_cmp('{1,2}','{1,2,1}')
union
select hashset_cmp('{1,2}','{1,2}');
----------------------------------------------------------------------------------------------------------------------
similar to elem_contained_by_range, range_contains_elem. we should already consider the operator <@ and @>? 

CREATE OR REPLACE FUNCTION elem_contained_by_hashset(int4, int4hashset)
RETURNS bool
LANGUAGE sql
IMMUTABLE PARALLEL SAFE STRICT COST 1
RETURN hashset_contains ($2,$1);

Is the integer contained in the int4hashset?
integer  <@ int4hashset → boolean
1 <@ int4hashset'{1,7}' → t

CREATE OPERATOR <@ (
    leftarg = integer,
    rightarg = int4hashset,
    function = elem_contained_by_hashset
);

int4hashset @> integer → boolean
Does the int4hashset contain the element?
int4hashset'{1,7}' @> 1     → t

CREATE OPERATOR @> (
    leftarg = int4hashset,
    rightarg = integer,
    function = hashset_contains
);
-------------------

pgsql-hackers by date:

Previous
From: Masahiro Ikeda
Date:
Subject: Re: Support to define custom wait events for extensions
Next
From: "Andrey M. Borodin"
Date:
Subject: Add some more corruption error codes to relcache