]> granicus.if.org Git - postgresql/commitdiff
Add a 64-bit hash function for type citext.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 23 Nov 2018 18:24:45 +0000 (13:24 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 23 Nov 2018 18:24:45 +0000 (13:24 -0500)
Amul Sul, reviewed by Hironobu Suzuki

Discussion: https://postgr.es/m/CAAJ_b947JjnNr9Cp45iNjSqKf6PA5mCTmKsRwPjows93YwQrmw@mail.gmail.com

contrib/citext/Makefile
contrib/citext/citext--1.5--1.6.sql [new file with mode: 0644]
contrib/citext/citext.c
contrib/citext/citext.control
contrib/citext/expected/citext.out
contrib/citext/expected/citext_1.out
contrib/citext/sql/citext.sql

index e32a7de946444551a4051f7937341eb912fa78c2..e37dcf9b58468d93a57b2c6e0591f99c035afd58 100644 (file)
@@ -3,7 +3,9 @@
 MODULES = citext
 
 EXTENSION = citext
-DATA = citext--1.4.sql citext--1.4--1.5.sql \
+DATA = citext--1.4.sql \
+       citext--1.5--1.6.sql \
+       citext--1.4--1.5.sql \
        citext--1.3--1.4.sql \
        citext--1.2--1.3.sql citext--1.1--1.2.sql \
        citext--1.0--1.1.sql citext--unpackaged--1.0.sql
diff --git a/contrib/citext/citext--1.5--1.6.sql b/contrib/citext/citext--1.5--1.6.sql
new file mode 100644 (file)
index 0000000..3226898
--- /dev/null
@@ -0,0 +1,12 @@
+/* contrib/citext/citext--1.5--1.6.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION citext UPDATE TO '1.6'" to load this file. \quit
+
+CREATE FUNCTION citext_hash_extended(citext, int8)
+RETURNS int8
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+ALTER OPERATOR FAMILY citext_ops USING hash ADD
+    FUNCTION    2   citext_hash_extended(citext, int8);
index 2c0e48e2bc10f38cdb68d55c800b426129975c9e..24ceeb11fc248f47e0eb6a857361d7443fbc0a72 100644 (file)
@@ -153,6 +153,26 @@ citext_hash(PG_FUNCTION_ARGS)
        PG_RETURN_DATUM(result);
 }
 
+PG_FUNCTION_INFO_V1(citext_hash_extended);
+
+Datum
+citext_hash_extended(PG_FUNCTION_ARGS)
+{
+       text       *txt = PG_GETARG_TEXT_PP(0);
+       uint64          seed = PG_GETARG_INT64(1);
+       char       *str;
+       Datum           result;
+
+       str = str_tolower(VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt), DEFAULT_COLLATION_OID);
+       result = hash_any_extended((unsigned char *) str, strlen(str), seed);
+       pfree(str);
+
+       /* Avoid leaking memory for toasted inputs */
+       PG_FREE_IF_COPY(txt, 0);
+
+       PG_RETURN_DATUM(result);
+}
+
 /*
  *             ==================
  *             OPERATOR FUNCTIONS
index 4cd6e09331f9da463a78d7f0eb9556aebc7d599c..a872a3f012b06d661ffe7b7c06320d555ae7afef 100644 (file)
@@ -1,5 +1,5 @@
 # citext extension
 comment = 'data type for case-insensitive character strings'
-default_version = '1.5'
+default_version = '1.6'
 module_pathname = '$libdir/citext'
 relocatable = true
index 99365c57b01bd61628e83382b4165dbc1472d88f..20890db85916a28e43d63347fccef9dedc664ad4 100644 (file)
@@ -222,6 +222,18 @@ SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
  t
 (1 row)
 
+-- Check the citext_hash() and citext_hash_extended() function explicitly.
+SELECT v as value, citext_hash(v)::bit(32) as standard,
+       citext_hash_extended(v, 0)::bit(32) as extended0,
+       citext_hash_extended(v, 1)::bit(32) as extended1
+FROM   (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
+       ('muop28x03'), ('yi3nm0d73')) x(v)
+WHERE  citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
+       OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
+ value | standard | extended0 | extended1 
+-------+----------+-----------+-----------
+(0 rows)
+
 -- Do some tests using a table and index.
 CREATE TEMP TABLE try (
    name citext PRIMARY KEY
index 8aac72e2268b48b3475c47ef36a61e0a54bcdbff..755baad8e2182e02d7c5815363ed9ae4d27f99f7 100644 (file)
@@ -222,6 +222,18 @@ SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
  t
 (1 row)
 
+-- Check the citext_hash() and citext_hash_extended() function explicitly.
+SELECT v as value, citext_hash(v)::bit(32) as standard,
+       citext_hash_extended(v, 0)::bit(32) as extended0,
+       citext_hash_extended(v, 1)::bit(32) as extended1
+FROM   (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
+       ('muop28x03'), ('yi3nm0d73')) x(v)
+WHERE  citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
+       OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
+ value | standard | extended0 | extended1 
+-------+----------+-----------+-----------
+(0 rows)
+
 -- Do some tests using a table and index.
 CREATE TEMP TABLE try (
    name citext PRIMARY KEY
index 2732be436dc22c4ae656898b12a4d5539576ed65..0cc909eb52ae91b2cb360bca30ab5148d15c7891 100644 (file)
@@ -89,6 +89,15 @@ SELECT citext_cmp('aardvark'::citext, 'aardVark'::citext) AS zero;
 SELECT citext_cmp('AARDVARK'::citext, 'AARDVARK'::citext) AS zero;
 SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
 
+-- Check the citext_hash() and citext_hash_extended() function explicitly.
+SELECT v as value, citext_hash(v)::bit(32) as standard,
+       citext_hash_extended(v, 0)::bit(32) as extended0,
+       citext_hash_extended(v, 1)::bit(32) as extended1
+FROM   (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
+       ('muop28x03'), ('yi3nm0d73')) x(v)
+WHERE  citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
+       OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
+
 -- Do some tests using a table and index.
 
 CREATE TEMP TABLE try (