This repository has been archived by the owner on Oct 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigm_op.c
740 lines (632 loc) · 14.7 KB
/
bigm_op.c
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*-------------------------------------------------------------------------
*
* Portions Copyright (c) 2004-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 2013-2015, NTT DATA Corporation
*
* Changelog:
* 2013/01/09
* Support full text search using bigrams.
* Author: NTT DATA Corporation
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include <ctype.h>
#include "bigm.h"
#include "catalog/pg_type.h"
#include "tsearch/ts_locale.h"
#include "utils/array.h"
#include "utils/memutils.h"
PG_MODULE_MAGIC;
/* Last update date of pg_bigm */
#define BIGM_LAST_UPDATE "2015.09.10"
/* GUC variable */
bool bigm_enable_recheck = false;
int bigm_gin_key_limit = 0;
double bigm_similarity_limit = 0.3;
char *bigm_last_update = NULL;
PG_FUNCTION_INFO_V1(show_bigm);
PG_FUNCTION_INFO_V1(bigmtextcmp);
PG_FUNCTION_INFO_V1(likequery);
PG_FUNCTION_INFO_V1(bigm_similarity);
PG_FUNCTION_INFO_V1(bigm_similarity_op);
/*
* The function prototypes are created as a part of PG_FUNCTION_INFO_V1
* macro since 9.4, and hence the declaration of the function prototypes
* here is necessary only for 9.3 or before.
*/
#if PG_VERSION_NUM < 90400
Datum show_bigm(PG_FUNCTION_ARGS);
Datum bigmtextcmp(PG_FUNCTION_ARGS);
Datum likequery(PG_FUNCTION_ARGS);
Datum bigm_similarity(PG_FUNCTION_ARGS);
Datum bigm_similarity_op(PG_FUNCTION_ARGS);
#endif
void _PG_init(void);
void _PG_fini(void);
void
_PG_init(void)
{
/* Define custom GUC variables */
DefineCustomBoolVariable("pg_bigm.enable_recheck",
"Recheck that heap tuples fetched from index "
"match the query.",
NULL,
&bigm_enable_recheck,
true,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("pg_bigm.gin_key_limit",
"Sets the maximum number of bi-gram keys allowed to "
"use for GIN index search.",
"Zero means no limit.",
&bigm_gin_key_limit,
0,
0, INT_MAX,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
DefineCustomRealVariable("pg_bigm.similarity_limit",
"Sets the similarity threshold used by the "
"=% operator.",
NULL,
&bigm_similarity_limit,
0.3,
0.0, 1.0,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
/* Can't be set in postgresql.conf */
DefineCustomStringVariable("pg_bigm.last_update",
"Shows the last update date of pg_bigm.",
NULL,
&bigm_last_update,
BIGM_LAST_UPDATE,
PGC_INTERNAL,
GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE,
NULL,
NULL,
NULL);
EmitWarningsOnPlaceholders("pg_bigm");
}
void
_PG_fini(void)
{
}
static int
comp_bigm(const void *a, const void *b, void *arg)
{
int res;
bool *haveDups = (bool *) arg;
res = CMPBIGM(a, b);
if (res == 0)
*haveDups = true;
return res;
}
static int
unique_array(bigm *a, int len)
{
bigm *curend,
*tmp;
curend = tmp = a;
while (tmp - a < len)
if (CMPBIGM(tmp, curend))
{
curend++;
if (curend != tmp)
memcpy(curend, tmp, BIGMSIZE);
tmp++;
}
else
tmp++;
return curend + 1 - a;
}
#define iswordchr(c) (!t_isspace(c))
/*
* Finds first word in string, returns pointer to the word,
* endword points to the character after word
*/
static char *
find_word(char *str, int lenstr, char **endword, int *charlen)
{
char *beginword = str;
while (beginword - str < lenstr && !iswordchr(beginword))
beginword += pg_mblen(beginword);
if (beginword - str >= lenstr)
return NULL;
*endword = beginword;
*charlen = 0;
while (*endword - str < lenstr && iswordchr(*endword))
{
*endword += pg_mblen(*endword);
(*charlen)++;
}
return beginword;
}
/*
* The function is named compact_bigram to maintain consistency with pg_trgm,
* though it does not reduce multibyte characters to hash values like in
* compact_trigram.
*/
static void
compact_bigram(bigm *bptr, char *str, int bytelen)
{
CPBIGM(bptr, str, bytelen);
}
/*
* Adds bigrams from words (already padded).
*/
static bigm *
make_bigrams(bigm *bptr, char *str, int bytelen, int charlen)
{
char *ptr = str;
if (charlen < 2)
{
compact_bigram(bptr, ptr, pg_mblen(str));
bptr->pmatch = true;
bptr++;
return bptr;
}
if (bytelen > charlen)
{
/* Find multibyte character boundaries and call compact_bigram */
int lenfirst = pg_mblen(str),
lenlast = pg_mblen(str + lenfirst);
while ((ptr - str) + lenfirst + lenlast <= bytelen)
{
compact_bigram(bptr, ptr, lenfirst + lenlast);
ptr += lenfirst;
bptr++;
lenfirst = lenlast;
lenlast = pg_mblen(ptr + lenfirst);
}
}
else
{
/* Fast path when there are no multibyte characters */
Assert(bytelen == charlen);
while (ptr - str < bytelen - 1 /* number of bigrams = strlen - 1 */ )
{
CPBIGM(bptr, ptr, 2);
ptr++;
bptr++;
}
}
return bptr;
}
BIGM *
generate_bigm(char *str, int slen)
{
BIGM *bgm;
char *buf;
bigm *bptr;
int len,
charlen,
bytelen;
char *bword,
*eword;
/*
* Guard against possible overflow in the palloc requests below.
* We need to prevent integer overflow in the multiplications here.
*/
if ((Size) slen > (MaxAllocSize - VARHDRSZ) / sizeof(bigm) - 1 ||
(Size) slen > MaxAllocSize - 4)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("out of memory")));
bgm = (BIGM *) palloc(VARHDRSZ + sizeof(bigm) * (slen + 1));
SET_VARSIZE(bgm, VARHDRSZ);
if (slen + LPADDING + RPADDING < 2 || slen == 0)
return bgm;
bptr = GETARR(bgm);
buf = palloc(sizeof(char) * (slen + 4));
if (LPADDING > 0)
{
*buf = ' ';
if (LPADDING > 1)
*(buf + 1) = ' ';
}
eword = str;
while ((bword = find_word(eword, slen - (eword - str), &eword, &charlen)) != NULL)
{
bytelen = eword - bword;
memcpy(buf + LPADDING, bword, bytelen);
buf[LPADDING + bytelen] = ' ';
buf[LPADDING + bytelen + 1] = ' ';
/*
* count bigrams
*/
bptr = make_bigrams(bptr, buf, bytelen + LPADDING + RPADDING,
charlen + LPADDING + RPADDING);
}
pfree(buf);
if ((len = bptr - GETARR(bgm)) == 0)
return bgm;
/*
* Make bigrams unique.
*/
if (len > 1)
{
bool haveDups = false;
qsort_arg((void *) GETARR(bgm), len, sizeof(bigm), comp_bigm, (void *) &haveDups);
if (haveDups)
len = unique_array(GETARR(bgm), len);
}
SET_VARSIZE(bgm, CALCGTSIZE(len));
return bgm;
}
/*
* Extract the next non-wildcard part of a search string, ie, a word bounded
* by '_' or '%' meta-characters, non-word characters or string end.
*
* str: source string, of length lenstr bytes (need not be null-terminated)
* buf: where to return the substring (must be long enough)
* *bytelen: receives byte length of the found substring
* *charlen: receives character length of the found substring
*
* Returns pointer to end+1 of the found substring in the source string.
* Returns NULL if no word found (in which case buf, bytelen, charlen not set)
*
* If the found word is bounded by non-word characters or string boundaries
* then this function will include corresponding padding spaces into buf.
*/
static const char *
get_wildcard_part(const char *str, int lenstr,
char *buf, int *bytelen, int *charlen)
{
const char *beginword = str;
const char *endword;
char *s = buf;
bool in_leading_wildcard_meta = false;
bool in_trailing_wildcard_meta = false;
bool in_escape = false;
int clen;
/*
* Find the first word character, remembering whether preceding character
* was wildcard meta-character. Note that the in_escape state persists
* from this loop to the next one, since we may exit at a word character
* that is in_escape.
*/
while (beginword - str < lenstr)
{
if (in_escape)
{
if (iswordchr(beginword))
break;
in_escape = false;
in_leading_wildcard_meta = false;
}
else
{
if (ISESCAPECHAR(beginword))
in_escape = true;
else if (ISWILDCARDCHAR(beginword))
in_leading_wildcard_meta = true;
else if (iswordchr(beginword))
break;
else
in_leading_wildcard_meta = false;
}
beginword += pg_mblen(beginword);
}
/*
* Handle string end.
*/
if (beginword - str >= lenstr)
return NULL;
/*
* Add left padding spaces if preceding character wasn't wildcard
* meta-character.
*/
*charlen = 0;
if (!in_leading_wildcard_meta)
{
if (LPADDING > 0)
{
*s++ = ' ';
(*charlen)++;
if (LPADDING > 1)
{
*s++ = ' ';
(*charlen)++;
}
}
}
/*
* Copy data into buf until wildcard meta-character, non-word character or
* string boundary. Strip escapes during copy.
*/
endword = beginword;
while (endword - str < lenstr)
{
clen = pg_mblen(endword);
if (in_escape)
{
if (iswordchr(endword))
{
memcpy(s, endword, clen);
(*charlen)++;
s += clen;
}
else
{
/*
* Back up endword to the escape character when stopping at an
* escaped char, so that subsequent get_wildcard_part will
* restart from the escape character. We assume here that
* escape chars are single-byte.
*/
endword--;
break;
}
in_escape = false;
}
else
{
if (ISESCAPECHAR(endword))
in_escape = true;
else if (ISWILDCARDCHAR(endword))
{
in_trailing_wildcard_meta = true;
break;
}
else if (iswordchr(endword))
{
memcpy(s, endword, clen);
(*charlen)++;
s += clen;
}
else
break;
}
endword += clen;
}
/*
* Add right padding spaces if next character isn't wildcard
* meta-character.
*/
if (!in_trailing_wildcard_meta)
{
if (RPADDING > 0)
{
*s++ = ' ';
(*charlen)++;
if (RPADDING > 1)
{
*s++ = ' ';
(*charlen)++;
}
}
}
*bytelen = s - buf;
return endword;
}
/*
* Generates bigrams for wildcard search string.
*
* Returns array of bigrams that must occur in any string that matches the
* wildcard string. For example, given pattern "a%bcd%" the bigrams
* " a", "bcd" would be extracted.
*
* Set 'removeDups' to true if duplicate bigrams are removed.
*/
BIGM *
generate_wildcard_bigm(const char *str, int slen, bool *removeDups)
{
BIGM *bgm;
char *buf;
bigm *bptr;
int len,
charlen,
bytelen;
const char *eword;
*removeDups = false;
/*
* Guard against possible overflow in the palloc requests below.
* We need to prevent integer overflow in the multiplications here.
*/
if ((Size) slen > (MaxAllocSize - VARHDRSZ) / sizeof(bigm) - 1 ||
(Size) slen > MaxAllocSize - 4)
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("out of memory")));
bgm = (BIGM *) palloc(VARHDRSZ + sizeof(bigm) * (slen + 1));
SET_VARSIZE(bgm, VARHDRSZ);
if (slen + LPADDING + RPADDING < 2 || slen == 0)
return bgm;
bptr = GETARR(bgm);
buf = palloc(sizeof(char) * (slen + 4));
/*
* Extract bigrams from each substring extracted by get_wildcard_part.
*/
eword = str;
while ((eword = get_wildcard_part(eword, slen - (eword - str),
buf, &bytelen, &charlen)) != NULL)
{
/*
* count bigrams
*/
bptr = make_bigrams(bptr, buf, bytelen, charlen);
}
pfree(buf);
if ((len = bptr - GETARR(bgm)) == 0)
return bgm;
/*
* Make bigrams unique.
*/
if (len > 1)
{
bool haveDups = false;
qsort_arg((void *) GETARR(bgm), len, sizeof(bigm), comp_bigm, (void *) &haveDups);
if (haveDups)
{
*removeDups = true;
len = unique_array(GETARR(bgm), len);
}
}
SET_VARSIZE(bgm, CALCGTSIZE(len));
return bgm;
}
Datum
show_bigm(PG_FUNCTION_ARGS)
{
text *in = PG_GETARG_TEXT_P(0);
BIGM *bgm;
Datum *d;
ArrayType *a;
bigm *ptr;
int i;
bgm = generate_bigm(VARDATA(in), VARSIZE(in) - VARHDRSZ);
d = (Datum *) palloc(sizeof(Datum) * (1 + ARRNELEM(bgm)));
for (i = 0, ptr = GETARR(bgm); i < ARRNELEM(bgm); i++, ptr++)
{
text *item = cstring_to_text_with_len(ptr->str, ptr->bytelen);
d[i] = PointerGetDatum(item);
}
a = construct_array(
d,
ARRNELEM(bgm),
TEXTOID,
-1,
false,
'i'
);
for (i = 0; i < ARRNELEM(bgm); i++)
pfree(DatumGetPointer(d[i]));
pfree(d);
pfree(bgm);
PG_FREE_IF_COPY(in, 0);
PG_RETURN_POINTER(a);
}
static float4
cnt_sml_bigm(BIGM *bgm1, BIGM *bgm2)
{
bigm *ptr1,
*ptr2;
int count = 0;
int len1,
len2;
ptr1 = GETARR(bgm1);
ptr2 = GETARR(bgm2);
len1 = ARRNELEM(bgm1);
len2 = ARRNELEM(bgm2);
/* explicit test is needed to avoid 0/0 division when both lengths are 0 */
if (len1 <= 0 || len2 <= 0)
return (float4) 0.0;
while (ptr1 - GETARR(bgm1) < len1 && ptr2 - GETARR(bgm2) < len2)
{
int res = CMPBIGM(ptr1, ptr2);
if (res < 0)
ptr1++;
else if (res > 0)
ptr2++;
else
{
ptr1++;
ptr2++;
count++;
}
}
#ifdef DIVUNION
return ((float4) count) / ((float4) (len1 + len2 - count));
#else
return ((float4) count) / ((float4) ((len1 > len2) ? len1 : len2));
#endif
}
Datum
bigm_similarity(PG_FUNCTION_ARGS)
{
text *in1 = PG_GETARG_TEXT_P(0);
text *in2 = PG_GETARG_TEXT_P(1);
BIGM *bgm1,
*bgm2;
float4 res;
bgm1 = generate_bigm(VARDATA(in1), VARSIZE(in1) - VARHDRSZ);
bgm2 = generate_bigm(VARDATA(in2), VARSIZE(in2) - VARHDRSZ);
res = cnt_sml_bigm(bgm1, bgm2);
pfree(bgm1);
pfree(bgm2);
PG_FREE_IF_COPY(in1, 0);
PG_FREE_IF_COPY(in2, 1);
PG_RETURN_FLOAT4(res);
}
Datum
bigm_similarity_op(PG_FUNCTION_ARGS)
{
float4 res = DatumGetFloat4(DirectFunctionCall2(bigm_similarity,
PG_GETARG_DATUM(0),
PG_GETARG_DATUM(1)));
PG_RETURN_BOOL(res >= (float4) bigm_similarity_limit);
}
Datum
likequery(PG_FUNCTION_ARGS)
{
text *query = PG_GETARG_TEXT_PP(0);
const char *str;
int len;
const char *sp;
text *result;
char *rp;
int mblen;
str = VARDATA_ANY(query);
len = VARSIZE_ANY_EXHDR(query);
if (len == 0)
PG_RETURN_NULL();
result = (text *) palloc((Size) len * 2 + 2 + VARHDRSZ);
rp = VARDATA(result);
*rp++ = '%';
for (sp = str; (sp - str) < len;)
{
if (ISWILDCARDCHAR(sp) || ISESCAPECHAR(sp))
{
*rp++ = '\\';
*rp++ = *sp++;
}
else if (IS_HIGHBIT_SET(*sp))
{
mblen = pg_mblen(sp);
memcpy(rp, sp, mblen);
rp += mblen;
sp += mblen;
}
else
*rp++ = *sp++;
}
*rp++ = '%';
SET_VARSIZE(result, rp - VARDATA(result) + VARHDRSZ);
PG_RETURN_TEXT_P(result);
}
inline int
bigmstrcmp(char *arg1, int len1, char *arg2, int len2)
{
int i;
int len = Min(len1, len2);
for (i = 0; i < len; i++, arg1++, arg2++)
{
if (*arg1 == *arg2)
continue;
if (*arg1 < *arg2)
return -1;
else
return 1;
}
return (len1 == len2) ? 0 : ((len1 < len2) ? -1 : 1);
}
Datum
bigmtextcmp(PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_PP(0);
text *arg2 = PG_GETARG_TEXT_PP(1);
char *a1p = VARDATA_ANY(arg1);
char *a2p = VARDATA_ANY(arg2);
int len1 = VARSIZE_ANY_EXHDR(arg1);
int len2 = VARSIZE_ANY_EXHDR(arg2);
PG_RETURN_INT32(bigmstrcmp(a1p, len1, a2p, len2));
}