PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
tsquery_gist.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tsquery_gist.c
4  * GiST index support for tsquery
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  *
8  *
9  * IDENTIFICATION
10  * src/backend/utils/adt/tsquery_gist.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "access/stratnum.h"
18 #include "access/gist.h"
19 #include "tsearch/ts_utils.h"
20 #include "utils/builtins.h"
21 
22 #define GETENTRY(vec,pos) DatumGetTSQuerySign((vec)->vector[pos].key)
23 
24 
25 Datum
27 {
28  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
29  GISTENTRY *retval = entry;
30 
31  if (entry->leafkey)
32  {
34 
35  retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
36  sign = makeTSQuerySign(DatumGetTSQuery(entry->key));
37 
38  gistentryinit(*retval, TSQuerySignGetDatum(sign),
39  entry->rel, entry->page,
40  entry->offset, FALSE);
41  }
42 
43  PG_RETURN_POINTER(retval);
44 }
45 
46 Datum
48 {
50 }
51 
52 Datum
54 {
55  GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
56  TSQuery query = PG_GETARG_TSQUERY(1);
58 
59  /* Oid subtype = PG_GETARG_OID(3); */
60  bool *recheck = (bool *) PG_GETARG_POINTER(4);
61  TSQuerySign key = DatumGetTSQuerySign(entry->key);
62  TSQuerySign sq = makeTSQuerySign(query);
63  bool retval;
64 
65  /* All cases served by this function are inexact */
66  *recheck = true;
67 
68  switch (strategy)
69  {
71  if (GIST_LEAF(entry))
72  retval = (key & sq) == sq;
73  else
74  retval = (key & sq) != 0;
75  break;
77  if (GIST_LEAF(entry))
78  retval = (key & sq) == key;
79  else
80  retval = (key & sq) != 0;
81  break;
82  default:
83  retval = FALSE;
84  }
85  PG_RETURN_BOOL(retval);
86 }
87 
88 Datum
90 {
92  int *size = (int *) PG_GETARG_POINTER(1);
94  int i;
95 
96  sign = 0;
97 
98  for (i = 0; i < entryvec->n; i++)
99  sign |= GETENTRY(entryvec, i);
100 
101  *size = sizeof(TSQuerySign);
102 
103  PG_RETURN_TSQUERYSIGN(sign);
104 }
105 
106 Datum
108 {
111  bool *result = (bool *) PG_GETARG_POINTER(2);
112 
113  *result = (a == b) ? true : false;
114 
115  PG_RETURN_POINTER(result);
116 }
117 
118 static int
120 {
121  int size = 0,
122  i;
123 
124  for (i = 0; i < TSQS_SIGLEN; i++)
125  size += 0x01 & (sign >> i);
126 
127  return size;
128 }
129 
130 static int
132 {
133  TSQuerySign res = a ^ b;
134 
135  return sizebitvec(res);
136 }
137 
138 Datum
140 {
143  float *penalty = (float *) PG_GETARG_POINTER(2);
144 
145  *penalty = hemdist(origval, newval);
146 
147  PG_RETURN_POINTER(penalty);
148 }
149 
150 
151 typedef struct
152 {
153  OffsetNumber pos;
154  int32 cost;
155 } SPLITCOST;
156 
157 static int
158 comparecost(const void *a, const void *b)
159 {
160  if (((const SPLITCOST *) a)->cost == ((const SPLITCOST *) b)->cost)
161  return 0;
162  else
163  return (((const SPLITCOST *) a)->cost > ((const SPLITCOST *) b)->cost) ? 1 : -1;
164 }
165 
166 #define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
167 
168 Datum
170 {
173  OffsetNumber maxoff = entryvec->n - 2;
174  OffsetNumber k,
175  j;
176  TSQuerySign datum_l,
177  datum_r;
178  int32 size_alpha,
179  size_beta;
180  int32 size_waste,
181  waste = -1;
182  int32 nbytes;
183  OffsetNumber seed_1 = 0,
184  seed_2 = 0;
186  *right;
187 
188  SPLITCOST *costvector;
189 
190  nbytes = (maxoff + 2) * sizeof(OffsetNumber);
191  left = v->spl_left = (OffsetNumber *) palloc(nbytes);
192  right = v->spl_right = (OffsetNumber *) palloc(nbytes);
193  v->spl_nleft = v->spl_nright = 0;
194 
195  for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
196  for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
197  {
198  size_waste = hemdist(GETENTRY(entryvec, j), GETENTRY(entryvec, k));
199  if (size_waste > waste)
200  {
201  waste = size_waste;
202  seed_1 = k;
203  seed_2 = j;
204  }
205  }
206 
207 
208  if (seed_1 == 0 || seed_2 == 0)
209  {
210  seed_1 = 1;
211  seed_2 = 2;
212  }
213 
214  datum_l = GETENTRY(entryvec, seed_1);
215  datum_r = GETENTRY(entryvec, seed_2);
216 
217  maxoff = OffsetNumberNext(maxoff);
218  costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
219  for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
220  {
221  costvector[j - 1].pos = j;
222  size_alpha = hemdist(GETENTRY(entryvec, seed_1), GETENTRY(entryvec, j));
223  size_beta = hemdist(GETENTRY(entryvec, seed_2), GETENTRY(entryvec, j));
224  costvector[j - 1].cost = abs(size_alpha - size_beta);
225  }
226  qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
227 
228  for (k = 0; k < maxoff; k++)
229  {
230  j = costvector[k].pos;
231  if (j == seed_1)
232  {
233  *left++ = j;
234  v->spl_nleft++;
235  continue;
236  }
237  else if (j == seed_2)
238  {
239  *right++ = j;
240  v->spl_nright++;
241  continue;
242  }
243  size_alpha = hemdist(datum_l, GETENTRY(entryvec, j));
244  size_beta = hemdist(datum_r, GETENTRY(entryvec, j));
245 
246  if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.05))
247  {
248  datum_l |= GETENTRY(entryvec, j);
249  *left++ = j;
250  v->spl_nleft++;
251  }
252  else
253  {
254  datum_r |= GETENTRY(entryvec, j);
255  *right++ = j;
256  v->spl_nright++;
257  }
258  }
259 
260  *right = *left = FirstOffsetNumber;
261  v->spl_ldatum = TSQuerySignGetDatum(datum_l);
262  v->spl_rdatum = TSQuerySignGetDatum(datum_r);
263 
265 }
266 
267 /*
268  * Formerly, gtsquery_consistent was declared in pg_proc.h with arguments
269  * that did not match the documented conventions for GiST support functions.
270  * We fixed that, but we still need a pg_proc entry with the old signature
271  * to support reloading pre-9.6 contrib/tsearch2 opclass declarations.
272  * This compatibility function should go away eventually.
273  */
274 Datum
276 {
277  return gtsquery_consistent(fcinfo);
278 }
#define GIST_LEAF(entry)
Definition: gist.h:133
Relation rel
Definition: gist.h:124
static int sizebitvec(TSQuerySign sign)
Definition: tsquery_gist.c:119
#define DatumGetTSQuery(X)
Definition: ts_type.h:235
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
OffsetNumber pos
Definition: hstore_gist.c:323
#define PG_RETURN_TSQUERYSIGN(X)
Definition: ts_utils.h:221
#define TSQS_SIGLEN
Definition: ts_utils.h:217
struct NODE * left
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
#define PG_GETARG_TSQUERY(n)
Definition: ts_type.h:238
OffsetNumber * spl_left
Definition: gist.h:105
Datum spl_rdatum
Definition: gist.h:112
int32 n
Definition: gist.h:160
uint16 StrategyNumber
Definition: stratnum.h:22
struct NODE * right
return result
Definition: formatting.c:1633
#define RTContainedByStrategyNumber
Definition: stratnum.h:51
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:241
int spl_nleft
Definition: gist.h:106
signed int int32
Definition: c.h:256
#define TSQuerySignGetDatum(X)
Definition: ts_utils.h:219
int32 cost
Definition: hstore_gist.c:324
uint16 OffsetNumber
Definition: off.h:24
Page page
Definition: gist.h:125
#define FALSE
Definition: c.h:221
int spl_nright
Definition: gist.h:111
TSQuerySign makeTSQuerySign(TSQuery a)
Definition: tsquery_op.c:249
char sign
Definition: informix.c:693
Datum key
Definition: gist.h:123
uint64 TSQuerySign
Definition: ts_utils.h:215
#define FirstOffsetNumber
Definition: off.h:27
Datum gtsquery_picksplit(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:169
Datum gtsquery_compress(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:26
Datum gtsquery_penalty(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:139
bool leafkey
Definition: gist.h:127
#define WISH_F(a, b, c)
Definition: tsquery_gist.c:166
Datum gtsquery_union(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:89
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
uintptr_t Datum
Definition: postgres.h:372
static int comparecost(const void *a, const void *b)
Definition: tsquery_gist.c:158
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Datum gtsquery_consistent_oldsig(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:275
Datum gtsquery_consistent(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:53
#define PG_GETARG_TSQUERYSIGN(n)
Definition: ts_utils.h:222
Datum spl_ldatum
Definition: gist.h:107
Datum gtsquery_same(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:107
#define gistentryinit(e, k, r, pg, o, l)
Definition: gist.h:169
#define OffsetNumberNext(offsetNumber)
Definition: off.h:53
#define newval
OffsetNumber * spl_right
Definition: gist.h:110
#define RTContainsStrategyNumber
Definition: stratnum.h:50
#define PG_GETARG_UINT16(n)
Definition: fmgr.h:237
void * palloc(Size size)
Definition: mcxt.c:849
int i
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define GETENTRY(vec, pos)
Definition: tsquery_gist.c:22
#define qsort(a, b, c, d)
Definition: port.h:443
static int hemdist(TSQuerySign a, TSQuerySign b)
Definition: tsquery_gist.c:131
OffsetNumber offset
Definition: gist.h:126
#define DatumGetTSQuerySign(X)
Definition: ts_utils.h:220
Datum gtsquery_decompress(PG_FUNCTION_ARGS)
Definition: tsquery_gist.c:47