PostgreSQL Source Code  git master
trgm_gin.c File Reference
#include "postgres.h"
#include "access/gin.h"
#include "access/stratnum.h"
#include "fmgr.h"
#include "trgm.h"
Include dependency graph for trgm_gin.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (gin_extract_trgm)
 
 PG_FUNCTION_INFO_V1 (gin_extract_value_trgm)
 
 PG_FUNCTION_INFO_V1 (gin_extract_query_trgm)
 
 PG_FUNCTION_INFO_V1 (gin_trgm_consistent)
 
 PG_FUNCTION_INFO_V1 (gin_trgm_triconsistent)
 
Datum gin_extract_trgm (PG_FUNCTION_ARGS)
 
Datum gin_extract_value_trgm (PG_FUNCTION_ARGS)
 
Datum gin_extract_query_trgm (PG_FUNCTION_ARGS)
 
Datum gin_trgm_consistent (PG_FUNCTION_ARGS)
 
Datum gin_trgm_triconsistent (PG_FUNCTION_ARGS)
 

Function Documentation

◆ gin_extract_query_trgm()

Datum gin_extract_query_trgm ( PG_FUNCTION_ARGS  )

Definition at line 69 of file trgm_gin.c.

References ARRNELEM, createTrgmNFA(), CurrentMemoryContext, elog, EqualStrategyNumber, ERROR, generate_trgm(), generate_wildcard_trgm(), GETARR, GIN_SEARCH_MODE_ALL, i, ILikeStrategyNumber, Int32GetDatum, LikeStrategyNumber, palloc(), PG_GET_COLLATION, PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_GETARG_UINT16, PG_RETURN_POINTER, RegExpICaseStrategyNumber, RegExpStrategyNumber, SimilarityStrategyNumber, StrictWordSimilarityStrategyNumber, trgm2int(), val, VARDATA_ANY, VARSIZE_ANY_EXHDR, and WordSimilarityStrategyNumber.

Referenced by gin_extract_trgm().

70 {
71  text *val = (text *) PG_GETARG_TEXT_PP(0);
72  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
73  StrategyNumber strategy = PG_GETARG_UINT16(2);
74 
75  /* bool **pmatch = (bool **) PG_GETARG_POINTER(3); */
76  Pointer **extra_data = (Pointer **) PG_GETARG_POINTER(4);
77 
78  /* bool **nullFlags = (bool **) PG_GETARG_POINTER(5); */
79  int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
80  Datum *entries = NULL;
81  TRGM *trg;
82  int32 trglen;
83  trgm *ptr;
84  TrgmPackedGraph *graph;
85  int32 i;
86 
87  switch (strategy)
88  {
94  break;
96 #ifndef IGNORECASE
97  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
98 #endif
99  /* FALL THRU */
100  case LikeStrategyNumber:
101 
102  /*
103  * For wildcard search we extract all the trigrams that every
104  * potentially-matching string must include.
105  */
107  VARSIZE_ANY_EXHDR(val));
108  break;
110 #ifndef IGNORECASE
111  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
112 #endif
113  /* FALL THRU */
115  trg = createTrgmNFA(val, PG_GET_COLLATION(),
116  &graph, CurrentMemoryContext);
117  if (trg && ARRNELEM(trg) > 0)
118  {
119  /*
120  * Successful regex processing: store NFA-like graph as
121  * extra_data. GIN API requires an array of nentries
122  * Pointers, but we just put the same value in each element.
123  */
124  trglen = ARRNELEM(trg);
125  *extra_data = (Pointer *) palloc(sizeof(Pointer) * trglen);
126  for (i = 0; i < trglen; i++)
127  (*extra_data)[i] = (Pointer) graph;
128  }
129  else
130  {
131  /* No result: have to do full index scan. */
132  *nentries = 0;
133  *searchMode = GIN_SEARCH_MODE_ALL;
134  PG_RETURN_POINTER(entries);
135  }
136  break;
137  default:
138  elog(ERROR, "unrecognized strategy number: %d", strategy);
139  trg = NULL; /* keep compiler quiet */
140  break;
141  }
142 
143  trglen = ARRNELEM(trg);
144  *nentries = trglen;
145 
146  if (trglen > 0)
147  {
148  entries = (Datum *) palloc(sizeof(Datum) * trglen);
149  ptr = GETARR(trg);
150  for (i = 0; i < trglen; i++)
151  {
152  int32 item = trgm2int(ptr);
153 
154  entries[i] = Int32GetDatum(item);
155  ptr++;
156  }
157  }
158 
159  /*
160  * If no trigram was extracted then we have to scan all the index.
161  */
162  if (trglen == 0)
163  *searchMode = GIN_SEARCH_MODE_ALL;
164 
165  PG_RETURN_POINTER(entries);
166 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define RegExpStrategyNumber
Definition: trgm.h:34
TRGM * generate_wildcard_trgm(const char *str, int slen)
Definition: trgm_op.c:866
#define SimilarityStrategyNumber
Definition: trgm.h:30
TRGM * createTrgmNFA(text *text_re, Oid collation, TrgmPackedGraph **graph, MemoryContext rcontext)
Definition: trgm_regexp.c:521
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define RegExpICaseStrategyNumber
Definition: trgm.h:35
uint16 StrategyNumber
Definition: stratnum.h:22
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define ARRNELEM(x)
Definition: trgm.h:108
#define PG_GET_COLLATION()
Definition: fmgr.h:198
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define GETARR(x)
Definition: trgm.h:107
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:36
char * Pointer
Definition: c.h:418
#define ERROR
Definition: elog.h:45
Definition: trgm.h:67
#define EqualStrategyNumber
Definition: trgm.h:40
#define LikeStrategyNumber
Definition: trgm.h:32
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:935
uintptr_t Datum
Definition: postgres.h:367
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
char trgm[3]
Definition: trgm.h:42
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define Int32GetDatum(X)
Definition: postgres.h:479
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
int i
Definition: c.h:621
#define ILikeStrategyNumber
Definition: trgm.h:33
long val
Definition: informix.c:664

◆ gin_extract_trgm()

Datum gin_extract_trgm ( PG_FUNCTION_ARGS  )

Definition at line 23 of file trgm_gin.c.

References elog, ERROR, gin_extract_query_trgm(), gin_extract_value_trgm(), PG_NARGS, and PG_RETURN_NULL.

24 {
25  if (PG_NARGS() == 3)
26  return gin_extract_value_trgm(fcinfo);
27  if (PG_NARGS() == 7)
28  return gin_extract_query_trgm(fcinfo);
29  elog(ERROR, "unexpected number of arguments to gin_extract_trgm");
31 }
#define ERROR
Definition: elog.h:45
Datum gin_extract_query_trgm(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:69
Datum gin_extract_value_trgm(PG_FUNCTION_ARGS)
Definition: trgm_gin.c:34
#define PG_NARGS()
Definition: fmgr.h:203
#define elog(elevel,...)
Definition: elog.h:227
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ gin_extract_value_trgm()

Datum gin_extract_value_trgm ( PG_FUNCTION_ARGS  )

Definition at line 34 of file trgm_gin.c.

References ARRNELEM, generate_trgm(), GETARR, i, Int32GetDatum, palloc(), PG_GETARG_POINTER, PG_GETARG_TEXT_PP, PG_RETURN_POINTER, trgm2int(), val, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gin_extract_trgm().

35 {
36  text *val = (text *) PG_GETARG_TEXT_PP(0);
37  int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
38  Datum *entries = NULL;
39  TRGM *trg;
40  int32 trglen;
41 
42  *nentries = 0;
43 
45  trglen = ARRNELEM(trg);
46 
47  if (trglen > 0)
48  {
49  trgm *ptr;
50  int32 i;
51 
52  *nentries = trglen;
53  entries = (Datum *) palloc(sizeof(Datum) * trglen);
54 
55  ptr = GETARR(trg);
56  for (i = 0; i < trglen; i++)
57  {
58  int32 item = trgm2int(ptr);
59 
60  entries[i] = Int32GetDatum(item);
61  ptr++;
62  }
63  }
64 
65  PG_RETURN_POINTER(entries);
66 }
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define ARRNELEM(x)
Definition: trgm.h:108
signed int int32
Definition: c.h:429
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define GETARR(x)
Definition: trgm.h:107
Definition: trgm.h:67
uint32 trgm2int(trgm *ptr)
Definition: trgm_op.c:935
uintptr_t Datum
Definition: postgres.h:367
char trgm[3]
Definition: trgm.h:42
TRGM * generate_trgm(char *str, int slen)
Definition: trgm_op.c:356
#define Int32GetDatum(X)
Definition: postgres.h:479
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:950
int i
Definition: c.h:621
long val
Definition: informix.c:664

◆ gin_trgm_consistent()

Datum gin_trgm_consistent ( PG_FUNCTION_ARGS  )

Definition at line 169 of file trgm_gin.c.

References elog, EqualStrategyNumber, ERROR, i, ILikeStrategyNumber, index_strategy_get_limit(), LikeStrategyNumber, PG_GETARG_INT32, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_BOOL, RegExpICaseStrategyNumber, RegExpStrategyNumber, SimilarityStrategyNumber, StrictWordSimilarityStrategyNumber, trigramsMatchGraph(), and WordSimilarityStrategyNumber.

170 {
171  bool *check = (bool *) PG_GETARG_POINTER(0);
172  StrategyNumber strategy = PG_GETARG_UINT16(1);
173 
174  /* text *query = PG_GETARG_TEXT_PP(2); */
175  int32 nkeys = PG_GETARG_INT32(3);
176  Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
177  bool *recheck = (bool *) PG_GETARG_POINTER(5);
178  bool res;
179  int32 i,
180  ntrue;
181  double nlimit;
182 
183  /* All cases served by this function are inexact */
184  *recheck = true;
185 
186  switch (strategy)
187  {
191  nlimit = index_strategy_get_limit(strategy);
192 
193  /* Count the matches */
194  ntrue = 0;
195  for (i = 0; i < nkeys; i++)
196  {
197  if (check[i])
198  ntrue++;
199  }
200 
201  /*--------------------
202  * If DIVUNION is defined then similarity formula is:
203  * c / (len1 + len2 - c)
204  * where c is number of common trigrams and it stands as ntrue in
205  * this code. Here we don't know value of len2 but we can assume
206  * that c (ntrue) is a lower bound of len2, so upper bound of
207  * similarity is:
208  * c / (len1 + c - c) => c / len1
209  * If DIVUNION is not defined then similarity formula is:
210  * c / max(len1, len2)
211  * And again, c (ntrue) is a lower bound of len2, but c <= len1
212  * just by definition and, consequently, upper bound of
213  * similarity is just c / len1.
214  * So, independently on DIVUNION the upper bound formula is the same.
215  */
216  res = (nkeys == 0) ? false :
217  (((((float4) ntrue) / ((float4) nkeys))) >= nlimit);
218  break;
219  case ILikeStrategyNumber:
220 #ifndef IGNORECASE
221  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
222 #endif
223  /* FALL THRU */
224  case LikeStrategyNumber:
225  case EqualStrategyNumber:
226  /* Check if all extracted trigrams are presented. */
227  res = true;
228  for (i = 0; i < nkeys; i++)
229  {
230  if (!check[i])
231  {
232  res = false;
233  break;
234  }
235  }
236  break;
238 #ifndef IGNORECASE
239  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
240 #endif
241  /* FALL THRU */
243  if (nkeys < 1)
244  {
245  /* Regex processing gave no result: do full index scan */
246  res = true;
247  }
248  else
249  res = trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
250  check);
251  break;
252  default:
253  elog(ERROR, "unrecognized strategy number: %d", strategy);
254  res = false; /* keep compiler quiet */
255  break;
256  }
257 
258  PG_RETURN_BOOL(res);
259 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
Definition: trgm_regexp.c:636
#define RegExpStrategyNumber
Definition: trgm.h:34
#define SimilarityStrategyNumber
Definition: trgm.h:30
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define RegExpICaseStrategyNumber
Definition: trgm.h:35
uint16 StrategyNumber
Definition: stratnum.h:22
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:429
char * Pointer
Definition: c.h:418
#define ERROR
Definition: elog.h:45
#define EqualStrategyNumber
Definition: trgm.h:40
#define LikeStrategyNumber
Definition: trgm.h:32
double index_strategy_get_limit(StrategyNumber strategy)
Definition: trgm_op.c:132
float float4
Definition: c.h:564
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
#define elog(elevel,...)
Definition: elog.h:227
int i
#define ILikeStrategyNumber
Definition: trgm.h:33

◆ gin_trgm_triconsistent()

Datum gin_trgm_triconsistent ( PG_FUNCTION_ARGS  )

Definition at line 268 of file trgm_gin.c.

References Assert, elog, EqualStrategyNumber, ERROR, GIN_FALSE, GIN_MAYBE, GIN_TRUE, i, ILikeStrategyNumber, index_strategy_get_limit(), LikeStrategyNumber, palloc(), pfree(), PG_GETARG_INT32, PG_GETARG_POINTER, PG_GETARG_UINT16, PG_RETURN_GIN_TERNARY_VALUE, RegExpICaseStrategyNumber, RegExpStrategyNumber, SimilarityStrategyNumber, StrictWordSimilarityStrategyNumber, trigramsMatchGraph(), and WordSimilarityStrategyNumber.

269 {
271  StrategyNumber strategy = PG_GETARG_UINT16(1);
272 
273  /* text *query = PG_GETARG_TEXT_PP(2); */
274  int32 nkeys = PG_GETARG_INT32(3);
275  Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4);
277  int32 i,
278  ntrue;
279  bool *boolcheck;
280  double nlimit;
281 
282  switch (strategy)
283  {
287  nlimit = index_strategy_get_limit(strategy);
288 
289  /* Count the matches */
290  ntrue = 0;
291  for (i = 0; i < nkeys; i++)
292  {
293  if (check[i] != GIN_FALSE)
294  ntrue++;
295  }
296 
297  /*
298  * See comment in gin_trgm_consistent() about * upper bound
299  * formula
300  */
301  res = (nkeys == 0)
302  ? GIN_FALSE : (((((float4) ntrue) / ((float4) nkeys)) >= nlimit)
303  ? GIN_MAYBE : GIN_FALSE);
304  break;
305  case ILikeStrategyNumber:
306 #ifndef IGNORECASE
307  elog(ERROR, "cannot handle ~~* with case-sensitive trigrams");
308 #endif
309  /* FALL THRU */
310  case LikeStrategyNumber:
311  case EqualStrategyNumber:
312  /* Check if all extracted trigrams are presented. */
313  res = GIN_MAYBE;
314  for (i = 0; i < nkeys; i++)
315  {
316  if (check[i] == GIN_FALSE)
317  {
318  res = GIN_FALSE;
319  break;
320  }
321  }
322  break;
324 #ifndef IGNORECASE
325  elog(ERROR, "cannot handle ~* with case-sensitive trigrams");
326 #endif
327  /* FALL THRU */
329  if (nkeys < 1)
330  {
331  /* Regex processing gave no result: do full index scan */
332  res = GIN_MAYBE;
333  }
334  else
335  {
336  /*
337  * As trigramsMatchGraph implements a monotonic boolean
338  * function, promoting all GIN_MAYBE keys to GIN_TRUE will
339  * give a conservative result.
340  */
341  boolcheck = (bool *) palloc(sizeof(bool) * nkeys);
342  for (i = 0; i < nkeys; i++)
343  boolcheck[i] = (check[i] != GIN_FALSE);
344  if (!trigramsMatchGraph((TrgmPackedGraph *) extra_data[0],
345  boolcheck))
346  res = GIN_FALSE;
347  pfree(boolcheck);
348  }
349  break;
350  default:
351  elog(ERROR, "unrecognized strategy number: %d", strategy);
352  res = GIN_FALSE; /* keep compiler quiet */
353  break;
354  }
355 
356  /* All cases served by this function are inexact */
357  Assert(res != GIN_TRUE);
359 }
#define GIN_TRUE
Definition: gin.h:61
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
bool trigramsMatchGraph(TrgmPackedGraph *graph, bool *check)
Definition: trgm_regexp.c:636
#define RegExpStrategyNumber
Definition: trgm.h:34
#define SimilarityStrategyNumber
Definition: trgm.h:30
#define GIN_MAYBE
Definition: gin.h:62
#define StrictWordSimilarityStrategyNumber
Definition: trgm.h:38
#define RegExpICaseStrategyNumber
Definition: trgm.h:35
uint16 StrategyNumber
Definition: stratnum.h:22
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
signed int int32
Definition: c.h:429
#define PG_RETURN_GIN_TERNARY_VALUE(x)
Definition: gin.h:67
void pfree(void *pointer)
Definition: mcxt.c:1057
char * Pointer
Definition: c.h:418
#define ERROR
Definition: elog.h:45
#define EqualStrategyNumber
Definition: trgm.h:40
char GinTernaryValue
Definition: gin.h:58
#define LikeStrategyNumber
Definition: trgm.h:32
double index_strategy_get_limit(StrategyNumber strategy)
Definition: trgm_op.c:132
float float4
Definition: c.h:564
#define WordSimilarityStrategyNumber
Definition: trgm.h:36
#define Assert(condition)
Definition: c.h:804
#define GIN_FALSE
Definition: gin.h:60
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:272
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
int i
#define ILikeStrategyNumber
Definition: trgm.h:33

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( gin_extract_trgm  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( gin_extract_value_trgm  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( gin_extract_query_trgm  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( gin_trgm_consistent  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( gin_trgm_triconsistent  )