Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 48c41fa

Browse files
committed
Add a 64-bit hash function for type citext.
Amul Sul, reviewed by Hironobu Suzuki Discussion: https://postgr.es/m/CAAJ_b947JjnNr9Cp45iNjSqKf6PA5mCTmKsRwPjows93YwQrmw@mail.gmail.com
1 parent a314c34 commit 48c41fa

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

contrib/citext/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
MODULES = citext
44

55
EXTENSION = citext
6-
DATA = citext--1.4.sql citext--1.4--1.5.sql \
6+
DATA = citext--1.4.sql \
7+
citext--1.5--1.6.sql \
8+
citext--1.4--1.5.sql \
79
citext--1.3--1.4.sql \
810
citext--1.2--1.3.sql citext--1.1--1.2.sql \
911
citext--1.0--1.1.sql citext--unpackaged--1.0.sql

contrib/citext/citext--1.5--1.6.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* contrib/citext/citext--1.5--1.6.sql */
2+
3+
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
4+
\echo Use "ALTER EXTENSION citext UPDATE TO '1.6'" to load this file. \quit
5+
6+
CREATE FUNCTION citext_hash_extended(citext, int8)
7+
RETURNS int8
8+
AS 'MODULE_PATHNAME'
9+
LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
10+
11+
ALTER OPERATOR FAMILY citext_ops USING hash ADD
12+
FUNCTION 2 citext_hash_extended(citext, int8);

contrib/citext/citext.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ citext_hash(PG_FUNCTION_ARGS)
153153
PG_RETURN_DATUM(result);
154154
}
155155

156+
PG_FUNCTION_INFO_V1(citext_hash_extended);
157+
158+
Datum
159+
citext_hash_extended(PG_FUNCTION_ARGS)
160+
{
161+
text *txt = PG_GETARG_TEXT_PP(0);
162+
uint64 seed = PG_GETARG_INT64(1);
163+
char *str;
164+
Datum result;
165+
166+
str = str_tolower(VARDATA_ANY(txt), VARSIZE_ANY_EXHDR(txt), DEFAULT_COLLATION_OID);
167+
result = hash_any_extended((unsigned char *) str, strlen(str), seed);
168+
pfree(str);
169+
170+
/* Avoid leaking memory for toasted inputs */
171+
PG_FREE_IF_COPY(txt, 0);
172+
173+
PG_RETURN_DATUM(result);
174+
}
175+
156176
/*
157177
* ==================
158178
* OPERATOR FUNCTIONS

contrib/citext/citext.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# citext extension
22
comment = 'data type for case-insensitive character strings'
3-
default_version = '1.5'
3+
default_version = '1.6'
44
module_pathname = '$libdir/citext'
55
relocatable = true

contrib/citext/expected/citext.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
222222
t
223223
(1 row)
224224

225+
-- Check the citext_hash() and citext_hash_extended() function explicitly.
226+
SELECT v as value, citext_hash(v)::bit(32) as standard,
227+
citext_hash_extended(v, 0)::bit(32) as extended0,
228+
citext_hash_extended(v, 1)::bit(32) as extended1
229+
FROM (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
230+
('muop28x03'), ('yi3nm0d73')) x(v)
231+
WHERE citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
232+
OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
233+
value | standard | extended0 | extended1
234+
-------+----------+-----------+-----------
235+
(0 rows)
236+
225237
-- Do some tests using a table and index.
226238
CREATE TEMP TABLE try (
227239
name citext PRIMARY KEY

contrib/citext/expected/citext_1.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
222222
t
223223
(1 row)
224224

225+
-- Check the citext_hash() and citext_hash_extended() function explicitly.
226+
SELECT v as value, citext_hash(v)::bit(32) as standard,
227+
citext_hash_extended(v, 0)::bit(32) as extended0,
228+
citext_hash_extended(v, 1)::bit(32) as extended1
229+
FROM (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
230+
('muop28x03'), ('yi3nm0d73')) x(v)
231+
WHERE citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
232+
OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
233+
value | standard | extended0 | extended1
234+
-------+----------+-----------+-----------
235+
(0 rows)
236+
225237
-- Do some tests using a table and index.
226238
CREATE TEMP TABLE try (
227239
name citext PRIMARY KEY

contrib/citext/sql/citext.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ SELECT citext_cmp('aardvark'::citext, 'aardVark'::citext) AS zero;
8989
SELECT citext_cmp('AARDVARK'::citext, 'AARDVARK'::citext) AS zero;
9090
SELECT citext_cmp('B'::citext, 'a'::citext) > 0 AS true;
9191

92+
-- Check the citext_hash() and citext_hash_extended() function explicitly.
93+
SELECT v as value, citext_hash(v)::bit(32) as standard,
94+
citext_hash_extended(v, 0)::bit(32) as extended0,
95+
citext_hash_extended(v, 1)::bit(32) as extended1
96+
FROM (VALUES (NULL::citext), ('PostgreSQL'), ('eIpUEtqmY89'), ('AXKEJBTK'),
97+
('muop28x03'), ('yi3nm0d73')) x(v)
98+
WHERE citext_hash(v)::bit(32) != citext_hash_extended(v, 0)::bit(32)
99+
OR citext_hash(v)::bit(32) = citext_hash_extended(v, 1)::bit(32);
100+
92101
-- Do some tests using a table and index.
93102

94103
CREATE TEMP TABLE try (

0 commit comments

Comments
 (0)