| 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 fts/fts0ast.cc |
| 29 |
Full Text Search parser helper file. |
| 30 |
|
| 31 |
Created 2007/3/16 Sunny Bains. |
| 32 |
***********************************************************************/ |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
#include "row0sel.h" |
| 36 |
|
| 37 |
#include "fts0ast.h" |
| 38 |
#include "fts0fts.h" |
| 39 |
#include "fts0pars.h" |
| 40 |
#include "ha_prototypes.h" |
| 41 |
|
| 42 |
/* The FTS ast visit pass. */ |
| 43 |
enum fts_ast_visit_pass_t { |
| 44 |
FTS_PASS_FIRST, /*!< First visit pass, |
| 45 |
process operators excluding |
| 46 |
FTS_EXIST and FTS_IGNORE */ |
| 47 |
FTS_PASS_EXIST, /*!< Exist visit pass, |
| 48 |
process operator FTS_EXIST */ |
| 49 |
FTS_PASS_IGNORE /*!< Ignore visit pass, |
| 50 |
process operator FTS_IGNORE */ |
| 51 |
}; |
| 52 |
|
| 53 |
/** Create an empty fts_ast_node_t. |
| 54 |
@return Create a new node */ |
| 55 |
static fts_ast_node_t *fts_ast_node_create(void) { |
| 56 |
fts_ast_node_t *node; |
| 57 |
|
| 58 |
node = (fts_ast_node_t *)ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 59 |
sizeof(*node)); |
| 60 |
|
| 61 |
return (node); |
| 62 |
} |
| 63 |
|
| 64 |
/** Track node allocations, in case there is an error during parsing. */ |
| 65 |
static void fts_ast_state_add_node( |
| 66 |
fts_ast_state_t *state, /*!< in: ast instance */ |
| 67 |
fts_ast_node_t *node) /*!< in: node to add to ast */ |
| 68 |
{ |
| 69 |
if (!state->list.head) { |
| 70 |
ut_a(!state->list.tail); |
| 71 |
|
| 72 |
state->list.head = state->list.tail = node; |
| 73 |
} else { |
| 74 |
state->list.tail->next_alloc = node; |
| 75 |
state->list.tail = node; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** Create a operator fts_ast_node_t. |
| 80 |
@return new node */ |
| 81 |
fts_ast_node_t *fts_ast_create_node_oper( |
| 82 |
void *arg, /*!< in: ast state instance */ |
| 83 |
fts_ast_oper_t oper) /*!< in: ast operator */ |
| 84 |
{ |
| 85 |
fts_ast_node_t *node = fts_ast_node_create(); |
| 86 |
|
| 87 |
node->type = FTS_AST_OPER; |
| 88 |
node->oper = oper; |
| 89 |
|
| 90 |
fts_ast_state_add_node((fts_ast_state_t *)arg, node); |
| 91 |
|
| 92 |
return (node); |
| 93 |
} |
| 94 |
|
| 95 |
/** This function takes ownership of the ptr and is responsible |
| 96 |
for free'ing it |
| 97 |
@return new node or a node list with tokenized words */ |
| 98 |
fts_ast_node_t *fts_ast_create_node_term( |
| 99 |
void *arg, /*!< in: ast state instance */ |
| 100 |
const fts_ast_string_t *ptr) /*!< in: ast term string */ |
| 101 |
{ |
| 102 |
fts_ast_state_t *state = static_cast<fts_ast_state_t *>(arg); |
| 103 |
ulint len = ptr->len; |
| 104 |
ulint cur_pos = 0; |
| 105 |
fts_ast_node_t *node = nullptr; |
| 106 |
fts_ast_node_t *node_list = nullptr; |
| 107 |
fts_ast_node_t *first_node = nullptr; |
| 108 |
|
| 109 |
/* Scan the incoming string and filter out any "non-word" characters */ |
| 110 |
while (cur_pos < len) { |
| 111 |
fts_string_t str; |
| 112 |
ulint cur_len; |
| 113 |
|
| 114 |
cur_len = innobase_mysql_fts_get_token( |
| 115 |
state->charset, reinterpret_cast<const byte *>(ptr->str) + cur_pos, |
| 116 |
reinterpret_cast<const byte *>(ptr->str) + len, &str); |
| 117 |
|
| 118 |
if (cur_len == 0) { |
| 119 |
break; |
| 120 |
} |
| 121 |
|
| 122 |
cur_pos += cur_len; |
| 123 |
|
| 124 |
if (str.f_n_char > 0) { |
| 125 |
/* If the subsequent term (after the first one)'s size |
| 126 |
is less than fts_min_token_size or the term is greater |
| 127 |
than fts_max_token_size, we shall ignore that. This is |
| 128 |
to make consistent with MyISAM behavior */ |
| 129 |
if ((first_node && (str.f_n_char < fts_min_token_size)) || |
| 130 |
str.f_n_char > fts_max_token_size) { |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
node = fts_ast_node_create(); |
| 135 |
|
| 136 |
node->type = FTS_AST_TERM; |
| 137 |
|
| 138 |
node->term.ptr = fts_ast_string_create(str.f_str, str.f_len); |
| 139 |
|
| 140 |
fts_ast_state_add_node(static_cast<fts_ast_state_t *>(arg), node); |
| 141 |
|
| 142 |
if (first_node) { |
| 143 |
/* There is more than one word, create |
| 144 |
a list to organize them */ |
| 145 |
if (!node_list) { |
| 146 |
node_list = fts_ast_create_node_list( |
| 147 |
static_cast<fts_ast_state_t *>(arg), first_node); |
| 148 |
} |
| 149 |
|
| 150 |
fts_ast_add_node(node_list, node); |
| 151 |
} else { |
| 152 |
first_node = node; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return ((node_list != nullptr) ? node_list : first_node); |
| 158 |
} |
| 159 |
|
| 160 |
/** Create an AST term node, makes a copy of ptr for plugin parser |
| 161 |
@return node */ |
| 162 |
fts_ast_node_t *fts_ast_create_node_term_for_parser( |
| 163 |
void *arg, /*!< in: ast state */ |
| 164 |
const char *ptr, /*!< in: term string */ |
| 165 |
const ulint len) /*!< in: term string length */ |
| 166 |
{ |
| 167 |
fts_ast_node_t *node = nullptr; |
| 168 |
|
| 169 |
/* '%' as first char is forbidden for LIKE in internal SQL parser; |
| 170 |
'%' as last char is reserved for wildcard search;*/ |
| 171 |
if (len == 0 || len > FTS_MAX_WORD_LEN || ptr[0] == '%' || |
| 172 |
ptr[len - 1] == '%') { |
| 173 |
return (nullptr); |
| 174 |
} |
| 175 |
|
| 176 |
node = fts_ast_node_create(); |
| 177 |
|
| 178 |
node->type = FTS_AST_TERM; |
| 179 |
|
| 180 |
node->term.ptr = |
| 181 |
fts_ast_string_create(reinterpret_cast<const byte *>(ptr), len); |
| 182 |
|
| 183 |
fts_ast_state_add_node(static_cast<fts_ast_state_t *>(arg), node); |
| 184 |
|
| 185 |
return (node); |
| 186 |
} |
| 187 |
|
| 188 |
/** This function takes ownership of the ptr and is responsible |
| 189 |
for free'ing it. |
| 190 |
@return new node */ |
| 191 |
fts_ast_node_t *fts_ast_create_node_text( |
| 192 |
void *arg, /*!< in: ast state instance */ |
| 193 |
const fts_ast_string_t *ptr) /*!< in: ast text string */ |
| 194 |
{ |
| 195 |
ulint len = ptr->len; |
| 196 |
fts_ast_node_t *node = nullptr; |
| 197 |
|
| 198 |
/* Once we come here, the string must have at least 2 quotes "" |
| 199 |
around the query string, which could be empty. Also the query |
| 200 |
string may contain 0x00 in it, we don't treat it as null-terminated. */ |
| 201 |
ut_ad(len >= 2); |
| 202 |
ut_ad(ptr->str[0] == '\"' && ptr->str[len - 1] == '\"'); |
| 203 |
|
| 204 |
if (len == 2) { |
| 205 |
/* If the query string contains nothing except quotes, |
| 206 |
it's obviously an invalid query. */ |
| 207 |
return (nullptr); |
| 208 |
} |
| 209 |
|
| 210 |
node = fts_ast_node_create(); |
| 211 |
|
| 212 |
/*!< We ignore the actual quotes "" */ |
| 213 |
len -= 2; |
| 214 |
|
| 215 |
node->type = FTS_AST_TEXT; |
| 216 |
/*!< Skip copying the first quote */ |
| 217 |
node->text.ptr = |
| 218 |
fts_ast_string_create(reinterpret_cast<const byte *>(ptr->str + 1), len); |
| 219 |
node->text.distance = ULINT_UNDEFINED; |
| 220 |
|
| 221 |
fts_ast_state_add_node((fts_ast_state_t *)arg, node); |
| 222 |
|
| 223 |
return (node); |
| 224 |
} |
| 225 |
|
| 226 |
/** Create an AST phrase list node for plugin parser |
| 227 |
@return node */ |
| 228 |
fts_ast_node_t *fts_ast_create_node_phrase_list(void *arg) /*!< in: ast state */ |
| 229 |
{ |
| 230 |
fts_ast_node_t *node = fts_ast_node_create(); |
| 231 |
|
| 232 |
node->type = FTS_AST_PARSER_PHRASE_LIST; |
| 233 |
|
| 234 |
node->text.distance = ULINT_UNDEFINED; |
| 235 |
node->list.head = node->list.tail = nullptr; |
| 236 |
|
| 237 |
fts_ast_state_add_node(static_cast<fts_ast_state_t *>(arg), node); |
| 238 |
|
| 239 |
return (node); |
| 240 |
} |
| 241 |
|
| 242 |
/** This function takes ownership of the expr and is responsible |
| 243 |
for free'ing it. |
| 244 |
@return new node */ |
| 245 |
fts_ast_node_t *fts_ast_create_node_list( |
| 246 |
void *arg, /*!< in: ast state instance */ |
| 247 |
fts_ast_node_t *expr) /*!< in: ast expr instance */ |
| 248 |
{ |
| 249 |
fts_ast_node_t *node = fts_ast_node_create(); |
| 250 |
|
| 251 |
node->type = FTS_AST_LIST; |
| 252 |
node->list.head = node->list.tail = expr; |
| 253 |
|
| 254 |
fts_ast_state_add_node((fts_ast_state_t *)arg, node); |
| 255 |
|
| 256 |
return (node); |
| 257 |
} |
| 258 |
|
| 259 |
/** Create a sub-expression list node. This function takes ownership of |
| 260 |
expr and is responsible for deleting it. |
| 261 |
@return new node */ |
| 262 |
fts_ast_node_t *fts_ast_create_node_subexp_list( |
| 263 |
void *arg, /*!< in: ast state instance */ |
| 264 |
fts_ast_node_t *expr) /*!< in: ast expr instance */ |
| 265 |
{ |
| 266 |
fts_ast_node_t *node = fts_ast_node_create(); |
| 267 |
|
| 268 |
node->type = FTS_AST_SUBEXP_LIST; |
| 269 |
node->list.head = node->list.tail = expr; |
| 270 |
|
| 271 |
fts_ast_state_add_node((fts_ast_state_t *)arg, node); |
| 272 |
|
| 273 |
return (node); |
| 274 |
} |
| 275 |
|
| 276 |
/** Free an expr list node elements. */ |
| 277 |
static void fts_ast_free_list(fts_ast_node_t *node) /*!< in: ast node to free */ |
| 278 |
{ |
| 279 |
ut_a(node->type == FTS_AST_LIST || node->type == FTS_AST_SUBEXP_LIST || |
| 280 |
node->type == FTS_AST_PARSER_PHRASE_LIST); |
| 281 |
|
| 282 |
for (node = node->list.head; node != nullptr; |
| 283 |
node = fts_ast_free_node(node)) { |
| 284 |
/*!< No op */ |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/** Free a fts_ast_node_t instance. |
| 289 |
@return next node to free */ |
| 290 |
fts_ast_node_t *fts_ast_free_node( |
| 291 |
fts_ast_node_t *node) /*!< in: the node to free */ |
| 292 |
{ |
| 293 |
fts_ast_node_t *next_node; |
| 294 |
|
| 295 |
switch (node->type) { |
| 296 |
case FTS_AST_TEXT: |
| 297 |
if (node->text.ptr) { |
| 298 |
fts_ast_string_free(node->text.ptr); |
| 299 |
node->text.ptr = nullptr; |
| 300 |
} |
| 301 |
break; |
| 302 |
|
| 303 |
case FTS_AST_TERM: |
| 304 |
if (node->term.ptr) { |
| 305 |
fts_ast_string_free(node->term.ptr); |
| 306 |
node->term.ptr = nullptr; |
| 307 |
} |
| 308 |
break; |
| 309 |
|
| 310 |
case FTS_AST_LIST: |
| 311 |
case FTS_AST_SUBEXP_LIST: |
| 312 |
case FTS_AST_PARSER_PHRASE_LIST: |
| 313 |
fts_ast_free_list(node); |
| 314 |
node->list.head = node->list.tail = nullptr; |
| 315 |
break; |
| 316 |
|
| 317 |
case FTS_AST_OPER: |
| 318 |
break; |
| 319 |
|
| 320 |
default: |
| 321 |
ut_error; |
| 322 |
} |
| 323 |
|
| 324 |
/*!< Get next node before freeing the node itself */ |
| 325 |
next_node = node->next; |
| 326 |
|
| 327 |
ut::free(node); |
| 328 |
|
| 329 |
return (next_node); |
| 330 |
} |
| 331 |
|
| 332 |
/** This AST takes ownership of the expr and is responsible |
| 333 |
for free'ing it. |
| 334 |
@return in param "list" */ |
| 335 |
fts_ast_node_t *fts_ast_add_node( |
| 336 |
fts_ast_node_t *node, /*!< in: list instance */ |
| 337 |
fts_ast_node_t *elem) /*!< in: node to add to list */ |
| 338 |
{ |
| 339 |
if (!elem) { |
| 340 |
return (nullptr); |
| 341 |
} |
| 342 |
|
| 343 |
ut_a(!elem->next); |
| 344 |
ut_a(node->type == FTS_AST_LIST || node->type == FTS_AST_SUBEXP_LIST || |
| 345 |
node->type == FTS_AST_PARSER_PHRASE_LIST); |
| 346 |
|
| 347 |
if (!node->list.head) { |
| 348 |
ut_a(!node->list.tail); |
| 349 |
|
| 350 |
node->list.head = node->list.tail = elem; |
| 351 |
} else { |
| 352 |
ut_a(node->list.tail); |
| 353 |
|
| 354 |
node->list.tail->next = elem; |
| 355 |
node->list.tail = elem; |
| 356 |
} |
| 357 |
|
| 358 |
return (node); |
| 359 |
} |
| 360 |
|
| 361 |
/** Set the wildcard attribute of a term. */ |
| 362 |
void fts_ast_term_set_wildcard(fts_ast_node_t *node) /*!< in/out: set attribute |
| 363 |
of a term node */ |
| 364 |
{ |
| 365 |
if (!node) { |
| 366 |
return; |
| 367 |
} |
| 368 |
|
| 369 |
/* If it's a node list, the wildcard should be set to the tail node*/ |
| 370 |
if (node->type == FTS_AST_LIST) { |
| 371 |
ut_ad(node->list.tail != nullptr); |
| 372 |
node = node->list.tail; |
| 373 |
} |
| 374 |
|
| 375 |
ut_a(node->type == FTS_AST_TERM); |
| 376 |
ut_a(!node->term.wildcard); |
| 377 |
|
| 378 |
node->term.wildcard = true; |
| 379 |
} |
| 380 |
|
| 381 |
/** Set the proximity attribute of a text node. */ |
| 382 |
void fts_ast_text_set_distance(fts_ast_node_t *node, /*!< in/out: text node */ |
| 383 |
ulint distance) /*!< in: the text proximity |
| 384 |
distance */ |
| 385 |
{ |
| 386 |
if (node == nullptr) { |
| 387 |
return; |
| 388 |
} |
| 389 |
|
| 390 |
ut_a(node->type == FTS_AST_TEXT); |
| 391 |
ut_a(node->text.distance == ULINT_UNDEFINED); |
| 392 |
|
| 393 |
node->text.distance = distance; |
| 394 |
} |
| 395 |
|
| 396 |
/** Free node and expr allocations. */ |
| 397 |
void fts_ast_state_free(fts_ast_state_t *state) /*!< in: ast state to free */ |
| 398 |
{ |
| 399 |
fts_ast_node_t *node = state->list.head; |
| 400 |
|
| 401 |
/* Free the nodes that were allocated during parsing. */ |
| 402 |
while (node) { |
| 403 |
fts_ast_node_t *next = node->next_alloc; |
| 404 |
|
| 405 |
if (node->type == FTS_AST_TEXT && node->text.ptr) { |
| 406 |
fts_ast_string_free(node->text.ptr); |
| 407 |
node->text.ptr = nullptr; |
| 408 |
} else if (node->type == FTS_AST_TERM && node->term.ptr) { |
| 409 |
fts_ast_string_free(node->term.ptr); |
| 410 |
node->term.ptr = nullptr; |
| 411 |
} |
| 412 |
|
| 413 |
ut::free(node); |
| 414 |
node = next; |
| 415 |
} |
| 416 |
|
| 417 |
state->root = state->list.head = state->list.tail = nullptr; |
| 418 |
} |
| 419 |
|
| 420 |
/** Print the ast string |
| 421 |
@param[in] ast_str string to print */ |
| 422 |
static void fts_ast_string_print(const fts_ast_string_t *ast_str) { |
| 423 |
for (ulint i = 0; i < ast_str->len; ++i) { |
| 424 |
printf("%c", ast_str->str[i]); |
| 425 |
} |
| 426 |
|
| 427 |
printf("\n"); |
| 428 |
} |
| 429 |
|
| 430 |
/** Print an ast node recursively. */ |
| 431 |
static void fts_ast_node_print_recursive( |
| 432 |
fts_ast_node_t *node, /*!< in: ast node to print */ |
| 433 |
ulint level) /*!< in: recursive level */ |
| 434 |
{ |
| 435 |
/* Print alignment blank */ |
| 436 |
for (ulint i = 0; i < level; i++) { |
| 437 |
printf(" "); |
| 438 |
} |
| 439 |
|
| 440 |
switch (node->type) { |
| 441 |
case FTS_AST_TEXT: |
| 442 |
printf("TEXT: "); |
| 443 |
fts_ast_string_print(node->text.ptr); |
| 444 |
break; |
| 445 |
|
| 446 |
case FTS_AST_TERM: |
| 447 |
printf("TERM: "); |
| 448 |
fts_ast_string_print(node->term.ptr); |
| 449 |
break; |
| 450 |
|
| 451 |
case FTS_AST_LIST: |
| 452 |
printf("LIST: \n"); |
| 453 |
|
| 454 |
for (node = node->list.head; node; node = node->next) { |
| 455 |
fts_ast_node_print_recursive(node, level + 1); |
| 456 |
} |
| 457 |
break; |
| 458 |
|
| 459 |
case FTS_AST_SUBEXP_LIST: |
| 460 |
printf("SUBEXP_LIST: \n"); |
| 461 |
|
| 462 |
for (node = node->list.head; node; node = node->next) { |
| 463 |
fts_ast_node_print_recursive(node, level + 1); |
| 464 |
} |
| 465 |
break; |
| 466 |
|
| 467 |
case FTS_AST_OPER: |
| 468 |
printf("OPER: %d\n", node->oper); |
| 469 |
break; |
| 470 |
|
| 471 |
case FTS_AST_PARSER_PHRASE_LIST: |
| 472 |
printf("PARSER_PHRASE_LIST: \n"); |
| 473 |
|
| 474 |
for (node = node->list.head; node; node = node->next) { |
| 475 |
fts_ast_node_print_recursive(node, level + 1); |
| 476 |
} |
| 477 |
break; |
| 478 |
|
| 479 |
default: |
| 480 |
ut_error; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/** Print an ast node */ |
| 485 |
void fts_ast_node_print(fts_ast_node_t *node) /*!< in: ast node to print */ |
| 486 |
{ |
| 487 |
fts_ast_node_print_recursive(node, 0); |
| 488 |
} |
| 489 |
|
| 490 |
/** Check only union operation involved in the node |
| 491 |
@param[in] node ast node to check |
| 492 |
@return true if the node contains only union else false. */ |
| 493 |
bool fts_ast_node_check_union(fts_ast_node_t *node) { |
| 494 |
if (node->type == FTS_AST_LIST || node->type == FTS_AST_SUBEXP_LIST) { |
| 495 |
for (node = node->list.head; node; node = node->next) { |
| 496 |
if (!fts_ast_node_check_union(node)) { |
| 497 |
return (false); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
} else if (node->type == FTS_AST_PARSER_PHRASE_LIST) { |
| 502 |
/* Phrase search for plugin parser */ |
| 503 |
return (false); |
| 504 |
} else if (node->type == FTS_AST_OPER && |
| 505 |
(node->oper == FTS_IGNORE || node->oper == FTS_EXIST)) { |
| 506 |
return (false); |
| 507 |
} else if (node->type == FTS_AST_TEXT) { |
| 508 |
/* Distance or phrase search query. */ |
| 509 |
return (false); |
| 510 |
} |
| 511 |
|
| 512 |
return (true); |
| 513 |
} |
| 514 |
|
| 515 |
/** Traverse the AST - in-order traversal, except for the FTX_EXIST and |
| 516 |
FTS_IGNORE nodes, which will be ignored in the first pass of each level, and |
| 517 |
visited in a second and third pass after all other nodes in the same level are |
| 518 |
visited. |
| 519 |
@return DB_SUCCESS if all went well */ |
| 520 |
dberr_t fts_ast_visit(fts_ast_oper_t oper, /*!< in: current operator */ |
| 521 |
fts_ast_node_t *node, /*!< in: current root node */ |
| 522 |
fts_ast_callback visitor, /*!< in: callback function */ |
| 523 |
void *arg, /*!< in: arg for callback */ |
| 524 |
bool *has_ignore) /*!< out: true, if the operator |
| 525 |
was ignored during processing, |
| 526 |
currently we ignore FTS_EXIST |
| 527 |
and FTS_IGNORE operators */ |
| 528 |
{ |
| 529 |
dberr_t error = DB_SUCCESS; |
| 530 |
fts_ast_node_t *oper_node = nullptr; |
| 531 |
fts_ast_node_t *start_node; |
| 532 |
bool revisit = false; |
| 533 |
bool will_be_ignored = false; |
| 534 |
fts_ast_visit_pass_t visit_pass = FTS_PASS_FIRST; |
| 535 |
trx_t *trx = node->trx; |
| 536 |
start_node = node->list.head; |
| 537 |
|
| 538 |
ut_a(node->type == FTS_AST_LIST || node->type == FTS_AST_SUBEXP_LIST); |
| 539 |
|
| 540 |
if (oper == FTS_EXIST_SKIP) { |
| 541 |
visit_pass = FTS_PASS_EXIST; |
| 542 |
} else if (oper == FTS_IGNORE_SKIP) { |
| 543 |
visit_pass = FTS_PASS_IGNORE; |
| 544 |
} |
| 545 |
|
| 546 |
/* In the first pass of the tree, at the leaf level of the |
| 547 |
tree, FTS_EXIST and FTS_IGNORE operation will be ignored. |
| 548 |
It will be repeated at the level above the leaf level. |
| 549 |
|
| 550 |
The basic idea here is that when we encounter FTS_EXIST or |
| 551 |
FTS_IGNORE, we will change the operator node into FTS_EXIST_SKIP |
| 552 |
or FTS_IGNORE_SKIP, and term node & text node with the operators |
| 553 |
is ignored in the first pass. We have two passes during the revisit: |
| 554 |
We process nodes with FTS_EXIST_SKIP in the exist pass, and then |
| 555 |
process nodes with FTS_IGNORE_SKIP in the ignore pass. |
| 556 |
|
| 557 |
The order should be restrictly followed, or we will get wrong results. |
| 558 |
For example, we have a query 'a +b -c d +e -f'. |
| 559 |
first pass: process 'a' and 'd' by union; |
| 560 |
exist pass: process '+b' and '+e' by intersection; |
| 561 |
ignore pass: process '-c' and '-f' by difference. */ |
| 562 |
|
| 563 |
for (node = node->list.head; node && (error == DB_SUCCESS); |
| 564 |
node = node->next) { |
| 565 |
switch (node->type) { |
| 566 |
case FTS_AST_LIST: |
| 567 |
if (visit_pass != FTS_PASS_FIRST) { |
| 568 |
break; |
| 569 |
} |
| 570 |
|
| 571 |
error = fts_ast_visit(oper, node, visitor, arg, &will_be_ignored); |
| 572 |
|
| 573 |
/* If will_be_ignored is set to true, then |
| 574 |
we encountered and ignored a FTS_EXIST or FTS_IGNORE |
| 575 |
operator. */ |
| 576 |
if (will_be_ignored) { |
| 577 |
revisit = true; |
| 578 |
/* Remember oper for list in case '-abc&def', |
| 579 |
ignored oper is from previous node of list.*/ |
| 580 |
node->oper = oper; |
| 581 |
} |
| 582 |
|
| 583 |
break; |
| 584 |
|
| 585 |
case FTS_AST_OPER: |
| 586 |
oper = node->oper; |
| 587 |
oper_node = node; |
| 588 |
|
| 589 |
/* Change the operator for revisit */ |
| 590 |
if (oper == FTS_EXIST) { |
| 591 |
oper_node->oper = FTS_EXIST_SKIP; |
| 592 |
} else if (oper == FTS_IGNORE) { |
| 593 |
oper_node->oper = FTS_IGNORE_SKIP; |
| 594 |
} |
| 595 |
|
| 596 |
break; |
| 597 |
|
| 598 |
default: |
| 599 |
if (node->visited) { |
| 600 |
continue; |
| 601 |
} |
| 602 |
|
| 603 |
ut_a(oper == FTS_NONE || !oper_node || oper_node->oper == oper || |
| 604 |
oper_node->oper == FTS_EXIST_SKIP || |
| 605 |
oper_node->oper == FTS_IGNORE_SKIP); |
| 606 |
|
| 607 |
if (oper == FTS_EXIST || oper == FTS_IGNORE) { |
| 608 |
*has_ignore = true; |
| 609 |
continue; |
| 610 |
} |
| 611 |
|
| 612 |
/* Process leaf node according to its pass.*/ |
| 613 |
if (oper == FTS_EXIST_SKIP && visit_pass == FTS_PASS_EXIST) { |
| 614 |
error = visitor(FTS_EXIST, node, arg); |
| 615 |
node->visited = true; |
| 616 |
} else if (oper == FTS_IGNORE_SKIP && visit_pass == FTS_PASS_IGNORE) { |
| 617 |
error = visitor(FTS_IGNORE, node, arg); |
| 618 |
node->visited = true; |
| 619 |
} else if (visit_pass == FTS_PASS_FIRST) { |
| 620 |
error = visitor(oper, node, arg); |
| 621 |
node->visited = true; |
| 622 |
} |
| 623 |
} |
| 624 |
} |
| 625 |
if (trx_is_interrupted(trx)) { |
| 626 |
return (DB_INTERRUPTED); |
| 627 |
} |
| 628 |
|
| 629 |
if (revisit) { |
| 630 |
/* Exist pass processes the skipped FTS_EXIST operation. */ |
| 631 |
for (node = start_node; node && error == DB_SUCCESS; node = node->next) { |
| 632 |
if (node->type == FTS_AST_LIST && node->oper != FTS_IGNORE) { |
| 633 |
error = |
| 634 |
fts_ast_visit(FTS_EXIST_SKIP, node, visitor, arg, &will_be_ignored); |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
/* Ignore pass processes the skipped FTS_IGNORE operation. */ |
| 639 |
for (node = start_node; node && error == DB_SUCCESS; node = node->next) { |
| 640 |
if (node->type == FTS_AST_LIST) { |
| 641 |
error = fts_ast_visit(FTS_IGNORE_SKIP, node, visitor, arg, |
| 642 |
&will_be_ignored); |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
return (error); |
| 648 |
} |
| 649 |
|
| 650 |
/** |
| 651 |
Create an ast string object, with NUL-terminator, so the string |
| 652 |
has one more byte than len |
| 653 |
@param[in] str pointer to string |
| 654 |
@param[in] len length of the string |
| 655 |
@return ast string with NUL-terminator */ |
| 656 |
fts_ast_string_t *fts_ast_string_create(const byte *str, ulint len) { |
| 657 |
fts_ast_string_t *ast_str; |
| 658 |
|
| 659 |
ut_ad(len > 0); |
| 660 |
|
| 661 |
ast_str = static_cast<fts_ast_string_t *>( |
| 662 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(fts_ast_string_t))); |
| 663 |
|
| 664 |
ast_str->str = static_cast<byte *>( |
| 665 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, len + 1)); |
| 666 |
|
| 667 |
ast_str->len = len; |
| 668 |
memcpy(ast_str->str, str, len); |
| 669 |
ast_str->str[len] = '\0'; |
| 670 |
|
| 671 |
return (ast_str); |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
Free an ast string instance |
| 676 |
@param[in,out] ast_str string to free */ |
| 677 |
void fts_ast_string_free(fts_ast_string_t *ast_str) { |
| 678 |
if (ast_str != nullptr) { |
| 679 |
ut::free(ast_str->str); |
| 680 |
ut::free(ast_str); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
/** |
| 685 |
Translate ast string of type FTS_AST_NUMB to unsigned long by strtoul |
| 686 |
@param[in] ast_str string to translate |
| 687 |
@param[in] base the base |
| 688 |
@return translated number */ |
| 689 |
ulint fts_ast_string_to_ul(const fts_ast_string_t *ast_str, int base) { |
| 690 |
return (strtoul(reinterpret_cast<const char *>(ast_str->str), nullptr, base)); |
| 691 |
} |
| 692 |
|
| 693 |
#ifdef UNIV_DEBUG |
| 694 |
const char *fts_ast_node_type_get(fts_ast_type_t type) { |
| 695 |
switch (type) { |
| 696 |
case FTS_AST_OPER: |
| 697 |
return ("FTS_AST_OPER"); |
| 698 |
case FTS_AST_NUMB: |
| 699 |
return ("FTS_AST_NUMB"); |
| 700 |
case FTS_AST_TERM: |
| 701 |
return ("FTS_AST_TERM"); |
| 702 |
case FTS_AST_TEXT: |
| 703 |
return ("FTS_AST_TEXT"); |
| 704 |
case FTS_AST_LIST: |
| 705 |
return ("FTS_AST_LIST"); |
| 706 |
case FTS_AST_SUBEXP_LIST: |
| 707 |
return ("FTS_AST_SUBEXP_LIST"); |
| 708 |
case FTS_AST_PARSER_PHRASE_LIST: |
| 709 |
return ("FTS_AST_PARSER_PHRASE_LIST"); |
| 710 |
} |
| 711 |
ut_d(ut_error); |
| 712 |
ut_o(return ("FTS_UNKNOWN")); |
| 713 |
} |
| 714 |
#endif /* UNIV_DEBUG */ |