PostgreSQL Source Code git master
Loading...
Searching...
No Matches
trgm_op.c File Reference
#include "postgres.h"
#include <ctype.h>
#include "catalog/pg_collation_d.h"
#include "catalog/pg_type.h"
#include "common/int.h"
#include "lib/qunique.h"
#include "miscadmin.h"
#include "trgm.h"
#include "tsearch/ts_locale.h"
#include "utils/formatting.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_crc.h"
Include dependency graph for trgm_op.c:

Go to the source code of this file.

Data Structures

struct  pos_trgm
 
struct  growable_trgm_array
 

Macros

#define TRGM_BOUND_LEFT   0x01 /* trigram is left bound of word */
 
#define TRGM_BOUND_RIGHT   0x02 /* trigram is right bound of word */
 
#define WORD_SIMILARITY_CHECK_ONLY
 
#define WORD_SIMILARITY_STRICT
 
#define CMPCHAR(a, b)   ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )
 
#define CMPPCHAR_S(a, b, i)   CMPCHAR( *(((const signed char*)(a))+i), *(((const signed char*)(b))+i) )
 
#define CMPPCHAR_UNS(a, b, i)   CMPCHAR( *(((const unsigned char*)(a))+i), *(((const unsigned char*)(b))+i) )
 

Typedefs

typedef uint8 TrgmBound
 

Functions

 PG_MODULE_MAGIC_EXT (.name="pg_trgm",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (set_limit)
 
 PG_FUNCTION_INFO_V1 (show_limit)
 
 PG_FUNCTION_INFO_V1 (show_trgm)
 
 PG_FUNCTION_INFO_V1 (similarity)
 
 PG_FUNCTION_INFO_V1 (word_similarity)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity)
 
 PG_FUNCTION_INFO_V1 (similarity_dist)
 
 PG_FUNCTION_INFO_V1 (similarity_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_commutator_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_dist_op)
 
 PG_FUNCTION_INFO_V1 (word_similarity_dist_commutator_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_commutator_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_dist_op)
 
 PG_FUNCTION_INFO_V1 (strict_word_similarity_dist_commutator_op)
 
static int CMPTRGM_CHOOSE (const void *a, const void *b)
 
static void init_trgm_array (growable_trgm_array *arr, int slen)
 
static void enlarge_trgm_array (growable_trgm_array *arr, int needed)
 
void _PG_init (void)
 
static int CMPTRGM_SIGNED (const void *a, const void *b)
 
static int CMPTRGM_UNSIGNED (const void *a, const void *b)
 
Datum set_limit (PG_FUNCTION_ARGS)
 
double index_strategy_get_limit (StrategyNumber strategy)
 
Datum show_limit (PG_FUNCTION_ARGS)
 
static int comp_trgm (const void *a, const void *b)
 
static charfind_word (char *str, int lenstr, char **endword)
 
void compact_trigram (trgm *tptr, char *str, int bytelen)
 
static void make_trigrams (growable_trgm_array *dst, char *str, int bytelen)
 
static void generate_trgm_only (growable_trgm_array *dst, char *str, int slen, TrgmBound **bounds_p)
 
TRGMgenerate_trgm (char *str, int slen)
 
static pos_trgmmake_positional_trgm (trgm *trg1, int len1, trgm *trg2, int len2)
 
static int comp_ptrgm (const void *v1, const void *v2)
 
static float4 iterate_word_similarity (int *trg2indexes, bool *found, int ulen1, int len2, int len, uint8 flags, TrgmBound *bounds)
 
static float4 calc_word_similarity (char *str1, int slen1, char *str2, int slen2, uint8 flags)
 
static const charget_wildcard_part (const char *str, int lenstr, char *buf, int *bytelen)
 
TRGMgenerate_wildcard_trgm (const char *str, int slen)
 
uint32 trgm2int (trgm *ptr)
 
Datum show_trgm (PG_FUNCTION_ARGS)
 
float4 cnt_sml (TRGM *trg1, TRGM *trg2, bool inexact)
 
bool trgm_contained_by (TRGM *trg1, TRGM *trg2)
 
booltrgm_presence_map (TRGM *query, TRGM *key)
 
Datum similarity (PG_FUNCTION_ARGS)
 
Datum word_similarity (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity (PG_FUNCTION_ARGS)
 
Datum similarity_dist (PG_FUNCTION_ARGS)
 
Datum similarity_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_commutator_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_dist_op (PG_FUNCTION_ARGS)
 
Datum word_similarity_dist_commutator_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_commutator_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_dist_op (PG_FUNCTION_ARGS)
 
Datum strict_word_similarity_dist_commutator_op (PG_FUNCTION_ARGS)
 

Variables

double similarity_threshold = 0.3f
 
double word_similarity_threshold = 0.6f
 
double strict_word_similarity_threshold = 0.5f
 
int(* CMPTRGM )(const void *a, const void *b) = CMPTRGM_CHOOSE
 

Macro Definition Documentation

◆ CMPCHAR

#define CMPCHAR (   a,
  b 
)    ( ((a)==(b)) ? 0 : ( ((a)<(b)) ? -1 : 1 ) )

Definition at line 186 of file trgm_op.c.

◆ CMPPCHAR_S

#define CMPPCHAR_S (   a,
  b,
  i 
)    CMPCHAR( *(((const signed char*)(a))+i), *(((const signed char*)(b))+i) )

◆ CMPPCHAR_UNS

#define CMPPCHAR_UNS (   a,
  b,
  i 
)    CMPCHAR( *(((const unsigned char*)(a))+i), *(((const unsigned char*)(b))+i) )

◆ TRGM_BOUND_LEFT

#define TRGM_BOUND_LEFT   0x01 /* trigram is left bound of word */

Definition at line 60 of file trgm_op.c.

◆ TRGM_BOUND_RIGHT

#define TRGM_BOUND_RIGHT   0x02 /* trigram is right bound of word */

Definition at line 61 of file trgm_op.c.

◆ WORD_SIMILARITY_CHECK_ONLY

#define WORD_SIMILARITY_CHECK_ONLY
Value:
0x01 /* only check existence of similar
* search pattern in text */

Definition at line 64 of file trgm_op.c.

◆ WORD_SIMILARITY_STRICT

#define WORD_SIMILARITY_STRICT
Value:
0x02 /* force bounds of extent to match
* word bounds */

Definition at line 65 of file trgm_op.c.

Typedef Documentation

◆ TrgmBound

Definition at line 59 of file trgm_op.c.

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 143 of file trgm_op.c.

146{
147 /* Define custom GUC variables. */
148 DefineCustomRealVariable("pg_trgm.similarity_threshold",
149 "Sets the threshold used by the % operator.",
150 "Valid range is 0.0 .. 1.0.",
152 0.3f,
153 0.0,
154 1.0,
156 0,
157 NULL,
158 NULL,
159 NULL);
160 DefineCustomRealVariable("pg_trgm.word_similarity_threshold",
161 "Sets the threshold used by the <% operator.",
162 "Valid range is 0.0 .. 1.0.",
164 0.6f,
165 0.0,
166 1.0,
168 0,
169 NULL,
170 NULL,
171 NULL);
172 DefineCustomRealVariable("pg_trgm.strict_word_similarity_threshold",
173 "Sets the threshold used by the <<% operator.",
174 "Valid range is 0.0 .. 1.0.",
176 0.5f,
177 0.0,
178 1.0,
180 0,
181 NULL,
182 NULL,
183 NULL);
184
void DefineCustomRealVariable(const char *name, const char *short_desc, const char *long_desc, double *valueAddr, double bootValue, double minValue, double maxValue, GucContext context, int flags, GucRealCheckHook check_hook, GucRealAssignHook assign_hook, GucShowHook show_hook)
Definition guc.c:5063
@ PGC_USERSET
Definition guc.h:79
static int fb(int x)
double strict_word_similarity_threshold
Definition trgm_op.c:29
double word_similarity_threshold
Definition trgm_op.c:28
double similarity_threshold
Definition trgm_op.c:27

References DefineCustomRealVariable(), fb(), MarkGUCPrefixReserved(), PGC_USERSET, similarity_threshold, strict_word_similarity_threshold, and word_similarity_threshold.

◆ calc_word_similarity()

static float4 calc_word_similarity ( char str1,
int  slen1,
char str2,
int  slen2,
uint8  flags 
)
static

Definition at line 814 of file trgm_op.c.

818{
819 bool *found;
820 pos_trgm *ptrg;
823 int len1,
824 len2,
825 len,
826 i,
827 j,
828 ulen1;
829 int *trg2indexes;
830 float4 result;
832
833 /* Make positional trigrams */
834
836 len1 = trg1.length;
838 len2 = trg2.length;
839
840 ptrg = make_positional_trgm(GETARR(trg1.datum), len1, GETARR(trg2.datum), len2);
841 len = len1 + len2;
842 qsort(ptrg, len, sizeof(pos_trgm), comp_ptrgm);
843
844 pfree(trg1.datum);
845 pfree(trg2.datum);
846
847 /*
848 * Merge positional trigrams array: enumerate each trigram and find its
849 * presence in required word.
850 */
851 trg2indexes = palloc_array(int, len2);
852 found = palloc0_array(bool, len);
853
854 ulen1 = 0;
855 j = 0;
856 for (i = 0; i < len; i++)
857 {
858 if (i > 0)
859 {
860 int cmp = CMPTRGM(ptrg[i - 1].trg, ptrg[i].trg);
861
862 if (cmp != 0)
863 {
864 if (found[j])
865 ulen1++;
866 j++;
867 }
868 }
869
870 if (ptrg[i].index >= 0)
871 {
872 trg2indexes[ptrg[i].index] = j;
873 }
874 else
875 {
876 found[j] = true;
877 }
878 }
879 if (found[j])
880 ulen1++;
881
882 /* Run iterative procedure to find maximum similarity with word */
883 result = iterate_word_similarity(trg2indexes, found, ulen1, len2, len,
884 flags, bounds);
885
887 pfree(found);
888 pfree(ptrg);
889
float float4
Definition c.h:655
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_array(type, count)
Definition fe_memutils.h:77
int j
Definition isn.c:78
int i
Definition isn.c:77
void pfree(void *pointer)
Definition mcxt.c:1616
const void size_t len
#define qsort(a, b, c, d)
Definition port.h:495
static int cmp(const chr *x, const chr *y, size_t len)
Definition type.h:96
#define GETARR(x)
Definition trgm.h:97
static float4 iterate_word_similarity(int *trg2indexes, bool *found, int ulen1, int len2, int len, uint8 flags, TrgmBound *bounds)
Definition trgm_op.c:646
uint8 TrgmBound
Definition trgm_op.c:59
int(* CMPTRGM)(const void *a, const void *b)
Definition trgm_op.c:49
static pos_trgm * make_positional_trgm(trgm *trg1, int len1, trgm *trg2, int len2)
Definition trgm_op.c:590
static int comp_ptrgm(const void *v1, const void *v2)
Definition trgm_op.c:617
static void generate_trgm_only(growable_trgm_array *dst, char *str, int slen, TrgmBound **bounds_p)
Definition trgm_op.c:448
#define WORD_SIMILARITY_STRICT
Definition trgm_op.c:65

References cmp(), CMPTRGM, comp_ptrgm(), fb(), generate_trgm_only(), GETARR, i, iterate_word_similarity(), j, len, make_positional_trgm(), palloc0_array, palloc_array, pfree(), qsort, and WORD_SIMILARITY_STRICT.

Referenced by strict_word_similarity(), strict_word_similarity_commutator_op(), strict_word_similarity_dist_commutator_op(), strict_word_similarity_dist_op(), strict_word_similarity_op(), word_similarity(), word_similarity_commutator_op(), word_similarity_dist_commutator_op(), word_similarity_dist_op(), and word_similarity_op().

◆ CMPTRGM_CHOOSE()

static int CMPTRGM_CHOOSE ( const void a,
const void b 
)
static

Definition at line 217 of file trgm_op.c.

220{
223 else
225
static int CMPTRGM_UNSIGNED(const void *a, const void *b)
Definition trgm_op.c:203
static int CMPTRGM_SIGNED(const void *a, const void *b)
Definition trgm_op.c:193
bool GetDefaultCharSignedness(void)
Definition xlog.c:4661

References a, b, CMPTRGM, CMPTRGM_SIGNED(), CMPTRGM_UNSIGNED(), and GetDefaultCharSignedness().

◆ CMPTRGM_SIGNED()

static int CMPTRGM_SIGNED ( const void a,
const void b 
)
inlinestatic

Definition at line 193 of file trgm_op.c.

196{
197#define CMPPCHAR_S(a,b,i) CMPCHAR( *(((const signed char*)(a))+i), *(((const signed char*)(b))+i) )
198
199 return CMPPCHAR_S(a, b, 0) ? CMPPCHAR_S(a, b, 0)
200 : (CMPPCHAR_S(a, b, 1) ? CMPPCHAR_S(a, b, 1)
int b
Definition isn.c:74
int a
Definition isn.c:73
#define CMPPCHAR_S(a, b, i)

References a, b, and CMPPCHAR_S.

Referenced by CMPTRGM_CHOOSE().

◆ CMPTRGM_UNSIGNED()

static int CMPTRGM_UNSIGNED ( const void a,
const void b 
)
inlinestatic

Definition at line 203 of file trgm_op.c.

206{
207#define CMPPCHAR_UNS(a,b,i) CMPCHAR( *(((const unsigned char*)(a))+i), *(((const unsigned char*)(b))+i) )
208
209 return CMPPCHAR_UNS(a, b, 0) ? CMPPCHAR_UNS(a, b, 0)
210 : (CMPPCHAR_UNS(a, b, 1) ? CMPPCHAR_UNS(a, b, 1)
#define CMPPCHAR_UNS(a, b, i)

References a, b, and CMPPCHAR_UNS.

Referenced by CMPTRGM_CHOOSE().

◆ cnt_sml()

float4 cnt_sml ( TRGM trg1,
TRGM trg2,
bool  inexact 
)

Definition at line 1168 of file trgm_op.c.

1171{
1172 trgm *ptr1,
1173 *ptr2;
1174 int count = 0;
1175 int len1,
1176 len2;
1177
1178 ptr1 = GETARR(trg1);
1179 ptr2 = GETARR(trg2);
1180
1181 len1 = ARRNELEM(trg1);
1182 len2 = ARRNELEM(trg2);
1183
1184 /* explicit test is needed to avoid 0/0 division when both lengths are 0 */
1185 if (len1 <= 0 || len2 <= 0)
1186 return (float4) 0.0;
1187
1188 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1189 {
1190 int res = CMPTRGM(ptr1, ptr2);
1191
1192 if (res < 0)
1193 ptr1++;
1194 else if (res > 0)
1195 ptr2++;
1196 else
1197 {
1198 ptr1++;
1199 ptr2++;
1200 count++;
1201 }
1202 }
1203
1204 /*
1205 * If inexact then len2 is equal to count, because we don't know actual
1206 * length of second string in inexact search and we can assume that count
1207 * is a lower bound of len2.
1208 */
#define ARRNELEM(x)
Definition trgm.h:98
char trgm[3]
Definition trgm.h:41

References ARRNELEM, CALCSML, CMPTRGM, fb(), and GETARR.

Referenced by gtrgm_consistent(), gtrgm_distance(), and similarity().

◆ comp_ptrgm()

static int comp_ptrgm ( const void v1,
const void v2 
)
static

Definition at line 617 of file trgm_op.c.

620{
621 const pos_trgm *p1 = (const pos_trgm *) v1;
622 const pos_trgm *p2 = (const pos_trgm *) v2;
623 int cmp;
624
625 cmp = CMPTRGM(p1->trg, p2->trg);
626 if (cmp != 0)
627 return cmp;
628

References cmp(), CMPTRGM, fb(), and pg_cmp_s32().

Referenced by calc_word_similarity().

◆ comp_trgm()

static int comp_trgm ( const void a,
const void b 
)
static

Definition at line 283 of file trgm_op.c.

286{

References a, b, and CMPTRGM.

Referenced by generate_trgm(), and generate_wildcard_trgm().

◆ compact_trigram()

void compact_trigram ( trgm tptr,
char str,
int  bytelen 
)

Definition at line 329 of file trgm_op.c.

332{
333 if (bytelen == 3)
334 {
335 CPTRGM(tptr, str);
336 }
337 else
338 {
340
344
345 /*
346 * use only 3 upper bytes from crc, hope, it's good enough hashing
347 */
348 CPTRGM(tptr, &crc);
const char * str
return crc
uint32 pg_crc32
Definition pg_crc.h:37
#define INIT_LEGACY_CRC32(crc)
Definition pg_crc.h:79
#define COMP_LEGACY_CRC32(crc, data, len)
Definition pg_crc.h:81
#define FIN_LEGACY_CRC32(crc)
Definition pg_crc.h:80
#define CPTRGM(a, b)
Definition trgm.h:43

References COMP_LEGACY_CRC32, CPTRGM, crc, fb(), FIN_LEGACY_CRC32, INIT_LEGACY_CRC32, and str.

Referenced by fillTrgm(), and make_trigrams().

◆ enlarge_trgm_array()

static void enlarge_trgm_array ( growable_trgm_array arr,
int  needed 
)
static

Definition at line 122 of file trgm_op.c.

125{
126 size_t new_needed = (size_t) arr->length + needed;
127
128 if (new_needed > arr->allocated)
129 {
130 /* Guard against possible overflow, like in init_trgm_array */
131 if (new_needed > MaxAllocSize / sizeof(trgm))
134 errmsg("out of memory")));
135
137 arr->allocated = new_needed;
int errcode(int sqlerrcode)
Definition elog.c:874
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
#define MaxAllocSize
Definition fe_memutils.h:22
#define CALCGTSIZE(flag, siglen)
Definition hstore_gist.c:60
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
#define ARRKEY
Definition trgm.h:87

References growable_trgm_array::allocated, ARRKEY, CALCGTSIZE, growable_trgm_array::datum, ereport, errcode(), errmsg(), ERROR, fb(), growable_trgm_array::length, MaxAllocSize, and repalloc().

Referenced by make_trigrams().

◆ find_word()

static char * find_word ( char str,
int  lenstr,
char **  endword 
)
static

Definition at line 293 of file trgm_op.c.

296{
297 char *beginword = str;
298 const char *endstr = str + lenstr;
299
300 while (beginword < endstr)
301 {
303
305 break;
306 beginword += clen;
307 }
308
309 if (beginword >= endstr)
310 return NULL;
311
313 while (*endword < endstr)
314 {
316
317 if (!ISWORDCHR(*endword, clen))
318 break;
319 *endword += clen;
320 }
321
int pg_mblen_range(const char *mbstr, const char *end)
Definition mbutils.c:1084
#define ISWORDCHR(c, len)
Definition trgm.h:50

References fb(), ISWORDCHR, pg_mblen_range(), and str.

Referenced by generate_trgm_only().

◆ generate_trgm()

TRGM * generate_trgm ( char str,
int  slen 
)

Definition at line 554 of file trgm_op.c.

557{
558 TRGM *trg;
560 int len;
561
563 len = arr.length;
564 trg = arr.datum;
565 trg->flag = ARRKEY;
566
567 /*
568 * Make trigrams unique.
569 */
570 if (len > 1)
571 {
572 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
573 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
574 }
575
577
static size_t qunique(void *array, size_t elements, size_t width, int(*compare)(const void *, const void *))
Definition qunique.h:21
Definition trgm.h:58
uint8 flag
Definition trgm.h:60
static int comp_trgm(const void *a, const void *b)
Definition trgm_op.c:283
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

References ARRKEY, CALCGTSIZE, comp_trgm(), growable_trgm_array::datum, fb(), TRGM::flag, generate_trgm_only(), GETARR, len, growable_trgm_array::length, qsort, qunique(), SET_VARSIZE(), and str.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), gtrgm_compress(), gtrgm_consistent(), gtrgm_distance(), show_trgm(), and similarity().

◆ generate_trgm_only()

static void generate_trgm_only ( growable_trgm_array dst,
char str,
int  slen,
TrgmBound **  bounds_p 
)
static

Definition at line 448 of file trgm_op.c.

451{
452 size_t buflen;
453 char *buf;
454 int bytelen;
455 char *bword,
456 *eword;
458 int bounds_allocated = 0;
459
461
462 /*
463 * If requested, allocate an array for the bounds, with the same size as
464 * the trigram array.
465 */
466 if (bounds_p)
467 {
468 bounds_allocated = dst->allocated;
470 }
471
472 if (slen + LPADDING + RPADDING < 3 || slen == 0)
473 return;
474
475 /*
476 * Allocate a buffer for case-folded, blank-padded words.
477 *
478 * As an initial guess, allocate a buffer large enough to hold the
479 * original string with padding, which is always enough when compiled with
480 * !IGNORECASE. If the case-folding produces a string longer than the
481 * original, we'll grow the buffer.
482 */
483 buflen = (size_t) slen + 4;
484 buf = (char *) palloc(buflen);
485 if (LPADDING > 0)
486 {
487 *buf = ' ';
488 if (LPADDING > 1)
489 *(buf + 1) = ' ';
490 }
491
492 eword = str;
493 while ((bword = find_word(eword, slen - (eword - str), &eword)) != NULL)
494 {
495 int oldlen;
496
497 /* Convert word to lower case before extracting trigrams from it */
498#ifdef IGNORECASE
499 {
500 char *lowered;
501
504
505 /* grow the buffer if necessary */
506 if (bytelen > buflen - 4)
507 {
508 pfree(buf);
509 buflen = (size_t) bytelen + 4;
510 buf = (char *) palloc(buflen);
511 if (LPADDING > 0)
512 {
513 *buf = ' ';
514 if (LPADDING > 1)
515 *(buf + 1) = ' ';
516 }
517 }
519 pfree(lowered);
520 }
521#else
522 bytelen = eword - bword;
524#endif
525
526 buf[LPADDING + bytelen] = ' ';
527 buf[LPADDING + bytelen + 1] = ' ';
528
529 /* Calculate trigrams marking their bounds if needed */
530 oldlen = dst->length;
532 if (bounds)
533 {
534 if (bounds_allocated < dst->length)
535 {
537 bounds_allocated = dst->allocated;
538 }
539
541 bounds[dst->length - 1] |= TRGM_BOUND_RIGHT;
542 }
543 }
544
char * str_tolower(const char *buff, size_t nbytes, Oid collid)
void * palloc(Size size)
Definition mcxt.c:1387
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:109
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define RPADDING
Definition trgm.h:17
#define LPADDING
Definition trgm.h:16
static void init_trgm_array(growable_trgm_array *arr, int slen)
Definition trgm_op.c:90
static void make_trigrams(growable_trgm_array *dst, char *str, int bytelen)
Definition trgm_op.c:354
#define TRGM_BOUND_RIGHT
Definition trgm_op.c:61
static char * find_word(char *str, int lenstr, char **endword)
Definition trgm_op.c:293
#define TRGM_BOUND_LEFT
Definition trgm_op.c:60

References buf, fb(), find_word(), init_trgm_array(), LPADDING, make_trigrams(), palloc(), palloc0_array, pfree(), repalloc0_array, RPADDING, str, str_tolower(), TRGM_BOUND_LEFT, and TRGM_BOUND_RIGHT.

Referenced by calc_word_similarity(), and generate_trgm().

◆ generate_wildcard_trgm()

TRGM * generate_wildcard_trgm ( const char str,
int  slen 
)

Definition at line 1044 of file trgm_op.c.

1047{
1048 TRGM *trg;
1050 char *buf;
1051 int len,
1052 bytelen;
1053 const char *eword;
1054
1055 if (slen + LPADDING + RPADDING < 3 || slen == 0)
1056 {
1057 trg = (TRGM *) palloc(TRGMHDRSIZE);
1058 trg->flag = ARRKEY;
1060 return trg;
1061 }
1062
1063 init_trgm_array(&arr, slen);
1064
1065 /* Allocate a buffer for blank-padded, but not yet case-folded, words */
1066 buf = palloc_array(char, slen + 4);
1067
1068 /*
1069 * Extract trigrams from each substring extracted by get_wildcard_part.
1070 */
1071 eword = str;
1072 while ((eword = get_wildcard_part(eword, slen - (eword - str),
1073 buf, &bytelen)) != NULL)
1074 {
1075 char *word;
1076
1077#ifdef IGNORECASE
1079 bytelen = strlen(word);
1080#else
1081 word = buf;
1082#endif
1083
1084 /*
1085 * count trigrams
1086 */
1087 make_trigrams(&arr, word, bytelen);
1088
1089#ifdef IGNORECASE
1090 pfree(word);
1091#endif
1092 }
1093
1094 pfree(buf);
1095
1096 /*
1097 * Make trigrams unique.
1098 */
1099 trg = arr.datum;
1100 len = arr.length;
1101 if (len > 1)
1102 {
1103 qsort(GETARR(trg), len, sizeof(trgm), comp_trgm);
1104 len = qunique(GETARR(trg), len, sizeof(trgm), comp_trgm);
1105 }
1106
1107 trg->flag = ARRKEY;
1109
static void word(struct vars *v, int dir, struct state *lp, struct state *rp)
Definition regcomp.c:1476
#define TRGMHDRSIZE
Definition trgm.h:64
static const char * get_wildcard_part(const char *str, int lenstr, char *buf, int *bytelen)
Definition trgm_op.c:907

References ARRKEY, buf, CALCGTSIZE, comp_trgm(), growable_trgm_array::datum, fb(), TRGM::flag, get_wildcard_part(), GETARR, init_trgm_array(), len, growable_trgm_array::length, LPADDING, make_trigrams(), palloc(), palloc_array, pfree(), qsort, qunique(), RPADDING, SET_VARSIZE(), str, str_tolower(), TRGMHDRSIZE, and word().

Referenced by gin_extract_query_trgm(), and gtrgm_consistent().

◆ get_wildcard_part()

static const char * get_wildcard_part ( const char str,
int  lenstr,
char buf,
int bytelen 
)
static

Definition at line 907 of file trgm_op.c.

911{
912 const char *beginword = str;
913 const char *endword;
914 const char *endstr = str + lenstr;
915 char *s = buf;
916 bool in_leading_wildcard_meta = false;
917 bool in_trailing_wildcard_meta = false;
918 bool in_escape = false;
919 int clen;
920
921 /*
922 * Find the first word character, remembering whether preceding character
923 * was wildcard meta-character. Note that the in_escape state persists
924 * from this loop to the next one, since we may exit at a word character
925 * that is in_escape.
926 */
927 while (beginword < endstr)
928 {
930
931 if (in_escape)
932 {
934 break;
935 in_escape = false;
937 }
938 else
939 {
941 in_escape = true;
942 else if (ISWILDCARDCHAR(beginword))
944 else if (ISWORDCHR(beginword, clen))
945 break;
946 else
948 }
949 beginword += clen;
950 }
951
952 /*
953 * Handle string end.
954 */
955 if (beginword - str >= lenstr)
956 return NULL;
957
958 /*
959 * Add left padding spaces if preceding character wasn't wildcard
960 * meta-character.
961 */
963 {
964 if (LPADDING > 0)
965 {
966 *s++ = ' ';
967 if (LPADDING > 1)
968 *s++ = ' ';
969 }
970 }
971
972 /*
973 * Copy data into buf until wildcard meta-character, non-word character or
974 * string boundary. Strip escapes during copy.
975 */
977 while (endword < endstr)
978 {
980 if (in_escape)
981 {
982 if (ISWORDCHR(endword, clen))
983 {
984 memcpy(s, endword, clen);
985 s += clen;
986 }
987 else
988 {
989 /*
990 * Back up endword to the escape character when stopping at an
991 * escaped char, so that subsequent get_wildcard_part will
992 * restart from the escape character. We assume here that
993 * escape chars are single-byte.
994 */
995 endword--;
996 break;
997 }
998 in_escape = false;
999 }
1000 else
1001 {
1002 if (ISESCAPECHAR(endword))
1003 in_escape = true;
1004 else if (ISWILDCARDCHAR(endword))
1005 {
1007 break;
1008 }
1009 else if (ISWORDCHR(endword, clen))
1010 {
1011 memcpy(s, endword, clen);
1012 s += clen;
1013 }
1014 else
1015 break;
1016 }
1017 endword += clen;
1018 }
1019
1020 /*
1021 * Add right padding spaces if next character isn't wildcard
1022 * meta-character.
1023 */
1025 {
1026 if (RPADDING > 0)
1027 {
1028 *s++ = ' ';
1029 if (RPADDING > 1)
1030 *s++ = ' ';
1031 }
1032 }
1033
1034 *bytelen = s - buf;
#define ISESCAPECHAR(x)
Definition trgm.h:54
#define ISWILDCARDCHAR(x)
Definition trgm.h:55

References buf, fb(), ISESCAPECHAR, ISWILDCARDCHAR, ISWORDCHR, LPADDING, pg_mblen_range(), RPADDING, and str.

Referenced by generate_wildcard_trgm().

◆ index_strategy_get_limit()

double index_strategy_get_limit ( StrategyNumber  strategy)

Definition at line 254 of file trgm_op.c.

257{
258 switch (strategy)
259 {
266 default:
267 elog(ERROR, "unrecognized strategy number: %d", strategy);
268 break;
269 }
270
#define elog(elevel,...)
Definition elog.h:226
#define WordSimilarityStrategyNumber
Definition trgm.h:35
#define StrictWordSimilarityStrategyNumber
Definition trgm.h:37
#define SimilarityStrategyNumber
Definition trgm.h:29

References elog, ERROR, similarity_threshold, SimilarityStrategyNumber, strict_word_similarity_threshold, StrictWordSimilarityStrategyNumber, word_similarity_threshold, and WordSimilarityStrategyNumber.

Referenced by gin_trgm_consistent(), gin_trgm_triconsistent(), and gtrgm_consistent().

◆ init_trgm_array()

static void init_trgm_array ( growable_trgm_array arr,
int  slen 
)
static

Definition at line 90 of file trgm_op.c.

93{
94 size_t init_size;
95
96 /*
97 * In the extreme case, the input string consists entirely of one
98 * character words, like "a b c", where each word is expanded to two
99 * trigrams. This is not a strict upper bound though, because when
100 * IGNORECASE is defined, we convert the input string to lowercase before
101 * extracting the trigrams, which in rare cases can expand one input
102 * character into multiple characters.
103 */
104 init_size = (size_t) slen + 1;
105
106 /*
107 * Guard against possible overflow in the palloc request. (We don't worry
108 * about the additive constants, since palloc can detect requests that are
109 * a little above MaxAllocSize --- we just need to prevent integer
110 * overflow in the multiplications.)
111 */
112 if (init_size > MaxAllocSize / sizeof(trgm))
115 errmsg("out of memory")));
116
118 arr->allocated = init_size;

References growable_trgm_array::allocated, ARRKEY, CALCGTSIZE, growable_trgm_array::datum, ereport, errcode(), errmsg(), ERROR, fb(), growable_trgm_array::length, MaxAllocSize, and palloc().

Referenced by generate_trgm_only(), and generate_wildcard_trgm().

◆ iterate_word_similarity()

static float4 iterate_word_similarity ( int trg2indexes,
bool found,
int  ulen1,
int  len2,
int  len,
uint8  flags,
TrgmBound bounds 
)
static

Definition at line 646 of file trgm_op.c.

655{
656 int *lastpos,
657 i,
658 ulen2 = 0,
659 count = 0,
660 upper = -1,
661 lower;
663 smlr_max = 0.0f;
664 double threshold;
665
666 Assert(bounds || !(flags & WORD_SIMILARITY_STRICT));
667
668 /* Select appropriate threshold */
672
673 /*
674 * Consider first trigram as initial lower bound for strict word
675 * similarity, or initialize it later with first trigram present for plain
676 * word similarity.
677 */
678 lower = (flags & WORD_SIMILARITY_STRICT) ? 0 : -1;
679
680 /* Memorise last position of each trigram */
681 lastpos = palloc_array(int, len);
682 memset(lastpos, -1, sizeof(int) * len);
683
684 for (i = 0; i < len2; i++)
685 {
686 int trgindex;
687
689
690 /* Get index of next trigram */
692
693 /* Update last position of this trigram */
694 if (lower >= 0 || found[trgindex])
695 {
696 if (lastpos[trgindex] < 0)
697 {
698 ulen2++;
699 if (found[trgindex])
700 count++;
701 }
702 lastpos[trgindex] = i;
703 }
704
705 /*
706 * Adjust upper bound if trigram is upper bound of word for strict
707 * word similarity, or if trigram is present in required substring for
708 * plain word similarity
709 */
711 : found[trgindex])
712 {
713 int prev_lower,
714 tmp_ulen2,
715 tmp_lower,
716 tmp_count;
717
718 upper = i;
719 if (lower == -1)
720 {
721 lower = i;
722 ulen2 = 1;
723 }
724
725 smlr_cur = CALCSML(count, ulen1, ulen2);
726
727 /* Also try to adjust lower bound for greater similarity */
728 tmp_count = count;
732 {
733 float smlr_tmp;
734 int tmp_trgindex;
735
736 /*
737 * Adjust lower bound only if trigram is lower bound of word
738 * for strict word similarity, or consider every trigram as
739 * lower bound for plain word similarity.
740 */
741 if (!(flags & WORD_SIMILARITY_STRICT)
743 {
745 if (smlr_tmp > smlr_cur)
746 {
750 count = tmp_count;
751 }
752
753 /*
754 * If we only check that word similarity is greater than
755 * threshold we do not need to calculate a maximum
756 * similarity.
757 */
758 if ((flags & WORD_SIMILARITY_CHECK_ONLY)
759 && smlr_cur >= threshold)
760 break;
761 }
762
765 {
766 tmp_ulen2--;
767 if (found[tmp_trgindex])
768 tmp_count--;
769 }
770 }
771
773
774 /*
775 * if we only check that word similarity is greater than threshold
776 * we do not need to calculate a maximum similarity.
777 */
779 break;
780
782 {
783 int tmp_trgindex;
784
787 lastpos[tmp_trgindex] = -1;
788 }
789 }
790 }
791
792 pfree(lastpos);
793
#define Max(x, y)
Definition c.h:1013
#define Assert(condition)
Definition c.h:885
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
#define CALCSML(count, len1, len2)
Definition trgm.h:107
#define WORD_SIMILARITY_CHECK_ONLY
Definition trgm_op.c:64

References Assert, CALCSML, CHECK_FOR_INTERRUPTS, fb(), i, len, lower(), Max, palloc_array, pfree(), strict_word_similarity_threshold, TRGM_BOUND_LEFT, TRGM_BOUND_RIGHT, upper(), WORD_SIMILARITY_CHECK_ONLY, WORD_SIMILARITY_STRICT, and word_similarity_threshold.

Referenced by calc_word_similarity().

◆ make_positional_trgm()

static pos_trgm * make_positional_trgm ( trgm trg1,
int  len1,
trgm trg2,
int  len2 
)
static

Definition at line 590 of file trgm_op.c.

593{
594 pos_trgm *result;
595 int i,
596 len = len1 + len2;
597
598 result = palloc_array(pos_trgm, len);
599
600 for (i = 0; i < len1; i++)
601 {
602 memcpy(&result[i].trg, &trg1[i], sizeof(trgm));
603 result[i].index = -1;
604 }
605
606 for (i = 0; i < len2; i++)
607 {
608 memcpy(&result[i + len1].trg, &trg2[i], sizeof(trgm));
609 result[i + len1].index = i;
610 }
611
int index
Definition trgm_op.c:55

References fb(), i, pos_trgm::index, len, and palloc_array.

Referenced by calc_word_similarity().

◆ make_trigrams()

static void make_trigrams ( growable_trgm_array dst,
char str,
int  bytelen 
)
static

Definition at line 354 of file trgm_op.c.

357{
358 trgm *tptr;
359 char *ptr = str;
360
361 if (bytelen < 3)
362 return;
363
364 /* max number of trigrams = strlen - 2 */
366 tptr = GETARR(dst->datum) + dst->length;
367
369 {
370 while (ptr < str + bytelen - 2)
371 {
372 CPTRGM(tptr, ptr);
373 ptr++;
374 tptr++;
375 }
376 }
377 else
378 {
379 int lenfirst,
380 lenmiddle,
381 lenlast;
382 char *endptr;
383
384 /*
385 * Fast path as long as there are no multibyte characters
386 */
387 if (!IS_HIGHBIT_SET(ptr[0]) && !IS_HIGHBIT_SET(ptr[1]))
388 {
389 while (!IS_HIGHBIT_SET(ptr[2]))
390 {
391 CPTRGM(tptr, ptr);
392 ptr++;
393 tptr++;
394
395 if (ptr == str + bytelen - 2)
396 goto done;
397 }
398
399 lenfirst = 1;
400 lenmiddle = 1;
401 lenlast = pg_mblen_unbounded(ptr + 2);
402 }
403 else
404 {
406 if (ptr + lenfirst >= str + bytelen)
407 goto done;
409 if (ptr + lenfirst + lenmiddle >= str + bytelen)
410 goto done;
412 }
413
414 /*
415 * Slow path to handle any remaining multibyte characters
416 *
417 * As we go, 'ptr' points to the beginning of the current
418 * three-character string and 'endptr' points to just past it.
419 */
420 endptr = ptr + lenfirst + lenmiddle + lenlast;
421 while (endptr <= str + bytelen)
422 {
423 compact_trigram(tptr, ptr, endptr - ptr);
424 tptr++;
425
426 /* Advance to the next character */
427 if (endptr == str + bytelen)
428 break;
429 ptr += lenfirst;
432 lenlast = pg_mblen_unbounded(endptr);
433 endptr += lenlast;
434 }
435 }
436
437done:
438 dst->length = tptr - GETARR(dst->datum);
#define IS_HIGHBIT_SET(ch)
Definition c.h:1172
int GetDatabaseEncoding(void)
Definition mbutils.c:1389
int pg_mblen_unbounded(const char *mbstr)
Definition mbutils.c:1137
void compact_trigram(trgm *tptr, char *str, int bytelen)
Definition trgm_op.c:329
static void enlarge_trgm_array(growable_trgm_array *arr, int needed)
Definition trgm_op.c:122
int pg_encoding_max_length(int encoding)
Definition wchar.c:2235

References Assert, compact_trigram(), CPTRGM, enlarge_trgm_array(), fb(), GETARR, GetDatabaseEncoding(), IS_HIGHBIT_SET, pg_encoding_max_length(), pg_mblen_unbounded(), and str.

Referenced by generate_trgm_only(), and generate_wildcard_trgm().

◆ PG_FUNCTION_INFO_V1() [1/16]

PG_FUNCTION_INFO_V1 ( set_limit  )

◆ PG_FUNCTION_INFO_V1() [2/16]

PG_FUNCTION_INFO_V1 ( show_limit  )

◆ PG_FUNCTION_INFO_V1() [3/16]

PG_FUNCTION_INFO_V1 ( show_trgm  )

◆ PG_FUNCTION_INFO_V1() [4/16]

PG_FUNCTION_INFO_V1 ( similarity  )

◆ PG_FUNCTION_INFO_V1() [5/16]

PG_FUNCTION_INFO_V1 ( similarity_dist  )

◆ PG_FUNCTION_INFO_V1() [6/16]

PG_FUNCTION_INFO_V1 ( similarity_op  )

◆ PG_FUNCTION_INFO_V1() [7/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity  )

◆ PG_FUNCTION_INFO_V1() [8/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [9/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [10/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [11/16]

PG_FUNCTION_INFO_V1 ( strict_word_similarity_op  )

◆ PG_FUNCTION_INFO_V1() [12/16]

PG_FUNCTION_INFO_V1 ( word_similarity  )

◆ PG_FUNCTION_INFO_V1() [13/16]

PG_FUNCTION_INFO_V1 ( word_similarity_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [14/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_commutator_op  )

◆ PG_FUNCTION_INFO_V1() [15/16]

PG_FUNCTION_INFO_V1 ( word_similarity_dist_op  )

◆ PG_FUNCTION_INFO_V1() [16/16]

PG_FUNCTION_INFO_V1 ( word_similarity_op  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "pg_trgm",
version = PG_VERSION 
)

◆ set_limit()

Datum set_limit ( PG_FUNCTION_ARGS  )

Definition at line 232 of file trgm_op.c.

235{
237 char *nlimit_str;
239 bool is_varlena;
240
242
244
245 SetConfigOption("pg_trgm.similarity_threshold", nlimit_str,
247
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition fmgr.c:1763
#define PG_GETARG_FLOAT4(n)
Definition fmgr.h:282
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
@ PGC_S_SESSION
Definition guc.h:126
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition lsyscache.c:3059
static Datum Float4GetDatum(float4 X)
Definition postgres.h:478
unsigned int Oid

References fb(), Float4GetDatum(), getTypeOutputInfo(), OidOutputFunctionCall(), PG_GETARG_FLOAT4, PG_RETURN_FLOAT4, PGC_S_SESSION, PGC_USERSET, SetConfigOption(), and similarity_threshold.

◆ show_limit()

Datum show_limit ( PG_FUNCTION_ARGS  )

Definition at line 277 of file trgm_op.c.

280{

References PG_RETURN_FLOAT4, and similarity_threshold.

◆ show_trgm()

Datum show_trgm ( PG_FUNCTION_ARGS  )

Definition at line 1126 of file trgm_op.c.

1129{
1130 text *in = PG_GETARG_TEXT_PP(0);
1131 TRGM *trg;
1132 Datum *d;
1133 ArrayType *a;
1134 trgm *ptr;
1135 int i;
1136
1138 d = palloc_array(Datum, 1 + ARRNELEM(trg));
1139
1140 for (i = 0, ptr = GETARR(trg); i < ARRNELEM(trg); i++, ptr++)
1141 {
1142 text *item = (text *) palloc(VARHDRSZ + Max(12, pg_database_encoding_max_length() * 3));
1143
1145 {
1146 snprintf(VARDATA(item), 12, "0x%06x", trgm2int(ptr));
1147 SET_VARSIZE(item, VARHDRSZ + strlen(VARDATA(item)));
1148 }
1149 else
1150 {
1151 SET_VARSIZE(item, VARHDRSZ + 3);
1152 CPTRGM(VARDATA(item), ptr);
1153 }
1154 d[i] = PointerGetDatum(item);
1155 }
1156
1158
1159 for (i = 0; i < ARRNELEM(trg); i++)
1160 pfree(DatumGetPointer(d[i]));
1161
1162 pfree(d);
1163 pfree(trg);
1164 PG_FREE_IF_COPY(in, 0);
1165
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
#define VARHDRSZ
Definition c.h:723
#define PG_FREE_IF_COPY(ptr, n)
Definition fmgr.h:260
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
int pg_database_encoding_max_length(void)
Definition mbutils.c:1674
#define snprintf
Definition port.h:260
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
Definition c.h:718
#define ISPRINTABLETRGM(t)
Definition trgm.h:52
TRGM * generate_trgm(char *str, int slen)
Definition trgm_op.c:554
uint32 trgm2int(trgm *ptr)
Definition trgm_op.c:1112
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References a, ARRNELEM, construct_array_builtin(), CPTRGM, DatumGetPointer(), fb(), generate_trgm(), GETARR, i, ISPRINTABLETRGM, Max, palloc(), palloc_array, pfree(), pg_database_encoding_max_length(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, PointerGetDatum(), SET_VARSIZE(), snprintf, trgm2int(), VARDATA(), VARDATA_ANY(), VARHDRSZ, and VARSIZE_ANY_EXHDR().

◆ similarity()

Datum similarity ( PG_FUNCTION_ARGS  )

Definition at line 1294 of file trgm_op.c.

1297{
1300 TRGM *trg1,
1301 *trg2;
1302 float4 res;
1303
1306
1307 res = cnt_sml(trg1, trg2, false);
1308
1309 pfree(trg1);
1310 pfree(trg2);
1311 PG_FREE_IF_COPY(in1, 0);
1312 PG_FREE_IF_COPY(in2, 1);
1313
float4 cnt_sml(TRGM *trg1, TRGM *trg2, bool inexact)
Definition trgm_op.c:1168

References cnt_sml(), fb(), generate_trgm(), pfree(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

Referenced by similarity_dist(), and similarity_op().

◆ similarity_dist()

Datum similarity_dist ( PG_FUNCTION_ARGS  )

Definition at line 1348 of file trgm_op.c.

1351{
1353 PG_GETARG_DATUM(0),
1354 PG_GETARG_DATUM(1)));
1355
#define DirectFunctionCall2(func, arg1, arg2)
Definition fmgr.h:686
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
static float4 DatumGetFloat4(Datum X)
Definition postgres.h:461
Datum similarity(PG_FUNCTION_ARGS)
Definition trgm_op.c:1294

References DatumGetFloat4(), DirectFunctionCall2, PG_GETARG_DATUM, PG_RETURN_FLOAT4, and similarity().

◆ similarity_op()

◆ strict_word_similarity()

Datum strict_word_similarity ( PG_FUNCTION_ARGS  )

Definition at line 1332 of file trgm_op.c.

1335{
1338 float4 res;
1339
1343
1344 PG_FREE_IF_COPY(in1, 0);
1345 PG_FREE_IF_COPY(in2, 1);
static float4 calc_word_similarity(char *str1, int slen1, char *str2, int slen2, uint8 flags)
Definition trgm_op.c:814

References calc_word_similarity(), fb(), PG_FREE_IF_COPY, PG_GETARG_TEXT_PP, PG_RETURN_FLOAT4, VARDATA_ANY(), VARSIZE_ANY_EXHDR(), and WORD_SIMILARITY_STRICT.

◆ strict_word_similarity_commutator_op()

◆ strict_word_similarity_dist_commutator_op()

◆ strict_word_similarity_dist_op()

◆ strict_word_similarity_op()

◆ trgm2int()

uint32 trgm2int ( trgm ptr)

Definition at line 1112 of file trgm_op.c.

1115{
1116 uint32 val = 0;
1117
1118 val |= *(((unsigned char *) ptr));
1119 val <<= 8;
1120 val |= *(((unsigned char *) ptr) + 1);
1121 val <<= 8;
1122 val |= *(((unsigned char *) ptr) + 2);
1123
uint32_t uint32
Definition c.h:558
long val
Definition informix.c:689

References val.

Referenced by gin_extract_query_trgm(), gin_extract_value_trgm(), and show_trgm().

◆ trgm_contained_by()

bool trgm_contained_by ( TRGM trg1,
TRGM trg2 
)

Definition at line 1216 of file trgm_op.c.

1219{
1220 trgm *ptr1,
1221 *ptr2;
1222 int len1,
1223 len2;
1224
1225 ptr1 = GETARR(trg1);
1226 ptr2 = GETARR(trg2);
1227
1228 len1 = ARRNELEM(trg1);
1229 len2 = ARRNELEM(trg2);
1230
1231 while (ptr1 - GETARR(trg1) < len1 && ptr2 - GETARR(trg2) < len2)
1232 {
1233 int res = CMPTRGM(ptr1, ptr2);
1234
1235 if (res < 0)
1236 return false;
1237 else if (res > 0)
1238 ptr2++;
1239 else
1240 {
1241 ptr1++;
1242 ptr2++;
1243 }
1244 }
1245 if (ptr1 - GETARR(trg1) < len1)
1246 return false;
1247 else

References ARRNELEM, CMPTRGM, fb(), and GETARR.

Referenced by gtrgm_consistent().

◆ trgm_presence_map()

bool * trgm_presence_map ( TRGM query,
TRGM key 
)

Definition at line 1255 of file trgm_op.c.

1258{
1259 bool *result;
1260 trgm *ptrq = GETARR(query),
1261 *ptrk = GETARR(key);
1262 int lenq = ARRNELEM(query),
1263 lenk = ARRNELEM(key),
1264 i;
1265
1266 result = palloc0_array(bool, lenq);
1267
1268 /* for each query trigram, do a binary search in the key array */
1269 for (i = 0; i < lenq; i++)
1270 {
1271 int lo = 0;
1272 int hi = lenk;
1273
1274 while (lo < hi)
1275 {
1276 int mid = (lo + hi) / 2;
1277 int res = CMPTRGM(ptrq, ptrk + mid);
1278
1279 if (res < 0)
1280 hi = mid;
1281 else if (res > 0)
1282 lo = mid + 1;
1283 else
1284 {
1285 result[i] = true;
1286 break;
1287 }
1288 }
1289 ptrq++;
1290 }
1291

References ARRNELEM, CMPTRGM, fb(), GETARR, i, and palloc0_array.

Referenced by gtrgm_consistent().

◆ word_similarity()

◆ word_similarity_commutator_op()

◆ word_similarity_dist_commutator_op()

Datum word_similarity_dist_commutator_op ( PG_FUNCTION_ARGS  )

◆ word_similarity_dist_op()

Datum word_similarity_dist_op ( PG_FUNCTION_ARGS  )

◆ word_similarity_op()

Variable Documentation

◆ CMPTRGM

◆ similarity_threshold

double similarity_threshold = 0.3f

Definition at line 27 of file trgm_op.c.

Referenced by _PG_init(), index_strategy_get_limit(), set_limit(), show_limit(), and similarity_op().

◆ strict_word_similarity_threshold

double strict_word_similarity_threshold = 0.5f

◆ word_similarity_threshold

double word_similarity_threshold = 0.6f