]> granicus.if.org Git - postgresql/blob - src/include/tsearch/ts_type.h
Remove the aggregate form of ts_rewrite(), since it doesn't work as desired
[postgresql] / src / include / tsearch / ts_type.h
1 /*-------------------------------------------------------------------------
2  *
3  * ts_type.h
4  *        Definitions for the tsvector and tsquery types
5  *
6  * Copyright (c) 1998-2007, PostgreSQL Global Development Group
7  *
8  * $PostgreSQL: pgsql/src/include/tsearch/ts_type.h,v 1.7 2007/10/24 02:24:49 tgl Exp $
9  *
10  *-------------------------------------------------------------------------
11  */
12 #ifndef _PG_TSTYPE_H_
13 #define _PG_TSTYPE_H_
14
15 #include "fmgr.h"
16 #include "utils/pg_crc.h"
17
18
19 /*
20  * TSVector type.
21  * Note, tsvectorsend/recv believe that sizeof(WordEntry) == 4
22  */
23
24 typedef struct
25 {
26         uint32
27                                 haspos:1,
28                                 len:11,                 /* MAX 2Kb */
29                                 pos:20;                 /* MAX 1Mb */
30 } WordEntry;
31
32 #define MAXSTRLEN ( (1<<11) - 1)
33 #define MAXSTRPOS ( (1<<20) - 1)
34
35 /*
36  * Equivalent to
37  * typedef struct {
38  *              uint16
39  *                      weight:2,
40  *                      pos:14;
41  * }
42  */
43
44 typedef uint16 WordEntryPos;
45
46 typedef struct
47 {
48         uint16 npos;
49         WordEntryPos pos[1]; /* var length */
50 } WordEntryPosVector;
51
52
53 #define WEP_GETWEIGHT(x)        ( (x) >> 14 )
54 #define WEP_GETPOS(x)           ( (x) & 0x3fff )
55
56 #define WEP_SETWEIGHT(x,v)  ( (x) = ( (v) << 14 ) | ( (x) & 0x3fff ) )
57 #define WEP_SETPOS(x,v)         ( (x) = ( (x) & 0xc000 ) | ( (v) & 0x3fff ) )
58
59 #define MAXENTRYPOS (1<<14)
60 #define MAXNUMPOS       (256)
61 #define LIMITPOS(x) ( ( (x) >= MAXENTRYPOS ) ? (MAXENTRYPOS-1) : (x) )
62
63 /*
64  * Structure of tsvector datatype:
65  * 1) standard varlena header
66  * 2) int4              size - number of lexemes or WordEntry array, which is the same
67  * 3) Array of WordEntry - sorted array, comparison based on word's length
68  *                                                      and strncmp(). WordEntry->pos points number of
69  *                                                      bytes from end of WordEntry array to start of
70  *                                                      corresponding lexeme.
71  * 4) Lexeme's storage:
72  *        lexeme (without null-terminator)
73  *    if haspos is true:
74  *              padding byte if necessary to make the number of positions 2-byte aligned
75  *              uint16          number of positions that follow.
76  *              uint16[]        positions
77  *
78  * The positions must be sorted.
79  */
80
81 typedef struct
82 {
83         int32           vl_len_;                /* varlena header (do not touch directly!) */
84         int32           size;
85         WordEntry       entries[1]; /* var size */
86         /* lexemes follow */
87 } TSVectorData;
88
89 typedef TSVectorData *TSVector;
90
91 #define DATAHDRSIZE (offsetof(TSVectorData, entries))
92 #define CALCDATASIZE(x, lenstr) (DATAHDRSIZE + (x) * sizeof(WordEntry) + (lenstr) )
93 #define ARRPTR(x)       ( (x)->entries )
94
95 /* returns a pointer to the beginning of lexemes */
96 #define STRPTR(x)       ( (char *) &(x)->entries[x->size] )
97
98 #define _POSVECPTR(x, e)        ((WordEntryPosVector *)(STRPTR(x) + SHORTALIGN((e)->pos + (e)->len)))
99 #define POSDATALEN(x,e) ( ( (e)->haspos ) ? (_POSVECPTR(x,e)->npos) : 0 )
100 #define POSDATAPTR(x,e) (_POSVECPTR(x,e)->pos)
101
102 /*
103  * fmgr interface macros
104  */
105
106 #define DatumGetTSVector(X)                     ((TSVector) PG_DETOAST_DATUM(X))
107 #define DatumGetTSVectorCopy(X)         ((TSVector) PG_DETOAST_DATUM_COPY(X))
108 #define TSVectorGetDatum(X)                     PointerGetDatum(X)
109 #define PG_GETARG_TSVECTOR(n)           DatumGetTSVector(PG_GETARG_DATUM(n))
110 #define PG_GETARG_TSVECTOR_COPY(n)      DatumGetTSVectorCopy(PG_GETARG_DATUM(n))
111 #define PG_RETURN_TSVECTOR(x)           return TSVectorGetDatum(x)
112
113 /*
114  * I/O
115  */
116 extern Datum tsvectorin(PG_FUNCTION_ARGS);
117 extern Datum tsvectorout(PG_FUNCTION_ARGS);
118 extern Datum tsvectorsend(PG_FUNCTION_ARGS);
119 extern Datum tsvectorrecv(PG_FUNCTION_ARGS);
120
121 /*
122  * operations with tsvector
123  */
124 extern Datum tsvector_lt(PG_FUNCTION_ARGS);
125 extern Datum tsvector_le(PG_FUNCTION_ARGS);
126 extern Datum tsvector_eq(PG_FUNCTION_ARGS);
127 extern Datum tsvector_ne(PG_FUNCTION_ARGS);
128 extern Datum tsvector_ge(PG_FUNCTION_ARGS);
129 extern Datum tsvector_gt(PG_FUNCTION_ARGS);
130 extern Datum tsvector_cmp(PG_FUNCTION_ARGS);
131
132 extern Datum tsvector_length(PG_FUNCTION_ARGS);
133 extern Datum tsvector_strip(PG_FUNCTION_ARGS);
134 extern Datum tsvector_setweight(PG_FUNCTION_ARGS);
135 extern Datum tsvector_concat(PG_FUNCTION_ARGS);
136 extern Datum tsvector_update_trigger_byid(PG_FUNCTION_ARGS);
137 extern Datum tsvector_update_trigger_bycolumn(PG_FUNCTION_ARGS);
138
139 extern Datum ts_match_vq(PG_FUNCTION_ARGS);
140 extern Datum ts_match_qv(PG_FUNCTION_ARGS);
141 extern Datum ts_match_tt(PG_FUNCTION_ARGS);
142 extern Datum ts_match_tq(PG_FUNCTION_ARGS);
143
144 extern Datum ts_stat1(PG_FUNCTION_ARGS);
145 extern Datum ts_stat2(PG_FUNCTION_ARGS);
146
147 extern Datum ts_rank_tt(PG_FUNCTION_ARGS);
148 extern Datum ts_rank_wtt(PG_FUNCTION_ARGS);
149 extern Datum ts_rank_ttf(PG_FUNCTION_ARGS);
150 extern Datum ts_rank_wttf(PG_FUNCTION_ARGS);
151 extern Datum ts_rankcd_tt(PG_FUNCTION_ARGS);
152 extern Datum ts_rankcd_wtt(PG_FUNCTION_ARGS);
153 extern Datum ts_rankcd_ttf(PG_FUNCTION_ARGS);
154 extern Datum ts_rankcd_wttf(PG_FUNCTION_ARGS);
155
156
157 /*
158  * TSQuery
159  *
160  *
161  */
162
163 typedef int8 QueryItemType;
164
165 /* Valid values for QueryItemType: */
166 #define QI_VAL 1
167 #define QI_OPR 2
168 #define QI_VALSTOP 3    /* This is only used in an intermediate stack representation in parse_tsquery. It's not a legal type elsewhere. */
169
170 /*
171  * QueryItem is one node in tsquery - operator or operand.
172  */
173 typedef struct
174 {
175         QueryItemType           type;   /* operand or kind of operator (ts_tokentype) */
176         uint8           weight;                 /* weights of operand to search. It's a bitmask of allowed weights.
177                                                                  * if it =0 then any weight are allowed.
178                                                                  * Weights and bit map:
179                                                                  * A: 1<<3
180                                                                  * B: 1<<2
181                                                                  * C: 1<<1
182                                                                  * D: 1<<0
183                                                                  */
184         int32   valcrc;                         /* XXX: pg_crc32 would be a more appropriate data type, 
185                                                                  * but we use comparisons to signed integers in the code. 
186                                                                  * They would need to be changed as well. */
187
188         /* pointer to text value of operand, must correlate with WordEntry */
189         uint32
190                                 length:12,
191                                 distance:20;
192 } QueryOperand;
193
194
195 /* Legal values for QueryOperator.operator */
196 #define OP_NOT  1
197 #define OP_AND  2
198 #define OP_OR   3
199
200 typedef struct 
201 {
202         QueryItemType   type;
203         int8            oper;           /* see above */
204         uint32          left;           /* pointer to left operand. Right operand is
205                                                          * item + 1, left operand is placed
206                                                          * item+item->left */
207 } QueryOperator;
208
209 /*
210  * Note: TSQuery is 4-bytes aligned, so make sure there's no fields
211  * inside QueryItem requiring 8-byte alignment, like int64.
212  */
213 typedef union
214 {
215         QueryItemType   type;
216         QueryOperator operator;
217         QueryOperand operand;
218 } QueryItem;
219
220 /*
221  * Storage:
222  *      (len)(size)(array of QueryItem)(operands as '\0'-terminated c-strings)
223  */
224
225 typedef struct
226 {
227         int32           vl_len_;                /* varlena header (do not touch directly!) */
228         int4            size;                   /* number of QueryItems */
229         char            data[1];
230 } TSQueryData;
231
232 typedef TSQueryData *TSQuery;
233
234 #define HDRSIZETQ       ( VARHDRSZ + sizeof(int4) )
235
236 /* Computes the size of header and all QueryItems. size is the number of
237  * QueryItems, and lenofoperand is the total length of all operands
238  */
239 #define COMPUTESIZE(size, lenofoperand) ( HDRSIZETQ + (size) * sizeof(QueryItem) + (lenofoperand) )
240
241 /* Returns a pointer to the first QueryItem in a TSVector */
242 #define GETQUERY(x)  ((QueryItem*)( (char*)(x)+HDRSIZETQ ))
243
244 /* Returns a pointer to the beginning of operands in a TSVector */
245 #define GETOPERAND(x)   ( (char*)GETQUERY(x) + ((TSQuery)(x))->size * sizeof(QueryItem) )
246
247 /*
248  * fmgr interface macros
249  * Note, TSQuery type marked as plain storage, so it can't be toasted
250  * but PG_DETOAST_DATUM_COPY is used for simplicity
251  */
252
253 #define DatumGetTSQuery(X)                      ((TSQuery) DatumGetPointer(X))
254 #define DatumGetTSQueryCopy(X)          ((TSQuery) PG_DETOAST_DATUM_COPY(X))
255 #define TSQueryGetDatum(X)                      PointerGetDatum(X)
256 #define PG_GETARG_TSQUERY(n)            DatumGetTSQuery(PG_GETARG_DATUM(n))
257 #define PG_GETARG_TSQUERY_COPY(n)       DatumGetTSQueryCopy(PG_GETARG_DATUM(n))
258 #define PG_RETURN_TSQUERY(x)            return TSQueryGetDatum(x)
259
260 /*
261  * I/O
262  */
263 extern Datum tsqueryin(PG_FUNCTION_ARGS);
264 extern Datum tsqueryout(PG_FUNCTION_ARGS);
265 extern Datum tsquerysend(PG_FUNCTION_ARGS);
266 extern Datum tsqueryrecv(PG_FUNCTION_ARGS);
267
268 /*
269  * operations with tsquery
270  */
271 extern Datum tsquery_lt(PG_FUNCTION_ARGS);
272 extern Datum tsquery_le(PG_FUNCTION_ARGS);
273 extern Datum tsquery_eq(PG_FUNCTION_ARGS);
274 extern Datum tsquery_ne(PG_FUNCTION_ARGS);
275 extern Datum tsquery_ge(PG_FUNCTION_ARGS);
276 extern Datum tsquery_gt(PG_FUNCTION_ARGS);
277 extern Datum tsquery_cmp(PG_FUNCTION_ARGS);
278
279 extern Datum tsquerytree(PG_FUNCTION_ARGS);
280 extern Datum tsquery_numnode(PG_FUNCTION_ARGS);
281
282 extern Datum tsquery_and(PG_FUNCTION_ARGS);
283 extern Datum tsquery_or(PG_FUNCTION_ARGS);
284 extern Datum tsquery_not(PG_FUNCTION_ARGS);
285
286 extern Datum tsquery_rewrite(PG_FUNCTION_ARGS);
287 extern Datum tsquery_rewrite_query(PG_FUNCTION_ARGS);
288
289 extern Datum tsq_mcontains(PG_FUNCTION_ARGS);
290 extern Datum tsq_mcontained(PG_FUNCTION_ARGS);
291
292 #endif   /* _PG_TSTYPE_H_ */