Re: Do we want a hashset type?

From: "Joel Jacobson" <joel(at)compiler(dot)org>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: Do we want a hashset type?
Date: 2023-06-27 20:25:52
Message-ID: b9562b5e-1dd7-4c18-ab97-b1924e213110@app.fastmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Jun 27, 2023, at 10:26, Joel Jacobson wrote:
> Attachments:
> * hashset-0.0.1-b7e5614-full.patch
> * hashset-0.0.1-b7e5614-incremental.patch

To help verify that the semantics, I thought it might be helpful to provide
a comprehensive set of examples that tries to cover all different ways of varying
the arguments to the functions.

Please let me know if you find any possible errors or if you think it looks good.

SELECT NULL::int4hashset;
int4hashset
-------------

(1 row)

SELECT '{}'::int4hashset;
int4hashset
-------------
{}
(1 row)

SELECT int4hashset();
int4hashset
-------------
{}
(1 row)

SELECT '{NULL}'::int4hashset;
int4hashset
-------------
{NULL}
(1 row)

SELECT '{NULL,NULL}'::int4hashset;
int4hashset
-------------
{NULL}
(1 row)

SELECT '{1,3,2,NULL,2,NULL,3,1}'::int4hashset;
int4hashset
--------------
{2,1,3,NULL}
(1 row)

SELECT hashset_add(NULL, NULL);
hashset_add
-------------
{NULL}
(1 row)

SELECT hashset_add(NULL, 1);
hashset_add
-------------
{1}
(1 row)

SELECT hashset_add('{}', 1);
hashset_add
-------------
{1}
(1 row)

SELECT hashset_add('{NULL}', 1);
hashset_add
-------------
{1,NULL}
(1 row)

SELECT hashset_add('{1}', 1);
hashset_add
-------------
{1}
(1 row)

SELECT hashset_add('{1}', 2);
hashset_add
-------------
{1,2}
(1 row)

SELECT hashset_add('{1}', NULL);
hashset_add
-------------
{1,NULL}
(1 row)

SELECT hashset_contains(NULL, NULL);
hashset_contains
------------------

(1 row)

SELECT hashset_contains('{}', NULL);
hashset_contains
------------------
f
(1 row)

SELECT hashset_contains('{NULL}', NULL);
hashset_contains
------------------

(1 row)

SELECT hashset_contains('{1}', 1);
hashset_contains
------------------
t
(1 row)

SELECT hashset_contains('{1,NULL}', 1);
hashset_contains
------------------
t
(1 row)

SELECT hashset_contains('{1}', 2);
hashset_contains
------------------
f
(1 row)

SELECT hashset_contains('{1,NULL}', 2);
hashset_contains
------------------

(1 row)

SELECT hashset_to_array(NULL);
hashset_to_array
------------------

(1 row)

SELECT hashset_to_array('{}');
hashset_to_array
------------------
{}
(1 row)

SELECT hashset_to_array('{NULL}');
hashset_to_array
------------------
{NULL}
(1 row)

SELECT hashset_to_array('{3,1,NULL,2}');
hashset_to_array
------------------
{1,3,2,NULL}
(1 row)

SELECT hashset_to_sorted_array(NULL);
hashset_to_sorted_array
-------------------------

(1 row)

SELECT hashset_to_sorted_array('{}');
hashset_to_sorted_array
-------------------------
{}
(1 row)

SELECT hashset_to_sorted_array('{NULL}');
hashset_to_sorted_array
-------------------------
{NULL}
(1 row)

SELECT hashset_to_sorted_array('{3,1,NULL,2}');
hashset_to_sorted_array
-------------------------
{1,2,3,NULL}
(1 row)

SELECT hashset_cardinality(NULL);
hashset_cardinality
---------------------

(1 row)

SELECT hashset_cardinality('{}');
hashset_cardinality
---------------------
0
(1 row)

SELECT hashset_cardinality('{NULL}');
hashset_cardinality
---------------------
1
(1 row)

SELECT hashset_cardinality('{NULL,NULL}');
hashset_cardinality
---------------------
1
(1 row)

SELECT hashset_cardinality('{1}');
hashset_cardinality
---------------------
1
(1 row)

SELECT hashset_cardinality('{1,1}');
hashset_cardinality
---------------------
1
(1 row)

SELECT hashset_cardinality('{1,2}');
hashset_cardinality
---------------------
2
(1 row)

SELECT hashset_cardinality('{1,2,NULL}');
hashset_cardinality
---------------------
3
(1 row)

SELECT hashset_union(NULL, NULL);
hashset_union
---------------

(1 row)

SELECT hashset_union(NULL, '{}');
hashset_union
---------------

(1 row)

SELECT hashset_union('{}', NULL);
hashset_union
---------------

(1 row)

SELECT hashset_union('{}', '{}');
hashset_union
---------------
{}
(1 row)

SELECT hashset_union('{}', '{NULL}');
hashset_union
---------------
{NULL}
(1 row)

SELECT hashset_union('{NULL}', '{}');
hashset_union
---------------
{NULL}
(1 row)

SELECT hashset_union('{NULL}', '{NULL}');
hashset_union
---------------
{NULL}
(1 row)

SELECT hashset_union('{}', '{1}');
hashset_union
---------------
{1}
(1 row)

SELECT hashset_union('{1}', '{}');
hashset_union
---------------
{1}
(1 row)

SELECT hashset_union('{1}', '{1}');
hashset_union
---------------
{1}
(1 row)

SELECT hashset_union('{1}', NULL);
hashset_union
---------------

(1 row)

SELECT hashset_union(NULL, '{1}');
hashset_union
---------------

(1 row)

SELECT hashset_union('{1}', '{NULL}');
hashset_union
---------------
{1,NULL}
(1 row)

SELECT hashset_union('{NULL}', '{1}');
hashset_union
---------------
{1,NULL}
(1 row)

SELECT hashset_union('{1}', '{2}');
hashset_union
---------------
{1,2}
(1 row)

SELECT hashset_union('{1,2}', '{2,3}');
hashset_union
---------------
{3,1,2}
(1 row)

SELECT hashset_intersection(NULL, NULL);
hashset_intersection
----------------------

(1 row)

SELECT hashset_intersection(NULL, '{}');
hashset_intersection
----------------------

(1 row)

SELECT hashset_intersection('{}', NULL);
hashset_intersection
----------------------

(1 row)

SELECT hashset_intersection('{}', '{}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{}', '{NULL}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{NULL}', '{}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{NULL}', '{NULL}');
hashset_intersection
----------------------
{NULL}
(1 row)

SELECT hashset_intersection('{}', '{1}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{1}', '{}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{1}', '{1}');
hashset_intersection
----------------------
{1}
(1 row)

SELECT hashset_intersection('{1}', NULL);
hashset_intersection
----------------------

(1 row)

SELECT hashset_intersection(NULL, '{1}');
hashset_intersection
----------------------

(1 row)

SELECT hashset_intersection('{1}', '{NULL}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{NULL}', '{1}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{1}', '{2}');
hashset_intersection
----------------------
{}
(1 row)

SELECT hashset_intersection('{1,2}', '{2,3}');
hashset_intersection
----------------------
{2}
(1 row)

SELECT hashset_difference(NULL, NULL);
hashset_difference
--------------------

(1 row)

SELECT hashset_difference(NULL, '{}');
hashset_difference
--------------------

(1 row)

SELECT hashset_difference('{}', NULL);
hashset_difference
--------------------

(1 row)

SELECT hashset_difference('{}', '{}');
hashset_difference
--------------------
{}
(1 row)

SELECT hashset_difference('{}', '{NULL}');
hashset_difference
--------------------
{}
(1 row)

SELECT hashset_difference('{NULL}', '{}');
hashset_difference
--------------------
{NULL}
(1 row)

SELECT hashset_difference('{NULL}', '{NULL}');
hashset_difference
--------------------
{}
(1 row)

SELECT hashset_difference('{}', '{1}');
hashset_difference
--------------------
{}
(1 row)

SELECT hashset_difference('{1}', '{}');
hashset_difference
--------------------
{1}
(1 row)

SELECT hashset_difference('{1}', '{1}');
hashset_difference
--------------------
{}
(1 row)

SELECT hashset_difference('{1}', NULL);
hashset_difference
--------------------

(1 row)

SELECT hashset_difference(NULL, '{1}');
hashset_difference
--------------------

(1 row)

SELECT hashset_difference('{1}', '{NULL}');
hashset_difference
--------------------
{1}
(1 row)

SELECT hashset_difference('{NULL}', '{1}');
hashset_difference
--------------------
{NULL}
(1 row)

SELECT hashset_difference('{1}', '{2}');
hashset_difference
--------------------
{1}
(1 row)

SELECT hashset_difference('{1,2}', '{2,3}');
hashset_difference
--------------------
{1}
(1 row)

SELECT hashset_symmetric_difference(NULL, NULL);
hashset_symmetric_difference
------------------------------

(1 row)

SELECT hashset_symmetric_difference(NULL, '{}');
hashset_symmetric_difference
------------------------------

(1 row)

SELECT hashset_symmetric_difference('{}', NULL);
hashset_symmetric_difference
------------------------------

(1 row)

SELECT hashset_symmetric_difference('{}', '{}');
hashset_symmetric_difference
------------------------------
{}
(1 row)

SELECT hashset_symmetric_difference('{}', '{NULL}');
hashset_symmetric_difference
------------------------------
{NULL}
(1 row)

SELECT hashset_symmetric_difference('{NULL}', '{}');
hashset_symmetric_difference
------------------------------
{NULL}
(1 row)

SELECT hashset_symmetric_difference('{NULL}', '{NULL}');
hashset_symmetric_difference
------------------------------
{}
(1 row)

SELECT hashset_symmetric_difference('{}', '{1}');
hashset_symmetric_difference
------------------------------
{1}
(1 row)

SELECT hashset_symmetric_difference('{1}', '{}');
hashset_symmetric_difference
------------------------------
{1}
(1 row)

SELECT hashset_symmetric_difference('{1}', '{1}');
hashset_symmetric_difference
------------------------------
{}
(1 row)

SELECT hashset_symmetric_difference('{1}', NULL);
hashset_symmetric_difference
------------------------------

(1 row)

SELECT hashset_symmetric_difference(NULL, '{1}');
hashset_symmetric_difference
------------------------------

(1 row)

SELECT hashset_symmetric_difference('{1}', '{NULL}');
hashset_symmetric_difference
------------------------------
{1,NULL}
(1 row)

SELECT hashset_symmetric_difference('{NULL}', '{1}');
hashset_symmetric_difference
------------------------------
{1,NULL}
(1 row)

SELECT hashset_symmetric_difference('{1}', '{2}');
hashset_symmetric_difference
------------------------------
{1,2}
(1 row)

SELECT hashset_symmetric_difference('{1,2}', '{2,3}');
hashset_symmetric_difference
------------------------------
{1,3}
(1 row)

SELECT hashset_eq(NULL, NULL);
hashset_eq
------------

(1 row)

SELECT hashset_eq(NULL, '{}');
hashset_eq
------------

(1 row)

SELECT hashset_eq('{}', NULL);
hashset_eq
------------

(1 row)

SELECT hashset_eq('{}', '{}');
hashset_eq
------------
t
(1 row)

SELECT hashset_eq('{}', '{NULL}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{NULL}', '{}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{NULL}', '{NULL}');
hashset_eq
------------
t
(1 row)

SELECT hashset_eq('{}', '{1}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{1}', '{}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{1}', '{1}');
hashset_eq
------------
t
(1 row)

SELECT hashset_eq('{1}', NULL);
hashset_eq
------------

(1 row)

SELECT hashset_eq(NULL, '{1}');
hashset_eq
------------

(1 row)

SELECT hashset_eq('{1}', '{NULL}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{NULL}', '{1}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{1}', '{2}');
hashset_eq
------------
f
(1 row)

SELECT hashset_eq('{1,2}', '{2,3}');
hashset_eq
------------
f
(1 row)

SELECT hashset_ne(NULL, NULL);
hashset_ne
------------

(1 row)

SELECT hashset_ne(NULL, '{}');
hashset_ne
------------

(1 row)

SELECT hashset_ne('{}', NULL);
hashset_ne
------------

(1 row)

SELECT hashset_ne('{}', '{}');
hashset_ne
------------
f
(1 row)

SELECT hashset_ne('{}', '{NULL}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{NULL}', '{}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{NULL}', '{NULL}');
hashset_ne
------------
f
(1 row)

SELECT hashset_ne('{}', '{1}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{1}', '{}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{1}', '{1}');
hashset_ne
------------
f
(1 row)

SELECT hashset_ne('{1}', NULL);
hashset_ne
------------

(1 row)

SELECT hashset_ne(NULL, '{1}');
hashset_ne
------------

(1 row)

SELECT hashset_ne('{1}', '{NULL}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{NULL}', '{1}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{1}', '{2}');
hashset_ne
------------
t
(1 row)

SELECT hashset_ne('{1,2}', '{2,3}');
hashset_ne
------------
t
(1 row)

/Joel

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Roberto Mello 2023-06-27 21:44:28 Re: PostgreSQL 16 Beta 2 release announcement draft
Previous Message Peter Geoghegan 2023-06-27 19:50:37 Re: POC, WIP: OR-clause support for indexes