| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2007, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/fts0ast.h |
| 29 |
The FTS query parser (AST) abstract syntax tree routines |
| 30 |
|
| 31 |
Created 2007/03/16/03 Sunny Bains |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef INNOBASE_FST0AST_H |
| 35 |
#define INNOBASE_FST0AST_H |
| 36 |
|
| 37 |
#include "ha_prototypes.h" |
| 38 |
#include "mem0mem.h" |
| 39 |
|
| 40 |
/* The type of AST Node */ |
| 41 |
enum fts_ast_type_t { |
| 42 |
FTS_AST_OPER, /*!< Operator */ |
| 43 |
FTS_AST_NUMB, /*!< Number */ |
| 44 |
FTS_AST_TERM, /*!< Term (or word) */ |
| 45 |
FTS_AST_TEXT, /*!< Text string */ |
| 46 |
FTS_AST_PARSER_PHRASE_LIST, /*!< Phase for plugin parser |
| 47 |
The difference from text type |
| 48 |
is that we tokenize text into |
| 49 |
term list */ |
| 50 |
FTS_AST_LIST, /*!< Expression list */ |
| 51 |
FTS_AST_SUBEXP_LIST /*!< Sub-Expression list */ |
| 52 |
}; |
| 53 |
|
| 54 |
/* The FTS query operators that we support */ |
| 55 |
enum fts_ast_oper_t { |
| 56 |
FTS_NONE, /*!< No operator */ |
| 57 |
|
| 58 |
FTS_IGNORE, /*!< Ignore rows that contain |
| 59 |
this word */ |
| 60 |
|
| 61 |
FTS_EXIST, /*!< Include rows that contain |
| 62 |
this word */ |
| 63 |
|
| 64 |
FTS_NEGATE, /*!< Include rows that contain |
| 65 |
this word but rank them |
| 66 |
lower*/ |
| 67 |
|
| 68 |
FTS_INCR_RATING, /*!< Increase the rank for this |
| 69 |
word*/ |
| 70 |
|
| 71 |
FTS_DECR_RATING, /*!< Decrease the rank for this |
| 72 |
word*/ |
| 73 |
|
| 74 |
FTS_DISTANCE, /*!< Proximity distance */ |
| 75 |
FTS_IGNORE_SKIP, /*!< Transient node operator |
| 76 |
signifies that this is a |
| 77 |
FTS_IGNORE node, and ignored in |
| 78 |
the first pass of |
| 79 |
fts_ast_visit() */ |
| 80 |
FTS_EXIST_SKIP /*!< Transient node operator |
| 81 |
signifies that this ia a |
| 82 |
FTS_EXIST node, and ignored in |
| 83 |
the first pass of |
| 84 |
fts_ast_visit() */ |
| 85 |
}; |
| 86 |
|
| 87 |
/* Data types used by the FTS parser */ |
| 88 |
struct fts_lexer_t; |
| 89 |
struct fts_ast_node_t; |
| 90 |
struct fts_ast_state_t; |
| 91 |
struct fts_ast_string_t; |
| 92 |
|
| 93 |
typedef dberr_t (*fts_ast_callback)(fts_ast_oper_t, fts_ast_node_t *, void *); |
| 94 |
|
| 95 |
/******************************************************************** |
| 96 |
Parse the string using the lexer setup within state.*/ |
| 97 |
int fts_parse( |
| 98 |
/* out: 0 on OK, 1 on error */ |
| 99 |
fts_ast_state_t *state); /*!< in: ast state instance.*/ |
| 100 |
|
| 101 |
/******************************************************************** |
| 102 |
Create an AST operator node */ |
| 103 |
extern fts_ast_node_t *fts_ast_create_node_oper( |
| 104 |
void *arg, /*!< in: ast state */ |
| 105 |
fts_ast_oper_t oper); /*!< in: ast operator */ |
| 106 |
/******************************************************************** |
| 107 |
Create an AST term node, makes a copy of ptr */ |
| 108 |
extern fts_ast_node_t *fts_ast_create_node_term( |
| 109 |
void *arg, /*!< in: ast state */ |
| 110 |
const fts_ast_string_t *ptr); /*!< in: term string */ |
| 111 |
/******************************************************************** |
| 112 |
Create an AST text node */ |
| 113 |
extern fts_ast_node_t *fts_ast_create_node_text( |
| 114 |
void *arg, /*!< in: ast state */ |
| 115 |
const fts_ast_string_t *ptr); /*!< in: text string */ |
| 116 |
/******************************************************************** |
| 117 |
Create an AST expr list node */ |
| 118 |
extern fts_ast_node_t *fts_ast_create_node_list( |
| 119 |
void *arg, /*!< in: ast state */ |
| 120 |
fts_ast_node_t *expr); /*!< in: ast expr */ |
| 121 |
/******************************************************************** |
| 122 |
Create a sub-expression list node. This function takes ownership of |
| 123 |
expr and is responsible for deleting it. */ |
| 124 |
extern fts_ast_node_t *fts_ast_create_node_subexp_list( |
| 125 |
/* out: new node */ |
| 126 |
void *arg, /*!< in: ast state instance */ |
| 127 |
fts_ast_node_t *expr); /*!< in: ast expr instance */ |
| 128 |
/******************************************************************** |
| 129 |
Set the wildcard attribute of a term.*/ |
| 130 |
extern void fts_ast_term_set_wildcard( |
| 131 |
fts_ast_node_t *node); /*!< in: term to change */ |
| 132 |
/******************************************************************** |
| 133 |
Set the proximity attribute of a text node. */ |
| 134 |
void fts_ast_text_set_distance(fts_ast_node_t *node, /*!< in/out: text node */ |
| 135 |
ulint distance); /*!< in: the text proximity |
| 136 |
distance */ |
| 137 |
/** Free a fts_ast_node_t instance. |
| 138 |
@return next node to free */ |
| 139 |
fts_ast_node_t *fts_ast_free_node( |
| 140 |
fts_ast_node_t *node); /*!< in: node to free */ |
| 141 |
/******************************************************************** |
| 142 |
Add a sub-expression to an AST*/ |
| 143 |
extern fts_ast_node_t *fts_ast_add_node( |
| 144 |
fts_ast_node_t *list, /*!< in: list node instance */ |
| 145 |
fts_ast_node_t *node); /*!< in: (sub) expr to add */ |
| 146 |
/******************************************************************** |
| 147 |
Print the AST node recursively.*/ |
| 148 |
extern void fts_ast_node_print( |
| 149 |
fts_ast_node_t *node); /*!< in: ast node to print */ |
| 150 |
/******************************************************************** |
| 151 |
Free node and expr allocations.*/ |
| 152 |
extern void fts_ast_state_free(fts_ast_state_t *state); /*!< in: state instance |
| 153 |
to free */ |
| 154 |
/** Check only union operation involved in the node |
| 155 |
@param[in] node ast node to check |
| 156 |
@return true if the node contains only union else false. */ |
| 157 |
bool fts_ast_node_check_union(fts_ast_node_t *node); |
| 158 |
|
| 159 |
/** Traverse the AST - in-order traversal. |
| 160 |
@return DB_SUCCESS if all went well */ |
| 161 |
[[nodiscard]] dberr_t fts_ast_visit( |
| 162 |
fts_ast_oper_t oper, /*!< in: FTS operator */ |
| 163 |
fts_ast_node_t *node, /*!< in: instance to traverse*/ |
| 164 |
fts_ast_callback visitor, /*!< in: callback */ |
| 165 |
void *arg, /*!< in: callback arg */ |
| 166 |
bool *has_ignore); /*!< out: whether we encounter |
| 167 |
and ignored processing an |
| 168 |
operator, currently we only |
| 169 |
ignore FTS_IGNORE operator */ |
| 170 |
/******************************************************************** |
| 171 |
Create a lex instance.*/ |
| 172 |
[[nodiscard]] fts_lexer_t *fts_lexer_create( |
| 173 |
bool boolean_mode, /*!< in: query type */ |
| 174 |
const byte *query, /*!< in: query string */ |
| 175 |
ulint query_len) /*!< in: query string len */ |
| 176 |
MY_ATTRIBUTE((malloc)); |
| 177 |
/******************************************************************** |
| 178 |
Free an fts_lexer_t instance.*/ |
| 179 |
void fts_lexer_free(fts_lexer_t *fts_lexer); /*!< in: lexer instance to |
| 180 |
free */ |
| 181 |
|
| 182 |
/** |
| 183 |
Create an ast string object, with NUL-terminator, so the string |
| 184 |
has one more byte than len |
| 185 |
@param[in] str pointer to string |
| 186 |
@param[in] len length of the string |
| 187 |
@return ast string with NUL-terminator */ |
| 188 |
fts_ast_string_t *fts_ast_string_create(const byte *str, ulint len); |
| 189 |
|
| 190 |
/** |
| 191 |
Free an ast string instance |
| 192 |
@param[in,out] ast_str string to free */ |
| 193 |
void fts_ast_string_free(fts_ast_string_t *ast_str); |
| 194 |
|
| 195 |
/** |
| 196 |
Translate ast string of type FTS_AST_NUMB to unsigned long by strtoul |
| 197 |
@param[in] ast_str string to translate |
| 198 |
@param[in] base the base |
| 199 |
@return translated number */ |
| 200 |
ulint fts_ast_string_to_ul(const fts_ast_string_t *ast_str, int base); |
| 201 |
|
| 202 |
/* String of length len. |
| 203 |
We always store the string of length len with a terminating '\0', |
| 204 |
regardless of there is any 0x00 in the string itself */ |
| 205 |
struct fts_ast_string_t { |
| 206 |
/*!< Pointer to string. */ |
| 207 |
byte *str; |
| 208 |
|
| 209 |
/*!< Length of the string. */ |
| 210 |
ulint len; |
| 211 |
}; |
| 212 |
|
| 213 |
/* Query term type */ |
| 214 |
struct fts_ast_term_t { |
| 215 |
fts_ast_string_t *ptr; /*!< Pointer to term string.*/ |
| 216 |
bool wildcard; /*!< true if wild card set.*/ |
| 217 |
}; |
| 218 |
|
| 219 |
/* Query text type */ |
| 220 |
struct fts_ast_text_t { |
| 221 |
fts_ast_string_t *ptr; /*!< Pointer to text string.*/ |
| 222 |
ulint distance; /*!< > 0 if proximity distance |
| 223 |
set */ |
| 224 |
}; |
| 225 |
|
| 226 |
/* The list of nodes in an expr list */ |
| 227 |
struct fts_ast_list_t { |
| 228 |
fts_ast_node_t *head; /*!< Children list head */ |
| 229 |
fts_ast_node_t *tail; /*!< Children list tail */ |
| 230 |
}; |
| 231 |
|
| 232 |
/* FTS AST node to store the term, text, operator and sub-expressions.*/ |
| 233 |
struct fts_ast_node_t { |
| 234 |
fts_ast_type_t type; /*!< The type of node */ |
| 235 |
fts_ast_text_t text; /*!< Text node */ |
| 236 |
fts_ast_term_t term; /*!< Term node */ |
| 237 |
fts_ast_oper_t oper; /*!< Operator value */ |
| 238 |
fts_ast_list_t list; /*!< Expression list */ |
| 239 |
fts_ast_node_t *next; /*!< Link for expr list */ |
| 240 |
fts_ast_node_t *next_alloc; /*!< For tracking allocations */ |
| 241 |
bool visited; /*!< whether this node is |
| 242 |
already processed */ |
| 243 |
trx_t *trx; |
| 244 |
/* Used by plugin parser */ |
| 245 |
fts_ast_node_t *up_node; /*!< Direct up node */ |
| 246 |
bool go_up; /*!< Flag if go one level up */ |
| 247 |
}; |
| 248 |
|
| 249 |
/* To track state during parsing */ |
| 250 |
struct fts_ast_state_t { |
| 251 |
mem_heap_t *heap; /*!< Heap to use for alloc */ |
| 252 |
fts_ast_node_t *root; /*!< If all goes OK, then this |
| 253 |
will point to the root.*/ |
| 254 |
|
| 255 |
fts_ast_list_t list; /*!< List of nodes allocated */ |
| 256 |
|
| 257 |
fts_lexer_t *lexer; /*!< Lexer callback + arg */ |
| 258 |
CHARSET_INFO *charset; /*!< charset used for |
| 259 |
tokenization */ |
| 260 |
/* Used by plugin parser */ |
| 261 |
fts_ast_node_t *cur_node; /*!< Current node into which |
| 262 |
we add new node */ |
| 263 |
int depth; /*!< Depth of parsing state */ |
| 264 |
}; |
| 265 |
|
| 266 |
/** Create an AST term node, makes a copy of ptr for plugin parser |
| 267 |
@return node */ |
| 268 |
extern fts_ast_node_t *fts_ast_create_node_term_for_parser( |
| 269 |
/*==========i=====================*/ |
| 270 |
void *arg, /*!< in: ast state */ |
| 271 |
const char *ptr, /*!< in: term string */ |
| 272 |
const ulint len); /*!< in: term string length */ |
| 273 |
|
| 274 |
/** Create an AST phrase list node for plugin parser |
| 275 |
@return node */ |
| 276 |
extern fts_ast_node_t *fts_ast_create_node_phrase_list( |
| 277 |
void *arg); /*!< in: ast state */ |
| 278 |
|
| 279 |
#ifdef UNIV_DEBUG |
| 280 |
const char *fts_ast_node_type_get(fts_ast_type_t type); |
| 281 |
#endif /* UNIV_DEBUG */ |
| 282 |
|
| 283 |
#endif /* INNOBASE_FSTS0AST_H */ |