-
Notifications
You must be signed in to change notification settings - Fork 40
/
pg_similarity--1.0.sql
378 lines (317 loc) · 9.09 KB
/
pg_similarity--1.0.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_similarity" to load this file. \quit
-- Block
CREATE FUNCTION block (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'block'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION block_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'block_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~++ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = block_op,
COMMUTATOR = '~++',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Cosine
CREATE FUNCTION cosine (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'cosine'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION cosine_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'cosine_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~## (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = cosine_op,
COMMUTATOR = '~##',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Dice
CREATE FUNCTION dice (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'dice'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION dice_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'dice_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~-~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = dice_op,
COMMUTATOR = '~-~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Euclidean
CREATE FUNCTION euclidean (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'euclidean'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION euclidean_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'euclidean_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~!! (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = euclidean_op,
COMMUTATOR = '~!!',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Hamming
CREATE FUNCTION hamming (varbit, varbit) RETURNS float8
AS 'MODULE_PATHNAME','hamming'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION hamming_op (varbit, varbit) RETURNS bool
AS 'MODULE_PATHNAME', 'hamming_op'
LANGUAGE C STABLE STRICT;
CREATE FUNCTION hamming_text (text, text) RETURNS float8
AS 'MODULE_PATHNAME','hamming_text'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION hamming_text_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'hamming_text_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~@~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = hamming_text_op,
COMMUTATOR = '~@~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Jaccard
CREATE FUNCTION jaccard (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'jaccard'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION jaccard_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'jaccard_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~?? (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = jaccard_op,
COMMUTATOR = '~??',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Jaro
CREATE FUNCTION jaro (text, text) RETURNS float8
AS 'MODULE_PATHNAME','jaro'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION jaro_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'jaro_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~%% (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = jaro_op,
COMMUTATOR = '~%%',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Jaro-Winkler
CREATE FUNCTION jarowinkler (text, text) RETURNS float8
AS 'MODULE_PATHNAME','jarowinkler'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION jarowinkler_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'jarowinkler_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~@@ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = jarowinkler_op,
COMMUTATOR = '~@@',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Levenshtein
CREATE FUNCTION lev (text, text) RETURNS float8
AS 'MODULE_PATHNAME','lev'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION lev_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'lev_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~== (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = lev_op,
COMMUTATOR = '~==',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Those functions are here just for academic purposes
--CREATE FUNCTION levslow (text, text) RETURNS float8
--AS 'MODULE_PATHNAME','levslow'
--LANGUAGE C IMMUTABLE STRICT;
--CREATE FUNCTION levslow_op (text, text) RETURNS bool
--AS 'MODULE_PATHNAME', 'levslow_op'
--LANGUAGE C STABLE STRICT;
--CREATE OPERATOR ~@@ (
-- LEFTARG = text,
-- RIGHTARG = text,
-- PROCEDURE = levslow_op,
-- COMMUTATOR = '~@@',
-- RESTRICT = contsel,
-- JOIN = contjoinsel
--);
-- Matching Coefficient
CREATE FUNCTION matchingcoefficient (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'matchingcoefficient'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION matchingcoefficient_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'matchingcoefficient_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~^^ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = matchingcoefficient_op,
COMMUTATOR = '~^^',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Monge-Elkan
CREATE FUNCTION mongeelkan (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'mongeelkan'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION mongeelkan_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'mongeelkan_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~|| (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = mongeelkan_op,
COMMUTATOR = '~||',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Needleman-Wunsch
CREATE FUNCTION needlemanwunsch (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'needlemanwunsch'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION needlemanwunsch_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'needlemanwunsch_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~#~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = needlemanwunsch_op,
COMMUTATOR = '~#~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Overlap Coefficient
CREATE FUNCTION overlapcoefficient (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'overlapcoefficient'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION overlapcoefficient_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'overlapcoefficient_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~** (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = overlapcoefficient_op,
COMMUTATOR = '~**',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Q-Gram
CREATE FUNCTION qgram (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'qgram'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION qgram_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'qgram_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~~~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = qgram_op,
COMMUTATOR = '~~~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Smith-Waterman
CREATE FUNCTION smithwaterman (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'smithwaterman'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION smithwaterman_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'smithwaterman_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~=~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = smithwaterman_op,
COMMUTATOR = '~=~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Smith-Waterman-Gotoh
CREATE FUNCTION smithwatermangotoh (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'smithwatermangotoh'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION smithwatermangotoh_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'smithwatermangotoh_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~!~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = smithwatermangotoh_op,
COMMUTATOR = '~!~',
RESTRICT = contsel,
JOIN = contjoinsel
);
-- Soundex
CREATE FUNCTION soundex (text, text) RETURNS float8
AS 'MODULE_PATHNAME', 'soundex'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION soundex_op (text, text) RETURNS bool
AS 'MODULE_PATHNAME', 'soundex_op'
LANGUAGE C STABLE STRICT;
CREATE OPERATOR ~*~ (
LEFTARG = text,
RIGHTARG = text,
PROCEDURE = soundex_op,
COMMUTATOR = '~*~',
RESTRICT = contsel,
JOIN = contjoinsel
);
--
-- GIN support
--
CREATE FUNCTION gin_extract_value_token(internal, internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION gin_extract_query_token(internal, internal, int2, internal, internal, internal, internal)
RETURNS internal
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION gin_token_consistent(internal, int2, internal, int4, internal, internal, internal, internal)
RETURNS bool
AS 'MODULE_PATHNAME'
LANGUAGE C IMMUTABLE STRICT;
CREATE OPERATOR CLASS gin_similarity_ops
FOR TYPE text USING gin
AS
OPERATOR 1 ~++, -- block
OPERATOR 2 ~##, -- cosine
OPERATOR 3 ~-~, -- dice
OPERATOR 4 ~!!, -- euclidean
OPERATOR 5 ~??, -- jaccard
-- OPERATOR 6 ~%%, -- jaro
-- OPERATOR 7 ~@@, -- jarowinkler
-- OPERATOR 8 ~==, -- lev
OPERATOR 9 ~^^, -- matchingcoefficient
-- OPERATOR 10 ~||, -- mongeelkan
-- OPERATOR 11 ~#~, -- needlemanwunsch
OPERATOR 12 ~**, -- overlapcoefficient
OPERATOR 13 ~~~, -- qgram
-- OPERATOR 14 ~=~, -- smithwaterman
-- OPERATOR 15 ~!~, -- smithwatermangotoh
-- OPERATOR 16 ~*~, -- soundex
FUNCTION 1 bttextcmp(text, text),
FUNCTION 2 gin_extract_value_token(internal, internal, internal),
FUNCTION 3 gin_extract_query_token(internal, internal, int2, internal, internal, internal, internal),
FUNCTION 4 gin_token_consistent(internal, int2, internal, int4, internal, internal, internal, internal),
STORAGE text;