ts_utils.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*-------------------------------------------------------------------------
  2. *
  3. * ts_utils.h
  4. * helper utilities for tsearch
  5. *
  6. * Copyright (c) 1998-2022, PostgreSQL Global Development Group
  7. *
  8. * src/include/tsearch/ts_utils.h
  9. *
  10. *-------------------------------------------------------------------------
  11. */
  12. #ifndef _PG_TS_UTILS_H_
  13. #define _PG_TS_UTILS_H_
  14. #include "nodes/pg_list.h"
  15. #include "tsearch/ts_public.h"
  16. #include "tsearch/ts_type.h"
  17. /*
  18. * Common parse definitions for tsvector and tsquery
  19. */
  20. /* tsvector parser support. */
  21. struct TSVectorParseStateData; /* opaque struct in tsvector_parser.c */
  22. typedef struct TSVectorParseStateData *TSVectorParseState;
  23. #define P_TSV_OPR_IS_DELIM (1 << 0)
  24. #define P_TSV_IS_TSQUERY (1 << 1)
  25. #define P_TSV_IS_WEB (1 << 2)
  26. extern TSVectorParseState init_tsvector_parser(char *input, int flags);
  27. extern void reset_tsvector_parser(TSVectorParseState state, char *input);
  28. extern bool gettoken_tsvector(TSVectorParseState state,
  29. char **token, int *len,
  30. WordEntryPos **pos, int *poslen,
  31. char **endptr);
  32. extern void close_tsvector_parser(TSVectorParseState state);
  33. /* phrase operator begins with '<' */
  34. #define ISOPERATOR(x) \
  35. ( pg_mblen(x) == 1 && ( *(x) == '!' || \
  36. *(x) == '&' || \
  37. *(x) == '|' || \
  38. *(x) == '(' || \
  39. *(x) == ')' || \
  40. *(x) == '<' \
  41. ) )
  42. /* parse_tsquery */
  43. struct TSQueryParserStateData; /* private in backend/utils/adt/tsquery.c */
  44. typedef struct TSQueryParserStateData *TSQueryParserState;
  45. typedef void (*PushFunction) (Datum opaque, TSQueryParserState state,
  46. char *token, int tokenlen,
  47. int16 tokenweights, /* bitmap as described in
  48. * QueryOperand struct */
  49. bool prefix);
  50. #define P_TSQ_PLAIN (1 << 0)
  51. #define P_TSQ_WEB (1 << 1)
  52. extern TSQuery parse_tsquery(char *buf,
  53. PushFunction pushval,
  54. Datum opaque,
  55. int flags);
  56. /* Functions for use by PushFunction implementations */
  57. extern void pushValue(TSQueryParserState state,
  58. char *strval, int lenval, int16 weight, bool prefix);
  59. extern void pushStop(TSQueryParserState state);
  60. extern void pushOperator(TSQueryParserState state, int8 oper, int16 distance);
  61. /*
  62. * parse plain text and lexize words
  63. */
  64. typedef struct
  65. {
  66. uint16 len;
  67. uint16 nvariant;
  68. union
  69. {
  70. uint16 pos;
  71. /*
  72. * When apos array is used, apos[0] is the number of elements in the
  73. * array (excluding apos[0]), and alen is the allocated size of the
  74. * array.
  75. */
  76. uint16 *apos;
  77. } pos;
  78. uint16 flags; /* currently, only TSL_PREFIX */
  79. char *word;
  80. uint32 alen;
  81. } ParsedWord;
  82. typedef struct
  83. {
  84. ParsedWord *words;
  85. int32 lenwords;
  86. int32 curwords;
  87. int32 pos;
  88. } ParsedText;
  89. extern void parsetext(Oid cfgId, ParsedText *prs, char *buf, int32 buflen);
  90. /*
  91. * headline framework, flow in common to generate:
  92. * 1 parse text with hlparsetext
  93. * 2 parser-specific function to find part
  94. * 3 generateHeadline to generate result text
  95. */
  96. extern void hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query,
  97. char *buf, int32 buflen);
  98. extern text *generateHeadline(HeadlineParsedText *prs);
  99. /*
  100. * TSQuery execution support
  101. *
  102. * TS_execute() executes a tsquery against data that can be represented in
  103. * various forms. The TSExecuteCallback callback function is called to check
  104. * whether a given primitive tsquery value is matched in the data.
  105. */
  106. /* TS_execute requires ternary logic to handle NOT with phrase matches */
  107. typedef enum
  108. {
  109. TS_NO, /* definitely no match */
  110. TS_YES, /* definitely does match */
  111. TS_MAYBE /* can't verify match for lack of pos data */
  112. } TSTernaryValue;
  113. /*
  114. * struct ExecPhraseData is passed to a TSExecuteCallback function if we need
  115. * lexeme position data (because of a phrase-match operator in the tsquery).
  116. * The callback should fill in position data when it returns TS_YES (success).
  117. * If it cannot return position data, it should leave "data" unchanged and
  118. * return TS_MAYBE. The caller of TS_execute() must then arrange for a later
  119. * recheck with position data available.
  120. *
  121. * The reported lexeme positions must be sorted and unique. Callers must only
  122. * consult the position bits of the pos array, ie, WEP_GETPOS(data->pos[i]).
  123. * This allows the returned "pos" to point directly to the WordEntryPos
  124. * portion of a tsvector value. If "allocated" is true then the pos array
  125. * is palloc'd workspace and caller may free it when done.
  126. *
  127. * "negate" means that the pos array contains positions where the query does
  128. * not match, rather than positions where it does. "width" is positive when
  129. * the match is wider than one lexeme. Neither of these fields normally need
  130. * to be touched by TSExecuteCallback functions; they are used for
  131. * phrase-search processing within TS_execute.
  132. *
  133. * All fields of the ExecPhraseData struct are initially zeroed by caller.
  134. */
  135. typedef struct ExecPhraseData
  136. {
  137. int npos; /* number of positions reported */
  138. bool allocated; /* pos points to palloc'd data? */
  139. bool negate; /* positions are where query is NOT matched */
  140. WordEntryPos *pos; /* ordered, non-duplicate lexeme positions */
  141. int width; /* width of match in lexemes, less 1 */
  142. } ExecPhraseData;
  143. /*
  144. * Signature for TSQuery lexeme check functions
  145. *
  146. * arg: opaque value passed through from caller of TS_execute
  147. * val: lexeme to test for presence of
  148. * data: to be filled with lexeme positions; NULL if position data not needed
  149. *
  150. * Return TS_YES if lexeme is present in data, TS_MAYBE if it might be
  151. * present, TS_NO if it definitely is not present. If data is not NULL,
  152. * it must be filled with lexeme positions if available. If position data
  153. * is not available, leave *data as zeroes and return TS_MAYBE, never TS_YES.
  154. */
  155. typedef TSTernaryValue (*TSExecuteCallback) (void *arg, QueryOperand *val,
  156. ExecPhraseData *data);
  157. /*
  158. * Flag bits for TS_execute
  159. */
  160. #define TS_EXEC_EMPTY (0x00)
  161. /*
  162. * If TS_EXEC_SKIP_NOT is set, then NOT sub-expressions are automatically
  163. * evaluated to be true. This was formerly the default behavior. It's now
  164. * deprecated because it tends to give silly answers, but some applications
  165. * might still have a use for it.
  166. */
  167. #define TS_EXEC_SKIP_NOT (0x01)
  168. /*
  169. * If TS_EXEC_PHRASE_NO_POS is set, allow OP_PHRASE to be executed lossily
  170. * in the absence of position information: a true result indicates that the
  171. * phrase might be present. Without this flag, OP_PHRASE always returns
  172. * false if lexeme position information is not available.
  173. */
  174. #define TS_EXEC_PHRASE_NO_POS (0x02)
  175. extern bool TS_execute(QueryItem *curitem, void *arg, uint32 flags,
  176. TSExecuteCallback chkcond);
  177. extern TSTernaryValue TS_execute_ternary(QueryItem *curitem, void *arg,
  178. uint32 flags,
  179. TSExecuteCallback chkcond);
  180. extern bool tsquery_requires_match(QueryItem *curitem);
  181. /*
  182. * to_ts* - text transformation to tsvector, tsquery
  183. */
  184. extern TSVector make_tsvector(ParsedText *prs);
  185. extern int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix);
  186. /*
  187. * Possible strategy numbers for indexes
  188. * TSearchStrategyNumber - (tsvector|text) @@ tsquery
  189. * TSearchWithClassStrategyNumber - tsvector @@@ tsquery
  190. */
  191. #define TSearchStrategyNumber 1
  192. #define TSearchWithClassStrategyNumber 2
  193. /*
  194. * TSQuery Utilities
  195. */
  196. extern QueryItem *clean_NOT(QueryItem *ptr, int32 *len);
  197. extern TSQuery cleanup_tsquery_stopwords(TSQuery in);
  198. typedef struct QTNode
  199. {
  200. QueryItem *valnode;
  201. uint32 flags;
  202. int32 nchild;
  203. char *word;
  204. uint32 sign;
  205. struct QTNode **child;
  206. } QTNode;
  207. /* bits in QTNode.flags */
  208. #define QTN_NEEDFREE 0x01
  209. #define QTN_NOCHANGE 0x02
  210. #define QTN_WORDFREE 0x04
  211. typedef uint64 TSQuerySign;
  212. #define TSQS_SIGLEN (sizeof(TSQuerySign)*BITS_PER_BYTE)
  213. #define TSQuerySignGetDatum(X) Int64GetDatum((int64) (X))
  214. #define DatumGetTSQuerySign(X) ((TSQuerySign) DatumGetInt64(X))
  215. #define PG_RETURN_TSQUERYSIGN(X) return TSQuerySignGetDatum(X)
  216. #define PG_GETARG_TSQUERYSIGN(n) DatumGetTSQuerySign(PG_GETARG_DATUM(n))
  217. extern QTNode *QT2QTN(QueryItem *in, char *operand);
  218. extern TSQuery QTN2QT(QTNode *in);
  219. extern void QTNFree(QTNode *in);
  220. extern void QTNSort(QTNode *in);
  221. extern void QTNTernary(QTNode *in);
  222. extern void QTNBinary(QTNode *in);
  223. extern int QTNodeCompare(QTNode *an, QTNode *bn);
  224. extern QTNode *QTNCopy(QTNode *in);
  225. extern void QTNClearFlags(QTNode *in, uint32 flags);
  226. extern bool QTNEq(QTNode *a, QTNode *b);
  227. extern TSQuerySign makeTSQuerySign(TSQuery a);
  228. extern QTNode *findsubquery(QTNode *root, QTNode *ex, QTNode *subs,
  229. bool *isfind);
  230. #endif /* _PG_TS_UTILS_H_ */