Debian Codegraph @

lib/table_selector.c

raw symbols SCIP
1 /*
2 Copyright (C) 2010-2018 Brazil
3 Copyright (C) 2018-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"
21 #include "grn_expr.h"
22 #include "grn_expr_code.h"
23 #include "grn_expr_executor.h"
24 #include "grn_ii.h"
25 #include "grn_posting.h"
26 #include "grn_report.h"
27 #include "grn_scan_info.h"
28 #include "grn_selector.h"
29 #include "grn_table.h"
30 #include "grn_table_selector.h"
31 #include "grn_util.h"
32
33 #include <stdio.h>
34
35 static double grn_table_select_enough_filtered_ratio = 0.01;
36 static int64_t grn_table_select_max_n_enough_filtered_records = 1000;
37 static bool grn_table_select_min_id_skip_enable = true;
38 static bool grn_query_log_show_condition = true;
39
40 void
41 grn_table_selector_init_from_env(void)
42 {
43 {
44 char grn_table_select_enough_filtered_ratio_env[GRN_ENV_BUFFER_SIZE];
45 grn_getenv("GRN_TABLE_SELECT_ENOUGH_FILTERED_RATIO",
46 grn_table_select_enough_filtered_ratio_env,
47 GRN_ENV_BUFFER_SIZE);
48 if (grn_table_select_enough_filtered_ratio_env[0]) {
49 grn_table_select_enough_filtered_ratio =
50 atof(grn_table_select_enough_filtered_ratio_env);
51 }
52 }
53
54 {
55 char
56 grn_table_select_max_n_enough_filtered_records_env[GRN_ENV_BUFFER_SIZE];
57 grn_getenv("GRN_TABLE_SELECT_MAX_N_ENOUGH_FILTERED_RECORDS",
58 grn_table_select_max_n_enough_filtered_records_env,
59 GRN_ENV_BUFFER_SIZE);
60 if (grn_table_select_max_n_enough_filtered_records_env[0]) {
61 grn_table_select_max_n_enough_filtered_records =
62 atoi(grn_table_select_max_n_enough_filtered_records_env);
63 }
64 }
65
66 {
67 char env[GRN_ENV_BUFFER_SIZE];
68 grn_getenv("GRN_TABLE_SELECT_MIN_ID_SKIP_ENABLE", env, GRN_ENV_BUFFER_SIZE);
69 if (env[0]) {
70 if (strcmp(env, "no") == 0) {
71 grn_table_select_min_id_skip_enable = false;
72 } else {
73 grn_table_select_min_id_skip_enable = true;
74 }
75 } else {
76 grn_getenv("GRN_TABLE_SELECT_AND_MIN_SKIP_ENABLE",
77 env,
78 GRN_ENV_BUFFER_SIZE);
79 if (env[0]) {
80 if (strcmp(env, "no") == 0) {
81 grn_table_select_min_id_skip_enable = false;
82 } else {
83 grn_table_select_min_id_skip_enable = true;
84 }
85 }
86 }
87 }
88
89 {
90 char grn_query_log_show_condition_env[GRN_ENV_BUFFER_SIZE];
91 grn_getenv("GRN_QUERY_LOG_SHOW_CONDITION",
92 grn_query_log_show_condition_env,
93 GRN_ENV_BUFFER_SIZE);
94 if (strcmp(grn_query_log_show_condition_env, "no") == 0) {
95 grn_query_log_show_condition = false;
96 } else {
97 grn_query_log_show_condition = true;
98 }
99 }
100 }
101
102 void
103 grn_table_selector_init(grn_ctx *ctx,
104 grn_table_selector *table_selector,
105 grn_obj *table,
106 grn_obj *expr,
107 grn_operator op)
108 {
109 table_selector->table = table;
110 table_selector->expr = expr;
111 table_selector->op = op;
112 table_selector->min_id = GRN_ID_NIL;
113 table_selector->use_sequential_scan = false;
114 table_selector->query_options = grn_expr_get_query_options(ctx, expr);
115 table_selector->weight_factor = 1.0;
116 table_selector->enough_filtered_ratio =
117 grn_table_select_enough_filtered_ratio;
118 table_selector->max_n_enough_filtered_records =
119 grn_table_select_max_n_enough_filtered_records;
120 table_selector->ensure_using_select_result = false;
121 grn_fuzzy_search_optarg fuzzy_options = {0};
122 table_selector->fuzzy_options = fuzzy_options;
123 table_selector->fuzzy_options.flags |=
124 GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION |
125 GRN_TABLE_FUZZY_SEARCH_USE_PREFIX_LENGTH |
126 GRN_TABLE_FUZZY_SEARCH_SKIP_TOKENIZE;
127 table_selector->fuzzy_options.max_expansion =
128 GRN_TABLE_SELECTOR_FUZZY_MAX_EXPANSIONS_DEFAULT;
129 grn_table_selector_data data = {0};
130 table_selector->data = data;
131 }
132
133 void
134 grn_table_selector_fin(grn_ctx *ctx, grn_table_selector *table_selector)
135 {
136 if (table_selector->data.scanner) {
137 grn_scanner_close(ctx, table_selector->data.scanner);
138 }
139 }
140
141 grn_table_selector *
142 grn_table_selector_open(grn_ctx *ctx,
143 grn_obj *table,
144 grn_obj *expr,
145 grn_operator op)
146 {
147 GRN_API_ENTER;
148 grn_table_selector *table_selector = GRN_CALLOC(sizeof(grn_table_selector));
149 if (table_selector) {
150 grn_table_selector_init(ctx, table_selector, table, expr, op);
151 }
152 GRN_API_RETURN(table_selector);
153 }
154
155 grn_rc
156 grn_table_selector_close(grn_ctx *ctx, grn_table_selector *table_selector)
157 {
158 GRN_API_ENTER;
159 if (table_selector) {
160 grn_table_selector_fin(ctx, table_selector);
161 GRN_FREE(table_selector);
162 }
163 GRN_API_RETURN(ctx->rc);
164 }
165
166 grn_id
167 grn_table_selector_get_min_id(grn_ctx *ctx, grn_table_selector *table_selector)
168 {
169 return table_selector->min_id;
170 }
171
172 grn_rc
173 grn_table_selector_set_min_id(grn_ctx *ctx,
174 grn_table_selector *table_selector,
175 grn_id min_id)
176 {
177 GRN_API_ENTER;
178 table_selector->min_id = min_id;
179 GRN_API_RETURN(ctx->rc);
180 }
181
182 bool
183 grn_table_selector_get_use_sequential_scan(grn_ctx *ctx,
184 grn_table_selector *table_selector)
185 {
186 return table_selector->use_sequential_scan;
187 }
188
189 grn_rc
190 grn_table_selector_set_use_sequential_scan(grn_ctx *ctx,
191 grn_table_selector *table_selector,
192 bool use)
193 {
194 GRN_API_ENTER;
195 table_selector->use_sequential_scan = use;
196 GRN_API_RETURN(ctx->rc);
197 }
198
199 float
200 grn_table_selector_get_weight_factor(grn_ctx *ctx,
201 grn_table_selector *table_selector)
202 {
203 return table_selector->weight_factor;
204 }
205
206 grn_rc
207 grn_table_selector_set_weight_factor(grn_ctx *ctx,
208 grn_table_selector *table_selector,
209 float factor)
210 {
211 GRN_API_ENTER;
212 table_selector->weight_factor = factor;
213 GRN_API_RETURN(ctx->rc);
214 }
215
216 double
217 grn_table_selector_get_enough_filtered_ratio(grn_ctx *ctx,
218 grn_table_selector *table_selector)
219 {
220 return table_selector->enough_filtered_ratio;
221 }
222
223 grn_rc
224 grn_table_selector_set_enough_filtered_ratio(grn_ctx *ctx,
225 grn_table_selector *table_selector,
226 double ratio)
227 {
228 GRN_API_ENTER;
229 table_selector->enough_filtered_ratio = ratio;
230 GRN_API_RETURN(ctx->rc);
231 }
232
233 int64_t
234 grn_table_selector_get_max_n_enough_filtered_records(
235 grn_ctx *ctx, grn_table_selector *table_selector)
236 {
237 return table_selector->max_n_enough_filtered_records;
238 }
239
240 grn_rc
241 grn_table_selector_set_max_n_enough_filtered_records(
242 grn_ctx *ctx, grn_table_selector *table_selector, int64_t n)
243 {
244 GRN_API_ENTER;
245 table_selector->max_n_enough_filtered_records = n;
246 GRN_API_RETURN(ctx->rc);
247 }
248
249 bool
250 grn_table_selector_get_ensure_using_select_result(
251 grn_ctx *ctx, grn_table_selector *table_selector)
252 {
253 return table_selector->ensure_using_select_result;
254 }
255
256 grn_rc
257 grn_table_selector_set_ensure_using_select_result(
258 grn_ctx *ctx, grn_table_selector *table_selector, bool use)
259 {
260 GRN_API_ENTER;
261 table_selector->ensure_using_select_result = use;
262 GRN_API_RETURN(ctx->rc);
263 }
264
265 uint32_t
266 grn_table_selector_get_fuzzy_max_distance(grn_ctx *ctx,
267 grn_table_selector *table_selector)
268 {
269 return table_selector->fuzzy_options.max_distance;
270 }
271
272 grn_rc
273 grn_table_selector_set_fuzzy_max_distance(grn_ctx *ctx,
274 grn_table_selector *table_selector,
275 uint32_t distance)
276 {
277 GRN_API_ENTER;
278 table_selector->fuzzy_options.max_distance = distance;
279 GRN_API_RETURN(ctx->rc);
280 }
281
282 uint32_t
283 grn_table_selector_get_fuzzy_max_expansions(grn_ctx *ctx,
284 grn_table_selector *table_selector)
285 {
286 return table_selector->fuzzy_options.max_expansion;
287 }
288
289 grn_rc
290 grn_table_selector_set_fuzzy_max_expansions(grn_ctx *ctx,
291 grn_table_selector *table_selector,
292 uint32_t expansions)
293 {
294 GRN_API_ENTER;
295 table_selector->fuzzy_options.max_expansion = expansions;
296 GRN_API_RETURN(ctx->rc);
297 }
298
299 uint32_t
300 grn_table_selector_get_fuzzy_prefix_length(grn_ctx *ctx,
301 grn_table_selector *table_selector)
302 {
303 return table_selector->fuzzy_options.prefix_length;
304 }
305
306 grn_rc
307 grn_table_selector_set_fuzzy_prefix_length(grn_ctx *ctx,
308 grn_table_selector *table_selector,
309 uint32_t length)
310 {
311 GRN_API_ENTER;
312 table_selector->fuzzy_options.prefix_length = length;
313 GRN_API_RETURN(ctx->rc);
314 }
315
316 float
317 grn_table_selector_get_fuzzy_max_distance_ratio(
318 grn_ctx *ctx, grn_table_selector *table_selector)
319 {
320 return table_selector->fuzzy_options.max_distance_ratio;
321 }
322
323 grn_rc
324 grn_table_selector_set_fuzzy_max_distance_ratio(
325 grn_ctx *ctx, grn_table_selector *table_selector, float ratio)
326 {
327 GRN_API_ENTER;
328 if (ratio <= FLT_EPSILON) {
329 table_selector->fuzzy_options.max_distance_ratio = 0.0f;
330 table_selector->fuzzy_options.flags &=
331 ~GRN_TABLE_FUZZY_SEARCH_USE_MAX_DISTANCE_RATIO;
332 } else {
333 table_selector->fuzzy_options.max_distance_ratio = ratio;
334 table_selector->fuzzy_options.flags |=
335 GRN_TABLE_FUZZY_SEARCH_USE_MAX_DISTANCE_RATIO;
336 }
337 GRN_API_RETURN(ctx->rc);
338 }
339
340 bool
341 grn_table_selector_get_fuzzy_with_transpotion(
342 grn_ctx *ctx, grn_table_selector *table_selector)
343 {
344 return table_selector->fuzzy_options.flags &
345 GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION;
346 }
347
348 grn_rc
349 grn_table_selector_set_fuzzy_with_transposition(
350 grn_ctx *ctx, grn_table_selector *table_selector, bool with)
351 {
352 GRN_API_ENTER;
353 if (with) {
354 table_selector->fuzzy_options.flags |=
355 GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION;
356 } else {
357 table_selector->fuzzy_options.flags &=
358 ~GRN_TABLE_FUZZY_SEARCH_WITH_TRANSPOSITION;
359 }
360 GRN_API_RETURN(ctx->rc);
361 }
362
363 bool
364 grn_table_selector_get_fuzzy_tokenize(grn_ctx *ctx,
365 grn_table_selector *table_selector)
366 {
367 return !(table_selector->fuzzy_options.flags &
368 GRN_TABLE_FUZZY_SEARCH_SKIP_TOKENIZE);
369 }
370
371 grn_rc
372 grn_table_selector_set_fuzzy_tokenize(grn_ctx *ctx,
373 grn_table_selector *table_selector,
374 bool tokenize)
375 {
376 GRN_API_ENTER;
377 if (tokenize) {
378 table_selector->fuzzy_options.flags &=
379 ~GRN_TABLE_FUZZY_SEARCH_SKIP_TOKENIZE;
380 } else {
381 table_selector->fuzzy_options.flags |= GRN_TABLE_FUZZY_SEARCH_SKIP_TOKENIZE;
382 }
383 GRN_API_RETURN(ctx->rc);
384 }
385
386 static grn_obj *
387 selector_create_result_set(grn_ctx *ctx, grn_table_selector *table_selector)
388 {
389 grn_obj *result_set =
390 grn_table_create(ctx,
391 NULL,
392 0,
393 NULL,
394 GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
395 table_selector->table,
396 NULL);
397 if (!result_set) {
398 return NULL;
399 }
400
401 grn_obj *initial_result_set = table_selector->data.initial_result_set;
402 grn_hash *columns = grn_table_all_columns(ctx, initial_result_set);
403 if (!columns) {
404 return result_set;
405 }
406 GRN_HASH_EACH_BEGIN(ctx, columns, cursor, id)
407 {
408 void *key;
409 grn_hash_cursor_get_key(ctx, cursor, &key);
410 grn_id column_id = *((grn_id *)key);
411 grn_obj *column = grn_ctx_at(ctx, column_id);
412 char name[GRN_TABLE_MAX_KEY_SIZE];
413 int name_size = grn_column_name(ctx, column, name, GRN_TABLE_MAX_KEY_SIZE);
414 grn_obj *range = grn_ctx_at(ctx, DB_OBJ(column)->range);
415 grn_column_create(ctx,
416 result_set,
417 name,
418 (unsigned int)name_size,
419 NULL,
420 grn_column_get_flags(ctx, column),
421 range);
422 grn_obj_unref(ctx, range);
423 grn_obj_unref(ctx, column);
424 }
425 GRN_HASH_EACH_END(ctx, cursor);
426 return result_set;
427 }
428
429 static inline int32_t
430 exec_result_to_score(grn_ctx *ctx, grn_obj *result, grn_obj *score_buffer)
431 {
432 if (!result) {
433 return 0;
434 }
435
436 switch (result->header.type) {
437 case GRN_VOID:
438 return 0;
439 case GRN_BULK:
440 switch (result->header.domain) {
441 case GRN_DB_BOOL:
442 return GRN_BOOL_VALUE(result) ? 1 : 0;
443 case GRN_DB_INT32:
444 return GRN_INT32_VALUE(result);
445 default:
446 GRN_BULK_REWIND(score_buffer);
447 if (grn_obj_cast(ctx, result, score_buffer, false) != GRN_SUCCESS) {
448 return 1;
449 }
450 return GRN_INT32_VALUE(score_buffer);
451 }
452 case GRN_UVECTOR:
453 case GRN_PVECTOR:
454 case GRN_VECTOR:
455 return 1;
456 default:
457 return 1; /* TODO: 1 is reasonable? */
458 }
459 }
460
461 static inline void
462 select_sequential_scan(grn_ctx *ctx,
463 grn_table_selector *table_selector,
464 grn_obj *result_set)
465 {
466 grn_expr_executor executor;
467 grn_expr_executor_init(ctx, &executor, table_selector->expr);
468 if (ctx->rc != GRN_SUCCESS) {
469 return;
470 }
471
472 grn_obj score_buffer;
473 GRN_INT32_INIT(&score_buffer, 0);
474 switch (table_selector->op) {
475 case GRN_OP_OR:
476 GRN_TABLE_EACH_BEGIN(ctx, table_selector->table, cursor, id)
477 {
478 grn_obj *result = grn_expr_executor_exec(ctx, &executor, id);
479 if (ctx->rc != GRN_SUCCESS) {
480 break;
481 }
482 int32_t score = exec_result_to_score(ctx, result, &score_buffer);
483 if (score > 0) {
484 void *value;
485 if (grn_hash_add(ctx,
486 (grn_hash *)result_set,
487 &id,
488 sizeof(grn_id),
489 &value,
490 NULL) != GRN_ID_NIL) {
491 grn_rset_recinfo *ri = value;
492 grn_table_add_subrec(ctx,
493 result_set,
494 ri,
495 score,
496 (grn_rset_posinfo *)&id,
497 1);
498 }
499 }
500 }
501 GRN_TABLE_EACH_END(ctx, cursor);
502 break;
503 case GRN_OP_AND:
504 GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)result_set, cursor, id)
505 {
506 void *key;
507 grn_hash_cursor_get_key(ctx, cursor, &key);
508 grn_id record_id = *((grn_id *)key);
509 grn_obj *result = grn_expr_executor_exec(ctx, &executor, record_id);
510 if (ctx->rc != GRN_SUCCESS) {
511 break;
512 }
513 int32_t score = exec_result_to_score(ctx, result, &score_buffer);
514 if (score > 0) {
515 void *value;
516 grn_hash_cursor_get_value(ctx, cursor, &value);
517 grn_rset_recinfo *ri = value;
518 grn_table_add_subrec(ctx,
519 result_set,
520 ri,
521 score,
522 (grn_rset_posinfo *)&record_id,
523 1);
524 } else {
525 grn_hash_cursor_delete(ctx, cursor, NULL);
526 }
527 }
528 GRN_HASH_EACH_END(ctx, cursor);
529 break;
530 case GRN_OP_AND_NOT:
531 GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)result_set, cursor, id)
532 {
533 void *key;
534 grn_hash_cursor_get_key(ctx, cursor, &key);
535 grn_id record_id = *((grn_id *)key);
536 grn_obj *result = grn_expr_executor_exec(ctx, &executor, record_id);
537 if (ctx->rc != GRN_SUCCESS) {
538 break;
539 }
540 int32_t score = exec_result_to_score(ctx, result, &score_buffer);
541 if (score > 0) {
542 grn_hash_cursor_delete(ctx, cursor, NULL);
543 }
544 }
545 GRN_HASH_EACH_END(ctx, cursor);
546 break;
547 case GRN_OP_ADJUST:
548 GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)result_set, cursor, id)
549 {
550 void *key;
551 grn_hash_cursor_get_key(ctx, cursor, &key);
552 grn_id record_id = *((grn_id *)key);
553 grn_obj *result = grn_expr_executor_exec(ctx, &executor, record_id);
554 if (ctx->rc != GRN_SUCCESS) {
555 break;
556 }
557 int32_t score = exec_result_to_score(ctx, result, &score_buffer);
558 if (score > 0) {
559 void *value;
560 grn_hash_cursor_get_value(ctx, cursor, &value);
561 grn_rset_recinfo *ri = value;
562 grn_table_add_subrec(ctx,
563 result_set,
564 ri,
565 score,
566 (grn_rset_posinfo *)&record_id,
567 1);
568 }
569 }
570 GRN_HASH_EACH_END(ctx, cursor);
571 break;
572 default:
573 break;
574 }
575 GRN_OBJ_FIN(ctx, &score_buffer);
576 grn_expr_executor_fin(ctx, &executor);
577 }
578
579 static inline void
580 select_index_report(grn_ctx *ctx, const char *tag, grn_obj *index)
581 {
582 grn_report_index(ctx, "[table-selector][select]", tag, index);
583 }
584
585 static inline void
586 select_index_not_used_report(grn_ctx *ctx,
587 const char *tag,
588 grn_obj *index,
589 const char *reason)
590 {
591 grn_report_index_not_used(ctx,
592 "[table-selector][select]",
593 tag,
594 index,
595 reason);
596 }
597
598 static inline bool
599 select_index_use_sequential_search(grn_ctx *ctx,
600 grn_table_selector *table_selector,
601 grn_obj *result_set,
602 grn_operator logical_op,
603 const char *tag,
604 grn_obj *index)
605 {
606 if (logical_op != GRN_OP_AND) {
607 return false;
608 }
609
610 grn_obj *table = grn_ctx_at(ctx, result_set->header.domain);
611 unsigned int n_records = grn_table_size(ctx, table);
612 unsigned int n_filtered_records = grn_table_size(ctx, result_set);
613 grn_obj_unref(ctx, table);
614 double filtered_ratio;
615 if (n_records == 0) {
616 filtered_ratio = 1.0;
617 } else {
618 filtered_ratio = (double)n_filtered_records / (double)n_records;
619 }
620
621 if (filtered_ratio >= table_selector->enough_filtered_ratio) {
622 return false;
623 }
624
625 if (n_filtered_records > table_selector->max_n_enough_filtered_records) {
626 return false;
627 }
628
629 grn_obj reason;
630 GRN_TEXT_INIT(&reason, 0);
631 grn_text_printf(ctx,
632 &reason,
633 "enough filtered: "
634 "%.2f%%(%d/%d) < %.2f%% && %d <= %" GRN_FMT_INT64D,
635 filtered_ratio * 100,
636 n_filtered_records,
637 n_records,
638 table_selector->enough_filtered_ratio * 100,
639 n_filtered_records,
640 table_selector->max_n_enough_filtered_records);
641 GRN_TEXT_PUTC(ctx, &reason, '\0');
642 select_index_not_used_report(ctx, tag, index, GRN_TEXT_VALUE(&reason));
643 GRN_OBJ_FIN(ctx, &reason);
644 return true;
645 }
646
647 static inline grn_id
648 select_index_resolve_key(grn_ctx *ctx, grn_obj *domain, grn_obj *key)
649 {
650 if (GRN_OBJ_GET_DOMAIN(key) == DB_OBJ(domain)->id) {
651 return GRN_RECORD_VALUE(key);
652 } else if (GRN_OBJ_GET_DOMAIN(key) == domain->header.domain) {
653 return grn_table_get(ctx,
654 domain,
655 GRN_BULK_HEAD(key),
656 (unsigned int)GRN_BULK_VSIZE(key));
657 } else {
658 grn_id id = GRN_ID_NIL;
659 grn_obj casted_key;
660 GRN_OBJ_INIT(&casted_key, GRN_BULK, 0, domain->header.domain);
661 if (grn_obj_cast(ctx, key, &casted_key, false) == GRN_SUCCESS) {
662 id = grn_table_get(ctx,
663 domain,
664 GRN_BULK_HEAD(&casted_key),
665 (unsigned int)GRN_BULK_VSIZE(&casted_key));
666 }
667 GRN_OBJ_FIN(ctx, &casted_key);
668 return id;
669 }
670 }
671
672 static inline grn_rc
673 select_index_equal(grn_ctx *ctx,
674 grn_table_selector *table_selector,
675 grn_obj *index,
676 grn_operator op,
677 grn_obj *result_set,
678 grn_operator logical_op)
679 {
680 scan_info *si = table_selector->data.scan_info;
681
682 if (si->query->header.type != GRN_BULK) {
683 /* We can't use index for non bulk value such as vector. */
684 return GRN_FUNCTION_NOT_IMPLEMENTED;
685 }
686
687 if (GRN_BULK_VSIZE(si->query) == 0) {
688 /* We can't use index for empty value. */
689 return GRN_FUNCTION_NOT_IMPLEMENTED;
690 }
691
692 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
693 if (grn_obj_is_accessor(ctx, index)) {
694 grn_search_optarg optarg;
695 memset(&optarg, 0, sizeof(grn_search_optarg));
696 optarg.mode = GRN_OP_EQUAL;
697 optarg.vector_size = 1;
698 rc = grn_obj_search(ctx, index, si->query, result_set, logical_op, &optarg);
699 } else {
700 const char *tag = "[equal]";
701
702 grn_obj *domain = grn_ctx_at(ctx, index->header.domain);
703 if (domain) {
704 bool optimizable = false;
705
706 if (domain->header.domain == GRN_DB_SHORT_TEXT) {
707 grn_obj *normalizer = NULL;
708 grn_table_get_info(ctx, domain, NULL, NULL, NULL, &normalizer, NULL);
709 if (normalizer == grn_ctx_get(ctx, "NormalizerAuto", -1)) {
710 optimizable = true;
711 }
712 } else {
713 optimizable = true;
714 }
715 if (optimizable && select_index_use_sequential_search(ctx,
716 table_selector,
717 result_set,
718 logical_op,
719 tag,
720 index)) {
721 grn_obj_unref(ctx, domain);
722 domain = NULL;
723 }
724 }
725
726 if (domain) {
727 select_index_report(ctx, tag, index);
728
729 grn_id tid = select_index_resolve_key(ctx, domain, si->query);
730 if (tid == GRN_ID_NIL) {
731 rc = GRN_SUCCESS;
732 } else {
733 grn_obj *index_cursor =
734 grn_index_cursor_open(ctx, NULL, index, GRN_ID_NIL, GRN_ID_MAX, 0);
735 if (index_cursor) {
736 grn_search_optarg *options = &(table_selector->data.search_options);
737 rc = grn_index_cursor_set_term_id(ctx, index_cursor, tid);
738 if (rc == GRN_SUCCESS) {
739 if (options->weight_vector_float) {
740 rc = grn_index_cursor_set_scales(ctx,
741 index_cursor,
742 options->weight_vector_float,
743 (size_t)(options->vector_size));
744 } else {
745 rc = grn_index_cursor_set_scale(ctx,
746 index_cursor,
747 options->weight_float);
748 }
749 }
750 if (options->start_position) {
751 if (rc == GRN_SUCCESS) {
752 rc =
753 grn_index_cursor_set_start_position(ctx,
754 index_cursor,
755 *(options->start_position));
756 }
757 }
758 if (rc == GRN_SUCCESS) {
759 rc = grn_result_set_add_index_cursor(ctx,
760 (grn_hash *)result_set,
761 index_cursor,
762 1,
763 1.0,
764 logical_op);
765 }
766 grn_obj_close(ctx, index_cursor);
767 }
768 }
769
770 grn_obj_unref(ctx, domain);
771 }
772
773 if (rc == GRN_SUCCESS) {
774 grn_ii_resolve_sel_and(ctx, (grn_hash *)result_set, logical_op);
775 }
776 }
777
778 return rc;
779 }
780
781 static inline grn_rc
782 select_index_not_equal(grn_ctx *ctx,
783 grn_table_selector *table_selector,
784 grn_obj *index,
785 grn_operator op,
786 grn_obj *result_set,
787 grn_operator logical_op)
788 {
789 scan_info *si = table_selector->data.scan_info;
790
791 if (GRN_BULK_VSIZE(si->query) == 0) {
792 /* We can't use index for empty value. */
793 return GRN_FUNCTION_NOT_IMPLEMENTED;
794 }
795
796 if (logical_op != GRN_OP_AND) {
797 /* We can't use index for OR and AND_NOT. */
798 return GRN_FUNCTION_NOT_IMPLEMENTED;
799 }
800
801 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
802 if (grn_obj_is_accessor(ctx, index)) {
803 grn_obj dest;
804 grn_accessor *a = (grn_accessor *)index;
805 grn_id id;
806 switch (a->action) {
807 case GRN_ACCESSOR_GET_ID:
808 select_index_report(ctx, "[not-equal][accessor][id]", a->obj);
809 GRN_UINT32_INIT(&dest, 0);
810 if (!grn_obj_cast(ctx, si->query, &dest, false)) {
811 id = GRN_UINT32_VALUE(&dest);
812 if (id != GRN_ID_NIL) {
813 if (id == grn_table_at(ctx, a->obj, id)) {
814 grn_hash_delete(ctx,
815 (grn_hash *)result_set,
816 &id,
817 sizeof(grn_id),
818 NULL);
819 }
820 }
821 rc = GRN_SUCCESS;
822 }
823 GRN_OBJ_FIN(ctx, &dest);
824 break;
825 case GRN_ACCESSOR_GET_KEY:
826 select_index_report(ctx, "[not-equal][accessor][key]", a->obj);
827 GRN_OBJ_INIT(&dest, GRN_BULK, 0, a->obj->header.domain);
828 if (!grn_obj_cast(ctx, si->query, &dest, false)) {
829 id = grn_table_get(ctx,
830 a->obj,
831 GRN_BULK_HEAD(&dest),
832 (unsigned int)GRN_BULK_VSIZE(&dest));
833 if (id != GRN_ID_NIL) {
834 grn_hash_delete(ctx,
835 (grn_hash *)result_set,
836 &id,
837 sizeof(grn_id),
838 NULL);
839 }
840 rc = GRN_SUCCESS;
841 }
842 GRN_OBJ_FIN(ctx, &dest);
843 break;
844 }
845 } else {
846 grn_obj *domain = grn_ctx_at(ctx, index->header.domain);
847 if (domain) {
848 grn_id tid;
849
850 select_index_report(ctx, "[not-equal]", index);
851
852 tid = select_index_resolve_key(ctx, domain, si->query);
853 if (tid == GRN_ID_NIL) {
854 rc = GRN_SUCCESS;
855 } else {
856 grn_ii *ii = (grn_ii *)index;
857 grn_ii_cursor *ii_cursor;
858
859 ii_cursor = grn_ii_cursor_open(ctx,
860 ii,
861 tid,
862 GRN_ID_NIL,
863 GRN_ID_MAX,
864 (int)(ii->n_elements),
865 0);
866 if (ii_cursor) {
867 grn_search_optarg *options = &(table_selector->data.search_options);
868 if (options->weight_vector_float) {
869 rc = grn_ii_cursor_set_scales(ctx,
870 ii_cursor,
871 options->weight_vector_float,
872 (size_t)(options->vector_size));
873 } else {
874 rc = grn_ii_cursor_set_scale(ctx, ii_cursor, options->weight_float);
875 }
876 grn_posting *posting;
877 while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
878 if (options->start_position) {
879 while ((posting = grn_ii_cursor_next_pos(ctx, ii_cursor))) {
880 if (posting->pos == (uint32_t)(*(options->start_position))) {
881 break;
882 }
883 }
884 if (!posting) {
885 continue;
886 }
887 }
888
889 grn_hash_delete(ctx,
890 (grn_hash *)result_set,
891 &(posting->rid),
892 sizeof(grn_id),
893 NULL);
894 }
895 grn_ii_cursor_close(ctx, ii_cursor);
896 rc = GRN_SUCCESS;
897 }
898 }
899 }
900 }
901
902 return rc;
903 }
904
905 static inline grn_rc
906 select_index_fix_column_prefix(grn_ctx *ctx,
907 grn_table_selector *table_selector,
908 grn_obj *index,
909 grn_operator op,
910 grn_obj *result_set,
911 grn_operator logical_op)
912 {
913 const char *tag = "[table][select][index][fix][column][prefix]";
914 grn_obj *lexicon = grn_ctx_at(ctx, index->header.domain);
915 if (!lexicon) {
916 grn_obj inspected;
917 GRN_TEXT_INIT(&inspected, 0);
918 grn_inspect(ctx, &inspected, index);
919 ERR(GRN_OBJECT_CORRUPT,
920 "%s index's domain is corrupt: %.*s",
921 tag,
922 (int)GRN_TEXT_LEN(&inspected),
923 GRN_TEXT_VALUE(&inspected));
924 GRN_OBJ_FIN(ctx, &inspected);
925 return ctx->rc;
926 }
927
928 scan_info *si = table_selector->data.scan_info;
929 grn_obj query_buffer;
930 grn_obj *query;
931 grn_table_cursor *table_cursor = NULL;
932 grn_obj *index_cursor = NULL;
933 if (grn_type_id_is_compatible(ctx,
934 lexicon->header.domain,
935 si->query->header.domain)) {
936 query = si->query;
937 } else {
938 GRN_OBJ_INIT(&query_buffer, GRN_BULK, 0, lexicon->header.domain);
939 query = &query_buffer;
940 grn_rc rc = grn_obj_cast(ctx, si->query, &query_buffer, false);
941 if (rc != GRN_SUCCESS) {
942 grn_obj query_inspected;
943 grn_obj domain_inspected;
944 GRN_TEXT_INIT(&query_inspected, 0);
945 GRN_TEXT_INIT(&domain_inspected, 0);
946 grn_inspect(ctx, &query_inspected, si->query);
947 grn_obj *domain = grn_ctx_at(ctx, query_buffer.header.domain);
948 grn_inspect(ctx, &domain_inspected, domain);
949 grn_obj_unref(ctx, domain);
950 ERR(rc,
951 "%s failed to cast query: "
952 "<%.*s> -> <%.*s>",
953 tag,
954 (int)GRN_TEXT_LEN(&query_inspected),
955 GRN_TEXT_VALUE(&query_inspected),
956 (int)GRN_TEXT_LEN(&domain_inspected),
957 GRN_TEXT_VALUE(&domain_inspected));
958 GRN_OBJ_FIN(ctx, &domain_inspected);
959 GRN_OBJ_FIN(ctx, &query_inspected);
960 goto exit;
961 }
962 }
963
964 table_cursor = grn_table_cursor_open(ctx,
965 lexicon,
966 GRN_BULK_HEAD(si->query),
967 (unsigned int)GRN_BULK_VSIZE(si->query),
968 NULL,
969 0,
970 0,
971 -1,
972 GRN_CURSOR_PREFIX | GRN_CURSOR_GE);
973 if (!table_cursor) {
974 grn_obj query_inspected;
975 GRN_TEXT_INIT(&query_inspected, 0);
976 grn_inspect(ctx, &query_inspected, si->query);
977 ERR(ctx->rc,
978 "%s failed to create table cursor: <%.*s>",
979 tag,
980 (int)GRN_TEXT_LEN(&query_inspected),
981 GRN_TEXT_VALUE(&query_inspected));
982 GRN_OBJ_FIN(ctx, &query_inspected);
983 goto exit;
984 }
985
986 index_cursor =
987 grn_index_cursor_open(ctx, table_cursor, index, GRN_ID_NIL, GRN_ID_MAX, 0);
988 if (!index_cursor) {
989 grn_obj query_inspected;
990 GRN_TEXT_INIT(&query_inspected, 0);
991 grn_inspect(ctx, &query_inspected, si->query);
992 ERR(ctx->rc,
993 "%s failed to create table cursor: <%.*s>",
994 tag,
995 (int)GRN_TEXT_LEN(&query_inspected),
996 GRN_TEXT_VALUE(&query_inspected));
997 GRN_OBJ_FIN(ctx, &query_inspected);
998 goto exit;
999 }
1000 grn_search_optarg *options = &(table_selector->data.search_options);
1001 if (options->weight_vector_float) {
1002 grn_index_cursor_set_scales(ctx,
1003 index_cursor,
1004 options->weight_vector_float,
1005 (size_t)(options->vector_size));
1006 } else {
1007 grn_index_cursor_set_scale(ctx, index_cursor, options->weight_float);
1008 }
1009 if (options->start_position) {
1010 grn_index_cursor_set_start_position(ctx,
1011 index_cursor,
1012 *(options->start_position));
1013 }
1014 select_index_report(ctx, "[prefix]", index);
1015 grn_result_set_add_index_cursor(ctx,
1016 (grn_hash *)result_set,
1017 index_cursor,
1018 1,
1019 1.0,
1020 logical_op);
1021 exit:
1022 if (index_cursor) {
1023 grn_obj_close(ctx, index_cursor);
1024 }
1025 if (table_cursor) {
1026 grn_table_cursor_close(ctx, table_cursor);
1027 }
1028 if (query == &query_buffer) {
1029 GRN_OBJ_FIN(ctx, &query_buffer);
1030 }
1031 grn_obj_unref(ctx, lexicon);
1032 return ctx->rc;
1033 }
1034
1035 static inline grn_rc
1036 select_index_fix(grn_ctx *ctx,
1037 grn_table_selector *table_selector,
1038 grn_obj *index,
1039 grn_operator op,
1040 grn_obj *result_set,
1041 grn_operator logical_op)
1042 {
1043 scan_info *si = table_selector->data.scan_info;
1044 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1045
1046 if (grn_obj_is_accessor(ctx, index)) {
1047 grn_accessor *a = (grn_accessor *)index;
1048 switch (a->action) {
1049 case GRN_ACCESSOR_GET_ID:
1050 /* todo */
1051 break;
1052 case GRN_ACCESSOR_GET_KEY:
1053 index = a->obj;
1054 break;
1055 }
1056 }
1057
1058 if (grn_obj_is_table(ctx, index)) {
1059 grn_obj *table = index;
1060 if (op == GRN_OP_SUFFIX) {
1061 select_index_report(ctx, "[suffix][accessor][key]", table);
1062 } else {
1063 select_index_report(ctx, "[prefix][accessor][key]", table);
1064 }
1065 grn_obj dest;
1066 GRN_OBJ_INIT(&dest, GRN_BULK, 0, table->header.domain);
1067 if (!grn_obj_cast(ctx, si->query, &dest, false)) {
1068 if (op == GRN_OP_PREFIX) {
1069 grn_table_cursor *cursor =
1070 grn_table_cursor_open(ctx,
1071 index,
1072 GRN_BULK_HEAD(&dest),
1073 (uint32_t)GRN_BULK_VSIZE(&dest),
1074 NULL,
1075 0,
1076 0,
1077 -1,
1078 GRN_CURSOR_PREFIX | GRN_CURSOR_GE);
1079 if (cursor) {
1080 rc = grn_result_set_add_table_cursor(ctx,
1081 (grn_hash *)result_set,
1082 cursor,
1083 1.0,
1084 logical_op);
1085 grn_table_cursor_close(ctx, cursor);
1086 }
1087 } else {
1088 grn_hash *pres;
1089 if ((pres = grn_hash_create(ctx,
1090 NULL,
1091 sizeof(grn_id),
1092 0,
1093 GRN_OBJ_TABLE_HASH_KEY))) {
1094 rc = grn_table_search(ctx,
1095 index,
1096 GRN_BULK_HEAD(&dest),
1097 (uint32_t)GRN_BULK_VSIZE(&dest),
1098 op,
1099 (grn_obj *)pres,
1100 GRN_OP_OR);
1101 if (rc == GRN_SUCCESS) {
1102 grn_posting_internal posting = {0};
1103 posting.sid = 1;
1104 posting.pos = 0;
1105 posting.weight_float = 1;
1106 GRN_HASH_EACH_BEGIN(ctx, pres, cursor, id)
1107 {
1108 void *key;
1109 grn_hash_cursor_get_key(ctx, cursor, &key);
1110 posting.rid = *((grn_id *)key);
1111 grn_ii_posting_add_float(ctx,
1112 (grn_posting *)(&posting),
1113 (grn_hash *)result_set,
1114 logical_op);
1115 }
1116 GRN_HASH_EACH_END(ctx, cursor);
1117 grn_hash_close(ctx, pres);
1118 }
1119 }
1120 }
1121 grn_ii_resolve_sel_and(ctx, (grn_hash *)result_set, logical_op);
1122 }
1123 GRN_OBJ_FIN(ctx, &dest);
1124 } else {
1125 if (op == GRN_OP_PREFIX) {
1126 return select_index_fix_column_prefix(ctx,
1127 table_selector,
1128 index,
1129 op,
1130 result_set,
1131 logical_op);
1132 }
1133 grn_obj *lexicon = grn_ctx_at(ctx, index->header.domain);
1134 if (lexicon) {
1135 grn_hash *keys;
1136 if ((keys = grn_hash_create(ctx,
1137 NULL,
1138 sizeof(grn_id),
1139 0,
1140 GRN_OBJ_TABLE_HASH_KEY))) {
1141 select_index_report(ctx, "[suffix]", index);
1142 grn_table_search(ctx,
1143 lexicon,
1144 GRN_BULK_HEAD(si->query),
1145 (uint32_t)GRN_BULK_VSIZE(si->query),
1146 op,
1147 (grn_obj *)keys,
1148 GRN_OP_OR);
1149 GRN_HASH_EACH_BEGIN(ctx, keys, cursor, id)
1150 {
1151 void *key;
1152 grn_hash_cursor_get_key(ctx, cursor, &key);
1153 grn_id id = *((grn_id *)key);
1154 grn_ii_at(ctx,
1155 (grn_ii *)index,
1156 id,
1157 (grn_hash *)result_set,
1158 logical_op);
1159 }
1160 GRN_HASH_EACH_END(ctx, cursor);
1161 grn_hash_close(ctx, keys);
1162 }
1163 grn_obj_unref(ctx, lexicon);
1164 }
1165 rc = GRN_SUCCESS;
1166 }
1167 return rc;
1168 }
1169
1170 static inline grn_rc
1171 select_index_match(grn_ctx *ctx,
1172 grn_table_selector *table_selector,
1173 grn_obj *index,
1174 grn_operator op,
1175 grn_obj *result_set,
1176 grn_operator logical_op)
1177 {
1178 grn_table_selector_data *data = &(table_selector->data);
1179 scan_info *si = data->scan_info;
1180 grn_search_optarg *options = &(data->search_options);
1181 return grn_obj_search(ctx, index, si->query, result_set, logical_op, options);
1182 }
1183
1184 static inline grn_rc
1185 select_index_extract(grn_ctx *ctx,
1186 grn_table_selector *table_selector,
1187 grn_obj *index,
1188 grn_operator op,
1189 grn_obj *result_set,
1190 grn_operator logical_op)
1191 {
1192 if (!grn_obj_is_accessor(ctx, index)) {
1193 return GRN_FUNCTION_NOT_IMPLEMENTED;
1194 }
1195
1196 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1197 scan_info *si = table_selector->data.scan_info;
1198 grn_accessor *a = (grn_accessor *)index;
1199 switch (a->action) {
1200 case GRN_ACCESSOR_GET_KEY:
1201 select_index_report(ctx, "[term-extract][accessor][key]", a->obj);
1202 grn_table_search(ctx,
1203 a->obj,
1204 GRN_TEXT_VALUE(si->query),
1205 (uint32_t)GRN_TEXT_LEN(si->query),
1206 GRN_OP_TERM_EXTRACT,
1207 result_set,
1208 logical_op);
1209 rc = GRN_SUCCESS;
1210 break;
1211 }
1212
1213 return rc;
1214 }
1215
1216 static inline grn_rc
1217 select_index_call(grn_ctx *ctx,
1218 grn_table_selector *table_selector,
1219 grn_obj *index,
1220 grn_operator op,
1221 grn_obj *result_set,
1222 grn_operator logical_op)
1223 {
1224 grn_table_selector_data *data = &(table_selector->data);
1225 scan_info *si = data->scan_info;
1226 grn_obj *selector = si->args[0];
1227
1228 if (!grn_obj_is_selector_proc(ctx, selector)) {
1229 return GRN_FUNCTION_NOT_IMPLEMENTED;
1230 }
1231
1232 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1233
1234 grn_operator selector_op = grn_proc_get_selector_operator(ctx, selector);
1235
1236 grn_index_datum index_datum;
1237 unsigned int n_index_datum;
1238 if (grn_obj_is_index_column(ctx, index)) {
1239 index_datum.index = index;
1240 n_index_datum = 1;
1241 } else if (grn_obj_is_key_accessor(ctx, index)) {
1242 index_datum.index = ((grn_accessor *)index)->obj;
1243 n_index_datum = 1;
1244 } else if (grn_obj_is_accessor(ctx, index)) {
1245 n_index_datum = grn_column_find_index_data(ctx,
1246 ((grn_accessor *)index)->obj,
1247 selector_op,
1248 &index_datum,
1249 1);
1250 } else {
1251 n_index_datum =
1252 grn_column_find_index_data(ctx, index, selector_op, &index_datum, 1);
1253 }
1254 if (n_index_datum > 0) {
1255 if (grn_logger_pass(ctx, GRN_REPORT_INDEX_LOG_LEVEL)) {
1256 char name[GRN_TABLE_MAX_KEY_SIZE];
1257 int name_size;
1258 char tag[GRN_TABLE_MAX_KEY_SIZE];
1259 name_size = grn_obj_name(ctx, selector, name, GRN_TABLE_MAX_KEY_SIZE);
1260 grn_snprintf(tag,
1261 GRN_TABLE_MAX_KEY_SIZE,
1262 GRN_TABLE_MAX_KEY_SIZE,
1263 "[selector][%.*s]",
1264 name_size,
1265 name);
1266 select_index_report(ctx, tag, index_datum.index);
1267 }
1268
1269 grn_obj *table;
1270 bool table_is_referred = false;
1271 if (grn_obj_is_key_accessor(ctx, index)) {
1272 table = index_datum.index;
1273 } else {
1274 grn_id range = grn_obj_get_range(ctx, index_datum.index);
1275 table = grn_ctx_at(ctx, range);
1276 table_is_referred = true;
1277 }
1278 grn_selector_data selector_data;
1279 grn_selector_data_init(ctx,
1280 &selector_data,
1281 selector,
1282 data->expr,
1283 table,
1284 index_datum.index,
1285 (size_t)(si->nargs),
1286 si->args,
1287 si->weight_factor,
1288 result_set,
1289 logical_op);
1290 bool can_swap_result_set = table_selector->ensure_using_select_result;
1291 if (can_swap_result_set) {
1292 grn_selector_data_set_can_swap_result_set(ctx, &selector_data, true);
1293 }
1294 rc = grn_selector_run(ctx, &selector_data);
1295 if (rc == GRN_SUCCESS && can_swap_result_set &&
1296 result_set != selector_data.result_set) {
1297 grn_obj_unref(ctx, result_set);
1298 table_selector->data.result_set = selector_data.result_set;
1299 }
1300 grn_selector_data_fin(ctx, &selector_data);
1301 if (table_is_referred) {
1302 grn_obj_unref(ctx, table);
1303 }
1304 }
1305
1306 return rc;
1307 }
1308
1309 static inline grn_rc
1310 select_index_range_id(grn_ctx *ctx,
1311 grn_table_selector *table_selector,
1312 grn_obj *table,
1313 grn_operator op,
1314 grn_obj *result_set,
1315 grn_operator logical_op)
1316 {
1317 const char *tag = "[range][id]";
1318
1319 if (select_index_use_sequential_search(ctx,
1320 table_selector,
1321 result_set,
1322 logical_op,
1323 tag,
1324 table)) {
1325 return GRN_FUNCTION_NOT_IMPLEMENTED;
1326 }
1327
1328 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1329 scan_info *si = table_selector->data.scan_info;
1330 grn_obj id;
1331 GRN_UINT32_INIT(&id, 0);
1332 if (grn_obj_cast(ctx, si->query, &id, false) == GRN_SUCCESS) {
1333 grn_table_cursor *cursor;
1334 grn_id min = GRN_ID_NIL;
1335 grn_id max = GRN_ID_MAX;
1336 int offset = 0;
1337 int limit = -1;
1338 int flags = GRN_CURSOR_BY_ID | GRN_CURSOR_ASCENDING;
1339
1340 select_index_report(ctx, tag, table);
1341
1342 switch (op) {
1343 case GRN_OP_LESS:
1344 max = GRN_UINT32_VALUE(&id);
1345 if (max > GRN_ID_NIL) {
1346 max--;
1347 }
1348 break;
1349 case GRN_OP_GREATER:
1350 min = GRN_UINT32_VALUE(&id) + 1;
1351 break;
1352 case GRN_OP_LESS_EQUAL:
1353 max = GRN_UINT32_VALUE(&id);
1354 break;
1355 case GRN_OP_GREATER_EQUAL:
1356 min = GRN_UINT32_VALUE(&id);
1357 break;
1358 default:
1359 break;
1360 }
1361 cursor =
1362 grn_table_cursor_open(ctx, table, NULL, 0, NULL, 0, offset, limit, flags);
1363 if (cursor) {
1364 uint32_t sid = GRN_UINT32_VALUE_AT(&(si->sections), 0);
1365 float weight = GRN_FLOAT32_VALUE_AT(&(si->weights), 0);
1366 weight *= table_selector->weight_factor * si->weight_factor;
1367
1368 if (sid == 0) {
1369 grn_posting_internal posting = {0};
1370
1371 posting.weight_float = weight;
1372 posting.scale = 1.0;
1373 while ((posting.rid = grn_table_cursor_next(ctx, cursor))) {
1374 if (posting.rid < min) {
1375 continue;
1376 }
1377 if (posting.rid > max) {
1378 break;
1379 }
1380 grn_ii_posting_add_float(ctx,
1381 (grn_posting *)(&posting),
1382 (grn_hash *)result_set,
1383 logical_op);
1384 }
1385 }
1386 rc = GRN_SUCCESS;
1387 grn_table_cursor_close(ctx, cursor);
1388 }
1389
1390 grn_ii_resolve_sel_and(ctx, (grn_hash *)result_set, logical_op);
1391 }
1392 GRN_OBJ_FIN(ctx, &id);
1393
1394 return rc;
1395 }
1396
1397 static inline grn_rc
1398 select_index_range_key(grn_ctx *ctx,
1399 grn_table_selector *table_selector,
1400 grn_obj *table,
1401 grn_operator op,
1402 grn_obj *result_set,
1403 grn_operator logical_op)
1404 {
1405 const char *tag = "[range][key]";
1406
1407 if (select_index_use_sequential_search(ctx,
1408 table_selector,
1409 result_set,
1410 logical_op,
1411 tag,
1412 table)) {
1413 return GRN_FUNCTION_NOT_IMPLEMENTED;
1414 }
1415
1416 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1417 scan_info *si = table_selector->data.scan_info;
1418 grn_obj key;
1419 GRN_OBJ_INIT(&key, GRN_BULK, 0, table->header.domain);
1420 if (grn_obj_cast(ctx, si->query, &key, false) == GRN_SUCCESS) {
1421 grn_table_cursor *cursor;
1422 const void *min = NULL, *max = NULL;
1423 unsigned int min_size = 0, max_size = 0;
1424 int offset = 0;
1425 int limit = -1;
1426 int flags = GRN_CURSOR_ASCENDING;
1427
1428 select_index_report(ctx, tag, table);
1429
1430 switch (op) {
1431 case GRN_OP_LESS:
1432 flags |= GRN_CURSOR_LT;
1433 max = GRN_BULK_HEAD(&key);
1434 max_size = (unsigned int)GRN_BULK_VSIZE(&key);
1435 break;
1436 case GRN_OP_GREATER:
1437 flags |= GRN_CURSOR_GT;
1438 min = GRN_BULK_HEAD(&key);
1439 min_size = (unsigned int)GRN_BULK_VSIZE(&key);
1440 break;
1441 case GRN_OP_LESS_EQUAL:
1442 flags |= GRN_CURSOR_LE;
1443 max = GRN_BULK_HEAD(&key);
1444 max_size = (unsigned int)GRN_BULK_VSIZE(&key);
1445 break;
1446 case GRN_OP_GREATER_EQUAL:
1447 flags |= GRN_CURSOR_GE;
1448 min = GRN_BULK_HEAD(&key);
1449 min_size = (unsigned int)GRN_BULK_VSIZE(&key);
1450 break;
1451 default:
1452 break;
1453 }
1454 cursor = grn_table_cursor_open(ctx,
1455 table,
1456 min,
1457 min_size,
1458 max,
1459 max_size,
1460 offset,
1461 limit,
1462 flags);
1463 if (cursor) {
1464 uint32_t sid = GRN_UINT32_VALUE_AT(&(si->sections), 0);
1465 float weight = GRN_FLOAT32_VALUE_AT(&(si->weights), 0);
1466 weight *= table_selector->weight_factor * si->weight_factor;
1467
1468 if (sid == 0) {
1469 rc = grn_result_set_add_table_cursor(ctx,
1470 (grn_hash *)result_set,
1471 cursor,
1472 weight,
1473 logical_op);
1474 } else {
1475 rc = GRN_SUCCESS;
1476 }
1477 rc = GRN_SUCCESS;
1478 grn_table_cursor_close(ctx, cursor);
1479 }
1480
1481 grn_ii_resolve_sel_and(ctx, (grn_hash *)result_set, logical_op);
1482 }
1483 GRN_OBJ_FIN(ctx, &key);
1484
1485 return rc;
1486 }
1487
1488 static inline grn_rc
1489 select_index_range_column(grn_ctx *ctx,
1490 grn_table_selector *table_selector,
1491 grn_obj *index,
1492 grn_operator op,
1493 grn_obj *result_set,
1494 grn_operator logical_op)
1495 {
1496 const char *tag = "[range]";
1497
1498 grn_obj *lexicon = grn_ctx_at(ctx, index->header.domain);
1499 if (!lexicon) {
1500 return GRN_FUNCTION_NOT_IMPLEMENTED;
1501 }
1502
1503 if (select_index_use_sequential_search(ctx,
1504 table_selector,
1505 result_set,
1506 logical_op,
1507 tag,
1508 lexicon)) {
1509 grn_obj_unref(ctx, lexicon);
1510 return GRN_FUNCTION_NOT_IMPLEMENTED;
1511 }
1512
1513 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1514 scan_info *si = table_selector->data.scan_info;
1515 grn_obj range;
1516 GRN_OBJ_INIT(&range, GRN_BULK, 0, lexicon->header.domain);
1517 if (grn_obj_cast(ctx, si->query, &range, false) == GRN_SUCCESS) {
1518 grn_table_cursor *cursor;
1519 const void *min = NULL, *max = NULL;
1520 unsigned int min_size = 0, max_size = 0;
1521 int offset = 0;
1522 int limit = -1;
1523 int flags = GRN_CURSOR_ASCENDING;
1524
1525 select_index_report(ctx, "[range]", index);
1526
1527 switch (op) {
1528 case GRN_OP_LESS:
1529 flags |= GRN_CURSOR_LT;
1530 max = GRN_BULK_HEAD(&range);
1531 max_size = (unsigned int)GRN_BULK_VSIZE(&range);
1532 break;
1533 case GRN_OP_GREATER:
1534 flags |= GRN_CURSOR_GT;
1535 min = GRN_BULK_HEAD(&range);
1536 min_size = (unsigned int)GRN_BULK_VSIZE(&range);
1537 break;
1538 case GRN_OP_LESS_EQUAL:
1539 flags |= GRN_CURSOR_LE;
1540 max = GRN_BULK_HEAD(&range);
1541 max_size = (unsigned int)GRN_BULK_VSIZE(&range);
1542 break;
1543 case GRN_OP_GREATER_EQUAL:
1544 flags |= GRN_CURSOR_GE;
1545 min = GRN_BULK_HEAD(&range);
1546 min_size = (unsigned int)GRN_BULK_VSIZE(&range);
1547 break;
1548 default:
1549 break;
1550 }
1551 cursor = grn_table_cursor_open(ctx,
1552 lexicon,
1553 min,
1554 min_size,
1555 max,
1556 max_size,
1557 offset,
1558 limit,
1559 flags);
1560 if (cursor) {
1561 grn_obj *index_cursor =
1562 grn_index_cursor_open(ctx, cursor, index, GRN_ID_NIL, GRN_ID_MAX, 0);
1563 if (index_cursor) {
1564 grn_search_optarg *options = &(table_selector->data.search_options);
1565 if (options->weight_vector_float) {
1566 grn_index_cursor_set_scales(ctx,
1567 index_cursor,
1568 options->weight_vector_float,
1569 (size_t)(options->vector_size));
1570 } else {
1571 grn_index_cursor_set_scale(ctx, index_cursor, options->weight_float);
1572 }
1573 if (options->start_position) {
1574 grn_index_cursor_set_start_position(ctx,
1575 index_cursor,
1576 *(options->start_position));
1577 }
1578 rc = grn_result_set_add_index_cursor(ctx,
1579 (grn_hash *)result_set,
1580 index_cursor,
1581 1,
1582 1.0,
1583 logical_op);
1584 grn_obj_close(ctx, index_cursor);
1585 }
1586 grn_table_cursor_close(ctx, cursor);
1587 }
1588
1589 grn_ii_resolve_sel_and(ctx, (grn_hash *)result_set, logical_op);
1590 }
1591 GRN_OBJ_FIN(ctx, &range);
1592 grn_obj_unref(ctx, lexicon);
1593
1594 return rc;
1595 }
1596
1597 static inline grn_rc
1598 select_index_range(grn_ctx *ctx,
1599 grn_table_selector *table_selector,
1600 grn_obj *index,
1601 grn_operator op,
1602 grn_obj *result_set,
1603 grn_operator logical_op)
1604 {
1605 grn_obj *target = index;
1606
1607 if (grn_obj_is_accessor(ctx, index)) {
1608 target = ((grn_accessor *)index)->obj;
1609 if (grn_obj_is_id_accessor(ctx, index)) {
1610 return select_index_range_id(ctx,
1611 table_selector,
1612 target,
1613 op,
1614 result_set,
1615 logical_op);
1616 }
1617 }
1618
1619 switch (target->header.type) {
1620 case GRN_TABLE_PAT_KEY:
1621 case GRN_TABLE_DAT_KEY:
1622 /* target == table */
1623 return select_index_range_key(ctx,
1624 table_selector,
1625 target,
1626 op,
1627 result_set,
1628 logical_op);
1629 default:
1630 return select_index_range_column(ctx,
1631 table_selector,
1632 target,
1633 op,
1634 result_set,
1635 logical_op);
1636 }
1637 }
1638
1639 static grn_rc
1640 select_index_dispatch(grn_ctx *ctx,
1641 grn_obj *index,
1642 grn_operator op,
1643 grn_obj *result_set,
1644 grn_operator logical_op,
1645 void *user_data)
1646 {
1647 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1648 grn_table_selector *table_selector = user_data;
1649
1650 switch (op) {
1651 case GRN_OP_EQUAL:
1652 rc = select_index_equal(ctx,
1653 table_selector,
1654 index,
1655 op,
1656 result_set,
1657 logical_op);
1658 break;
1659 case GRN_OP_NOT_EQUAL:
1660 rc = select_index_not_equal(ctx,
1661 table_selector,
1662 index,
1663 op,
1664 result_set,
1665 logical_op);
1666 break;
1667 case GRN_OP_MATCH:
1668 case GRN_OP_NEAR:
1669 case GRN_OP_NEAR_NO_OFFSET:
1670 case GRN_OP_NEAR_PHRASE:
1671 case GRN_OP_ORDERED_NEAR_PHRASE:
1672 case GRN_OP_NEAR_PHRASE_PRODUCT:
1673 case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT:
1674 case GRN_OP_SIMILAR:
1675 case GRN_OP_REGEXP:
1676 case GRN_OP_QUORUM:
1677 rc = select_index_match(ctx,
1678 table_selector,
1679 index,
1680 op,
1681 result_set,
1682 logical_op);
1683 break;
1684 case GRN_OP_PREFIX:
1685 case GRN_OP_SUFFIX:
1686 rc =
1687 select_index_fix(ctx, table_selector, index, op, result_set, logical_op);
1688 break;
1689 case GRN_OP_TERM_EXTRACT:
1690 rc = select_index_extract(ctx,
1691 table_selector,
1692 index,
1693 op,
1694 result_set,
1695 logical_op);
1696 break;
1697 case GRN_OP_CALL:
1698 rc =
1699 select_index_call(ctx, table_selector, index, op, result_set, logical_op);
1700 break;
1701 case GRN_OP_LESS:
1702 case GRN_OP_GREATER:
1703 case GRN_OP_LESS_EQUAL:
1704 case GRN_OP_GREATER_EQUAL:
1705 rc = select_index_range(ctx,
1706 table_selector,
1707 index,
1708 op,
1709 result_set,
1710 logical_op);
1711 break;
1712 default:
1713 /* todo : implement */
1714 /* todo : handle SCAN_PRE_CONST */
1715 break;
1716 }
1717
1718 return rc;
1719 }
1720
1721 static inline bool
1722 select_index(grn_ctx *ctx, grn_table_selector *table_selector)
1723 {
1724 grn_table_selector_data *data = &(table_selector->data);
1725 scan_info *si = data->scan_info;
1726
1727 if (!si->query) {
1728 if (si->op != GRN_OP_CALL || !grn_obj_is_selector_proc(ctx, si->args[0])) {
1729 return false;
1730 }
1731 }
1732
1733 grn_rc rc = GRN_FUNCTION_NOT_IMPLEMENTED;
1734 size_t n_indexes = GRN_PTR_VECTOR_SIZE(&(si->index));
1735 if (n_indexes > 0) {
1736 unsigned int previous_n_hits = grn_table_size(ctx, data->result_set);
1737
1738 grn_id minimum_min_id = GRN_ID_NIL;
1739 bool minimum_min_id_is_set = false;
1740
1741 grn_obj weights_buffer;
1742 GRN_FLOAT32_INIT(&weights_buffer, GRN_OBJ_VECTOR);
1743
1744 int original_flags = ctx->flags;
1745 ctx->flags |= GRN_CTX_TEMPORARY_DISABLE_II_RESOLVE_SEL_AND;
1746 size_t i;
1747 for (i = 0; i < n_indexes; i++) {
1748 grn_obj *index = GRN_PTR_VALUE_AT(&(si->index), i);
1749
1750 grn_search_optarg *options = &(data->search_options);
1751 memset(options, 0, sizeof(*options));
1752 if (si->op == GRN_OP_MATCH) {
1753 options->mode = GRN_OP_EXACT;
1754 } else {
1755 options->mode = si->op;
1756 }
1757 options->match_info.flags = GRN_MATCH_INFO_GET_MIN_RECORD_ID;
1758 options->max_interval = si->max_interval;
1759 options->additional_last_interval = si->additional_last_interval;
1760 if (GRN_INT32_VECTOR_SIZE(&(si->max_element_intervals)) > 0) {
1761 options->max_element_intervals = &(si->max_element_intervals);
1762 }
1763 options->min_interval = &(si->min_interval);
1764 options->similarity_threshold = si->similarity_threshold;
1765 options->quorum_threshold = si->quorum_threshold;
1766 options->query_options = table_selector->query_options;
1767 options->fuzzy = table_selector->fuzzy_options;
1768 uint32_t section = GRN_UINT32_VALUE_AT(&(si->sections), i);
1769 float weight = GRN_FLOAT32_VALUE_AT(&(si->weights), i);
1770 weight *= table_selector->weight_factor * si->weight_factor;
1771 if (section == 0) {
1772 options->weight_vector = NULL;
1773 options->weight_vector_float = NULL;
1774 options->vector_size = -1;
1775 options->weight_float = weight;
1776 } else {
1777 uint32_t weight_index = section - 1;
1778 size_t current_vector_size;
1779 current_vector_size = GRN_FLOAT32_VECTOR_SIZE(&weights_buffer);
1780 if (weight_index < current_vector_size) {
1781 GRN_FLOAT32_VALUE_AT(&weights_buffer, weight_index) = weight;
1782 } else {
1783 GRN_FLOAT32_SET_AT(ctx, &weights_buffer, weight_index, weight);
1784 }
1785 options->weight_vector = NULL;
1786 options->weight_vector_float = &GRN_FLOAT32_VALUE(&weights_buffer);
1787 options->vector_size = (int)GRN_FLOAT32_VECTOR_SIZE(&weights_buffer);
1788 options->weight_float = 0.0;
1789 }
1790 options->scorer = GRN_PTR_VALUE_AT(&(si->scorers), i);
1791 options->scorer_args_expr = GRN_PTR_VALUE_AT(&(si->scorer_args_exprs), i);
1792 options->scorer_args_expr_offset =
1793 GRN_UINT32_VALUE_AT(&(si->scorer_args_expr_offsets), i);
1794
1795 bool use_min_id_skip_enable;
1796 use_min_id_skip_enable =
1797 (grn_table_select_min_id_skip_enable && !GRN_ACCESSORP(index));
1798 if (use_min_id_skip_enable) {
1799 options->match_info.min = data->min_id;
1800 } else {
1801 options->match_info.min = GRN_ID_NIL;
1802 }
1803 int32_t start_position = GRN_INT32_VALUE_AT(&(si->start_positions), i);
1804 if (start_position >= 0) {
1805 options->start_position = &start_position;
1806 } else {
1807 options->start_position = NULL;
1808 }
1809 if (n_indexes == 1 && grn_obj_is_accessor(ctx, index)) {
1810 rc = grn_accessor_execute(ctx,
1811 index,
1812 select_index_dispatch,
1813 table_selector,
1814 si->op,
1815 data->result_set,
1816 si->logical_op);
1817 } else {
1818 if (i < n_indexes - 1) {
1819 if (section > 0 && index == GRN_PTR_VALUE_AT(&(si->index), i + 1) &&
1820 !options->scorer && !GRN_PTR_VALUE_AT(&(si->scorers), i + 1) &&
1821 start_position < 0 &&
1822 GRN_INT32_VALUE_AT(&(si->start_positions), i + 1) < 0) {
1823 continue;
1824 }
1825 }
1826 rc = select_index_dispatch(ctx,
1827 index,
1828 si->op,
1829 data->result_set,
1830 si->logical_op,
1831 table_selector);
1832 }
1833 rc = grn_ctx_rc_propagate(ctx, rc);
1834 if (rc != GRN_SUCCESS) {
1835 break;
1836 }
1837 if (options->weight_vector_float) {
1838 int i;
1839 for (i = 0; i < options->vector_size; i++) {
1840 options->weight_vector_float[i] = 0;
1841 }
1842 }
1843 GRN_BULK_REWIND(&weights_buffer);
1844 if (use_min_id_skip_enable &&
1845 (!minimum_min_id_is_set ||
1846 options->match_info.min < minimum_min_id)) {
1847 minimum_min_id_is_set = true;
1848 minimum_min_id = options->match_info.min;
1849 }
1850 if (options->match_info.flags & GRN_MATCH_INFO_ONLY_SKIP_TOKEN) {
1851 data->is_skipped = true;
1852 }
1853 }
1854 ctx->flags = original_flags;
1855
1856 GRN_OBJ_FIN(ctx, &weights_buffer);
1857
1858 if (rc == GRN_SUCCESS) {
1859 bool need_resolve_sel_and = !data->is_skipped;
1860 if (si->op == GRN_OP_NOT_EQUAL) {
1861 need_resolve_sel_and = false;
1862 }
1863 if (need_resolve_sel_and) {
1864 grn_ii_resolve_sel_and(ctx,
1865 (grn_hash *)(data->result_set),
1866 si->logical_op);
1867 }
1868 if ((si->logical_op == GRN_OP_AND) ||
1869 (si->logical_op == GRN_OP_OR && previous_n_hits == 0)) {
1870 data->min_id = minimum_min_id;
1871 } else {
1872 data->min_id = table_selector->min_id;
1873 }
1874 }
1875 } else {
1876 if (si->op == GRN_OP_CALL && grn_obj_is_selector_proc(ctx, si->args[0])) {
1877 grn_obj *selector = si->args[0];
1878 grn_proc *proc = (grn_proc *)(selector);
1879 grn_operator selector_op = grn_proc_get_selector_operator(ctx, selector);
1880 bool callable = false;
1881 if (selector_op == GRN_OP_NOP) {
1882 callable = true;
1883 } else if (si->nargs > 1) {
1884 grn_obj *first_arg = si->args[1];
1885 if (grn_obj_is_index_column(ctx, first_arg)) {
1886 callable = grn_index_column_is_usable(ctx, first_arg, selector_op);
1887 }
1888 }
1889 if (callable) {
1890 if (grn_logger_pass(ctx, GRN_REPORT_INDEX_LOG_LEVEL)) {
1891 char proc_name[GRN_TABLE_MAX_KEY_SIZE];
1892 int proc_name_size;
1893 char tag[GRN_TABLE_MAX_KEY_SIZE];
1894 proc_name_size = grn_obj_name(ctx,
1895 (grn_obj *)proc,
1896 proc_name,
1897 GRN_TABLE_MAX_KEY_SIZE);
1898 proc_name[proc_name_size] = '\0';
1899 grn_snprintf(tag,
1900 GRN_TABLE_MAX_KEY_SIZE,
1901 GRN_TABLE_MAX_KEY_SIZE,
1902 "[selector][no-index][%s]",
1903 proc_name);
1904 select_index_report(ctx, tag, table_selector->table);
1905 }
1906 grn_selector_data selector_data;
1907 grn_selector_data_init(ctx,
1908 &selector_data,
1909 selector,
1910 data->expr,
1911 table_selector->table,
1912 NULL,
1913 (size_t)(si->nargs),
1914 si->args,
1915 si->weight_factor,
1916 data->result_set,
1917 si->logical_op);
1918 bool can_swap_result_set = table_selector->ensure_using_select_result;
1919 if (can_swap_result_set) {
1920 grn_selector_data_set_can_swap_result_set(ctx, &selector_data, true);
1921 }
1922 rc = grn_selector_run(ctx, &selector_data);
1923 if (rc == GRN_SUCCESS && can_swap_result_set &&
1924 data->result_set != selector_data.result_set) {
1925 grn_obj *result_set = data->result_set;
1926 data->result_set = selector_data.result_set;
1927 if (result_set != data->initial_result_set) {
1928 grn_obj_close(ctx, result_set);
1929 }
1930 }
1931 grn_selector_data_fin(ctx, &selector_data);
1932 }
1933 }
1934 }
1935
1936 rc = grn_ctx_rc_propagate(ctx, rc);
1937 switch (rc) {
1938 case GRN_SUCCESS:
1939 return true;
1940 break;
1941 case GRN_FUNCTION_NOT_IMPLEMENTED:
1942 ERRCLR(ctx);
1943 return false;
1944 default:
1945 /* TODO: report error */
1946 return false;
1947 }
1948 }
1949
1950 static void
1951 inspect_condition_argument(grn_ctx *ctx, grn_obj *buffer, grn_obj *argument)
1952 {
1953 switch (argument->header.type) {
1954 case GRN_BULK:
1955 if (grn_type_id_is_builtin(ctx, argument->header.domain)) {
1956 grn_inspect(ctx, buffer, argument);
1957 } else {
1958 grn_record_inspect_without_columns(ctx, buffer, argument);
1959 }
1960 break;
1961 case GRN_UVECTOR:
1962 if (grn_type_id_is_builtin(ctx, argument->header.domain)) {
1963 grn_inspect(ctx, buffer, argument);
1964 } else {
1965 grn_uvector_record_inspect_without_columns(ctx, buffer, argument);
1966 }
1967 break;
1968 case GRN_TABLE_HASH_KEY:
1969 case GRN_TABLE_PAT_KEY:
1970 case GRN_TABLE_NO_KEY:
1971 case GRN_COLUMN_FIX_SIZE:
1972 case GRN_COLUMN_VAR_SIZE:
1973 case GRN_COLUMN_INDEX:
1974 grn_inspect_name(ctx, buffer, argument);
1975 break;
1976 default:
1977 grn_inspect(ctx, buffer, argument);
1978 break;
1979 }
1980 }
1981
1982 static void
1983 inspect_condition_call(grn_ctx *ctx,
1984 grn_obj *buffer,
1985 grn_expr_code *codes,
1986 uint32_t start,
1987 uint32_t end)
1988 {
1989 uint32_t i;
1990
1991 for (i = start; i <= end; i++) {
1992 grn_expr_code *code = codes + i;
1993 if (i == start) {
1994 if (grn_obj_is_proc(ctx, code->value)) {
1995 grn_inspect_name(ctx, buffer, code->value);
1996 } else {
1997 inspect_condition_argument(ctx, buffer, code->value);
1998 }
1999 GRN_TEXT_PUTC(ctx, buffer, '(');
2000 } else if (code->value) {
2001 if (i > start + 1) {
2002 GRN_TEXT_PUTS(ctx, buffer, ", ");
2003 }
2004 inspect_condition_argument(ctx, buffer, code->value);
2005 }
2006 }
2007 GRN_TEXT_PUTC(ctx, buffer, ')');
2008 }
2009
2010 static const char *
2011 inspect_condition(grn_ctx *ctx, grn_obj *buffer, scan_info *si, grn_expr *expr)
2012 {
2013 uint32_t i;
2014 uint32_t n_codes;
2015 grn_operator last_operator;
2016
2017 if (!grn_query_log_show_condition) {
2018 return "";
2019 }
2020
2021 GRN_BULK_REWIND(buffer);
2022
2023 GRN_TEXT_PUTS(ctx, buffer, ": ");
2024
2025 if (si->flags & SCAN_POP) {
2026 GRN_TEXT_PUTS(ctx, buffer, grn_operator_to_string(si->logical_op));
2027 GRN_TEXT_PUTC(ctx, buffer, '\0');
2028 return GRN_TEXT_VALUE(buffer);
2029 }
2030
2031 n_codes = si->end - si->start + 1;
2032 last_operator = expr->codes[si->end].op;
2033
2034 switch (last_operator) {
2035 case GRN_OP_CALL:
2036 inspect_condition_call(ctx, buffer, expr->codes, si->start, si->end);
2037 break;
2038 case GRN_OP_EQUAL:
2039 case GRN_OP_NOT_EQUAL:
2040 case GRN_OP_LESS:
2041 case GRN_OP_GREATER:
2042 case GRN_OP_LESS_EQUAL:
2043 case GRN_OP_GREATER_EQUAL:
2044 case GRN_OP_MATCH:
2045 case GRN_OP_NEAR:
2046 case GRN_OP_NEAR_NO_OFFSET:
2047 case GRN_OP_NEAR_PHRASE:
2048 case GRN_OP_ORDERED_NEAR_PHRASE:
2049 case GRN_OP_NEAR_PHRASE_PRODUCT:
2050 case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT:
2051 case GRN_OP_PREFIX:
2052 if (n_codes == 3) {
2053 grn_expr_code *arg1 = expr->codes + si->start;
2054 grn_expr_code *arg2 = expr->codes + si->start + 1;
2055
2056 if (arg1->value->header.type == GRN_EXPR) {
2057 GRN_TEXT_PUTS(ctx, buffer, "(match columns)");
2058 } else {
2059 inspect_condition_argument(ctx, buffer, arg1->value);
2060 }
2061 GRN_TEXT_PUTC(ctx, buffer, ' ');
2062 GRN_TEXT_PUTS(ctx, buffer, grn_operator_to_string(last_operator));
2063 GRN_TEXT_PUTC(ctx, buffer, ' ');
2064 inspect_condition_argument(ctx, buffer, arg2->value);
2065 } else {
2066 GRN_TEXT_PUTS(ctx, buffer, grn_operator_to_string(last_operator));
2067 GRN_TEXT_PUTC(ctx, buffer, '(');
2068 for (i = si->start; i < si->end; i++) {
2069 grn_expr_code *code = expr->codes + i;
2070 if (i != si->start) {
2071 GRN_TEXT_PUTS(ctx, buffer, ", ");
2072 }
2073 if (code->modify > 0 && code[code->modify].op == GRN_OP_CALL) {
2074 uint32_t modify = (uint32_t)(code->modify);
2075 inspect_condition_call(ctx, buffer, code, 0, modify);
2076 if (i + modify < si->end &&
2077 code[code->modify + 1].op == GRN_OP_PUSH) {
2078 i += modify;
2079 } else {
2080 i += modify - 1;
2081 }
2082 } else {
2083 if (code->value) {
2084 if (i == si->start && code->value->header.type == GRN_EXPR) {
2085 GRN_TEXT_PUTS(ctx, buffer, "(match columns)");
2086 } else {
2087 inspect_condition_argument(ctx, buffer, code->value);
2088 }
2089 } else {
2090 GRN_TEXT_PUTS(ctx, buffer, grn_operator_to_string(code->op));
2091 }
2092 }
2093 }
2094 GRN_TEXT_PUTC(ctx, buffer, ')');
2095 }
2096 break;
2097 default:
2098 for (i = si->start; i <= si->end; i++) {
2099 grn_expr_code *code = expr->codes + i;
2100 if (i > si->start) {
2101 GRN_TEXT_PUTC(ctx, buffer, ' ');
2102 }
2103 if (code->value) {
2104 inspect_condition_argument(ctx, buffer, code->value);
2105 } else {
2106 GRN_TEXT_PUTS(ctx, buffer, grn_operator_to_string(code->op));
2107 }
2108 }
2109 break;
2110 }
2111
2112 GRN_TEXT_PUTC(ctx, buffer, '\0');
2113 return GRN_TEXT_VALUE(buffer);
2114 }
2115
2116 grn_obj *
2117 grn_table_selector_select(grn_ctx *ctx,
2118 grn_table_selector *table_selector,
2119 grn_obj *result_set)
2120 {
2121 bool result_set_created = false;
2122 GRN_API_ENTER;
2123 if (result_set) {
2124 if (result_set->header.type != GRN_TABLE_HASH_KEY ||
2125 (result_set->header.domain != DB_OBJ(table_selector->table)->id)) {
2126 ERR(GRN_INVALID_ARGUMENT,
2127 "[table-selector][select] result set must be a hash table");
2128 GRN_API_RETURN(NULL);
2129 }
2130 } else {
2131 result_set = grn_table_create(ctx,
2132 NULL,
2133 0,
2134 NULL,
2135 GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
2136 table_selector->table,
2137 NULL);
2138 if (!result_set) {
2139 GRN_API_RETURN(NULL);
2140 }
2141 result_set_created = true;
2142 }
2143
2144 uint32_t result_set_size = GRN_HASH_SIZE((grn_hash *)result_set);
2145 if (table_selector->op != GRN_OP_OR && result_set_size == 0) {
2146 GRN_API_RETURN(result_set);
2147 }
2148
2149 if (table_selector->use_sequential_scan) {
2150 select_sequential_scan(ctx, table_selector, result_set);
2151 if (ctx->rc != GRN_SUCCESS) {
2152 if (result_set_created) {
2153 grn_obj_close(ctx, result_set);
2154 }
2155 result_set = NULL;
2156 }
2157 GRN_API_RETURN(result_set);
2158 }
2159
2160 grn_table_selector_data *data = &(table_selector->data);
2161 if (data->scanner) {
2162 grn_scanner_close(ctx, data->scanner);
2163 }
2164 data->scanner = grn_scanner_open(ctx,
2165 table_selector->expr,
2166 table_selector->op,
2167 result_set_size > 0);
2168 if (!data->scanner) {
2169 if (ctx->rc == GRN_SUCCESS) {
2170 grn_table_selector sequential_table_selector;
2171 grn_table_selector_init(ctx,
2172 &sequential_table_selector,
2173 table_selector->table,
2174 table_selector->expr,
2175 table_selector->op);
2176 grn_table_selector_set_use_sequential_scan(ctx,
2177 &sequential_table_selector,
2178 true);
2179 grn_table_selector_select(ctx, &sequential_table_selector, result_set);
2180 grn_table_selector_fin(ctx, &sequential_table_selector);
2181 if (ctx->rc != GRN_SUCCESS) {
2182 if (result_set_created) {
2183 grn_obj_close(ctx, result_set);
2184 }
2185 result_set = NULL;
2186 }
2187 }
2188 GRN_API_RETURN(result_set);
2189 }
2190
2191 grn_obj result_set_stack;
2192 GRN_PTR_INIT(&result_set_stack, GRN_OBJ_VECTOR, GRN_ID_NIL);
2193
2194 grn_obj *base_result_set = NULL;
2195
2196 data->initial_result_set = result_set;
2197 data->result_set = result_set;
2198 data->min_id = table_selector->min_id;
2199 data->expr = data->scanner->expr;
2200 data->variable = grn_expr_get_var_by_offset(ctx, data->expr, 0);
2201 data->is_skipped = false;
2202 data->is_first_unskipped_scan_info = true;
2203
2204 /* This is an optimization for the following cases:
2205 *
2206 * XXX && sub_filter(..., "YYY || ZZZ")
2207 * XXX && query(..., "YYY OR ZZZ")
2208 *
2209 * In these cases, sub_filter()/query() use
2210 * grn_table_selector_select() with GRN_OP_OR and result_set with
2211 * some records. If we use a copy of result_set as a base result set
2212 * and GRN_OP_AND internally, we can reduce set operation cost.
2213 *
2214 * But if result_set has many records, this optimization will be
2215 * slower than the normal operation. So we don't use this
2216 * optimization if result_set already has many records. 1_000_000 is
2217 * a heuristic value. This may not be a good value.
2218 */
2219 if (result_set_size > 0 && result_set_size < 1000000 &&
2220 table_selector->op == GRN_OP_AND) {
2221 bool have_push = false;
2222 uint32_t i;
2223 for (i = 0; i < data->scanner->n_sis; i++) {
2224 scan_info *si = data->scanner->sis[i];
2225 if (si->flags & SCAN_PUSH) {
2226 have_push = true;
2227 break;
2228 }
2229 }
2230 if (have_push) {
2231 base_result_set = selector_create_result_set(ctx, table_selector);
2232 if (base_result_set) {
2233 grn_result_set_copy(ctx,
2234 (grn_hash *)result_set,
2235 (grn_hash *)base_result_set);
2236 }
2237 }
2238 }
2239
2240 grn_expr *e = (grn_expr *)(data->scanner->expr);
2241 grn_expr_code *codes = e->codes;
2242 uint32_t codes_curr = e->codes_curr;
2243 grn_obj condition_inspect_buffer;
2244 GRN_TEXT_INIT(&condition_inspect_buffer, 0);
2245 {
2246 uint32_t i;
2247 for (i = 0; i < data->scanner->n_sis; i++) {
2248 scan_info *si = data->scanner->sis[i];
2249 data->nth_scan_info = (int)i;
2250 data->scan_info = si;
2251 if (i > 0 && data->is_first_unskipped_scan_info) {
2252 if (data->is_skipped) {
2253 if (si->logical_op == GRN_OP_AND) {
2254 si->logical_op = GRN_OP_OR;
2255 }
2256 } else {
2257 data->is_first_unskipped_scan_info = false;
2258 }
2259 }
2260 data->is_skipped = false;
2261 if (si->flags & SCAN_POP) {
2262 grn_obj *result_set_;
2263 GRN_PTR_POP(&result_set_stack, result_set_);
2264 grn_obj *table1 = result_set_;
2265 grn_obj *table2 = data->result_set;
2266 /* This is an optimization. In general, we do "result_set_ AND
2267 * data->result_set" and update result_set_ in-place. This may
2268 * be a heavy process when result_set_ is very large and
2269 * data->result_set is small. Because we need to remove many
2270 * records from result_set_. If we do "data->result_set AND
2271 * result_set_" and update data->result_set, it's faster than
2272 * before. Because we don't need to remove many records.
2273 *
2274 * We can't do this optimization when result_set_ is the given
2275 * result_set to keep backward compatibility. Callers must use
2276 * grn_table_selector_select()'s return value instead of the
2277 * given result_set as the result but some callers may not do
2278 * it. So this optimization is enabled only when
2279 * grn_table_selector_set_ensure_using_select_result(ctx,
2280 * table_selector, true) is called. (Or result_set_ isn't the
2281 * given result_set.)
2282 *
2283 * This optimization will work for GRN_OP_OR but we target
2284 * only GRN_OP_AND for now. We'll add support for GRN_OP_OR
2285 * when we have an use case. */
2286 if ((table_selector->ensure_using_select_result ||
2287 table1 != result_set) &&
2288 si->logical_op == GRN_OP_AND) {
2289 if (grn_table_size(ctx, table1) > grn_table_size(ctx, table2)) {
2290 grn_obj *tmp = table1;
2291 table1 = table2;
2292 table2 = tmp;
2293 }
2294 }
2295 grn_table_setoperation_with_weight_factor(ctx,
2296 table1,
2297 table2,
2298 table1,
2299 si->logical_op,
2300 si->weight_factor);
2301 if (table2 != result_set) {
2302 grn_obj_close(ctx, table2);
2303 }
2304 data->result_set = table1;
2305 data->min_id = table_selector->min_id;
2306 } else {
2307 bool processed = false;
2308 if (si->flags & SCAN_PUSH) {
2309 grn_obj *new_result_set =
2310 selector_create_result_set(ctx, table_selector);
2311 if (!new_result_set) {
2312 break;
2313 }
2314 if (base_result_set && si->logical_op == GRN_OP_OR) {
2315 GRN_LOG(ctx,
2316 GRN_REPORT_INDEX_LOG_LEVEL,
2317 "[table-selector][select][push][initial] <%u>",
2318 grn_table_size(ctx, base_result_set));
2319 grn_table_setoperation(ctx,
2320 new_result_set,
2321 base_result_set,
2322 new_result_set,
2323 GRN_OP_OR);
2324 si->logical_op = GRN_OP_AND;
2325 }
2326 GRN_PTR_PUT(ctx, &result_set_stack, data->result_set);
2327 data->result_set = new_result_set;
2328 data->min_id = table_selector->min_id;
2329 }
2330 if (si->logical_op != GRN_OP_AND) {
2331 data->min_id = table_selector->min_id;
2332 }
2333 processed = select_index(ctx, table_selector);
2334 if (!processed && ctx->rc == GRN_SUCCESS) {
2335 e->codes = codes + si->start;
2336 e->codes_curr = si->end - si->start + 1;
2337 grn_table_selector sequential_table_selector;
2338 grn_table_selector_init(ctx,
2339 &sequential_table_selector,
2340 table_selector->table,
2341 data->scanner->expr,
2342 si->logical_op);
2343 grn_table_selector_set_use_sequential_scan(ctx,
2344 &sequential_table_selector,
2345 true);
2346 grn_table_selector_select(ctx,
2347 &sequential_table_selector,
2348 data->result_set);
2349 grn_table_selector_fin(ctx, &sequential_table_selector);
2350 e->codes = codes;
2351 e->codes_curr = codes_curr;
2352 data->min_id = table_selector->min_id;
2353 }
2354 }
2355 GRN_QUERY_LOG(
2356 ctx,
2357 GRN_QUERY_LOG_SIZE,
2358 ":",
2359 "%sfilter(%d)%s",
2360 grn_expr_get_query_log_tag_prefix(ctx, table_selector->expr),
2361 grn_table_size(ctx, data->result_set),
2362 inspect_condition(ctx, &condition_inspect_buffer, si, e));
2363 if (ctx->rc != GRN_SUCCESS) {
2364 if (result_set_created) {
2365 grn_obj_close(ctx, data->result_set);
2366 }
2367 data->result_set = NULL;
2368 break;
2369 }
2370 }
2371 }
2372 GRN_OBJ_FIN(ctx, &condition_inspect_buffer);
2373 e->codes = codes;
2374 e->codes_curr = codes_curr;
2375
2376 result_set = data->result_set;
2377
2378 if (base_result_set) {
2379 grn_obj_close(ctx, base_result_set);
2380 }
2381
2382 {
2383 size_t i = 0;
2384 if (!result_set_created) {
2385 i++;
2386 }
2387 size_t n_result_sets = GRN_PTR_VECTOR_SIZE(&result_set_stack);
2388 for (; i < n_result_sets; i++) {
2389 grn_obj *result_set_ = GRN_PTR_VALUE_AT(&result_set_stack, i);
2390 grn_obj_close(ctx, result_set_);
2391 }
2392 }
2393 GRN_OBJ_FIN(ctx, &result_set_stack);
2394
2395 grn_scanner_close(ctx, data->scanner);
2396 data->scanner = NULL;
2397
2398 GRN_API_RETURN(result_set);
2399 }