| 1 | /* |
| 2 | Copyright (C) 2010-2018 Brazil |
| 3 | Copyright (C) 2018-2026 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_tokenizer.h" |
| 22 | |
| 23 | #include "grn_accessor.h" |
| 24 | #include "grn_bulk.h" |
| 25 | #include "grn_db.h" |
| 26 | #include "grn_ctx_impl.h" |
| 27 | #include "grn_ctx_impl_mrb.h" |
| 28 | #include <string.h> |
| 29 | #include "grn_ii.h" |
| 30 | #include "grn_index_cursor.h" |
| 31 | #include "grn_geo.h" |
| 32 | #include "grn_expr.h" |
| 33 | #include "grn_expr_code.h" |
| 34 | #include "grn_expr_executor.h" |
| 35 | #include "grn_scan_info.h" |
| 36 | #include "grn_scanner.h" |
| 37 | #include "grn_util.h" |
| 38 | #include "grn_report.h" |
| 39 | #include "grn_table_selector.h" |
| 40 | #include "grn_token_cursor.h" |
| 41 | #include "grn_posting.h" |
| 42 | #include "grn_selector.h" |
| 43 | #include "grn_mrb.h" |
| 44 | #include "mrb/mrb_expr.h" |
| 45 | |
| 46 | #include <math.h> |
| 47 | |
| 48 | static bool grn_scan_info_regexp_dot_asterisk_enable = true; |
| 49 | |
| 50 | void |
| 51 | grn_expr_init_from_env(void) |
| 52 | { |
| 53 | { |
| 54 | char grn_scan_info_regexp_dot_asterisk_enable_env[GRN_ENV_BUFFER_SIZE]; |
| 55 | grn_getenv("GRN_SCAN_INFO_REGEXP_DOT_ASTERISK_ENABLE", |
| 56 | grn_scan_info_regexp_dot_asterisk_enable_env, |
| 57 | GRN_ENV_BUFFER_SIZE); |
| 58 | if (strcmp(grn_scan_info_regexp_dot_asterisk_enable_env, "no") == 0) { |
| 59 | grn_scan_info_regexp_dot_asterisk_enable = false; |
| 60 | } else { |
| 61 | grn_scan_info_regexp_dot_asterisk_enable = true; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | grn_obj * |
| 67 | grn_expr_alloc(grn_ctx *ctx, grn_obj *expr, grn_id domain, unsigned char flags) |
| 68 | { |
| 69 | grn_obj *res = NULL; |
| 70 | grn_expr *e = (grn_expr *)expr; |
| 71 | if (e) { |
| 72 | if (e->values_curr >= e->values_size) { |
| 73 | // todo : expand values. |
| 74 | ERR(GRN_NO_MEMORY_AVAILABLE, "no more e->values"); |
| 75 | return NULL; |
| 76 | } |
| 77 | res = &e->values[e->values_curr++]; |
| 78 | if (e->values_curr > e->values_tail) { |
| 79 | e->values_tail = e->values_curr; |
| 80 | } |
| 81 | grn_obj_reinit(ctx, res, domain, flags); |
| 82 | } |
| 83 | return res; |
| 84 | } |
| 85 | |
| 86 | grn_hash * |
| 87 | grn_expr_get_vars(grn_ctx *ctx, grn_obj *expr, unsigned int *nvars) |
| 88 | { |
| 89 | const char *tag = "[expr][get-vars]"; |
| 90 | grn_hash *vars = NULL; |
| 91 | if (expr->header.type == GRN_PROC || expr->header.type == GRN_EXPR) { |
| 92 | grn_id id = DB_OBJ(expr)->id; |
| 93 | grn_expr *e = (grn_expr *)expr; |
| 94 | int added = 0; |
| 95 | grn_hash **vp; |
| 96 | grn_ctx *target_ctx = ctx; |
| 97 | while (target_ctx->impl->parent) { |
| 98 | target_ctx = target_ctx->impl->parent; |
| 99 | } |
| 100 | if (target_ctx != ctx) { |
| 101 | CRITICAL_SECTION_ENTER(target_ctx->impl->temporary_objects_lock); |
| 102 | } |
| 103 | if (grn_hash_add(target_ctx, |
| 104 | target_ctx->impl->expr_vars, |
| 105 | &id, |
| 106 | sizeof(grn_id), |
| 107 | (void **)&vp, |
| 108 | &added)) { |
| 109 | if (!*vp) { |
| 110 | *vp = grn_hash_create(target_ctx, |
| 111 | NULL, |
| 112 | GRN_TABLE_MAX_KEY_SIZE, |
| 113 | sizeof(grn_obj), |
| 114 | GRN_OBJ_KEY_VAR_SIZE | GRN_OBJ_TEMPORARY | |
| 115 | GRN_HASH_TINY); |
| 116 | if (*vp) { |
| 117 | uint32_t i; |
| 118 | grn_obj *value; |
| 119 | grn_expr_var *v; |
| 120 | for (v = e->vars, i = e->nvars; i; v++, i--) { |
| 121 | grn_hash_add(target_ctx, |
| 122 | *vp, |
| 123 | v->name, |
| 124 | v->name_size, |
| 125 | (void **)&value, |
| 126 | &added); |
| 127 | GRN_OBJ_INIT(value, |
| 128 | v->value.header.type, |
| 129 | 0, |
| 130 | v->value.header.domain); |
| 131 | GRN_TEXT_PUT(target_ctx, |
| 132 | value, |
| 133 | GRN_TEXT_VALUE(&v->value), |
| 134 | GRN_TEXT_LEN(&v->value)); |
| 135 | } |
| 136 | } else { |
| 137 | grn_rc rc = ctx->rc; |
| 138 | if (rc == GRN_SUCCESS) { |
| 139 | rc = GRN_UNKNOWN_ERROR; |
| 140 | } |
| 141 | ERR(rc, "%s[%u] failed to allocate vars", tag, id); |
| 142 | } |
| 143 | } |
| 144 | vars = *vp; |
| 145 | } else { |
| 146 | grn_rc rc = ctx->rc; |
| 147 | if (rc == GRN_SUCCESS) { |
| 148 | rc = GRN_UNKNOWN_ERROR; |
| 149 | } |
| 150 | ERR(rc, "%s[%u] failed to allocate vars location", tag, id); |
| 151 | } |
| 152 | if (target_ctx != ctx) { |
| 153 | CRITICAL_SECTION_LEAVE(target_ctx->impl->temporary_objects_lock); |
| 154 | } |
| 155 | } |
| 156 | *nvars = vars ? GRN_HASH_SIZE(vars) : 0; |
| 157 | return vars; |
| 158 | } |
| 159 | |
| 160 | grn_rc |
| 161 | grn_expr_clear_vars(grn_ctx *ctx, grn_obj *expr) |
| 162 | { |
| 163 | if (expr->header.type == GRN_PROC || expr->header.type == GRN_EXPR) { |
| 164 | grn_hash **vp; |
| 165 | grn_id eid, id = DB_OBJ(expr)->id; |
| 166 | grn_ctx *target_ctx = ctx; |
| 167 | while (target_ctx->impl->parent) { |
| 168 | target_ctx = target_ctx->impl->parent; |
| 169 | } |
| 170 | if (target_ctx != ctx) { |
| 171 | CRITICAL_SECTION_ENTER(target_ctx->impl->temporary_objects_lock); |
| 172 | } |
| 173 | if ((eid = grn_hash_get(target_ctx, |
| 174 | target_ctx->impl->expr_vars, |
| 175 | &id, |
| 176 | sizeof(grn_id), |
| 177 | (void **)&vp))) { |
| 178 | if (*vp) { |
| 179 | grn_obj *value; |
| 180 | GRN_HASH_EACH(target_ctx, *vp, i, NULL, NULL, (void **)&value, { |
| 181 | GRN_OBJ_FIN(target_ctx, value); |
| 182 | }); |
| 183 | grn_hash_close(target_ctx, *vp); |
| 184 | } |
| 185 | grn_hash_delete_by_id(target_ctx, target_ctx->impl->expr_vars, eid, NULL); |
| 186 | } |
| 187 | if (target_ctx != ctx) { |
| 188 | CRITICAL_SECTION_LEAVE(target_ctx->impl->temporary_objects_lock); |
| 189 | } |
| 190 | } |
| 191 | return ctx->rc; |
| 192 | } |
| 193 | |
| 194 | grn_obj * |
| 195 | grn_proc_get_info(grn_ctx *ctx, |
| 196 | grn_user_data *user_data, |
| 197 | grn_expr_var **vars, |
| 198 | unsigned int *nvars, |
| 199 | grn_obj **caller) |
| 200 | { |
| 201 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 202 | if (caller) { |
| 203 | *caller = pctx->caller; |
| 204 | } |
| 205 | if (pctx->proc) { |
| 206 | if (vars) { |
| 207 | *vars = pctx->proc->vars; |
| 208 | // *vars = grn_expr_get_vars(ctx, (grn_obj *)pctx->proc, nvars); |
| 209 | } |
| 210 | if (nvars) { |
| 211 | *nvars = pctx->proc->nvars; |
| 212 | } |
| 213 | } else { |
| 214 | if (vars) { |
| 215 | *vars = NULL; |
| 216 | } |
| 217 | if (nvars) { |
| 218 | *nvars = 0; |
| 219 | } |
| 220 | } |
| 221 | return (grn_obj *)pctx->proc; |
| 222 | } |
| 223 | |
| 224 | grn_obj * |
| 225 | grn_proc_get_vars(grn_ctx *ctx, grn_user_data *user_data) |
| 226 | { |
| 227 | uint32_t n; |
| 228 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 229 | if (pctx->proc) { |
| 230 | return (grn_obj *)grn_expr_get_vars(ctx, (grn_obj *)pctx->proc, &n); |
| 231 | } else { |
| 232 | return NULL; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | grn_obj * |
| 237 | grn_proc_get_var(grn_ctx *ctx, |
| 238 | grn_user_data *user_data, |
| 239 | const char *name, |
| 240 | unsigned int name_size) |
| 241 | { |
| 242 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 243 | return pctx->proc |
| 244 | ? grn_expr_get_var(ctx, (grn_obj *)pctx->proc, name, name_size) |
| 245 | : NULL; |
| 246 | } |
| 247 | |
| 248 | grn_obj * |
| 249 | grn_proc_get_var_by_offset(grn_ctx *ctx, |
| 250 | grn_user_data *user_data, |
| 251 | unsigned int offset) |
| 252 | { |
| 253 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 254 | return pctx->proc |
| 255 | ? grn_expr_get_var_by_offset(ctx, (grn_obj *)pctx->proc, offset) |
| 256 | : NULL; |
| 257 | } |
| 258 | |
| 259 | grn_obj * |
| 260 | grn_proc_get_or_add_var(grn_ctx *ctx, |
| 261 | grn_user_data *user_data, |
| 262 | const char *name, |
| 263 | unsigned int name_size) |
| 264 | { |
| 265 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 266 | return pctx->proc ? grn_expr_get_or_add_var(ctx, |
| 267 | (grn_obj *)pctx->proc, |
| 268 | name, |
| 269 | name_size) |
| 270 | : NULL; |
| 271 | } |
| 272 | |
| 273 | grn_obj * |
| 274 | grn_proc_alloc(grn_ctx *ctx, |
| 275 | grn_user_data *user_data, |
| 276 | grn_id domain, |
| 277 | unsigned char flags) |
| 278 | { |
| 279 | grn_proc_ctx *pctx = (grn_proc_ctx *)user_data; |
| 280 | return pctx->caller |
| 281 | ? grn_expr_alloc(ctx, (grn_obj *)pctx->caller, domain, flags) |
| 282 | : NULL; |
| 283 | } |
| 284 | |
| 285 | grn_proc_type |
| 286 | grn_proc_get_type(grn_ctx *ctx, grn_obj *proc) |
| 287 | { |
| 288 | grn_proc *proc_ = (grn_proc *)proc; |
| 289 | return proc_ ? proc_->type : GRN_PROC_INVALID; |
| 290 | } |
| 291 | |
| 292 | grn_rc |
| 293 | grn_proc_set_selector(grn_ctx *ctx, grn_obj *proc, grn_selector_func selector) |
| 294 | { |
| 295 | grn_proc *proc_ = (grn_proc *)proc; |
| 296 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 297 | return GRN_INVALID_ARGUMENT; |
| 298 | } |
| 299 | proc_->callbacks.function.selector = selector; |
| 300 | return GRN_SUCCESS; |
| 301 | } |
| 302 | |
| 303 | grn_rc |
| 304 | grn_proc_set_selector_operator(grn_ctx *ctx, grn_obj *proc, grn_operator op) |
| 305 | { |
| 306 | grn_proc *proc_ = (grn_proc *)proc; |
| 307 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 308 | return GRN_INVALID_ARGUMENT; |
| 309 | } |
| 310 | proc_->callbacks.function.selector_op = op; |
| 311 | return GRN_SUCCESS; |
| 312 | } |
| 313 | |
| 314 | grn_operator |
| 315 | grn_proc_get_selector_operator(grn_ctx *ctx, grn_obj *proc) |
| 316 | { |
| 317 | grn_proc *proc_ = (grn_proc *)proc; |
| 318 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 319 | return GRN_OP_NOP; |
| 320 | } |
| 321 | return proc_->callbacks.function.selector_op; |
| 322 | } |
| 323 | |
| 324 | grn_rc |
| 325 | grn_proc_set_is_stable(grn_ctx *ctx, grn_obj *proc, bool is_stable) |
| 326 | { |
| 327 | grn_proc *proc_ = (grn_proc *)proc; |
| 328 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 329 | return GRN_INVALID_ARGUMENT; |
| 330 | } |
| 331 | proc_->callbacks.function.is_stable = is_stable; |
| 332 | return GRN_SUCCESS; |
| 333 | } |
| 334 | |
| 335 | bool |
| 336 | grn_proc_is_stable(grn_ctx *ctx, grn_obj *proc) |
| 337 | { |
| 338 | grn_proc *proc_ = (grn_proc *)proc; |
| 339 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 340 | return false; |
| 341 | } |
| 342 | return proc_->callbacks.function.is_stable; |
| 343 | } |
| 344 | |
| 345 | grn_rc |
| 346 | grn_proc_set_applier(grn_ctx *ctx, grn_obj *proc, grn_applier_func applier) |
| 347 | { |
| 348 | grn_proc *proc_ = (grn_proc *)proc; |
| 349 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 350 | return GRN_INVALID_ARGUMENT; |
| 351 | } |
| 352 | proc_->callbacks.function.applier = applier; |
| 353 | return GRN_SUCCESS; |
| 354 | } |
| 355 | |
| 356 | grn_rc |
| 357 | grn_proc_set_sorter(grn_ctx *ctx, grn_obj *proc, grn_sorter_func sorter) |
| 358 | { |
| 359 | grn_proc *proc_ = (grn_proc *)proc; |
| 360 | if (!grn_obj_is_function_proc(ctx, proc)) { |
| 361 | return GRN_INVALID_ARGUMENT; |
| 362 | } |
| 363 | proc_->callbacks.function.sorter = sorter; |
| 364 | return GRN_SUCCESS; |
| 365 | } |
| 366 | |
| 367 | /* grn_expr */ |
| 368 | |
| 369 | grn_obj * |
| 370 | grn_ctx_pop(grn_ctx *ctx) |
| 371 | { |
| 372 | if (ctx && ctx->impl && ctx->impl->stack_curr) { |
| 373 | return ctx->impl->stack[--ctx->impl->stack_curr]; |
| 374 | } |
| 375 | return NULL; |
| 376 | } |
| 377 | |
| 378 | grn_rc |
| 379 | grn_ctx_expand_stack(grn_ctx *ctx) |
| 380 | { |
| 381 | uint32_t stack_size = ctx->impl->stack_size * 2; |
| 382 | grn_obj **stack = |
| 383 | (grn_obj **)GRN_REALLOC(ctx->impl->stack, sizeof(grn_obj *) * stack_size); |
| 384 | if (!stack) { |
| 385 | return ctx->rc; |
| 386 | } |
| 387 | ctx->impl->stack = stack; |
| 388 | ctx->impl->stack_size = stack_size; |
| 389 | return GRN_SUCCESS; |
| 390 | } |
| 391 | |
| 392 | grn_rc |
| 393 | grn_ctx_push(grn_ctx *ctx, grn_obj *obj) |
| 394 | { |
| 395 | if (!ctx || !ctx->impl) { |
| 396 | return GRN_INVALID_ARGUMENT; |
| 397 | } |
| 398 | if (ctx->impl->stack_curr >= ctx->impl->stack_size) { |
| 399 | grn_rc rc = grn_ctx_expand_stack(ctx); |
| 400 | if (rc != GRN_SUCCESS) { |
| 401 | return rc; |
| 402 | } |
| 403 | } |
| 404 | ctx->impl->stack[ctx->impl->stack_curr++] = obj; |
| 405 | return GRN_SUCCESS; |
| 406 | } |
| 407 | |
| 408 | grn_obj * |
| 409 | grn_expr_alloc_const(grn_ctx *ctx, grn_obj *expr) |
| 410 | { |
| 411 | grn_expr *e = (grn_expr *)expr; |
| 412 | uint32_t id = e->nconsts % GRN_EXPR_CONST_BLK_SIZE; |
| 413 | uint32_t blk_id = e->nconsts / GRN_EXPR_CONST_BLK_SIZE; |
| 414 | |
| 415 | if (id == 0) { |
| 416 | uint32_t nblks = blk_id + 1; |
| 417 | grn_obj **blks = |
| 418 | (grn_obj **)GRN_REALLOC(e->const_blks, sizeof(grn_obj *) * nblks); |
| 419 | if (!blks) { |
| 420 | ERR(GRN_NO_MEMORY_AVAILABLE, "realloc failed"); |
| 421 | return NULL; |
| 422 | } |
| 423 | e->const_blks = blks; |
| 424 | blks[blk_id] = GRN_MALLOCN(grn_obj, GRN_EXPR_CONST_BLK_SIZE); |
| 425 | if (!blks[blk_id]) { |
| 426 | ERR(GRN_NO_MEMORY_AVAILABLE, "malloc failed"); |
| 427 | return NULL; |
| 428 | } |
| 429 | } |
| 430 | e->nconsts++; |
| 431 | return &e->const_blks[blk_id][id]; |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | grn_obj_pack(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) |
| 436 | { |
| 437 | grn_text_benc(ctx, buf, obj->header.type); |
| 438 | if (GRN_DB_OBJP(obj)) { |
| 439 | grn_text_benc(ctx, buf, DB_OBJ(obj)->id); |
| 440 | } else { |
| 441 | // todo : support vector, query, accessor, snip.. |
| 442 | uint32_t vs = GRN_BULK_VSIZE(obj); |
| 443 | grn_text_benc(ctx, buf, obj->header.domain); |
| 444 | grn_text_benc(ctx, buf, vs); |
| 445 | if (vs) { |
| 446 | GRN_TEXT_PUT(ctx, buf, GRN_BULK_HEAD(obj), vs); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | const uint8_t * |
| 452 | grn_obj_unpack(grn_ctx *ctx, |
| 453 | const uint8_t *p, |
| 454 | const uint8_t *pe, |
| 455 | uint8_t type, |
| 456 | uint8_t flags, |
| 457 | grn_obj *obj) |
| 458 | { |
| 459 | grn_id domain; |
| 460 | uint32_t vs; |
| 461 | GRN_B_DEC(domain, p); |
| 462 | GRN_OBJ_INIT(obj, type, flags, domain); |
| 463 | GRN_B_DEC(vs, p); |
| 464 | if (pe < p + vs) { |
| 465 | ERR(GRN_INVALID_FORMAT, "benced image is corrupt"); |
| 466 | return p; |
| 467 | } |
| 468 | grn_bulk_write(ctx, obj, (const char *)p, vs); |
| 469 | return p + vs; |
| 470 | } |
| 471 | |
| 472 | typedef enum { |
| 473 | GRN_EXPR_PACK_TYPE_NULL = 0, |
| 474 | GRN_EXPR_PACK_TYPE_VARIABLE = 1, |
| 475 | GRN_EXPR_PACK_TYPE_OTHERS = 2 |
| 476 | } grn_expr_pack_type; |
| 477 | |
| 478 | void |
| 479 | grn_expr_pack(grn_ctx *ctx, grn_obj *buf, grn_obj *expr) |
| 480 | { |
| 481 | grn_expr_code *c; |
| 482 | grn_expr_var *v; |
| 483 | grn_expr *e = (grn_expr *)expr; |
| 484 | uint32_t i, j; |
| 485 | grn_text_benc(ctx, buf, e->nvars); |
| 486 | for (i = e->nvars, v = e->vars; i; i--, v++) { |
| 487 | grn_text_benc(ctx, buf, v->name_size); |
| 488 | if (v->name_size) { |
| 489 | GRN_TEXT_PUT(ctx, buf, v->name, v->name_size); |
| 490 | } |
| 491 | grn_obj_pack(ctx, buf, &v->value); |
| 492 | } |
| 493 | i = e->codes_curr; |
| 494 | grn_text_benc(ctx, buf, i); |
| 495 | for (c = e->codes; i; i--, c++) { |
| 496 | grn_text_benc(ctx, buf, c->op); |
| 497 | grn_text_benc(ctx, buf, c->nargs); |
| 498 | if (!c->value) { |
| 499 | grn_text_benc(ctx, buf, GRN_EXPR_PACK_TYPE_NULL); |
| 500 | } else { |
| 501 | for (j = 0, v = e->vars; j < e->nvars; j++, v++) { |
| 502 | if (&v->value == c->value) { |
| 503 | grn_text_benc(ctx, buf, GRN_EXPR_PACK_TYPE_VARIABLE); |
| 504 | grn_text_benc(ctx, buf, j); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | if (j == e->nvars) { |
| 509 | grn_text_benc(ctx, buf, GRN_EXPR_PACK_TYPE_OTHERS); |
| 510 | grn_obj_pack(ctx, buf, c->value); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | const uint8_t * |
| 517 | grn_expr_unpack(grn_ctx *ctx, |
| 518 | const uint8_t *p, |
| 519 | const uint8_t *pe, |
| 520 | grn_obj *expr) |
| 521 | { |
| 522 | grn_obj *v; |
| 523 | grn_expr_pack_type type; |
| 524 | uint32_t i, n, ns; |
| 525 | grn_expr_code *code; |
| 526 | grn_expr *e = (grn_expr *)expr; |
| 527 | GRN_B_DEC(n, p); |
| 528 | for (i = 0; i < n; i++) { |
| 529 | uint32_t object_type; |
| 530 | GRN_B_DEC(ns, p); |
| 531 | v = grn_expr_add_var(ctx, expr, ns ? (const char *)p : NULL, ns); |
| 532 | p += ns; |
| 533 | GRN_B_DEC(object_type, p); |
| 534 | if (GRN_TYPE <= object_type && |
| 535 | object_type <= GRN_COLUMN_INDEX) { /* error */ |
| 536 | } |
| 537 | p = grn_obj_unpack(ctx, p, pe, object_type, 0, v); |
| 538 | if (pe < p) { |
| 539 | ERR(GRN_INVALID_FORMAT, "benced image is corrupt"); |
| 540 | return p; |
| 541 | } |
| 542 | } |
| 543 | GRN_B_DEC(n, p); |
| 544 | /* confirm e->codes_size >= n */ |
| 545 | e->codes_curr = n; |
| 546 | for (i = 0, code = e->codes; i < n; i++, code++) { |
| 547 | GRN_B_DEC(code->op, p); |
| 548 | GRN_B_DEC(code->nargs, p); |
| 549 | GRN_B_DEC(type, p); |
| 550 | switch (type) { |
| 551 | case GRN_EXPR_PACK_TYPE_NULL: |
| 552 | code->value = NULL; |
| 553 | break; |
| 554 | case GRN_EXPR_PACK_TYPE_VARIABLE: |
| 555 | { |
| 556 | uint32_t offset; |
| 557 | GRN_B_DEC(offset, p); |
| 558 | code->value = &e->vars[i].value; |
| 559 | } |
| 560 | break; |
| 561 | case GRN_EXPR_PACK_TYPE_OTHERS: |
| 562 | { |
| 563 | uint32_t object_type; |
| 564 | GRN_B_DEC(object_type, p); |
| 565 | if (GRN_TYPE <= object_type && object_type <= GRN_COLUMN_INDEX) { |
| 566 | grn_id id; |
| 567 | GRN_B_DEC(id, p); |
| 568 | code->value = grn_ctx_at(ctx, id); |
| 569 | } else { |
| 570 | if (!(v = grn_expr_alloc_const(ctx, expr))) { |
| 571 | return NULL; |
| 572 | } |
| 573 | p = grn_obj_unpack(ctx, p, pe, object_type, GRN_OBJ_EXPRCONST, v); |
| 574 | code->value = v; |
| 575 | } |
| 576 | } |
| 577 | break; |
| 578 | } |
| 579 | if (pe < p) { |
| 580 | ERR(GRN_INVALID_FORMAT, "benced image is corrupt"); |
| 581 | return p; |
| 582 | } |
| 583 | } |
| 584 | return p; |
| 585 | } |
| 586 | |
| 587 | grn_obj * |
| 588 | grn_expr_open(grn_ctx *ctx, |
| 589 | grn_obj_spec *spec, |
| 590 | const uint8_t *p, |
| 591 | const uint8_t *pe) |
| 592 | { |
| 593 | grn_expr *expr = NULL; |
| 594 | if ((expr = GRN_CALLOC(sizeof(grn_expr)))) { |
| 595 | int size = GRN_STACK_SIZE; |
| 596 | expr->const_blks = NULL; |
| 597 | expr->nconsts = 0; |
| 598 | GRN_TEXT_INIT(&expr->name_buf, 0); |
| 599 | GRN_TEXT_INIT(&expr->dfi, 0); |
| 600 | GRN_PTR_INIT(&expr->objs, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 601 | GRN_TEXT_INIT(&expr->query_log_tag_prefix, 0); |
| 602 | GRN_TEXT_PUTC(ctx, &expr->query_log_tag_prefix, '\0'); |
| 603 | expr->parent = NULL; |
| 604 | expr->condition = NULL; |
| 605 | expr->vars = NULL; |
| 606 | expr->nvars = 0; |
| 607 | GRN_DB_OBJ_SET_TYPE(expr, GRN_EXPR); |
| 608 | if ((expr->values = GRN_MALLOCN(grn_obj, size))) { |
| 609 | int i; |
| 610 | for (i = 0; i < size; i++) { |
| 611 | GRN_OBJ_INIT(&expr->values[i], GRN_BULK, GRN_OBJ_EXPRVALUE, GRN_ID_NIL); |
| 612 | } |
| 613 | expr->values_curr = 0; |
| 614 | expr->values_tail = 0; |
| 615 | expr->values_size = size; |
| 616 | if ((expr->codes = GRN_MALLOCN(grn_expr_code, size))) { |
| 617 | expr->codes_curr = 0; |
| 618 | expr->codes_size = size; |
| 619 | expr->obj.header = spec->header; |
| 620 | expr->cache.codes = NULL; |
| 621 | expr->cache.codes_curr = 0; |
| 622 | if (grn_expr_unpack(ctx, p, pe, (grn_obj *)expr) == pe) { |
| 623 | goto exit; |
| 624 | } else { |
| 625 | ERR(GRN_INVALID_FORMAT, "benced image is corrupt"); |
| 626 | } |
| 627 | GRN_FREE(expr->codes); |
| 628 | } |
| 629 | GRN_FREE(expr->values); |
| 630 | } |
| 631 | GRN_OBJ_FIN(ctx, &expr->name_buf); |
| 632 | GRN_OBJ_FIN(ctx, &expr->dfi); |
| 633 | GRN_OBJ_FIN(ctx, &expr->objs); |
| 634 | GRN_OBJ_FIN(ctx, &expr->query_log_tag_prefix); |
| 635 | GRN_FREE(expr); |
| 636 | expr = NULL; |
| 637 | } |
| 638 | exit: |
| 639 | return (grn_obj *)expr; |
| 640 | } |
| 641 | |
| 642 | static inline bool |
| 643 | grn_expr_is_takable_obj(grn_ctx *ctx, grn_obj *expr, grn_obj *obj) |
| 644 | { |
| 645 | if (grn_is_reference_count_enable()) { |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | if (grn_obj_is_table(ctx, obj)) { |
| 650 | /* object literal */ |
| 651 | return grn_obj_id(ctx, obj) == GRN_ID_NIL; |
| 652 | } |
| 653 | |
| 654 | if (grn_obj_is_column(ctx, obj)) { |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | /* Pass ownership of `obj` to `expr`. */ |
| 662 | void |
| 663 | grn_expr_take_obj(grn_ctx *ctx, grn_obj *expr, grn_obj *obj) |
| 664 | { |
| 665 | grn_log_reference_count("%p: expr_take_obj: %p\n", ctx, obj); |
| 666 | if (!grn_expr_is_takable_obj(ctx, expr, obj)) { |
| 667 | return; |
| 668 | } |
| 669 | |
| 670 | grn_expr *e = (grn_expr *)expr; |
| 671 | GRN_PTR_PUT(ctx, &(e->objs), obj); |
| 672 | } |
| 673 | |
| 674 | /* data flow info */ |
| 675 | typedef struct { |
| 676 | grn_expr_code *code; |
| 677 | grn_id domain; |
| 678 | unsigned char type; |
| 679 | } grn_expr_dfi; |
| 680 | |
| 681 | static grn_expr_dfi * |
| 682 | grn_expr_dfi_pop(grn_expr *expr) |
| 683 | { |
| 684 | if (GRN_BULK_VSIZE(&expr->dfi) >= sizeof(grn_expr_dfi)) { |
| 685 | grn_expr_dfi *dfi; |
| 686 | GRN_BULK_INCR_LEN(&expr->dfi, -((ssize_t)(sizeof(grn_expr_dfi)))); |
| 687 | dfi = (grn_expr_dfi *)GRN_BULK_CURR(&expr->dfi); |
| 688 | expr->code0 = dfi->code; |
| 689 | return dfi; |
| 690 | } else { |
| 691 | expr->code0 = NULL; |
| 692 | return NULL; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | static void |
| 697 | grn_expr_dfi_put(grn_ctx *ctx, |
| 698 | grn_expr *expr, |
| 699 | uint8_t type, |
| 700 | grn_id domain, |
| 701 | grn_expr_code *code) |
| 702 | { |
| 703 | grn_expr_dfi dfi; |
| 704 | dfi.type = type; |
| 705 | dfi.domain = domain; |
| 706 | dfi.code = code; |
| 707 | if (expr->code0) { |
| 708 | expr->code0->modify = code ? (code - expr->code0) : 0; |
| 709 | } |
| 710 | grn_bulk_write(ctx, &expr->dfi, (char *)&dfi, sizeof(grn_expr_dfi)); |
| 711 | expr->code0 = NULL; |
| 712 | } |
| 713 | |
| 714 | grn_obj * |
| 715 | grn_expr_create(grn_ctx *ctx, const char *name, unsigned int name_size) |
| 716 | { |
| 717 | grn_id id; |
| 718 | grn_obj *db; |
| 719 | grn_expr *expr = NULL; |
| 720 | if (!ctx || !ctx->impl || !(db = ctx->impl->db)) { |
| 721 | ERR(GRN_INVALID_ARGUMENT, "db not initialized"); |
| 722 | return NULL; |
| 723 | } |
| 724 | if (name_size) { |
| 725 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 726 | "[expr][create] named expression isn't implemented yet"); |
| 727 | return NULL; |
| 728 | } |
| 729 | GRN_API_ENTER; |
| 730 | if (grn_db_check_name(ctx, name, name_size)) { |
| 731 | GRN_DB_CHECK_NAME_ERR("[expr][create]", name, name_size); |
| 732 | GRN_API_RETURN(NULL); |
| 733 | } |
| 734 | if (!GRN_DB_P(db)) { |
| 735 | ERR(GRN_INVALID_ARGUMENT, "named expr is not supported"); |
| 736 | GRN_API_RETURN(NULL); |
| 737 | } |
| 738 | id = grn_obj_register(ctx, db, name, name_size); |
| 739 | if (id && (expr = GRN_CALLOC(sizeof(grn_expr)))) { |
| 740 | int size = GRN_STACK_SIZE; |
| 741 | expr->const_blks = NULL; |
| 742 | expr->nconsts = 0; |
| 743 | GRN_TEXT_INIT(&expr->name_buf, 0); |
| 744 | GRN_TEXT_INIT(&expr->dfi, 0); |
| 745 | GRN_PTR_INIT(&expr->objs, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 746 | GRN_TEXT_INIT(&expr->query_log_tag_prefix, 0); |
| 747 | GRN_TEXT_PUTC(ctx, &expr->query_log_tag_prefix, '\0'); |
| 748 | expr->parent = NULL; |
| 749 | expr->condition = NULL; |
| 750 | expr->code0 = NULL; |
| 751 | expr->vars = NULL; |
| 752 | expr->nvars = 0; |
| 753 | expr->cacheable = 1; |
| 754 | expr->taintable = 0; |
| 755 | expr->values_curr = 0; |
| 756 | expr->values_tail = 0; |
| 757 | expr->values_size = size; |
| 758 | expr->codes_curr = 0; |
| 759 | expr->codes_size = size; |
| 760 | expr->cache.codes = NULL; |
| 761 | expr->cache.codes_curr = 0; |
| 762 | GRN_DB_OBJ_SET_TYPE(expr, GRN_EXPR); |
| 763 | expr->obj.header.domain = GRN_ID_NIL; |
| 764 | expr->obj.range = GRN_ID_NIL; |
| 765 | if (!grn_db_obj_init(ctx, db, id, DB_OBJ(expr))) { |
| 766 | if ((expr->values = GRN_MALLOCN(grn_obj, size))) { |
| 767 | int i; |
| 768 | for (i = 0; i < size; i++) { |
| 769 | GRN_OBJ_INIT(&expr->values[i], |
| 770 | GRN_BULK, |
| 771 | GRN_OBJ_EXPRVALUE, |
| 772 | GRN_ID_NIL); |
| 773 | } |
| 774 | if ((expr->codes = GRN_MALLOCN(grn_expr_code, size))) { |
| 775 | goto exit; |
| 776 | } |
| 777 | GRN_FREE(expr->values); |
| 778 | } |
| 779 | } |
| 780 | GRN_OBJ_FIN(ctx, &expr->name_buf); |
| 781 | GRN_OBJ_FIN(ctx, &expr->dfi); |
| 782 | GRN_OBJ_FIN(ctx, &expr->objs); |
| 783 | GRN_OBJ_FIN(ctx, &expr->query_log_tag_prefix); |
| 784 | GRN_FREE(expr); |
| 785 | expr = NULL; |
| 786 | } |
| 787 | exit: |
| 788 | GRN_API_RETURN((grn_obj *)expr); |
| 789 | } |
| 790 | |
| 791 | grn_rc |
| 792 | grn_expr_close(grn_ctx *ctx, grn_obj *expr) |
| 793 | { |
| 794 | uint32_t i, j; |
| 795 | grn_expr *e = (grn_expr *)expr; |
| 796 | GRN_API_ENTER; |
| 797 | /* |
| 798 | if (e->obj.header.domain) { |
| 799 | grn_hash_delete(ctx, ctx->impl->qe, &e->obj.header.domain, sizeof(grn_id), |
| 800 | NULL); |
| 801 | } |
| 802 | */ |
| 803 | GRN_OBJ_FIN(ctx, &(e->query_log_tag_prefix)); |
| 804 | if (e->cache.codes) { |
| 805 | grn_expr_executor_fin(ctx, &(e->cache.executor)); |
| 806 | } |
| 807 | grn_expr_clear_vars(ctx, expr); |
| 808 | if (e->const_blks) { |
| 809 | uint32_t nblks = e->nconsts + GRN_EXPR_CONST_BLK_SIZE - 1; |
| 810 | nblks /= GRN_EXPR_CONST_BLK_SIZE; |
| 811 | for (i = 0; i < nblks; i++) { |
| 812 | uint32_t end; |
| 813 | if (i < nblks - 1) { |
| 814 | end = GRN_EXPR_CONST_BLK_SIZE; |
| 815 | } else { |
| 816 | end = ((e->nconsts - 1) % GRN_EXPR_CONST_BLK_SIZE) + 1; |
| 817 | } |
| 818 | for (j = 0; j < end; j++) { |
| 819 | grn_obj *const_obj = &e->const_blks[i][j]; |
| 820 | grn_obj_close(ctx, const_obj); |
| 821 | } |
| 822 | GRN_FREE(e->const_blks[i]); |
| 823 | } |
| 824 | GRN_FREE(e->const_blks); |
| 825 | } |
| 826 | GRN_OBJ_FIN(ctx, &e->name_buf); |
| 827 | GRN_OBJ_FIN(ctx, &e->dfi); |
| 828 | for (;;) { |
| 829 | grn_obj *obj; |
| 830 | GRN_PTR_POP(&e->objs, obj); |
| 831 | if (!obj) { |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | grn_log_reference_count("%p: expr_close: obj: %p\n", ctx, obj); |
| 836 | |
| 837 | #ifdef GRN_WITH_MEMORY_DEBUG |
| 838 | grn_obj_unlink(ctx, obj); |
| 839 | #else |
| 840 | if (obj->header.type == GRN_VOID) { |
| 841 | GRN_LOG(ctx, GRN_LOG_WARNING, "GRN_VOID object is tried to be unlinked"); |
| 842 | continue; |
| 843 | } |
| 844 | |
| 845 | if (obj->header.type == GRN_TABLE_HASH_KEY && |
| 846 | grn_obj_is_temporary(ctx, obj) && |
| 847 | ((grn_hash *)obj)->value_size == sizeof(grn_obj)) { |
| 848 | GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)obj, cursor, id) |
| 849 | { |
| 850 | void *value; |
| 851 | grn_hash_cursor_get_value(ctx, cursor, &value); |
| 852 | grn_obj *v = value; |
| 853 | GRN_OBJ_FIN(ctx, v); |
| 854 | } |
| 855 | GRN_HASH_EACH_END(ctx, cursor); |
| 856 | } |
| 857 | grn_obj_unlink(ctx, obj); |
| 858 | #endif |
| 859 | } |
| 860 | GRN_OBJ_FIN(ctx, &e->objs); |
| 861 | for (i = 0; i < e->nvars; i++) { |
| 862 | grn_obj_close(ctx, &e->vars[i].value); |
| 863 | } |
| 864 | if (e->vars) { |
| 865 | GRN_FREE(e->vars); |
| 866 | } |
| 867 | for (i = 0; i < e->values_tail; i++) { |
| 868 | grn_obj_close(ctx, &e->values[i]); |
| 869 | } |
| 870 | GRN_FREE(e->values); |
| 871 | GRN_FREE(e->codes); |
| 872 | GRN_FREE(e); |
| 873 | GRN_API_RETURN(ctx->rc); |
| 874 | } |
| 875 | |
| 876 | grn_obj * |
| 877 | grn_expr_add_var(grn_ctx *ctx, |
| 878 | grn_obj *expr, |
| 879 | const char *name, |
| 880 | unsigned int name_size) |
| 881 | { |
| 882 | uint32_t i; |
| 883 | char *p; |
| 884 | grn_expr_var *v; |
| 885 | grn_obj *res = NULL; |
| 886 | grn_expr *e = (grn_expr *)expr; |
| 887 | GRN_API_ENTER; |
| 888 | if (DB_OBJ(expr)->id & GRN_OBJ_TMP_OBJECT) { |
| 889 | res = grn_expr_get_or_add_var(ctx, expr, name, name_size); |
| 890 | } else { |
| 891 | if (!e->vars) { |
| 892 | if (!(e->vars = GRN_MALLOCN(grn_expr_var, GRN_STACK_SIZE))) { |
| 893 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 894 | "[expr][variable][add] failed to allocate: <%d>", |
| 895 | GRN_STACK_SIZE); |
| 896 | } |
| 897 | } |
| 898 | if (e->vars && e->nvars < GRN_STACK_SIZE) { |
| 899 | v = e->vars + e->nvars++; |
| 900 | if (name_size) { |
| 901 | GRN_TEXT_PUT(ctx, &e->name_buf, name, name_size); |
| 902 | } else { |
| 903 | uint32_t ol = GRN_TEXT_LEN(&e->name_buf); |
| 904 | GRN_TEXT_PUTC(ctx, &e->name_buf, '$'); |
| 905 | grn_text_itoa(ctx, &e->name_buf, e->nvars); |
| 906 | name_size = GRN_TEXT_LEN(&e->name_buf) - ol; |
| 907 | } |
| 908 | v->name_size = name_size; |
| 909 | res = &v->value; |
| 910 | GRN_VOID_INIT(res); |
| 911 | for (i = e->nvars, p = GRN_TEXT_VALUE(&e->name_buf), v = e->vars; i; |
| 912 | i--, v++) { |
| 913 | v->name = p; |
| 914 | p += v->name_size; |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | GRN_API_RETURN(res); |
| 919 | } |
| 920 | |
| 921 | grn_obj * |
| 922 | grn_expr_get_var(grn_ctx *ctx, |
| 923 | grn_obj *expr, |
| 924 | const char *name, |
| 925 | unsigned int name_size) |
| 926 | { |
| 927 | uint32_t n; |
| 928 | grn_obj *res = NULL; |
| 929 | grn_hash *vars = grn_expr_get_vars(ctx, expr, &n); |
| 930 | if (vars) { |
| 931 | grn_hash_get(ctx, vars, name, name_size, (void **)&res); |
| 932 | } |
| 933 | return res; |
| 934 | } |
| 935 | |
| 936 | grn_obj * |
| 937 | grn_expr_get_or_add_var(grn_ctx *ctx, |
| 938 | grn_obj *expr, |
| 939 | const char *name, |
| 940 | unsigned int name_size) |
| 941 | { |
| 942 | uint32_t n; |
| 943 | grn_obj *res = NULL; |
| 944 | grn_hash *vars = grn_expr_get_vars(ctx, expr, &n); |
| 945 | if (vars) { |
| 946 | int added = 0; |
| 947 | char name_buf[16]; |
| 948 | if (!name_size) { |
| 949 | char *rest; |
| 950 | name_buf[0] = '$'; |
| 951 | grn_itoa((int)GRN_HASH_SIZE(vars) + 1, |
| 952 | name_buf + 1, |
| 953 | name_buf + 16, |
| 954 | &rest); |
| 955 | name_size = rest - name_buf; |
| 956 | name = name_buf; |
| 957 | } |
| 958 | grn_hash_add(ctx, vars, name, name_size, (void **)&res, &added); |
| 959 | if (added) { |
| 960 | GRN_TEXT_INIT(res, 0); |
| 961 | } |
| 962 | } |
| 963 | return res; |
| 964 | } |
| 965 | |
| 966 | grn_obj * |
| 967 | grn_expr_get_var_by_offset(grn_ctx *ctx, grn_obj *expr, unsigned int offset) |
| 968 | { |
| 969 | uint32_t n; |
| 970 | grn_obj *res = NULL; |
| 971 | grn_hash *vars = grn_expr_get_vars(ctx, expr, &n); |
| 972 | if (vars) { |
| 973 | res = (grn_obj *)grn_hash_get_value_(ctx, vars, offset + 1, NULL); |
| 974 | } |
| 975 | return res; |
| 976 | } |
| 977 | |
| 978 | #define GRN_EXPR_VAR_QUERY_OPTIONS_NAME "$query_options" |
| 979 | |
| 980 | grn_obj * |
| 981 | grn_expr_get_query_options(grn_ctx *ctx, grn_obj *expr) |
| 982 | { |
| 983 | grn_obj *var = grn_expr_get_var(ctx, |
| 984 | expr, |
| 985 | GRN_EXPR_VAR_QUERY_OPTIONS_NAME, |
| 986 | strlen(GRN_EXPR_VAR_QUERY_OPTIONS_NAME)); |
| 987 | if (!var) { |
| 988 | return NULL; |
| 989 | } |
| 990 | return GRN_PTR_VALUE(var); |
| 991 | } |
| 992 | |
| 993 | grn_rc |
| 994 | grn_expr_set_query_options(grn_ctx *ctx, grn_obj *expr, grn_obj *query_options) |
| 995 | { |
| 996 | grn_obj *var = grn_expr_get_var(ctx, |
| 997 | expr, |
| 998 | GRN_EXPR_VAR_QUERY_OPTIONS_NAME, |
| 999 | strlen(GRN_EXPR_VAR_QUERY_OPTIONS_NAME)); |
| 1000 | if (!var) { |
| 1001 | var = grn_expr_add_var(ctx, |
| 1002 | expr, |
| 1003 | GRN_EXPR_VAR_QUERY_OPTIONS_NAME, |
| 1004 | strlen(GRN_EXPR_VAR_QUERY_OPTIONS_NAME)); |
| 1005 | GRN_PTR_INIT(var, 0, GRN_DB_OBJECT); |
| 1006 | } |
| 1007 | GRN_PTR_SET(ctx, var, query_options); |
| 1008 | return ctx->rc; |
| 1009 | } |
| 1010 | |
| 1011 | #define CONSTP(obj) ((obj) && ((obj)->header.impl_flags & GRN_OBJ_EXPRCONST)) |
| 1012 | |
| 1013 | #define PUSH_CODE(e, o, v, n, c) \ |
| 1014 | do { \ |
| 1015 | (c) = &(e)->codes[e->codes_curr++]; \ |
| 1016 | (c)->value = (v); \ |
| 1017 | (c)->nargs = (n); \ |
| 1018 | (c)->op = (o); \ |
| 1019 | (c)->flags = 0; \ |
| 1020 | (c)->modify = 0; \ |
| 1021 | } while (0) |
| 1022 | |
| 1023 | #define APPEND_UNARY_MINUS_OP(e) \ |
| 1024 | do { \ |
| 1025 | grn_expr_code *code_; \ |
| 1026 | grn_id domain; \ |
| 1027 | unsigned char type; \ |
| 1028 | grn_obj *x; \ |
| 1029 | dfi = grn_expr_dfi_pop(e); \ |
| 1030 | code_ = dfi->code; \ |
| 1031 | domain = dfi->domain; \ |
| 1032 | type = dfi->type; \ |
| 1033 | x = code_->value; \ |
| 1034 | if (CONSTP(x)) { \ |
| 1035 | switch (domain) { \ |
| 1036 | case GRN_DB_INT32: \ |
| 1037 | { \ |
| 1038 | int value; \ |
| 1039 | value = GRN_INT32_VALUE(x); \ |
| 1040 | if (value == (int)0x80000000) { \ |
| 1041 | domain = GRN_DB_INT64; \ |
| 1042 | x->header.domain = domain; \ |
| 1043 | GRN_INT64_SET(ctx, x, -((long long int)value)); \ |
| 1044 | } else { \ |
| 1045 | GRN_INT32_SET(ctx, x, -value); \ |
| 1046 | } \ |
| 1047 | } \ |
| 1048 | break; \ |
| 1049 | case GRN_DB_UINT32: \ |
| 1050 | { \ |
| 1051 | unsigned int value; \ |
| 1052 | value = GRN_UINT32_VALUE(x); \ |
| 1053 | if (value > (unsigned int)0x80000000) { \ |
| 1054 | domain = GRN_DB_INT64; \ |
| 1055 | x->header.domain = domain; \ |
| 1056 | GRN_INT64_SET(ctx, x, -((long long int)value)); \ |
| 1057 | } else { \ |
| 1058 | domain = GRN_DB_INT32; \ |
| 1059 | x->header.domain = domain; \ |
| 1060 | GRN_INT32_SET(ctx, x, -((int)value)); \ |
| 1061 | } \ |
| 1062 | } \ |
| 1063 | break; \ |
| 1064 | case GRN_DB_INT64: \ |
| 1065 | GRN_INT64_SET(ctx, x, -GRN_INT64_VALUE(x)); \ |
| 1066 | break; \ |
| 1067 | case GRN_DB_FLOAT32: \ |
| 1068 | GRN_FLOAT32_SET(ctx, x, -GRN_FLOAT32_VALUE(x)); \ |
| 1069 | break; \ |
| 1070 | case GRN_DB_FLOAT: \ |
| 1071 | GRN_FLOAT_SET(ctx, x, -GRN_FLOAT_VALUE(x)); \ |
| 1072 | break; \ |
| 1073 | default: \ |
| 1074 | PUSH_CODE(e, op, obj, nargs, code); \ |
| 1075 | break; \ |
| 1076 | } \ |
| 1077 | } else { \ |
| 1078 | PUSH_CODE(e, op, obj, nargs, code); \ |
| 1079 | } \ |
| 1080 | grn_expr_dfi_put(ctx, e, type, domain, code_); \ |
| 1081 | } while (0) |
| 1082 | |
| 1083 | #define PUSH_N_ARGS_ARITHMETIC_OP(e, op, obj, nargs, code) \ |
| 1084 | do { \ |
| 1085 | PUSH_CODE(e, op, obj, nargs, code); \ |
| 1086 | { \ |
| 1087 | int i = nargs; \ |
| 1088 | while (i--) { \ |
| 1089 | dfi = grn_expr_dfi_pop(e); \ |
| 1090 | } \ |
| 1091 | } \ |
| 1092 | grn_expr_dfi_put(ctx, e, type, domain, code); \ |
| 1093 | } while (0) |
| 1094 | |
| 1095 | static void |
| 1096 | grn_expr_append_obj_resolve_const(grn_ctx *ctx, grn_obj *obj, grn_id to_domain) |
| 1097 | { |
| 1098 | grn_obj dest; |
| 1099 | |
| 1100 | GRN_OBJ_INIT(&dest, GRN_BULK, 0, to_domain); |
| 1101 | if (!grn_obj_cast(ctx, obj, &dest, false)) { |
| 1102 | grn_obj_reinit(ctx, obj, to_domain, 0); |
| 1103 | grn_bulk_write(ctx, obj, GRN_BULK_HEAD(&dest), GRN_BULK_VSIZE(&dest)); |
| 1104 | } |
| 1105 | GRN_OBJ_FIN(ctx, &dest); |
| 1106 | } |
| 1107 | |
| 1108 | grn_obj * |
| 1109 | grn_expr_append_obj( |
| 1110 | grn_ctx *ctx, grn_obj *expr, grn_obj *obj, grn_operator op, int nargs) |
| 1111 | { |
| 1112 | uint8_t type = GRN_VOID; |
| 1113 | grn_id domain = GRN_ID_NIL; |
| 1114 | grn_expr_dfi *dfi; |
| 1115 | grn_expr_code *code; |
| 1116 | grn_obj *res = NULL; |
| 1117 | grn_expr *e = (grn_expr *)expr; |
| 1118 | GRN_API_ENTER; |
| 1119 | if (e->codes_curr >= e->codes_size) { |
| 1120 | grn_expr_dfi *dfis = (grn_expr_dfi *)GRN_BULK_HEAD(&e->dfi); |
| 1121 | size_t i, n_dfis = GRN_BULK_VSIZE(&e->dfi) / sizeof(grn_expr_dfi); |
| 1122 | uint32_t new_codes_size = e->codes_size * 2; |
| 1123 | size_t n_bytes = sizeof(grn_expr_code) * new_codes_size; |
| 1124 | grn_expr_code *new_codes = (grn_expr_code *)GRN_MALLOC(n_bytes); |
| 1125 | if (!new_codes) { |
| 1126 | ERR(GRN_NO_MEMORY_AVAILABLE, "stack is full"); |
| 1127 | goto exit; |
| 1128 | } |
| 1129 | grn_memcpy(new_codes, e->codes, sizeof(grn_expr_code) * e->codes_size); |
| 1130 | if (e->code0 >= e->codes && e->code0 < e->codes + e->codes_size) { |
| 1131 | e->code0 = new_codes + (e->code0 - e->codes); |
| 1132 | } |
| 1133 | for (i = 0; i < n_dfis; i++) { |
| 1134 | if (dfis[i].code >= e->codes && dfis[i].code < e->codes + e->codes_size) { |
| 1135 | dfis[i].code = new_codes + (dfis[i].code - e->codes); |
| 1136 | } |
| 1137 | } |
| 1138 | GRN_FREE(e->codes); |
| 1139 | e->codes = new_codes; |
| 1140 | e->codes_size = new_codes_size; |
| 1141 | } |
| 1142 | { |
| 1143 | switch (op) { |
| 1144 | case GRN_OP_PUSH: |
| 1145 | if (obj) { |
| 1146 | PUSH_CODE(e, op, obj, nargs, code); |
| 1147 | grn_expr_dfi_put(ctx, |
| 1148 | e, |
| 1149 | obj->header.type, |
| 1150 | GRN_OBJ_GET_DOMAIN(obj), |
| 1151 | code); |
| 1152 | } else { |
| 1153 | ERR(GRN_INVALID_ARGUMENT, "obj not assigned for GRN_OP_PUSH"); |
| 1154 | goto exit; |
| 1155 | } |
| 1156 | break; |
| 1157 | case GRN_OP_NOP: |
| 1158 | /* nop */ |
| 1159 | break; |
| 1160 | case GRN_OP_POP: |
| 1161 | if (obj) { |
| 1162 | ERR(GRN_INVALID_ARGUMENT, "obj assigned for GRN_OP_POP"); |
| 1163 | goto exit; |
| 1164 | } else { |
| 1165 | PUSH_CODE(e, op, obj, nargs, code); |
| 1166 | dfi = grn_expr_dfi_pop(e); |
| 1167 | } |
| 1168 | break; |
| 1169 | case GRN_OP_CALL: |
| 1170 | { |
| 1171 | grn_obj *proc = NULL; |
| 1172 | /* |
| 1173 | * This is for keeping backward compatibility. We want to |
| 1174 | * handle all "nargs" means that "N items on stack are used (N |
| 1175 | * items are popped)" but "nargs" for OP_CALL is used as "N |
| 1176 | * arguments" not "N items on stack are used" historically. It |
| 1177 | * means that called function isn't included in "nargs". |
| 1178 | * |
| 1179 | * We adjust "nargs" here to handle "code->nargs" more easily. |
| 1180 | * If we don't adjust "nargs" here, we need to care |
| 1181 | * "code->nargs" at all locations that use "code->nargs". We |
| 1182 | * need to use "code->nargs + 1" for OP_CALL and "code->nargs" |
| 1183 | * for not OP_CALL to compute N items should be popped. It's |
| 1184 | * wired. So we adjust "nargs" here. |
| 1185 | */ |
| 1186 | nargs++; |
| 1187 | if (e->codes_curr - (nargs - 1) > 0) { |
| 1188 | int i; |
| 1189 | grn_expr_code *code; |
| 1190 | code = &(e->codes[e->codes_curr - 1]); |
| 1191 | for (i = 0; i < nargs - 1; i++) { |
| 1192 | int rest_n_codes = 1; |
| 1193 | while (rest_n_codes > 0) { |
| 1194 | rest_n_codes += code->nargs; |
| 1195 | if (code->value) { |
| 1196 | rest_n_codes--; |
| 1197 | } |
| 1198 | rest_n_codes--; |
| 1199 | code--; |
| 1200 | } |
| 1201 | } |
| 1202 | proc = code->value; |
| 1203 | } |
| 1204 | if (!proc) { |
| 1205 | ERR(GRN_INVALID_ARGUMENT, "invalid function call expression"); |
| 1206 | goto exit; |
| 1207 | } |
| 1208 | if (!(grn_obj_is_function_proc(ctx, proc) || |
| 1209 | grn_obj_is_scorer_proc(ctx, proc) || |
| 1210 | grn_obj_is_window_function_proc(ctx, proc) || |
| 1211 | grn_obj_is_tokenizer_proc(ctx, proc) || |
| 1212 | grn_obj_is_normalizer_proc(ctx, proc) || |
| 1213 | grn_obj_is_token_filter_proc(ctx, proc) || |
| 1214 | grn_obj_is_aggregator_proc(ctx, proc) || |
| 1215 | grn_obj_is_extractor_proc(ctx, proc))) { |
| 1216 | grn_obj buffer; |
| 1217 | |
| 1218 | GRN_TEXT_INIT(&buffer, 0); |
| 1219 | switch (proc->header.type) { |
| 1220 | case GRN_TABLE_HASH_KEY: |
| 1221 | case GRN_TABLE_PAT_KEY: |
| 1222 | case GRN_TABLE_NO_KEY: |
| 1223 | case GRN_COLUMN_FIX_SIZE: |
| 1224 | case GRN_COLUMN_VAR_SIZE: |
| 1225 | case GRN_COLUMN_INDEX: |
| 1226 | grn_inspect_name(ctx, &buffer, proc); |
| 1227 | break; |
| 1228 | default: |
| 1229 | grn_inspect(ctx, &buffer, proc); |
| 1230 | break; |
| 1231 | } |
| 1232 | ERR(GRN_INVALID_ARGUMENT, |
| 1233 | "invalid function: <%.*s>", |
| 1234 | (int)GRN_TEXT_LEN(&buffer), |
| 1235 | GRN_TEXT_VALUE(&buffer)); |
| 1236 | GRN_OBJ_FIN(ctx, &buffer); |
| 1237 | goto exit; |
| 1238 | } |
| 1239 | |
| 1240 | PUSH_CODE(e, op, obj, nargs, code); |
| 1241 | { |
| 1242 | int i = nargs - 1; |
| 1243 | while (i--) { |
| 1244 | dfi = grn_expr_dfi_pop(e); |
| 1245 | } |
| 1246 | } |
| 1247 | if (!obj) { |
| 1248 | dfi = grn_expr_dfi_pop(e); |
| 1249 | } |
| 1250 | // todo : increment e->values_tail. |
| 1251 | /* cannot identify type of return value */ |
| 1252 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1253 | if (!grn_proc_is_stable(ctx, proc)) { |
| 1254 | e->cacheable = 0; |
| 1255 | } |
| 1256 | } |
| 1257 | break; |
| 1258 | case GRN_OP_INTERN: |
| 1259 | if (obj && CONSTP(obj)) { |
| 1260 | grn_obj *value; |
| 1261 | value = |
| 1262 | grn_expr_get_var(ctx, expr, GRN_TEXT_VALUE(obj), GRN_TEXT_LEN(obj)); |
| 1263 | if (!value) { |
| 1264 | value = grn_ctx_get(ctx, GRN_TEXT_VALUE(obj), GRN_TEXT_LEN(obj)); |
| 1265 | } |
| 1266 | if (value) { |
| 1267 | obj = value; |
| 1268 | op = GRN_OP_PUSH; |
| 1269 | type = obj->header.type; |
| 1270 | domain = GRN_OBJ_GET_DOMAIN(obj); |
| 1271 | } |
| 1272 | } |
| 1273 | PUSH_CODE(e, op, obj, nargs, code); |
| 1274 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1275 | break; |
| 1276 | case GRN_OP_EQUAL: |
| 1277 | PUSH_CODE(e, op, obj, nargs, code); |
| 1278 | if (nargs) { |
| 1279 | grn_id xd, yd = GRN_ID_NIL; |
| 1280 | grn_obj *x, *y = NULL; |
| 1281 | int i = nargs - 1; |
| 1282 | if (obj) { |
| 1283 | xd = GRN_OBJ_GET_DOMAIN(obj); |
| 1284 | x = obj; |
| 1285 | } else { |
| 1286 | dfi = grn_expr_dfi_pop(e); |
| 1287 | x = dfi->code->value; |
| 1288 | xd = dfi->domain; |
| 1289 | } |
| 1290 | while (i--) { |
| 1291 | dfi = grn_expr_dfi_pop(e); |
| 1292 | y = dfi->code->value; |
| 1293 | yd = dfi->domain; |
| 1294 | } |
| 1295 | if (CONSTP(x)) { |
| 1296 | if (CONSTP(y)) { |
| 1297 | /* todo */ |
| 1298 | } else { |
| 1299 | if (xd != yd && grn_obj_is_bulk(ctx, x) && |
| 1300 | !(grn_type_id_is_number_family(ctx, xd) && |
| 1301 | grn_type_id_is_number_family(ctx, yd))) { |
| 1302 | grn_expr_append_obj_resolve_const(ctx, x, yd); |
| 1303 | } |
| 1304 | } |
| 1305 | } else { |
| 1306 | if (CONSTP(y)) { |
| 1307 | if (xd != yd && grn_obj_is_bulk(ctx, y) && |
| 1308 | !(grn_type_id_is_number_family(ctx, xd) && |
| 1309 | grn_type_id_is_number_family(ctx, yd))) { |
| 1310 | grn_expr_append_obj_resolve_const(ctx, y, xd); |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1316 | break; |
| 1317 | case GRN_OP_TABLE_CREATE: |
| 1318 | case GRN_OP_EXPR_GET_VAR: |
| 1319 | case GRN_OP_MATCH: |
| 1320 | case GRN_OP_NEAR: |
| 1321 | case GRN_OP_NEAR_NO_OFFSET: |
| 1322 | case GRN_OP_NEAR_PHRASE: |
| 1323 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 1324 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 1325 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 1326 | case GRN_OP_SIMILAR: |
| 1327 | case GRN_OP_PREFIX: |
| 1328 | case GRN_OP_SUFFIX: |
| 1329 | case GRN_OP_NOT_EQUAL: |
| 1330 | case GRN_OP_LESS: |
| 1331 | case GRN_OP_GREATER: |
| 1332 | case GRN_OP_LESS_EQUAL: |
| 1333 | case GRN_OP_GREATER_EQUAL: |
| 1334 | case GRN_OP_GEO_DISTANCE1: |
| 1335 | case GRN_OP_GEO_DISTANCE2: |
| 1336 | case GRN_OP_GEO_DISTANCE3: |
| 1337 | case GRN_OP_GEO_DISTANCE4: |
| 1338 | case GRN_OP_GEO_WITHINP5: |
| 1339 | case GRN_OP_GEO_WITHINP6: |
| 1340 | case GRN_OP_GEO_WITHINP8: |
| 1341 | case GRN_OP_OBJ_SEARCH: |
| 1342 | case GRN_OP_TABLE_SELECT: |
| 1343 | case GRN_OP_TABLE_SORT: |
| 1344 | case GRN_OP_TABLE_GROUP: |
| 1345 | case GRN_OP_JSON_PUT: |
| 1346 | case GRN_OP_GET_REF: |
| 1347 | case GRN_OP_TERM_EXTRACT: |
| 1348 | case GRN_OP_REGEXP: |
| 1349 | case GRN_OP_QUORUM: |
| 1350 | PUSH_CODE(e, op, obj, nargs, code); |
| 1351 | if (nargs) { |
| 1352 | int i = nargs - 1; |
| 1353 | if (!obj) { |
| 1354 | dfi = grn_expr_dfi_pop(e); |
| 1355 | } |
| 1356 | while (i--) { |
| 1357 | dfi = grn_expr_dfi_pop(e); |
| 1358 | } |
| 1359 | } |
| 1360 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1361 | break; |
| 1362 | case GRN_OP_AND: |
| 1363 | case GRN_OP_OR: |
| 1364 | case GRN_OP_AND_NOT: |
| 1365 | case GRN_OP_ADJUST: |
| 1366 | PUSH_CODE(e, op, obj, nargs, code); |
| 1367 | if (nargs != 2) { |
| 1368 | GRN_LOG(ctx, GRN_LOG_WARNING, "nargs(%d) != 2 in relative op", nargs); |
| 1369 | } |
| 1370 | { |
| 1371 | int i = nargs; |
| 1372 | while (i--) { |
| 1373 | dfi = grn_expr_dfi_pop(e); |
| 1374 | if (dfi) { |
| 1375 | dfi->code->flags |= GRN_EXPR_CODE_RELATIONAL_EXPRESSION; |
| 1376 | } else { |
| 1377 | grn_obj inspected; |
| 1378 | GRN_TEXT_INIT(&inspected, 0); |
| 1379 | grn_inspect(ctx, &inspected, expr); |
| 1380 | ERR(GRN_SYNTAX_ERROR, |
| 1381 | "[expr][append][obj] " |
| 1382 | "stack under flow in relative op: <%s>(%d): %.*s", |
| 1383 | grn_operator_to_string(op), |
| 1384 | nargs, |
| 1385 | (int)GRN_TEXT_LEN(&inspected), |
| 1386 | GRN_TEXT_VALUE(&inspected)); |
| 1387 | GRN_OBJ_FIN(ctx, &inspected); |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1392 | break; |
| 1393 | case GRN_OP_NOT: |
| 1394 | if (nargs == 1) { |
| 1395 | PUSH_CODE(e, op, obj, nargs, code); |
| 1396 | } |
| 1397 | break; |
| 1398 | case GRN_OP_PLUS: |
| 1399 | if (nargs > 1) { |
| 1400 | PUSH_N_ARGS_ARITHMETIC_OP(e, op, obj, nargs, code); |
| 1401 | } |
| 1402 | break; |
| 1403 | case GRN_OP_MINUS: |
| 1404 | if (nargs == 1) { |
| 1405 | APPEND_UNARY_MINUS_OP(e); |
| 1406 | } else { |
| 1407 | PUSH_N_ARGS_ARITHMETIC_OP(e, op, obj, nargs, code); |
| 1408 | } |
| 1409 | break; |
| 1410 | case GRN_OP_BITWISE_NOT: |
| 1411 | dfi = grn_expr_dfi_pop(e); |
| 1412 | if (dfi) { |
| 1413 | type = dfi->type; |
| 1414 | domain = dfi->domain; |
| 1415 | switch (domain) { |
| 1416 | case GRN_DB_UINT8: |
| 1417 | domain = GRN_DB_INT16; |
| 1418 | break; |
| 1419 | case GRN_DB_UINT16: |
| 1420 | domain = GRN_DB_INT32; |
| 1421 | break; |
| 1422 | case GRN_DB_UINT32: |
| 1423 | case GRN_DB_UINT64: |
| 1424 | domain = GRN_DB_INT64; |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | PUSH_CODE(e, op, obj, nargs, code); |
| 1429 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1430 | break; |
| 1431 | case GRN_OP_STAR: |
| 1432 | case GRN_OP_SLASH: |
| 1433 | case GRN_OP_MOD: |
| 1434 | case GRN_OP_SHIFTL: |
| 1435 | case GRN_OP_SHIFTR: |
| 1436 | case GRN_OP_SHIFTRR: |
| 1437 | case GRN_OP_BITWISE_OR: |
| 1438 | case GRN_OP_BITWISE_XOR: |
| 1439 | case GRN_OP_BITWISE_AND: |
| 1440 | PUSH_N_ARGS_ARITHMETIC_OP(e, op, obj, nargs, code); |
| 1441 | break; |
| 1442 | case GRN_OP_INCR: |
| 1443 | case GRN_OP_DECR: |
| 1444 | case GRN_OP_INCR_POST: |
| 1445 | case GRN_OP_DECR_POST: |
| 1446 | { |
| 1447 | dfi = grn_expr_dfi_pop(e); |
| 1448 | if (dfi) { |
| 1449 | type = dfi->type; |
| 1450 | domain = dfi->domain; |
| 1451 | if (dfi->code) { |
| 1452 | if (dfi->code->op == GRN_OP_GET_VALUE) { |
| 1453 | dfi->code->op = GRN_OP_GET_REF; |
| 1454 | } |
| 1455 | if (dfi->code->value && |
| 1456 | grn_obj_is_persistent(ctx, dfi->code->value)) { |
| 1457 | e->cacheable = 0; |
| 1458 | e->taintable = 1; |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | PUSH_CODE(e, op, obj, nargs, code); |
| 1463 | } |
| 1464 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1465 | break; |
| 1466 | case GRN_OP_GET_VALUE: |
| 1467 | { |
| 1468 | grn_id vdomain = GRN_ID_NIL; |
| 1469 | if (obj) { |
| 1470 | if (nargs == 1) { |
| 1471 | grn_obj *v = grn_expr_get_var_by_offset(ctx, expr, 0); |
| 1472 | if (v) { |
| 1473 | vdomain = GRN_OBJ_GET_DOMAIN(v); |
| 1474 | } |
| 1475 | } else { |
| 1476 | dfi = grn_expr_dfi_pop(e); |
| 1477 | vdomain = dfi->domain; |
| 1478 | } |
| 1479 | if (vdomain && CONSTP(obj) && obj->header.type == GRN_BULK) { |
| 1480 | grn_obj *table = grn_ctx_at(ctx, vdomain); |
| 1481 | grn_obj *col = grn_obj_column(ctx, |
| 1482 | table, |
| 1483 | GRN_BULK_HEAD(obj), |
| 1484 | GRN_BULK_VSIZE(obj)); |
| 1485 | if (col) { |
| 1486 | obj = col; |
| 1487 | type = col->header.type; |
| 1488 | domain = grn_obj_get_range(ctx, col); |
| 1489 | grn_expr_take_obj(ctx, (grn_obj *)e, col); |
| 1490 | } |
| 1491 | } else { |
| 1492 | domain = grn_obj_get_range(ctx, obj); |
| 1493 | } |
| 1494 | PUSH_CODE(e, op, obj, nargs, code); |
| 1495 | } else { |
| 1496 | grn_expr_dfi *dfi0; |
| 1497 | dfi0 = grn_expr_dfi_pop(e); |
| 1498 | if (nargs == 1) { |
| 1499 | grn_obj *v = grn_expr_get_var_by_offset(ctx, expr, 0); |
| 1500 | if (v) { |
| 1501 | vdomain = GRN_OBJ_GET_DOMAIN(v); |
| 1502 | } |
| 1503 | } else { |
| 1504 | dfi = grn_expr_dfi_pop(e); |
| 1505 | vdomain = dfi->domain; |
| 1506 | } |
| 1507 | if (dfi0->code->op == GRN_OP_PUSH) { |
| 1508 | dfi0->code->op = op; |
| 1509 | dfi0->code->nargs = nargs; |
| 1510 | obj = dfi0->code->value; |
| 1511 | if (vdomain && obj && CONSTP(obj) && obj->header.type == GRN_BULK) { |
| 1512 | grn_obj *table = grn_ctx_at(ctx, vdomain); |
| 1513 | grn_obj *col = grn_obj_column(ctx, |
| 1514 | table, |
| 1515 | GRN_BULK_HEAD(obj), |
| 1516 | GRN_BULK_VSIZE(obj)); |
| 1517 | if (col) { |
| 1518 | dfi0->code->value = col; |
| 1519 | type = col->header.type; |
| 1520 | domain = grn_obj_get_range(ctx, col); |
| 1521 | grn_expr_take_obj(ctx, expr, col); |
| 1522 | } |
| 1523 | } else { |
| 1524 | domain = grn_obj_get_range(ctx, obj); |
| 1525 | } |
| 1526 | code = dfi0->code; |
| 1527 | } else { |
| 1528 | PUSH_CODE(e, op, obj, nargs, code); |
| 1529 | } |
| 1530 | } |
| 1531 | } |
| 1532 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1533 | break; |
| 1534 | case GRN_OP_ASSIGN: |
| 1535 | case GRN_OP_STAR_ASSIGN: |
| 1536 | case GRN_OP_SLASH_ASSIGN: |
| 1537 | case GRN_OP_MOD_ASSIGN: |
| 1538 | case GRN_OP_PLUS_ASSIGN: |
| 1539 | case GRN_OP_MINUS_ASSIGN: |
| 1540 | case GRN_OP_SHIFTL_ASSIGN: |
| 1541 | case GRN_OP_SHIFTR_ASSIGN: |
| 1542 | case GRN_OP_SHIFTRR_ASSIGN: |
| 1543 | case GRN_OP_AND_ASSIGN: |
| 1544 | case GRN_OP_OR_ASSIGN: |
| 1545 | case GRN_OP_XOR_ASSIGN: |
| 1546 | { |
| 1547 | if (obj) { |
| 1548 | type = obj->header.type; |
| 1549 | domain = GRN_OBJ_GET_DOMAIN(obj); |
| 1550 | } else { |
| 1551 | dfi = grn_expr_dfi_pop(e); |
| 1552 | if (dfi) { |
| 1553 | type = dfi->type; |
| 1554 | domain = dfi->domain; |
| 1555 | } |
| 1556 | } |
| 1557 | dfi = grn_expr_dfi_pop(e); |
| 1558 | if (dfi && (dfi->code)) { |
| 1559 | if (dfi->code->op == GRN_OP_GET_VALUE) { |
| 1560 | dfi->code->op = GRN_OP_GET_REF; |
| 1561 | } |
| 1562 | if (dfi->code->value && |
| 1563 | grn_obj_is_persistent(ctx, dfi->code->value)) { |
| 1564 | e->cacheable = 0; |
| 1565 | e->taintable = 1; |
| 1566 | } |
| 1567 | } |
| 1568 | PUSH_CODE(e, op, obj, nargs, code); |
| 1569 | } |
| 1570 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1571 | break; |
| 1572 | case GRN_OP_JUMP: |
| 1573 | dfi = grn_expr_dfi_pop(e); |
| 1574 | PUSH_CODE(e, op, obj, nargs, code); |
| 1575 | break; |
| 1576 | case GRN_OP_CJUMP: |
| 1577 | dfi = grn_expr_dfi_pop(e); |
| 1578 | PUSH_CODE(e, op, obj, nargs, code); |
| 1579 | break; |
| 1580 | case GRN_OP_COMMA: |
| 1581 | PUSH_CODE(e, op, obj, nargs, code); |
| 1582 | break; |
| 1583 | case GRN_OP_GET_MEMBER: |
| 1584 | dfi = grn_expr_dfi_pop(e); |
| 1585 | dfi = grn_expr_dfi_pop(e); |
| 1586 | if (dfi) { |
| 1587 | type = dfi->type; |
| 1588 | domain = dfi->domain; |
| 1589 | if (dfi->code) { |
| 1590 | if (dfi->code->op == GRN_OP_GET_VALUE) { |
| 1591 | dfi->code->op = GRN_OP_GET_REF; |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | PUSH_CODE(e, op, obj, nargs, code); |
| 1596 | grn_expr_dfi_put(ctx, e, type, domain, code); |
| 1597 | break; |
| 1598 | default: |
| 1599 | break; |
| 1600 | } |
| 1601 | } |
| 1602 | exit: |
| 1603 | if (!ctx->rc) { |
| 1604 | res = obj; |
| 1605 | } |
| 1606 | GRN_API_RETURN(res); |
| 1607 | } |
| 1608 | #undef PUSH_N_ARGS_ARITHMETIC_OP |
| 1609 | #undef APPEND_UNARY_MINUS_OP |
| 1610 | |
| 1611 | grn_obj * |
| 1612 | grn_expr_append_const( |
| 1613 | grn_ctx *ctx, grn_obj *expr, grn_obj *obj, grn_operator op, int nargs) |
| 1614 | { |
| 1615 | grn_obj *res = NULL; |
| 1616 | GRN_API_ENTER; |
| 1617 | if (!obj) { |
| 1618 | ERR(GRN_SYNTAX_ERROR, "constant is null"); |
| 1619 | goto exit; |
| 1620 | } |
| 1621 | if (GRN_DB_OBJP(obj) || GRN_ACCESSORP(obj)) { |
| 1622 | res = obj; |
| 1623 | } else { |
| 1624 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1625 | switch (obj->header.type) { |
| 1626 | case GRN_VOID: |
| 1627 | case GRN_BULK: |
| 1628 | case GRN_UVECTOR: |
| 1629 | GRN_OBJ_INIT(res, obj->header.type, 0, obj->header.domain); |
| 1630 | grn_bulk_write(ctx, res, GRN_BULK_HEAD(obj), GRN_BULK_VSIZE(obj)); |
| 1631 | break; |
| 1632 | case GRN_VECTOR: |
| 1633 | GRN_OBJ_INIT(res, obj->header.type, 0, obj->header.domain); |
| 1634 | unsigned int n = grn_vector_size(ctx, obj); |
| 1635 | for (unsigned int i = 0; i < n; i++) { |
| 1636 | const char *content; |
| 1637 | unsigned int content_length; |
| 1638 | float weight; |
| 1639 | grn_id domain; |
| 1640 | content_length = grn_vector_get_element_float(ctx, |
| 1641 | obj, |
| 1642 | i, |
| 1643 | &content, |
| 1644 | &weight, |
| 1645 | &domain); |
| 1646 | grn_vector_add_element_float(ctx, |
| 1647 | res, |
| 1648 | content, |
| 1649 | content_length, |
| 1650 | weight, |
| 1651 | domain); |
| 1652 | } |
| 1653 | break; |
| 1654 | default: |
| 1655 | res = NULL; |
| 1656 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "unsupported type"); |
| 1657 | goto exit; |
| 1658 | } |
| 1659 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1660 | } |
| 1661 | } |
| 1662 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1663 | exit: |
| 1664 | GRN_API_RETURN(res); |
| 1665 | } |
| 1666 | |
| 1667 | static grn_obj * |
| 1668 | grn_expr_add_str(grn_ctx *ctx, |
| 1669 | grn_obj *expr, |
| 1670 | const char *str, |
| 1671 | unsigned int str_size) |
| 1672 | { |
| 1673 | grn_obj *res = NULL; |
| 1674 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1675 | GRN_TEXT_INIT(res, 0); |
| 1676 | grn_bulk_write(ctx, res, str, str_size); |
| 1677 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1678 | } |
| 1679 | return res; |
| 1680 | } |
| 1681 | |
| 1682 | grn_obj * |
| 1683 | grn_expr_append_const_str(grn_ctx *ctx, |
| 1684 | grn_obj *expr, |
| 1685 | const char *str, |
| 1686 | unsigned int str_size, |
| 1687 | grn_operator op, |
| 1688 | int nargs) |
| 1689 | { |
| 1690 | grn_obj *res; |
| 1691 | GRN_API_ENTER; |
| 1692 | res = grn_expr_add_str(ctx, expr, str, str_size); |
| 1693 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1694 | GRN_API_RETURN(res); |
| 1695 | } |
| 1696 | |
| 1697 | grn_obj * |
| 1698 | grn_expr_append_const_int( |
| 1699 | grn_ctx *ctx, grn_obj *expr, int value, grn_operator op, int nargs) |
| 1700 | { |
| 1701 | return grn_expr_append_const_int32(ctx, expr, value, op, nargs); |
| 1702 | } |
| 1703 | |
| 1704 | grn_obj * |
| 1705 | grn_expr_append_const_int32( |
| 1706 | grn_ctx *ctx, grn_obj *expr, int32_t value, grn_operator op, int nargs) |
| 1707 | { |
| 1708 | grn_obj *res = NULL; |
| 1709 | GRN_API_ENTER; |
| 1710 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1711 | GRN_INT32_INIT(res, 0); |
| 1712 | GRN_INT32_SET(ctx, res, value); |
| 1713 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1714 | } |
| 1715 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1716 | GRN_API_RETURN(res); |
| 1717 | } |
| 1718 | |
| 1719 | grn_obj * |
| 1720 | grn_expr_append_const_uint32( |
| 1721 | grn_ctx *ctx, grn_obj *expr, uint32_t value, grn_operator op, int nargs) |
| 1722 | { |
| 1723 | grn_obj *res = NULL; |
| 1724 | GRN_API_ENTER; |
| 1725 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1726 | GRN_UINT32_INIT(res, 0); |
| 1727 | GRN_UINT32_SET(ctx, res, value); |
| 1728 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1729 | } |
| 1730 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1731 | GRN_API_RETURN(res); |
| 1732 | } |
| 1733 | |
| 1734 | grn_obj * |
| 1735 | grn_expr_append_const_bool( |
| 1736 | grn_ctx *ctx, grn_obj *expr, bool value, grn_operator op, int nargs) |
| 1737 | { |
| 1738 | grn_obj *res = NULL; |
| 1739 | GRN_API_ENTER; |
| 1740 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1741 | GRN_BOOL_INIT(res, 0); |
| 1742 | GRN_BOOL_SET(ctx, res, value); |
| 1743 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1744 | } |
| 1745 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1746 | GRN_API_RETURN(res); |
| 1747 | } |
| 1748 | |
| 1749 | grn_obj * |
| 1750 | grn_expr_append_const_float32( |
| 1751 | grn_ctx *ctx, grn_obj *expr, float value, grn_operator op, int nargs) |
| 1752 | { |
| 1753 | grn_obj *res = NULL; |
| 1754 | GRN_API_ENTER; |
| 1755 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1756 | GRN_FLOAT32_INIT(res, 0); |
| 1757 | GRN_FLOAT32_SET(ctx, res, value); |
| 1758 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1759 | } |
| 1760 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1761 | GRN_API_RETURN(res); |
| 1762 | } |
| 1763 | |
| 1764 | grn_obj * |
| 1765 | grn_expr_append_const_float( |
| 1766 | grn_ctx *ctx, grn_obj *expr, double value, grn_operator op, int nargs) |
| 1767 | { |
| 1768 | grn_obj *res = NULL; |
| 1769 | GRN_API_ENTER; |
| 1770 | if ((res = grn_expr_alloc_const(ctx, expr))) { |
| 1771 | GRN_FLOAT_INIT(res, 0); |
| 1772 | GRN_FLOAT_SET(ctx, res, value); |
| 1773 | res->header.impl_flags |= GRN_OBJ_EXPRCONST; |
| 1774 | } |
| 1775 | grn_expr_append_obj(ctx, expr, res, op, nargs); /* constant */ |
| 1776 | GRN_API_RETURN(res); |
| 1777 | } |
| 1778 | |
| 1779 | grn_rc |
| 1780 | grn_expr_append_op(grn_ctx *ctx, grn_obj *expr, grn_operator op, int nargs) |
| 1781 | { |
| 1782 | grn_expr_append_obj(ctx, expr, NULL, op, nargs); |
| 1783 | return ctx->rc; |
| 1784 | } |
| 1785 | |
| 1786 | grn_rc |
| 1787 | grn_expr_compile(grn_ctx *ctx, grn_obj *expr) |
| 1788 | { |
| 1789 | grn_obj_spec_save(ctx, DB_OBJ(expr)); |
| 1790 | return ctx->rc; |
| 1791 | } |
| 1792 | |
| 1793 | grn_obj * |
| 1794 | grn_expr_rewrite(grn_ctx *ctx, grn_obj *expr) |
| 1795 | { |
| 1796 | grn_obj *rewritten = NULL; |
| 1797 | |
| 1798 | GRN_API_ENTER; |
| 1799 | |
| 1800 | #ifdef GRN_WITH_MRUBY |
| 1801 | grn_ctx_impl_mrb_ensure_init(ctx); |
| 1802 | if (ctx->rc != GRN_SUCCESS) { |
| 1803 | GRN_API_RETURN(NULL); |
| 1804 | } |
| 1805 | if (ctx->impl->mrb.state) { |
| 1806 | rewritten = grn_mrb_expr_rewrite(ctx, expr); |
| 1807 | } |
| 1808 | #endif |
| 1809 | |
| 1810 | GRN_API_RETURN(rewritten); |
| 1811 | } |
| 1812 | |
| 1813 | grn_rc |
| 1814 | grn_proc_call(grn_ctx *ctx, grn_obj *proc, int nargs, grn_obj *caller) |
| 1815 | { |
| 1816 | grn_proc_ctx pctx; |
| 1817 | grn_obj *obj = NULL, **args; |
| 1818 | grn_proc *p = (grn_proc *)proc; |
| 1819 | if (nargs > (int)(ctx->impl->stack_curr)) { |
| 1820 | return GRN_INVALID_ARGUMENT; |
| 1821 | } |
| 1822 | GRN_API_ENTER; |
| 1823 | if (grn_obj_is_selector_only_proc(ctx, proc)) { |
| 1824 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 1825 | int name_size; |
| 1826 | name_size = grn_obj_name(ctx, proc, name, GRN_TABLE_MAX_KEY_SIZE); |
| 1827 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, |
| 1828 | "selector only proc can't be called: <%.*s>", |
| 1829 | name_size, |
| 1830 | name); |
| 1831 | GRN_API_RETURN(ctx->rc); |
| 1832 | } |
| 1833 | args = ctx->impl->stack + ctx->impl->stack_curr - nargs; |
| 1834 | pctx.proc = p; |
| 1835 | pctx.caller = caller; |
| 1836 | pctx.user_data.ptr = NULL; |
| 1837 | if (p->funcs[PROC_INIT]) { |
| 1838 | grn_obj *sub_obj; |
| 1839 | sub_obj = p->funcs[PROC_INIT](ctx, nargs, args, &pctx.user_data); |
| 1840 | if (sub_obj) { |
| 1841 | obj = sub_obj; |
| 1842 | } |
| 1843 | } |
| 1844 | pctx.phase = PROC_NEXT; |
| 1845 | if (p->funcs[PROC_NEXT]) { |
| 1846 | grn_obj *sub_obj; |
| 1847 | sub_obj = p->funcs[PROC_NEXT](ctx, nargs, args, &pctx.user_data); |
| 1848 | if (sub_obj) { |
| 1849 | obj = sub_obj; |
| 1850 | } |
| 1851 | } |
| 1852 | pctx.phase = PROC_FIN; |
| 1853 | if (p->funcs[PROC_FIN]) { |
| 1854 | grn_obj *sub_obj; |
| 1855 | sub_obj = p->funcs[PROC_FIN](ctx, nargs, args, &pctx.user_data); |
| 1856 | if (sub_obj) { |
| 1857 | obj = sub_obj; |
| 1858 | } |
| 1859 | } |
| 1860 | ctx->impl->stack_curr -= nargs; |
| 1861 | grn_ctx_push(ctx, obj); |
| 1862 | GRN_API_RETURN(ctx->rc); |
| 1863 | } |
| 1864 | |
| 1865 | grn_obj * |
| 1866 | grn_expr_exec(grn_ctx *ctx, grn_obj *expr, int nargs) |
| 1867 | { |
| 1868 | grn_obj *value = NULL; |
| 1869 | GRN_API_ENTER; |
| 1870 | if (expr->header.type == GRN_PROC) { |
| 1871 | grn_proc *proc = (grn_proc *)expr; |
| 1872 | if (proc->type == GRN_PROC_COMMAND) { |
| 1873 | grn_command_input *input; |
| 1874 | input = grn_command_input_open(ctx, expr); |
| 1875 | grn_command_run(ctx, expr, input); |
| 1876 | grn_command_input_close(ctx, input); |
| 1877 | value = NULL; |
| 1878 | } else { |
| 1879 | uint32_t stack_curr = ctx->impl->stack_curr; |
| 1880 | grn_proc_call(ctx, expr, nargs, expr); |
| 1881 | if (ctx->impl->stack_curr + nargs > stack_curr) { |
| 1882 | value = grn_ctx_pop(ctx); |
| 1883 | } |
| 1884 | if (ctx->impl->stack_curr + nargs > stack_curr) { |
| 1885 | /* |
| 1886 | GRN_LOG(ctx, GRN_LOG_WARNING, "nargs=%d stack balance=%d", |
| 1887 | nargs, stack_curr - ctx->impl->stack_curr); |
| 1888 | */ |
| 1889 | ctx->impl->stack_curr = stack_curr - nargs; |
| 1890 | } |
| 1891 | } |
| 1892 | } else { |
| 1893 | grn_expr *e = (grn_expr *)expr; |
| 1894 | grn_expr_executor *executor = &(e->cache.executor); |
| 1895 | grn_id id = GRN_ID_NIL; |
| 1896 | |
| 1897 | if (e->codes != e->cache.codes || e->codes_curr != e->cache.codes_curr) { |
| 1898 | if (e->cache.codes) { |
| 1899 | grn_expr_executor_fin(ctx, executor); |
| 1900 | } |
| 1901 | grn_expr_executor_init(ctx, executor, expr); |
| 1902 | if (ctx->rc != GRN_SUCCESS) { |
| 1903 | e->cache.codes = NULL; |
| 1904 | e->cache.codes_curr = 0; |
| 1905 | goto exit; |
| 1906 | } |
| 1907 | e->cache.codes = e->codes; |
| 1908 | e->cache.codes_curr = e->codes_curr; |
| 1909 | } |
| 1910 | |
| 1911 | if (executor->variable) { |
| 1912 | id = GRN_RECORD_VALUE(executor->variable); |
| 1913 | } |
| 1914 | value = grn_expr_executor_exec(ctx, executor, id); |
| 1915 | } |
| 1916 | exit: |
| 1917 | GRN_API_RETURN(value); |
| 1918 | } |
| 1919 | |
| 1920 | grn_obj * |
| 1921 | grn_expr_get_value(grn_ctx *ctx, grn_obj *expr, int offset) |
| 1922 | { |
| 1923 | grn_obj *res = NULL; |
| 1924 | grn_expr *e = (grn_expr *)expr; |
| 1925 | GRN_API_ENTER; |
| 1926 | if (0 <= offset && (uint32_t)offset < e->values_size) { |
| 1927 | res = &e->values[offset]; |
| 1928 | } |
| 1929 | GRN_API_RETURN(res); |
| 1930 | } |
| 1931 | |
| 1932 | #define DEFAULT_WEIGHT 5 |
| 1933 | #define DEFAULT_DECAYSTEP 2 |
| 1934 | #define DEFAULT_MAX_INTERVAL 10 |
| 1935 | #define DEFAULT_ADDITIONAL_LAST_INTERVAL 0 |
| 1936 | #define DEFAULT_SIMILARITY_THRESHOLD 0 |
| 1937 | #define DEFAULT_QUORUM_THRESHOLD 1 |
| 1938 | #define DEFAULT_TERM_EXTRACT_POLICY 0 |
| 1939 | #define DEFAULT_WEIGHT_VECTOR_SIZE 4096 |
| 1940 | |
| 1941 | static inline void |
| 1942 | grn_scan_info_free(grn_ctx *ctx, scan_info *si) |
| 1943 | { |
| 1944 | GRN_OBJ_FIN(ctx, &(si->weights)); |
| 1945 | GRN_OBJ_FIN(ctx, &(si->sections)); |
| 1946 | { |
| 1947 | size_t i; |
| 1948 | size_t n_indexes = GRN_PTR_VECTOR_SIZE(&(si->index)); |
| 1949 | for (i = 0; i < n_indexes; i++) { |
| 1950 | grn_obj_unref(ctx, GRN_PTR_VALUE_AT(&(si->index), i)); |
| 1951 | } |
| 1952 | } |
| 1953 | GRN_OBJ_FIN(ctx, &(si->index)); |
| 1954 | GRN_OBJ_FIN(ctx, &(si->max_element_intervals)); |
| 1955 | GRN_OBJ_FIN(ctx, &(si->scorers)); |
| 1956 | GRN_OBJ_FIN(ctx, &(si->scorer_args_exprs)); |
| 1957 | GRN_OBJ_FIN(ctx, &(si->scorer_args_expr_offsets)); |
| 1958 | GRN_OBJ_FIN(ctx, &(si->start_positions)); |
| 1959 | if (si->args != si->initial_args) { |
| 1960 | GRN_FREE(si->args); |
| 1961 | } |
| 1962 | GRN_FREE(si); |
| 1963 | } |
| 1964 | |
| 1965 | #define SI_FREE(si) grn_scan_info_free((ctx), (si)) |
| 1966 | |
| 1967 | #define SI_ALLOC_RAW(si, st) \ |
| 1968 | do { \ |
| 1969 | if (((si) = GRN_CALLOC(sizeof(scan_info)))) { \ |
| 1970 | GRN_UINT32_INIT(&(si)->sections, GRN_OBJ_VECTOR); \ |
| 1971 | GRN_FLOAT32_INIT(&(si)->weights, GRN_OBJ_VECTOR); \ |
| 1972 | GRN_PTR_INIT(&(si)->index, GRN_OBJ_VECTOR, GRN_ID_NIL); \ |
| 1973 | (si)->op = GRN_OP_NOP; \ |
| 1974 | (si)->logical_op = GRN_OP_OR; \ |
| 1975 | (si)->flags = SCAN_PUSH; \ |
| 1976 | (si)->weight_factor = 1.0; \ |
| 1977 | (si)->nargs = 0; \ |
| 1978 | (si)->max_nargs = GRN_SCAN_INFO_INITIAL_MAX_N_ARGS; \ |
| 1979 | (si)->args = (si)->initial_args; \ |
| 1980 | (si)->max_interval = DEFAULT_MAX_INTERVAL; \ |
| 1981 | (si)->additional_last_interval = DEFAULT_ADDITIONAL_LAST_INTERVAL; \ |
| 1982 | GRN_INT32_INIT(&(si)->max_element_intervals, GRN_OBJ_VECTOR); \ |
| 1983 | (si)->min_interval = GRN_II_DEFAULT_NEAR_MIN_INTERVAL; \ |
| 1984 | (si)->similarity_threshold = DEFAULT_SIMILARITY_THRESHOLD; \ |
| 1985 | (si)->quorum_threshold = DEFAULT_QUORUM_THRESHOLD; \ |
| 1986 | (si)->start = (st); \ |
| 1987 | (si)->end = (st); \ |
| 1988 | (si)->query = NULL; \ |
| 1989 | GRN_PTR_INIT(&(si)->scorers, GRN_OBJ_VECTOR, GRN_ID_NIL); \ |
| 1990 | GRN_PTR_INIT(&(si)->scorer_args_exprs, GRN_OBJ_VECTOR, GRN_ID_NIL); \ |
| 1991 | GRN_UINT32_INIT(&(si)->scorer_args_expr_offsets, GRN_OBJ_VECTOR); \ |
| 1992 | GRN_INT32_INIT(&(si)->start_positions, GRN_OBJ_VECTOR); \ |
| 1993 | } \ |
| 1994 | } while (0) |
| 1995 | |
| 1996 | #define SI_ALLOC(si, i, st) \ |
| 1997 | do { \ |
| 1998 | SI_ALLOC_RAW(si, st); \ |
| 1999 | if (!(si)) { \ |
| 2000 | int j; \ |
| 2001 | for (j = 0; j < i; j++) { \ |
| 2002 | SI_FREE(sis[j]); \ |
| 2003 | } \ |
| 2004 | GRN_FREE(sis); \ |
| 2005 | return NULL; \ |
| 2006 | } \ |
| 2007 | } while (0) |
| 2008 | |
| 2009 | static scan_info ** |
| 2010 | put_logical_op(grn_ctx *ctx, |
| 2011 | scan_info **sis, |
| 2012 | int *ip, |
| 2013 | grn_operator op, |
| 2014 | int start, |
| 2015 | float weight_factor) |
| 2016 | { |
| 2017 | int nparens = 1, ndifops = 0, i = *ip, j = i, r = 0; |
| 2018 | bool is_commutative_operator = true; |
| 2019 | switch (op) { |
| 2020 | case GRN_OP_AND_NOT: |
| 2021 | case GRN_OP_ADJUST: |
| 2022 | is_commutative_operator = false; |
| 2023 | break; |
| 2024 | default: |
| 2025 | break; |
| 2026 | } |
| 2027 | while (j--) { |
| 2028 | scan_info *s_ = sis[j]; |
| 2029 | if (s_->flags & SCAN_POP) { |
| 2030 | ndifops++; |
| 2031 | nparens++; |
| 2032 | } else { |
| 2033 | if (s_->flags & SCAN_PUSH) { |
| 2034 | if (!(--nparens)) { |
| 2035 | if (!r) { |
| 2036 | if (ndifops) { |
| 2037 | if (j && is_commutative_operator) { |
| 2038 | nparens = 1; |
| 2039 | ndifops = 0; |
| 2040 | r = j; |
| 2041 | } else { |
| 2042 | SI_ALLOC(s_, i, start); |
| 2043 | s_->flags = SCAN_POP; |
| 2044 | s_->logical_op = op; |
| 2045 | s_->weight_factor = weight_factor; |
| 2046 | sis[i++] = s_; |
| 2047 | *ip = i; |
| 2048 | break; |
| 2049 | } |
| 2050 | } else { |
| 2051 | s_->flags &= ~SCAN_PUSH; |
| 2052 | s_->logical_op = op; |
| 2053 | s_->weight_factor = weight_factor; |
| 2054 | break; |
| 2055 | } |
| 2056 | } else { |
| 2057 | if (ndifops) { |
| 2058 | SI_ALLOC(s_, i, start); |
| 2059 | s_->flags = SCAN_POP; |
| 2060 | s_->logical_op = op; |
| 2061 | s_->weight_factor = weight_factor; |
| 2062 | sis[i++] = s_; |
| 2063 | *ip = i; |
| 2064 | } else { |
| 2065 | s_->flags &= ~SCAN_PUSH; |
| 2066 | s_->logical_op = op; |
| 2067 | s_->weight_factor = weight_factor; |
| 2068 | grn_memcpy(&sis[i], &sis[j], sizeof(scan_info *) * (r - j)); |
| 2069 | grn_memmove(&sis[j], &sis[r], sizeof(scan_info *) * (i - r)); |
| 2070 | grn_memcpy(&sis[i + j - r], |
| 2071 | &sis[i], |
| 2072 | sizeof(scan_info *) * (r - j)); |
| 2073 | } |
| 2074 | break; |
| 2075 | } |
| 2076 | } |
| 2077 | } else { |
| 2078 | if (!is_commutative_operator || (op != s_->logical_op)) { |
| 2079 | ndifops++; |
| 2080 | } |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | if (j < 0) { |
| 2085 | ERR(GRN_INVALID_ARGUMENT, "unmatched nesting level"); |
| 2086 | for (j = 0; j < i; j++) { |
| 2087 | SI_FREE(sis[j]); |
| 2088 | } |
| 2089 | GRN_FREE(sis); |
| 2090 | return NULL; |
| 2091 | } |
| 2092 | return sis; |
| 2093 | } |
| 2094 | |
| 2095 | /* TODO: Remove me if nobody doesn't want to reuse the implementation again. */ |
| 2096 | #if 0 |
| 2097 | static const char *opstrs[] = { |
| 2098 | "PUSH", |
| 2099 | "POP", |
| 2100 | "NOP", |
| 2101 | "CALL", |
| 2102 | "INTERN", |
| 2103 | "GET_REF", |
| 2104 | "GET_VALUE", |
| 2105 | "AND", |
| 2106 | "AND_NOT", |
| 2107 | "OR", |
| 2108 | "ASSIGN", |
| 2109 | "STAR_ASSIGN", |
| 2110 | "SLASH_ASSIGN", |
| 2111 | "MOD_ASSIGN", |
| 2112 | "PLUS_ASSIGN", |
| 2113 | "MINUS_ASSIGN", |
| 2114 | "SHIFTL_ASSIGN", |
| 2115 | "SHIFTR_ASSIGN", |
| 2116 | "SHIFTRR_ASSIGN", |
| 2117 | "AND_ASSIGN", |
| 2118 | "XOR_ASSIGN", |
| 2119 | "OR_ASSIGN", |
| 2120 | "JUMP", |
| 2121 | "CJUMP", |
| 2122 | "COMMA", |
| 2123 | "BITWISE_OR", |
| 2124 | "BITWISE_XOR", |
| 2125 | "BITWISE_AND", |
| 2126 | "BITWISE_NOT", |
| 2127 | "EQUAL", |
| 2128 | "NOT_EQUAL", |
| 2129 | "LESS", |
| 2130 | "GREATER", |
| 2131 | "LESS_EQUAL", |
| 2132 | "GREATER_EQUAL", |
| 2133 | "IN", |
| 2134 | "MATCH", |
| 2135 | "NEAR", |
| 2136 | "NEAR_NO_OFFSET", |
| 2137 | "SIMILAR", |
| 2138 | "TERM_EXTRACT", |
| 2139 | "SHIFTL", |
| 2140 | "SHIFTR", |
| 2141 | "SHIFTRR", |
| 2142 | "PLUS", |
| 2143 | "MINUS", |
| 2144 | "STAR", |
| 2145 | "SLASH", |
| 2146 | "MOD", |
| 2147 | "DELETE", |
| 2148 | "INCR", |
| 2149 | "DECR", |
| 2150 | "INCR_POST", |
| 2151 | "DECR_POST", |
| 2152 | "NOT", |
| 2153 | "ADJUST", |
| 2154 | "EXACT", |
| 2155 | "LCP", |
| 2156 | "PARTIAL", |
| 2157 | "UNSPLIT", |
| 2158 | "PREFIX", |
| 2159 | "SUFFIX", |
| 2160 | "GEO_DISTANCE1", |
| 2161 | "GEO_DISTANCE2", |
| 2162 | "GEO_DISTANCE3", |
| 2163 | "GEO_DISTANCE4", |
| 2164 | "GEO_WITHINP5", |
| 2165 | "GEO_WITHINP6", |
| 2166 | "GEO_WITHINP8", |
| 2167 | "OBJ_SEARCH", |
| 2168 | "EXPR_GET_VAR", |
| 2169 | "TABLE_CREATE", |
| 2170 | "TABLE_SELECT", |
| 2171 | "TABLE_SORT", |
| 2172 | "TABLE_GROUP", |
| 2173 | "JSON_PUT", |
| 2174 | "GET_MEMBER", |
| 2175 | "REGEXP", |
| 2176 | "FUZZY", |
| 2177 | "QUORUM", |
| 2178 | "NEAR_PHRASE", |
| 2179 | "ORDERED_NEAR_PHRASE", |
| 2180 | "NEAR_PHRASE_PRODUCT", |
| 2181 | "ORDERED_NEAR_PHRASE_PRODUCT", |
| 2182 | }; |
| 2183 | |
| 2184 | static void |
| 2185 | put_value(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) |
| 2186 | { |
| 2187 | int len; |
| 2188 | char namebuf[GRN_TABLE_MAX_KEY_SIZE]; |
| 2189 | if ((len = grn_column_name(ctx, obj, namebuf, GRN_TABLE_MAX_KEY_SIZE))) { |
| 2190 | GRN_TEXT_PUT(ctx, buf, namebuf, len); |
| 2191 | } else { |
| 2192 | grn_text_otoj(ctx, buf, obj, NULL); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | static grn_rc |
| 2197 | grn_expr_inspect_internal(grn_ctx *ctx, grn_obj *buf, grn_obj *expr) |
| 2198 | { |
| 2199 | uint32_t i, j; |
| 2200 | grn_expr_var *var; |
| 2201 | grn_expr_code *code; |
| 2202 | grn_expr *e = (grn_expr *)expr; |
| 2203 | grn_hash *vars = grn_expr_get_vars(ctx, expr, &i); |
| 2204 | GRN_TEXT_PUTS(ctx, buf, "noname"); |
| 2205 | GRN_TEXT_PUTC(ctx, buf, '('); |
| 2206 | { |
| 2207 | int i = 0; |
| 2208 | grn_obj *value; |
| 2209 | const char *name; |
| 2210 | uint32_t name_len; |
| 2211 | GRN_HASH_EACH(ctx, vars, id, &name, &name_len, &value, { |
| 2212 | if (i++) { GRN_TEXT_PUTC(ctx, buf, ','); } |
| 2213 | GRN_TEXT_PUT(ctx, buf, name, name_len); |
| 2214 | GRN_TEXT_PUTC(ctx, buf, ':'); |
| 2215 | put_value(ctx, buf, value); |
| 2216 | }); |
| 2217 | } |
| 2218 | GRN_TEXT_PUTC(ctx, buf, ')'); |
| 2219 | GRN_TEXT_PUTC(ctx, buf, '{'); |
| 2220 | for (j = 0, code = e->codes; j < e->codes_curr; j++, code++) { |
| 2221 | if (j) { GRN_TEXT_PUTC(ctx, buf, ','); } |
| 2222 | grn_text_itoa(ctx, buf, code->modify); |
| 2223 | if (code->op == GRN_OP_PUSH) { |
| 2224 | for (i = 0, var = e->vars; i < e->nvars; i++, var++) { |
| 2225 | if (&var->value == code->value) { |
| 2226 | GRN_TEXT_PUTC(ctx, buf, '?'); |
| 2227 | if (var->name_size) { |
| 2228 | GRN_TEXT_PUT(ctx, buf, var->name, var->name_size); |
| 2229 | } else { |
| 2230 | grn_text_itoa(ctx, buf, (int)i); |
| 2231 | } |
| 2232 | break; |
| 2233 | } |
| 2234 | } |
| 2235 | if (i == e->nvars) { |
| 2236 | put_value(ctx, buf, code->value); |
| 2237 | } |
| 2238 | } else { |
| 2239 | if (code->value) { |
| 2240 | put_value(ctx, buf, code->value); |
| 2241 | GRN_TEXT_PUTC(ctx, buf, ' '); |
| 2242 | } |
| 2243 | GRN_TEXT_PUTS(ctx, buf, opstrs[code->op]); |
| 2244 | } |
| 2245 | } |
| 2246 | GRN_TEXT_PUTC(ctx, buf, '}'); |
| 2247 | return GRN_SUCCESS; |
| 2248 | } |
| 2249 | |
| 2250 | # define EXPRLOG(name, expr) \ |
| 2251 | do { \ |
| 2252 | grn_obj strbuf; \ |
| 2253 | GRN_TEXT_INIT(&strbuf, 0); \ |
| 2254 | grn_expr_inspect_internal(ctx, &strbuf, (expr)); \ |
| 2255 | GRN_TEXT_PUTC(ctx, &strbuf, '\0'); \ |
| 2256 | GRN_LOG(ctx, \ |
| 2257 | GRN_LOG_NOTICE, \ |
| 2258 | "%s=(%s)", \ |
| 2259 | (name), \ |
| 2260 | GRN_TEXT_VALUE(&strbuf)); \ |
| 2261 | GRN_OBJ_FIN(ctx, &strbuf); \ |
| 2262 | } while (0) |
| 2263 | #endif |
| 2264 | |
| 2265 | static void |
| 2266 | scan_info_put_index(grn_ctx *ctx, |
| 2267 | scan_info *si, |
| 2268 | grn_obj *index, |
| 2269 | uint32_t sid, |
| 2270 | int32_t start_position, |
| 2271 | float weight, |
| 2272 | grn_obj *scorer, |
| 2273 | grn_obj *scorer_args_expr, |
| 2274 | uint32_t scorer_args_expr_offset) |
| 2275 | { |
| 2276 | uint32_t n_indexes = GRN_PTR_VECTOR_SIZE(&(si->index)); |
| 2277 | uint32_t i; |
| 2278 | if (scorer) { |
| 2279 | i = n_indexes; |
| 2280 | } else { |
| 2281 | for (i = 0; i < n_indexes; i++) { |
| 2282 | grn_obj *existing_scorer = GRN_PTR_VALUE_AT(&(si->scorers), i); |
| 2283 | if (existing_scorer) { |
| 2284 | continue; |
| 2285 | } |
| 2286 | grn_obj *existing_index = GRN_PTR_VALUE_AT(&(si->index), i); |
| 2287 | uint32_t existing_sid = GRN_UINT32_VALUE_AT(&(si->sections), i); |
| 2288 | int32_t existing_start_position = |
| 2289 | GRN_INT32_VALUE_AT(&(si->start_positions), i); |
| 2290 | if (existing_index == index && existing_sid == sid && |
| 2291 | existing_start_position == start_position) { |
| 2292 | break; |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | if (i == n_indexes) { |
| 2297 | GRN_PTR_PUT(ctx, &(si->index), index); |
| 2298 | GRN_UINT32_PUT(ctx, &(si->sections), sid); |
| 2299 | GRN_FLOAT32_PUT(ctx, &(si->weights), weight); |
| 2300 | GRN_PTR_PUT(ctx, &(si->scorers), scorer); |
| 2301 | GRN_PTR_PUT(ctx, &(si->scorer_args_exprs), scorer_args_expr); |
| 2302 | GRN_UINT32_PUT(ctx, |
| 2303 | &(si->scorer_args_expr_offsets), |
| 2304 | scorer_args_expr_offset); |
| 2305 | GRN_INT32_PUT(ctx, &(si->start_positions), start_position); |
| 2306 | } else { |
| 2307 | GRN_PTR_SET_AT(ctx, &(si->index), i, index); |
| 2308 | GRN_UINT32_SET_AT(ctx, &(si->sections), i, sid); |
| 2309 | GRN_FLOAT32_SET_AT(ctx, &(si->weights), i, weight); |
| 2310 | GRN_PTR_SET_AT(ctx, &(si->scorers), i, scorer); |
| 2311 | GRN_PTR_SET_AT(ctx, &(si->scorer_args_exprs), i, scorer_args_expr); |
| 2312 | GRN_UINT32_SET_AT(ctx, |
| 2313 | &(si->scorer_args_expr_offsets), |
| 2314 | i, |
| 2315 | scorer_args_expr_offset); |
| 2316 | GRN_INT32_SET_AT(ctx, &(si->start_positions), i, start_position); |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | static float |
| 2321 | get_weight(grn_ctx *ctx, grn_expr_code *ec, uint32_t *offset) |
| 2322 | { |
| 2323 | if (ec->modify == 2 && ec[2].op == GRN_OP_STAR && ec[1].value && |
| 2324 | ec[1].value->header.type == GRN_BULK) { |
| 2325 | if (offset) { |
| 2326 | *offset = 2; |
| 2327 | } |
| 2328 | if (ec[1].value->header.domain == GRN_DB_INT32) { |
| 2329 | return (float)GRN_INT32_VALUE(ec[1].value); |
| 2330 | } else if (ec[1].value->header.domain == GRN_DB_UINT32) { |
| 2331 | return (float)GRN_UINT32_VALUE(ec[1].value); |
| 2332 | } else if (ec[1].value->header.domain == GRN_DB_FLOAT32) { |
| 2333 | return GRN_FLOAT32_VALUE(ec[1].value); |
| 2334 | } else if (ec[1].value->header.domain == GRN_DB_FLOAT) { |
| 2335 | return (float)GRN_FLOAT_VALUE(ec[1].value); |
| 2336 | } else { |
| 2337 | float weight = 1.0; |
| 2338 | grn_obj weight_buffer; |
| 2339 | GRN_FLOAT32_INIT(&weight_buffer, 0); |
| 2340 | if (!grn_obj_cast(ctx, ec[1].value, &weight_buffer, false)) { |
| 2341 | weight = GRN_FLOAT32_VALUE(&weight_buffer); |
| 2342 | } |
| 2343 | grn_obj_unlink(ctx, &weight_buffer); |
| 2344 | return weight; |
| 2345 | } |
| 2346 | } else { |
| 2347 | if (offset) { |
| 2348 | *offset = 0; |
| 2349 | } |
| 2350 | return 1.0; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | scan_info * |
| 2355 | grn_scan_info_open(grn_ctx *ctx, int start) |
| 2356 | { |
| 2357 | scan_info *si; |
| 2358 | SI_ALLOC_RAW(si, start); |
| 2359 | return si; |
| 2360 | } |
| 2361 | |
| 2362 | void |
| 2363 | grn_scan_info_close(grn_ctx *ctx, scan_info *si) |
| 2364 | { |
| 2365 | SI_FREE(si); |
| 2366 | } |
| 2367 | |
| 2368 | void |
| 2369 | grn_scan_info_put_index(grn_ctx *ctx, |
| 2370 | scan_info *si, |
| 2371 | grn_obj *index, |
| 2372 | uint32_t sid, |
| 2373 | int32_t start_position, |
| 2374 | float weight, |
| 2375 | grn_obj *scorer, |
| 2376 | grn_obj *scorer_args_expr, |
| 2377 | uint32_t scorer_args_expr_offset) |
| 2378 | { |
| 2379 | scan_info_put_index(ctx, |
| 2380 | si, |
| 2381 | index, |
| 2382 | sid, |
| 2383 | start_position, |
| 2384 | weight, |
| 2385 | scorer, |
| 2386 | scorer_args_expr, |
| 2387 | scorer_args_expr_offset); |
| 2388 | } |
| 2389 | |
| 2390 | scan_info ** |
| 2391 | grn_scan_info_put_logical_op(grn_ctx *ctx, |
| 2392 | scan_info **sis, |
| 2393 | int *ip, |
| 2394 | grn_operator op, |
| 2395 | int start, |
| 2396 | float weight_factor) |
| 2397 | { |
| 2398 | return put_logical_op(ctx, sis, ip, op, start, weight_factor); |
| 2399 | } |
| 2400 | |
| 2401 | float |
| 2402 | grn_expr_code_get_weight(grn_ctx *ctx, grn_expr_code *ec, uint32_t *offset) |
| 2403 | { |
| 2404 | return get_weight(ctx, ec, offset); |
| 2405 | } |
| 2406 | |
| 2407 | int |
| 2408 | grn_scan_info_get_flags(scan_info *si) |
| 2409 | { |
| 2410 | return si->flags; |
| 2411 | } |
| 2412 | |
| 2413 | void |
| 2414 | grn_scan_info_set_flags(scan_info *si, int flags) |
| 2415 | { |
| 2416 | si->flags = flags; |
| 2417 | } |
| 2418 | |
| 2419 | grn_operator |
| 2420 | grn_scan_info_get_logical_op(scan_info *si) |
| 2421 | { |
| 2422 | return si->logical_op; |
| 2423 | } |
| 2424 | |
| 2425 | void |
| 2426 | grn_scan_info_set_logical_op(scan_info *si, grn_operator logical_op) |
| 2427 | { |
| 2428 | si->logical_op = logical_op; |
| 2429 | } |
| 2430 | |
| 2431 | grn_operator |
| 2432 | grn_scan_info_get_op(scan_info *si) |
| 2433 | { |
| 2434 | return si->op; |
| 2435 | } |
| 2436 | |
| 2437 | void |
| 2438 | grn_scan_info_set_op(scan_info *si, grn_operator op) |
| 2439 | { |
| 2440 | si->op = op; |
| 2441 | } |
| 2442 | |
| 2443 | float |
| 2444 | grn_scan_info_get_weight_factor(scan_info *si) |
| 2445 | { |
| 2446 | return si->weight_factor; |
| 2447 | } |
| 2448 | |
| 2449 | void |
| 2450 | grn_scan_info_set_weight_factor(scan_info *si, float factor) |
| 2451 | { |
| 2452 | si->weight_factor = factor; |
| 2453 | } |
| 2454 | |
| 2455 | void |
| 2456 | grn_scan_info_set_end(scan_info *si, uint32_t end) |
| 2457 | { |
| 2458 | si->end = end; |
| 2459 | } |
| 2460 | |
| 2461 | void |
| 2462 | grn_scan_info_set_query(scan_info *si, grn_obj *query) |
| 2463 | { |
| 2464 | si->query = query; |
| 2465 | } |
| 2466 | |
| 2467 | int |
| 2468 | grn_scan_info_get_max_interval(scan_info *si) |
| 2469 | { |
| 2470 | return si->max_interval; |
| 2471 | } |
| 2472 | |
| 2473 | void |
| 2474 | grn_scan_info_set_max_interval(scan_info *si, int max_interval) |
| 2475 | { |
| 2476 | si->max_interval = max_interval; |
| 2477 | } |
| 2478 | |
| 2479 | int |
| 2480 | grn_scan_info_get_additional_last_interval(scan_info *si) |
| 2481 | { |
| 2482 | return si->additional_last_interval; |
| 2483 | } |
| 2484 | |
| 2485 | void |
| 2486 | grn_scan_info_set_additional_last_interval(scan_info *si, |
| 2487 | int additional_last_interval) |
| 2488 | { |
| 2489 | si->additional_last_interval = additional_last_interval; |
| 2490 | } |
| 2491 | |
| 2492 | grn_obj * |
| 2493 | grn_scan_info_get_max_element_intervals(scan_info *si) |
| 2494 | { |
| 2495 | return &(si->max_element_intervals); |
| 2496 | } |
| 2497 | |
| 2498 | void |
| 2499 | grn_scan_info_set_max_element_intervals(grn_ctx *ctx, |
| 2500 | scan_info *si, |
| 2501 | grn_obj *max_element_intervals) |
| 2502 | { |
| 2503 | GRN_BULK_REWIND(&(si->max_element_intervals)); |
| 2504 | if (!max_element_intervals) { |
| 2505 | return; |
| 2506 | } |
| 2507 | grn_uvector_copy(ctx, max_element_intervals, &(si->max_element_intervals)); |
| 2508 | } |
| 2509 | |
| 2510 | int |
| 2511 | grn_scan_info_get_min_interval(scan_info *si) |
| 2512 | { |
| 2513 | return si->min_interval; |
| 2514 | } |
| 2515 | |
| 2516 | void |
| 2517 | grn_scan_info_set_min_interval(scan_info *si, int min_interval) |
| 2518 | { |
| 2519 | si->min_interval = min_interval; |
| 2520 | } |
| 2521 | |
| 2522 | int |
| 2523 | grn_scan_info_get_similarity_threshold(scan_info *si) |
| 2524 | { |
| 2525 | return si->similarity_threshold; |
| 2526 | } |
| 2527 | |
| 2528 | void |
| 2529 | grn_scan_info_set_similarity_threshold(scan_info *si, int similarity_threshold) |
| 2530 | { |
| 2531 | si->similarity_threshold = similarity_threshold; |
| 2532 | } |
| 2533 | |
| 2534 | int |
| 2535 | grn_scan_info_get_quorum_threshold(scan_info *si) |
| 2536 | { |
| 2537 | return si->quorum_threshold; |
| 2538 | } |
| 2539 | |
| 2540 | void |
| 2541 | grn_scan_info_set_quorum_threshold(scan_info *si, int quorum_threshold) |
| 2542 | { |
| 2543 | si->quorum_threshold = quorum_threshold; |
| 2544 | } |
| 2545 | |
| 2546 | bool |
| 2547 | grn_scan_info_push_arg(grn_ctx *ctx, scan_info *si, grn_obj *arg) |
| 2548 | { |
| 2549 | if (si->nargs >= si->max_nargs) { |
| 2550 | grn_obj **args; |
| 2551 | int32_t max_nargs = si->max_nargs * 2; |
| 2552 | if (si->args == si->initial_args) { |
| 2553 | args = GRN_MALLOCN(grn_obj *, max_nargs); |
| 2554 | if (args == NULL) { |
| 2555 | return false; |
| 2556 | } |
| 2557 | grn_memcpy(args, si->args, sizeof(grn_obj *) * si->nargs); |
| 2558 | } else { |
| 2559 | args = (grn_obj **)GRN_REALLOC(si->args, sizeof(grn_obj *) * max_nargs); |
| 2560 | if (args == NULL) { |
| 2561 | return false; |
| 2562 | } |
| 2563 | } |
| 2564 | si->args = args; |
| 2565 | si->max_nargs = max_nargs; |
| 2566 | } |
| 2567 | |
| 2568 | si->args[si->nargs++] = arg; |
| 2569 | return true; |
| 2570 | } |
| 2571 | |
| 2572 | grn_obj * |
| 2573 | grn_scan_info_get_arg(grn_ctx *ctx, scan_info *si, int i) |
| 2574 | { |
| 2575 | if (i >= si->nargs) { |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | return si->args[i]; |
| 2579 | } |
| 2580 | |
| 2581 | static uint32_t |
| 2582 | scan_info_build_match_expr_codes_find_index(grn_ctx *ctx, |
| 2583 | scan_info *si, |
| 2584 | grn_expr *expr, |
| 2585 | uint32_t i, |
| 2586 | grn_obj **index, |
| 2587 | uint32_t *sid, |
| 2588 | int32_t *start_position) |
| 2589 | { |
| 2590 | grn_expr_code *ec; |
| 2591 | uint32_t offset = 1; |
| 2592 | grn_index_datum index_datum; |
| 2593 | unsigned int n_index_data = 0; |
| 2594 | bool use_start_position = false; |
| 2595 | bool may_use_get_member = false; |
| 2596 | |
| 2597 | ec = &(expr->codes[i]); |
| 2598 | switch (ec->value->header.type) { |
| 2599 | case GRN_ACCESSOR: |
| 2600 | n_index_data = |
| 2601 | grn_column_find_index_data(ctx, ec->value, si->op, &index_datum, 1); |
| 2602 | if (n_index_data > 0) { |
| 2603 | grn_accessor *a = (grn_accessor *)(ec->value); |
| 2604 | *sid = index_datum.section; |
| 2605 | if (a->next && a->obj != index_datum.index) { |
| 2606 | *index = ec->value; |
| 2607 | } else { |
| 2608 | *index = index_datum.index; |
| 2609 | } |
| 2610 | } |
| 2611 | break; |
| 2612 | case GRN_COLUMN_FIX_SIZE: |
| 2613 | case GRN_COLUMN_VAR_SIZE: |
| 2614 | n_index_data = |
| 2615 | grn_column_find_index_data(ctx, ec->value, si->op, &index_datum, 1); |
| 2616 | if (n_index_data > 0) { |
| 2617 | *index = index_datum.index; |
| 2618 | *sid = index_datum.section; |
| 2619 | if (grn_obj_is_vector_column(ctx, ec->value) && *sid == 0) { |
| 2620 | may_use_get_member = true; |
| 2621 | grn_obj *lexicon = grn_ctx_at(ctx, (*index)->header.domain); |
| 2622 | if (!grn_table_have_tokenizer(ctx, lexicon)) { |
| 2623 | use_start_position = true; |
| 2624 | } |
| 2625 | grn_obj_unref(ctx, lexicon); |
| 2626 | } |
| 2627 | } |
| 2628 | break; |
| 2629 | case GRN_COLUMN_INDEX: |
| 2630 | *index = ec->value; |
| 2631 | grn_obj_refer(ctx, *index); |
| 2632 | may_use_get_member = true; |
| 2633 | { |
| 2634 | grn_obj source_ids; |
| 2635 | GRN_UINT32_INIT(&source_ids, GRN_OBJ_VECTOR); |
| 2636 | grn_obj_get_info(ctx, *index, GRN_INFO_SOURCE, &source_ids); |
| 2637 | if (GRN_UINT32_VECTOR_SIZE(&source_ids) == 1) { |
| 2638 | grn_obj *source = grn_ctx_at(ctx, GRN_UINT32_VALUE_AT(&source_ids, 0)); |
| 2639 | if (grn_obj_is_vector_column(ctx, source)) { |
| 2640 | grn_obj *lexicon = grn_ctx_at(ctx, (*index)->header.domain); |
| 2641 | if (!grn_table_have_tokenizer(ctx, lexicon)) { |
| 2642 | use_start_position = true; |
| 2643 | } |
| 2644 | grn_obj_unref(ctx, lexicon); |
| 2645 | } |
| 2646 | grn_obj_unref(ctx, source); |
| 2647 | } |
| 2648 | } |
| 2649 | break; |
| 2650 | default: |
| 2651 | break; |
| 2652 | } |
| 2653 | |
| 2654 | if (may_use_get_member) { |
| 2655 | uint32_t n_rest_codes = expr->codes_curr - i; |
| 2656 | if (n_rest_codes >= 2 && ec[1].value && |
| 2657 | (ec[1].value->header.domain == GRN_DB_INT32 || |
| 2658 | ec[1].value->header.domain == GRN_DB_UINT32) && |
| 2659 | ec[2].op == GRN_OP_GET_MEMBER) { |
| 2660 | if (ec[1].value->header.domain == GRN_DB_INT32) { |
| 2661 | int32_t member = GRN_INT32_VALUE(ec[1].value); |
| 2662 | if (use_start_position) { |
| 2663 | *start_position = member; |
| 2664 | } else { |
| 2665 | *sid = member + 1; |
| 2666 | } |
| 2667 | } else { |
| 2668 | uint32_t member = GRN_UINT32_VALUE(ec[1].value); |
| 2669 | if (use_start_position) { |
| 2670 | *start_position = (int32_t)member; |
| 2671 | } else { |
| 2672 | *sid = member + 1; |
| 2673 | } |
| 2674 | } |
| 2675 | offset += 2; |
| 2676 | } |
| 2677 | } |
| 2678 | |
| 2679 | return offset; |
| 2680 | } |
| 2681 | |
| 2682 | static uint32_t |
| 2683 | scan_info_build_match_expr_codes( |
| 2684 | grn_ctx *ctx, scan_info *si, grn_expr *expr, uint32_t i, float weight) |
| 2685 | { |
| 2686 | grn_expr_code *ec; |
| 2687 | grn_obj *index = NULL; |
| 2688 | uint32_t sid = 0; |
| 2689 | int32_t start_position = -1; |
| 2690 | uint32_t offset = 0; |
| 2691 | |
| 2692 | ec = &(expr->codes[i]); |
| 2693 | if (!ec->value) { |
| 2694 | return i + 1; |
| 2695 | } |
| 2696 | |
| 2697 | switch (ec->value->header.type) { |
| 2698 | case GRN_ACCESSOR: |
| 2699 | case GRN_COLUMN_FIX_SIZE: |
| 2700 | case GRN_COLUMN_VAR_SIZE: |
| 2701 | case GRN_COLUMN_INDEX: |
| 2702 | offset = scan_info_build_match_expr_codes_find_index(ctx, |
| 2703 | si, |
| 2704 | expr, |
| 2705 | i, |
| 2706 | &index, |
| 2707 | &sid, |
| 2708 | &start_position); |
| 2709 | i += offset - 1; |
| 2710 | if (index) { |
| 2711 | if (ec->value->header.type == GRN_ACCESSOR) { |
| 2712 | si->flags |= SCAN_ACCESSOR; |
| 2713 | } |
| 2714 | scan_info_put_index(ctx, |
| 2715 | si, |
| 2716 | index, |
| 2717 | sid, |
| 2718 | start_position, |
| 2719 | get_weight(ctx, &(expr->codes[i]), &offset) + weight, |
| 2720 | NULL, |
| 2721 | NULL, |
| 2722 | 0); |
| 2723 | i += offset; |
| 2724 | } |
| 2725 | break; |
| 2726 | case GRN_PROC: |
| 2727 | if (!grn_obj_is_scorer_proc(ctx, ec->value)) { |
| 2728 | grn_obj inspected; |
| 2729 | GRN_TEXT_INIT(&inspected, 0); |
| 2730 | grn_inspect(ctx, &inspected, ec->value); |
| 2731 | ERR(GRN_INVALID_ARGUMENT, |
| 2732 | "procedure must be scorer: <%.*s>", |
| 2733 | (int)GRN_TEXT_LEN(&inspected), |
| 2734 | GRN_TEXT_VALUE(&inspected)); |
| 2735 | GRN_OBJ_FIN(ctx, &inspected); |
| 2736 | return expr->codes_curr; |
| 2737 | } |
| 2738 | i++; |
| 2739 | offset = scan_info_build_match_expr_codes_find_index(ctx, |
| 2740 | si, |
| 2741 | expr, |
| 2742 | i, |
| 2743 | &index, |
| 2744 | &sid, |
| 2745 | &start_position); |
| 2746 | i += offset; |
| 2747 | if (index) { |
| 2748 | uint32_t scorer_args_expr_offset = 0; |
| 2749 | if (expr->codes[i].op != GRN_OP_CALL) { |
| 2750 | scorer_args_expr_offset = i; |
| 2751 | } |
| 2752 | while (i < expr->codes_curr && expr->codes[i].op != GRN_OP_CALL) { |
| 2753 | i++; |
| 2754 | } |
| 2755 | scan_info_put_index(ctx, |
| 2756 | si, |
| 2757 | index, |
| 2758 | sid, |
| 2759 | start_position, |
| 2760 | get_weight(ctx, &(expr->codes[i]), &offset) + weight, |
| 2761 | ec->value, |
| 2762 | (grn_obj *)expr, |
| 2763 | scorer_args_expr_offset); |
| 2764 | i += offset; |
| 2765 | } |
| 2766 | break; |
| 2767 | default: |
| 2768 | { |
| 2769 | grn_obj inspected; |
| 2770 | GRN_TEXT_INIT(&inspected, 0); |
| 2771 | grn_inspect(ctx, &inspected, ec->value); |
| 2772 | |
| 2773 | ERR(GRN_INVALID_ARGUMENT, |
| 2774 | "invalid match target: %.*s", |
| 2775 | (int)GRN_TEXT_LEN(&inspected), |
| 2776 | GRN_TEXT_VALUE(&inspected)); |
| 2777 | |
| 2778 | GRN_OBJ_FIN(ctx, &inspected); |
| 2779 | return expr->codes_curr; |
| 2780 | } |
| 2781 | break; |
| 2782 | } |
| 2783 | |
| 2784 | return i + 1; |
| 2785 | } |
| 2786 | |
| 2787 | static void |
| 2788 | scan_info_build_match_expr(grn_ctx *ctx, |
| 2789 | scan_info *si, |
| 2790 | grn_expr *expr, |
| 2791 | float weight) |
| 2792 | { |
| 2793 | uint32_t i; |
| 2794 | i = 0; |
| 2795 | while (i < expr->codes_curr) { |
| 2796 | i = scan_info_build_match_expr_codes(ctx, si, expr, i, weight); |
| 2797 | if (ctx->rc != GRN_SUCCESS) { |
| 2798 | return; |
| 2799 | } |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | static bool |
| 2804 | is_index_searchable_regexp(grn_ctx *ctx, grn_obj *regexp) |
| 2805 | { |
| 2806 | const char *all_off_options = "?-mix:"; |
| 2807 | size_t all_off_options_length = strlen(all_off_options); |
| 2808 | const char *regexp_raw; |
| 2809 | const char *regexp_raw_end; |
| 2810 | bool escaping = false; |
| 2811 | bool in_paren = false; |
| 2812 | bool dot = false; |
| 2813 | |
| 2814 | if (!(regexp->header.domain == GRN_DB_SHORT_TEXT || |
| 2815 | regexp->header.domain == GRN_DB_TEXT || |
| 2816 | regexp->header.domain == GRN_DB_LONG_TEXT)) { |
| 2817 | return false; |
| 2818 | } |
| 2819 | |
| 2820 | regexp_raw = GRN_TEXT_VALUE(regexp); |
| 2821 | regexp_raw_end = regexp_raw + GRN_TEXT_LEN(regexp); |
| 2822 | |
| 2823 | while (regexp_raw < regexp_raw_end) { |
| 2824 | unsigned int char_len; |
| 2825 | |
| 2826 | char_len = grn_charlen(ctx, regexp_raw, regexp_raw_end); |
| 2827 | if (char_len == 0) { |
| 2828 | return false; |
| 2829 | } |
| 2830 | |
| 2831 | if (char_len == 1) { |
| 2832 | if (escaping) { |
| 2833 | escaping = false; |
| 2834 | switch (regexp_raw[0]) { |
| 2835 | case 'Z': |
| 2836 | case 'b': |
| 2837 | case 'B': |
| 2838 | case 'd': |
| 2839 | case 'D': |
| 2840 | case 'h': |
| 2841 | case 'H': |
| 2842 | case 'p': |
| 2843 | case 's': |
| 2844 | case 'S': |
| 2845 | case 'w': |
| 2846 | case 'W': |
| 2847 | case 'X': |
| 2848 | case 'k': |
| 2849 | case 'g': |
| 2850 | case '1': |
| 2851 | case '2': |
| 2852 | case '3': |
| 2853 | case '4': |
| 2854 | case '5': |
| 2855 | case '6': |
| 2856 | case '7': |
| 2857 | case '8': |
| 2858 | case '9': |
| 2859 | return false; |
| 2860 | default: |
| 2861 | break; |
| 2862 | } |
| 2863 | } else { |
| 2864 | switch (regexp_raw[0]) { |
| 2865 | case '(': |
| 2866 | if (in_paren) { |
| 2867 | return false; |
| 2868 | } |
| 2869 | const char *options = regexp_raw + 1; |
| 2870 | if ((size_t)(regexp_raw_end - options) >= all_off_options_length && |
| 2871 | memcmp(options, all_off_options, all_off_options_length) == 0) { |
| 2872 | in_paren = true; |
| 2873 | regexp_raw += all_off_options_length; |
| 2874 | continue; |
| 2875 | } |
| 2876 | return false; |
| 2877 | case ')': |
| 2878 | if (in_paren) { |
| 2879 | in_paren = false; |
| 2880 | } else { |
| 2881 | return false; |
| 2882 | } |
| 2883 | break; |
| 2884 | case '.': |
| 2885 | dot = true; |
| 2886 | break; |
| 2887 | case '*': |
| 2888 | if (!dot) { |
| 2889 | return false; |
| 2890 | } |
| 2891 | if (!grn_scan_info_regexp_dot_asterisk_enable) { |
| 2892 | return false; |
| 2893 | } |
| 2894 | dot = false; |
| 2895 | break; |
| 2896 | case '[': |
| 2897 | case ']': |
| 2898 | case '|': |
| 2899 | case '?': |
| 2900 | case '+': |
| 2901 | case '{': |
| 2902 | case '}': |
| 2903 | case '^': |
| 2904 | case '$': |
| 2905 | return false; |
| 2906 | case '\\': |
| 2907 | escaping = true; |
| 2908 | break; |
| 2909 | default: |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | } else { |
| 2914 | escaping = false; |
| 2915 | } |
| 2916 | |
| 2917 | regexp_raw += char_len; |
| 2918 | } |
| 2919 | |
| 2920 | if (in_paren) { |
| 2921 | return false; |
| 2922 | } |
| 2923 | |
| 2924 | return true; |
| 2925 | } |
| 2926 | |
| 2927 | static void |
| 2928 | scan_info_build_match(grn_ctx *ctx, |
| 2929 | scan_info *si, |
| 2930 | int32_t start_position, |
| 2931 | float weight) |
| 2932 | { |
| 2933 | grn_obj **p, **pe; |
| 2934 | |
| 2935 | if (si->op == GRN_OP_REGEXP) { |
| 2936 | p = si->args; |
| 2937 | pe = si->args + si->nargs; |
| 2938 | for (; p < pe; p++) { |
| 2939 | if ((*p)->header.type == GRN_BULK && |
| 2940 | !is_index_searchable_regexp(ctx, *p)) { |
| 2941 | return; |
| 2942 | } |
| 2943 | } |
| 2944 | } |
| 2945 | |
| 2946 | p = si->args; |
| 2947 | pe = si->args + si->nargs; |
| 2948 | for (; p < pe; p++) { |
| 2949 | if ((*p)->header.type == GRN_EXPR) { |
| 2950 | scan_info_build_match_expr(ctx, si, (grn_expr *)(*p), weight); |
| 2951 | } else if ((*p)->header.type == GRN_COLUMN_INDEX) { |
| 2952 | grn_obj *index = grn_ctx_at(ctx, grn_obj_id(ctx, (*p))); |
| 2953 | scan_info_put_index(ctx, |
| 2954 | si, |
| 2955 | index, |
| 2956 | 0, |
| 2957 | start_position, |
| 2958 | 1 + weight, |
| 2959 | NULL, |
| 2960 | NULL, |
| 2961 | 0); |
| 2962 | } else if (grn_obj_is_proc(ctx, *p)) { |
| 2963 | break; |
| 2964 | } else if (GRN_DB_OBJP(*p)) { |
| 2965 | grn_index_datum index_datum; |
| 2966 | unsigned int n_index_data; |
| 2967 | n_index_data = |
| 2968 | grn_column_find_index_data(ctx, *p, si->op, &index_datum, 1); |
| 2969 | if (n_index_data > 0) { |
| 2970 | scan_info_put_index(ctx, |
| 2971 | si, |
| 2972 | index_datum.index, |
| 2973 | index_datum.section, |
| 2974 | start_position, |
| 2975 | 1 + weight, |
| 2976 | NULL, |
| 2977 | NULL, |
| 2978 | 0); |
| 2979 | } |
| 2980 | } else if (GRN_ACCESSORP(*p)) { |
| 2981 | grn_index_datum index_datum; |
| 2982 | unsigned int n_index_data; |
| 2983 | si->flags |= SCAN_ACCESSOR; |
| 2984 | n_index_data = |
| 2985 | grn_column_find_index_data(ctx, *p, si->op, &index_datum, 1); |
| 2986 | if (n_index_data > 0) { |
| 2987 | grn_obj *index; |
| 2988 | if (((grn_accessor *)(*p))->next) { |
| 2989 | index = *p; |
| 2990 | if (index != index_datum.index) { |
| 2991 | grn_obj_unref(ctx, index_datum.index); |
| 2992 | } |
| 2993 | } else { |
| 2994 | index = index_datum.index; |
| 2995 | } |
| 2996 | scan_info_put_index(ctx, |
| 2997 | si, |
| 2998 | index, |
| 2999 | index_datum.section, |
| 3000 | start_position, |
| 3001 | 1 + weight, |
| 3002 | NULL, |
| 3003 | NULL, |
| 3004 | 0); |
| 3005 | } |
| 3006 | } else { |
| 3007 | switch (si->op) { |
| 3008 | case GRN_OP_NEAR: |
| 3009 | case GRN_OP_NEAR_NO_OFFSET: |
| 3010 | if (si->nargs >= 3 && *p == si->args[2] && |
| 3011 | (*p)->header.domain == GRN_DB_INT32) { |
| 3012 | si->max_interval = GRN_INT32_VALUE(*p); |
| 3013 | } else if (si->nargs >= 4 && *p == si->args[3] && |
| 3014 | (*p)->header.domain == GRN_DB_INT32 && |
| 3015 | grn_obj_is_uvector(ctx, *p)) { |
| 3016 | grn_uvector_copy(ctx, *p, &(si->max_element_intervals)); |
| 3017 | } else if (si->nargs == 5 && *p == si->args[4] && |
| 3018 | (*p)->header.domain == GRN_DB_INT32) { |
| 3019 | si->min_interval = GRN_INT32_VALUE(*p); |
| 3020 | } else { |
| 3021 | si->query = *p; |
| 3022 | } |
| 3023 | break; |
| 3024 | case GRN_OP_NEAR_PHRASE: |
| 3025 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 3026 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 3027 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 3028 | if (si->nargs >= 3 && *p == si->args[2] && |
| 3029 | (*p)->header.domain == GRN_DB_INT32) { |
| 3030 | si->max_interval = GRN_INT32_VALUE(*p); |
| 3031 | si->additional_last_interval = DEFAULT_ADDITIONAL_LAST_INTERVAL; |
| 3032 | } else if (si->nargs >= 4 && *p == si->args[3] && |
| 3033 | (*p)->header.domain == GRN_DB_INT32) { |
| 3034 | si->additional_last_interval = GRN_INT32_VALUE(*p); |
| 3035 | } else if (si->nargs >= 5 && *p == si->args[4] && |
| 3036 | (*p)->header.domain == GRN_DB_INT32 && |
| 3037 | grn_obj_is_uvector(ctx, *p)) { |
| 3038 | grn_uvector_copy(ctx, *p, &(si->max_element_intervals)); |
| 3039 | } else if (si->nargs == 6 && *p == si->args[5] && |
| 3040 | (*p)->header.domain == GRN_DB_INT32) { |
| 3041 | si->min_interval = GRN_INT32_VALUE(*p); |
| 3042 | } else { |
| 3043 | si->query = *p; |
| 3044 | } |
| 3045 | break; |
| 3046 | case GRN_OP_SIMILAR: |
| 3047 | if (si->nargs == 3 && *p == si->args[2] && |
| 3048 | (*p)->header.domain == GRN_DB_INT32) { |
| 3049 | si->similarity_threshold = GRN_INT32_VALUE(*p); |
| 3050 | } else { |
| 3051 | si->query = *p; |
| 3052 | } |
| 3053 | break; |
| 3054 | case GRN_OP_QUORUM: |
| 3055 | if (si->nargs == 3 && *p == si->args[2] && |
| 3056 | (*p)->header.domain == GRN_DB_INT32) { |
| 3057 | si->quorum_threshold = GRN_INT32_VALUE(*p); |
| 3058 | } else { |
| 3059 | si->query = *p; |
| 3060 | } |
| 3061 | break; |
| 3062 | default: |
| 3063 | si->query = *p; |
| 3064 | break; |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | } |
| 3069 | |
| 3070 | static bool |
| 3071 | grn_scan_info_build_full_not(grn_ctx *ctx, |
| 3072 | scan_info **sis, |
| 3073 | int *i, |
| 3074 | grn_expr_code *codes, |
| 3075 | grn_expr_code *code, |
| 3076 | grn_expr_code *code_end, |
| 3077 | grn_operator *next_code_op) |
| 3078 | { |
| 3079 | scan_info *last_si; |
| 3080 | |
| 3081 | if (*i == 0) { |
| 3082 | return true; |
| 3083 | } |
| 3084 | |
| 3085 | last_si = sis[*i - 1]; |
| 3086 | switch (last_si->op) { |
| 3087 | case GRN_OP_LESS: |
| 3088 | last_si->op = GRN_OP_GREATER_EQUAL; |
| 3089 | last_si->end++; |
| 3090 | break; |
| 3091 | case GRN_OP_LESS_EQUAL: |
| 3092 | last_si->op = GRN_OP_GREATER; |
| 3093 | last_si->end++; |
| 3094 | break; |
| 3095 | case GRN_OP_GREATER: |
| 3096 | last_si->op = GRN_OP_LESS_EQUAL; |
| 3097 | last_si->end++; |
| 3098 | break; |
| 3099 | case GRN_OP_GREATER_EQUAL: |
| 3100 | last_si->op = GRN_OP_LESS; |
| 3101 | last_si->end++; |
| 3102 | break; |
| 3103 | case GRN_OP_NOT_EQUAL: |
| 3104 | last_si->op = GRN_OP_EQUAL; |
| 3105 | last_si->end++; |
| 3106 | break; |
| 3107 | default: |
| 3108 | if (*i == 1) { |
| 3109 | if (GRN_BULK_VSIZE(&(last_si->index)) > 0) { |
| 3110 | scan_info *all_records_si = NULL; |
| 3111 | SI_ALLOC_RAW(all_records_si, 0); |
| 3112 | if (!all_records_si) { |
| 3113 | return false; |
| 3114 | } |
| 3115 | all_records_si->op = GRN_OP_CALL; |
| 3116 | grn_scan_info_push_arg(ctx, |
| 3117 | all_records_si, |
| 3118 | grn_ctx_get(ctx, "all_records", -1)); |
| 3119 | last_si->logical_op = GRN_OP_AND_NOT; |
| 3120 | last_si->flags &= ~SCAN_PUSH; |
| 3121 | sis[*i] = sis[*i - 1]; |
| 3122 | sis[*i - 1] = all_records_si; |
| 3123 | (*i)++; |
| 3124 | } else { |
| 3125 | if (last_si->op == GRN_OP_EQUAL) { |
| 3126 | last_si->op = GRN_OP_NOT_EQUAL; |
| 3127 | last_si->end++; |
| 3128 | } else { |
| 3129 | return false; |
| 3130 | } |
| 3131 | } |
| 3132 | } else { |
| 3133 | grn_expr_code *next_code = code + 1; |
| 3134 | |
| 3135 | if (next_code >= code_end) { |
| 3136 | return false; |
| 3137 | } |
| 3138 | |
| 3139 | switch (next_code->op) { |
| 3140 | case GRN_OP_AND: |
| 3141 | *next_code_op = GRN_OP_AND_NOT; |
| 3142 | break; |
| 3143 | case GRN_OP_AND_NOT: |
| 3144 | *next_code_op = GRN_OP_AND; |
| 3145 | break; |
| 3146 | case GRN_OP_OR: |
| 3147 | { |
| 3148 | scan_info *all_records_si = NULL; |
| 3149 | SI_ALLOC_RAW(all_records_si, 0); |
| 3150 | if (!all_records_si) { |
| 3151 | return false; |
| 3152 | } |
| 3153 | all_records_si->op = GRN_OP_CALL; |
| 3154 | grn_scan_info_push_arg(ctx, |
| 3155 | all_records_si, |
| 3156 | grn_ctx_get(ctx, "all_records", -1)); |
| 3157 | sis[*i] = sis[*i - 1]; |
| 3158 | sis[*i - 1] = all_records_si; |
| 3159 | (*i)++; |
| 3160 | put_logical_op(ctx, sis, i, GRN_OP_AND_NOT, code - codes, 1.0); |
| 3161 | } |
| 3162 | break; |
| 3163 | default: |
| 3164 | return false; |
| 3165 | break; |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | return true; |
| 3171 | } |
| 3172 | |
| 3173 | static float |
| 3174 | grn_weight_bulk_get(grn_ctx *ctx, grn_obj *bulk) |
| 3175 | { |
| 3176 | if (!bulk) { |
| 3177 | return 0.0; |
| 3178 | } |
| 3179 | if (bulk->header.domain == GRN_DB_INT32) { |
| 3180 | return (float)GRN_INT32_VALUE(bulk); |
| 3181 | } else if (bulk->header.domain == GRN_DB_UINT32) { |
| 3182 | return (float)GRN_UINT32_VALUE(bulk); |
| 3183 | } else if (bulk->header.domain == GRN_DB_INT64) { |
| 3184 | return (float)GRN_INT64_VALUE(bulk); |
| 3185 | } else if (bulk->header.domain == GRN_DB_UINT64) { |
| 3186 | return (float)GRN_UINT64_VALUE(bulk); |
| 3187 | } else if (bulk->header.domain == GRN_DB_FLOAT32) { |
| 3188 | return GRN_FLOAT32_VALUE(bulk); |
| 3189 | } else if (bulk->header.domain == GRN_DB_FLOAT) { |
| 3190 | return (float)GRN_FLOAT_VALUE(bulk); |
| 3191 | } else { |
| 3192 | return 0.0; |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | static scan_info ** |
| 3197 | grn_scan_info_build_full( |
| 3198 | grn_ctx *ctx, grn_obj *expr, int *n, grn_operator op, bool record_exist) |
| 3199 | { |
| 3200 | grn_obj *var; |
| 3201 | scan_stat stat; |
| 3202 | int i, m = 0, o = 0; |
| 3203 | int n_nots = 0; |
| 3204 | scan_info **sis, *si = NULL; |
| 3205 | grn_expr_code *c, *ce; |
| 3206 | grn_expr *e = (grn_expr *)expr; |
| 3207 | grn_operator next_code_op; |
| 3208 | |
| 3209 | if (!(var = grn_expr_get_var_by_offset(ctx, expr, 0))) { |
| 3210 | return NULL; |
| 3211 | } |
| 3212 | for (stat = SCAN_START, c = e->codes, ce = &e->codes[e->codes_curr]; c < ce; |
| 3213 | c++) { |
| 3214 | switch (c->op) { |
| 3215 | case GRN_OP_MATCH: |
| 3216 | case GRN_OP_NEAR: |
| 3217 | case GRN_OP_NEAR_NO_OFFSET: |
| 3218 | case GRN_OP_NEAR_PHRASE: |
| 3219 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 3220 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 3221 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 3222 | case GRN_OP_SIMILAR: |
| 3223 | case GRN_OP_PREFIX: |
| 3224 | case GRN_OP_SUFFIX: |
| 3225 | case GRN_OP_EQUAL: |
| 3226 | case GRN_OP_NOT_EQUAL: |
| 3227 | case GRN_OP_LESS: |
| 3228 | case GRN_OP_GREATER: |
| 3229 | case GRN_OP_LESS_EQUAL: |
| 3230 | case GRN_OP_GREATER_EQUAL: |
| 3231 | case GRN_OP_GEO_WITHINP5: |
| 3232 | case GRN_OP_GEO_WITHINP6: |
| 3233 | case GRN_OP_GEO_WITHINP8: |
| 3234 | case GRN_OP_TERM_EXTRACT: |
| 3235 | case GRN_OP_REGEXP: |
| 3236 | case GRN_OP_QUORUM: |
| 3237 | if (stat < SCAN_COL1 || SCAN_CONST < stat) { |
| 3238 | return NULL; |
| 3239 | } |
| 3240 | stat = SCAN_START; |
| 3241 | m++; |
| 3242 | break; |
| 3243 | case GRN_OP_BITWISE_OR: |
| 3244 | case GRN_OP_BITWISE_XOR: |
| 3245 | case GRN_OP_BITWISE_AND: |
| 3246 | case GRN_OP_BITWISE_NOT: |
| 3247 | case GRN_OP_SHIFTL: |
| 3248 | case GRN_OP_SHIFTR: |
| 3249 | case GRN_OP_SHIFTRR: |
| 3250 | case GRN_OP_PLUS: |
| 3251 | case GRN_OP_MINUS: |
| 3252 | case GRN_OP_STAR: |
| 3253 | case GRN_OP_MOD: |
| 3254 | if (stat < SCAN_COL1 || SCAN_CONST < stat) { |
| 3255 | return NULL; |
| 3256 | } |
| 3257 | stat = SCAN_START; |
| 3258 | if (m != o + 1) { |
| 3259 | return NULL; |
| 3260 | } |
| 3261 | break; |
| 3262 | case GRN_OP_AND: |
| 3263 | case GRN_OP_OR: |
| 3264 | case GRN_OP_AND_NOT: |
| 3265 | case GRN_OP_ADJUST: |
| 3266 | switch (stat) { |
| 3267 | case SCAN_START: |
| 3268 | o++; |
| 3269 | if (o >= m) { |
| 3270 | return NULL; |
| 3271 | } |
| 3272 | break; |
| 3273 | case SCAN_CONST: |
| 3274 | o++; |
| 3275 | m++; |
| 3276 | if (o >= m) { |
| 3277 | return NULL; |
| 3278 | } |
| 3279 | stat = SCAN_START; |
| 3280 | break; |
| 3281 | default: |
| 3282 | return NULL; |
| 3283 | break; |
| 3284 | } |
| 3285 | break; |
| 3286 | case GRN_OP_PUSH: |
| 3287 | { |
| 3288 | bool is_completed_term = false; |
| 3289 | if (c->modify > 0) { |
| 3290 | switch ((c + c->modify)->op) { |
| 3291 | case GRN_OP_AND: |
| 3292 | case GRN_OP_OR: |
| 3293 | case GRN_OP_AND_NOT: |
| 3294 | case GRN_OP_ADJUST: |
| 3295 | is_completed_term = true; |
| 3296 | break; |
| 3297 | default: |
| 3298 | is_completed_term = false; |
| 3299 | break; |
| 3300 | } |
| 3301 | } |
| 3302 | if (is_completed_term) { |
| 3303 | m++; |
| 3304 | stat = SCAN_START; |
| 3305 | } else { |
| 3306 | stat = (c->value == var) ? SCAN_VAR : SCAN_CONST; |
| 3307 | } |
| 3308 | } |
| 3309 | break; |
| 3310 | case GRN_OP_GET_VALUE: |
| 3311 | switch (stat) { |
| 3312 | case SCAN_START: |
| 3313 | case SCAN_CONST: |
| 3314 | case SCAN_VAR: |
| 3315 | stat = SCAN_COL1; |
| 3316 | break; |
| 3317 | case SCAN_COL1: |
| 3318 | stat = SCAN_COL2; |
| 3319 | break; |
| 3320 | case SCAN_COL2: |
| 3321 | break; |
| 3322 | default: |
| 3323 | return NULL; |
| 3324 | break; |
| 3325 | } |
| 3326 | break; |
| 3327 | case GRN_OP_CALL: |
| 3328 | if ((c->flags & GRN_EXPR_CODE_RELATIONAL_EXPRESSION) || c + 1 == ce) { |
| 3329 | stat = SCAN_START; |
| 3330 | m++; |
| 3331 | } else { |
| 3332 | stat = SCAN_COL2; |
| 3333 | } |
| 3334 | break; |
| 3335 | case GRN_OP_GET_REF: |
| 3336 | switch (stat) { |
| 3337 | case SCAN_START: |
| 3338 | stat = SCAN_COL1; |
| 3339 | break; |
| 3340 | default: |
| 3341 | return NULL; |
| 3342 | break; |
| 3343 | } |
| 3344 | break; |
| 3345 | case GRN_OP_GET_MEMBER: |
| 3346 | switch (stat) { |
| 3347 | case SCAN_CONST: |
| 3348 | { |
| 3349 | grn_expr_code *prev_c = c - 1; |
| 3350 | if (prev_c->value->header.domain < GRN_DB_INT8 || |
| 3351 | prev_c->value->header.domain > GRN_DB_UINT64) { |
| 3352 | return NULL; |
| 3353 | } |
| 3354 | } |
| 3355 | stat = SCAN_COL1; |
| 3356 | break; |
| 3357 | default: |
| 3358 | return NULL; |
| 3359 | break; |
| 3360 | } |
| 3361 | break; |
| 3362 | case GRN_OP_NOT: |
| 3363 | n_nots++; |
| 3364 | break; |
| 3365 | default: |
| 3366 | return NULL; |
| 3367 | break; |
| 3368 | } |
| 3369 | } |
| 3370 | if (stat || m != o + 1) { |
| 3371 | return NULL; |
| 3372 | } |
| 3373 | if (!(sis = GRN_MALLOCN(scan_info *, m + m + o + n_nots))) { |
| 3374 | return NULL; |
| 3375 | } |
| 3376 | |
| 3377 | int32_t start_position = -1; |
| 3378 | next_code_op = (grn_operator)-1; |
| 3379 | for (i = 0, stat = SCAN_START, c = e->codes, ce = &e->codes[e->codes_curr]; |
| 3380 | c < ce; |
| 3381 | c++) { |
| 3382 | grn_operator code_op; |
| 3383 | if (next_code_op == (grn_operator)-1) { |
| 3384 | code_op = c->op; |
| 3385 | } else { |
| 3386 | code_op = next_code_op; |
| 3387 | next_code_op = -1; |
| 3388 | } |
| 3389 | switch (code_op) { |
| 3390 | case GRN_OP_MATCH: |
| 3391 | case GRN_OP_NEAR: |
| 3392 | case GRN_OP_NEAR_NO_OFFSET: |
| 3393 | case GRN_OP_NEAR_PHRASE: |
| 3394 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 3395 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 3396 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 3397 | case GRN_OP_SIMILAR: |
| 3398 | case GRN_OP_PREFIX: |
| 3399 | case GRN_OP_SUFFIX: |
| 3400 | case GRN_OP_EQUAL: |
| 3401 | case GRN_OP_NOT_EQUAL: |
| 3402 | case GRN_OP_LESS: |
| 3403 | case GRN_OP_GREATER: |
| 3404 | case GRN_OP_LESS_EQUAL: |
| 3405 | case GRN_OP_GREATER_EQUAL: |
| 3406 | case GRN_OP_GEO_WITHINP5: |
| 3407 | case GRN_OP_GEO_WITHINP6: |
| 3408 | case GRN_OP_GEO_WITHINP8: |
| 3409 | case GRN_OP_TERM_EXTRACT: |
| 3410 | case GRN_OP_REGEXP: |
| 3411 | case GRN_OP_QUORUM: |
| 3412 | stat = SCAN_START; |
| 3413 | si->op = code_op; |
| 3414 | si->end = c - e->codes; |
| 3415 | sis[i++] = si; |
| 3416 | { |
| 3417 | float weight = grn_weight_bulk_get(ctx, c->value); |
| 3418 | scan_info_build_match(ctx, si, start_position, weight); |
| 3419 | start_position = -1; |
| 3420 | } |
| 3421 | if (ctx->rc != GRN_SUCCESS) { |
| 3422 | int j; |
| 3423 | for (j = 0; j < i; j++) { |
| 3424 | SI_FREE(sis[j]); |
| 3425 | } |
| 3426 | GRN_FREE(sis); |
| 3427 | return NULL; |
| 3428 | } |
| 3429 | si = NULL; |
| 3430 | break; |
| 3431 | case GRN_OP_AND: |
| 3432 | case GRN_OP_OR: |
| 3433 | case GRN_OP_AND_NOT: |
| 3434 | case GRN_OP_ADJUST: |
| 3435 | if (stat == SCAN_CONST) { |
| 3436 | si->op = GRN_OP_PUSH; |
| 3437 | si->end = si->start; |
| 3438 | sis[i++] = si; |
| 3439 | si = NULL; |
| 3440 | } |
| 3441 | { |
| 3442 | float weight_factor = 1.0; |
| 3443 | if (c->value && c->value->header.domain == GRN_DB_FLOAT32) { |
| 3444 | weight_factor = GRN_FLOAT32_VALUE(c->value); |
| 3445 | } |
| 3446 | if (!put_logical_op(ctx, |
| 3447 | sis, |
| 3448 | &i, |
| 3449 | code_op, |
| 3450 | c - e->codes, |
| 3451 | weight_factor)) { |
| 3452 | return NULL; |
| 3453 | } |
| 3454 | } |
| 3455 | stat = SCAN_START; |
| 3456 | break; |
| 3457 | case GRN_OP_PUSH: |
| 3458 | if (!si) { |
| 3459 | SI_ALLOC(si, i, c - e->codes); |
| 3460 | } |
| 3461 | if (c->value == var) { |
| 3462 | stat = SCAN_VAR; |
| 3463 | } else { |
| 3464 | grn_scan_info_push_arg(ctx, si, c->value); |
| 3465 | if (stat == SCAN_START) { |
| 3466 | si->flags |= SCAN_PRE_CONST; |
| 3467 | } |
| 3468 | stat = SCAN_CONST; |
| 3469 | } |
| 3470 | if (c->modify > 0) { |
| 3471 | bool is_completed_term = false; |
| 3472 | switch ((c + c->modify)->op) { |
| 3473 | case GRN_OP_AND: |
| 3474 | case GRN_OP_OR: |
| 3475 | case GRN_OP_AND_NOT: |
| 3476 | case GRN_OP_ADJUST: |
| 3477 | is_completed_term = true; |
| 3478 | break; |
| 3479 | default: |
| 3480 | is_completed_term = false; |
| 3481 | break; |
| 3482 | } |
| 3483 | if (is_completed_term) { |
| 3484 | si->op = GRN_OP_PUSH; |
| 3485 | si->end = si->start; |
| 3486 | sis[i++] = si; |
| 3487 | si = NULL; |
| 3488 | stat = SCAN_START; |
| 3489 | } |
| 3490 | } |
| 3491 | break; |
| 3492 | case GRN_OP_GET_VALUE: |
| 3493 | switch (stat) { |
| 3494 | case SCAN_START: |
| 3495 | if (!si) { |
| 3496 | SI_ALLOC(si, i, c - e->codes); |
| 3497 | } |
| 3498 | // fallthru |
| 3499 | case SCAN_CONST: |
| 3500 | case SCAN_VAR: |
| 3501 | stat = SCAN_COL1; |
| 3502 | grn_scan_info_push_arg(ctx, si, c->value); |
| 3503 | break; |
| 3504 | case SCAN_COL1: |
| 3505 | { |
| 3506 | int j; |
| 3507 | grn_obj inspected; |
| 3508 | GRN_TEXT_INIT(&inspected, 0); |
| 3509 | GRN_TEXT_PUTS(ctx, &inspected, "<"); |
| 3510 | grn_inspect_name(ctx, &inspected, c->value); |
| 3511 | GRN_TEXT_PUTS(ctx, &inspected, ">: <"); |
| 3512 | grn_inspect(ctx, &inspected, expr); |
| 3513 | GRN_TEXT_PUTS(ctx, &inspected, ">"); |
| 3514 | ERR(GRN_INVALID_ARGUMENT, |
| 3515 | "invalid expression: can't use column as a value: %.*s", |
| 3516 | (int)GRN_TEXT_LEN(&inspected), |
| 3517 | GRN_TEXT_VALUE(&inspected)); |
| 3518 | GRN_OBJ_FIN(ctx, &inspected); |
| 3519 | SI_FREE(si); |
| 3520 | for (j = 0; j < i; j++) { |
| 3521 | SI_FREE(sis[j]); |
| 3522 | } |
| 3523 | GRN_FREE(sis); |
| 3524 | return NULL; |
| 3525 | } |
| 3526 | stat = SCAN_COL2; |
| 3527 | break; |
| 3528 | case SCAN_COL2: |
| 3529 | break; |
| 3530 | default: |
| 3531 | break; |
| 3532 | } |
| 3533 | break; |
| 3534 | case GRN_OP_CALL: |
| 3535 | if (!si) { |
| 3536 | SI_ALLOC(si, i, c - e->codes); |
| 3537 | } |
| 3538 | if ((c->flags & GRN_EXPR_CODE_RELATIONAL_EXPRESSION) || c + 1 == ce) { |
| 3539 | stat = SCAN_START; |
| 3540 | si->op = code_op; |
| 3541 | si->end = c - e->codes; |
| 3542 | sis[i++] = si; |
| 3543 | /* better index resolving framework for functions should be implemented |
| 3544 | */ |
| 3545 | if (grn_obj_is_selector_proc(ctx, si->args[0])) { |
| 3546 | grn_obj *selector; |
| 3547 | grn_obj **p; |
| 3548 | grn_obj **pe; |
| 3549 | grn_operator selector_op; |
| 3550 | |
| 3551 | selector = si->args[0]; |
| 3552 | p = si->args + 1; |
| 3553 | pe = si->args + si->nargs; |
| 3554 | selector_op = grn_proc_get_selector_operator(ctx, selector); |
| 3555 | for (; p < pe; p++) { |
| 3556 | if (GRN_DB_OBJP(*p)) { |
| 3557 | grn_index_datum index_datum; |
| 3558 | unsigned int n_index_data; |
| 3559 | n_index_data = grn_column_find_index_data(ctx, |
| 3560 | *p, |
| 3561 | selector_op, |
| 3562 | &index_datum, |
| 3563 | 1); |
| 3564 | if (n_index_data > 0) { |
| 3565 | scan_info_put_index(ctx, |
| 3566 | si, |
| 3567 | index_datum.index, |
| 3568 | index_datum.section, |
| 3569 | start_position, |
| 3570 | 1, |
| 3571 | NULL, |
| 3572 | NULL, |
| 3573 | 0); |
| 3574 | } |
| 3575 | } else if (GRN_ACCESSORP(*p)) { |
| 3576 | grn_index_datum index_datum; |
| 3577 | unsigned int n_index_data; |
| 3578 | si->flags |= SCAN_ACCESSOR; |
| 3579 | n_index_data = grn_column_find_index_data(ctx, |
| 3580 | *p, |
| 3581 | selector_op, |
| 3582 | &index_datum, |
| 3583 | 1); |
| 3584 | if (n_index_data > 0) { |
| 3585 | scan_info_put_index(ctx, |
| 3586 | si, |
| 3587 | index_datum.index, |
| 3588 | index_datum.section, |
| 3589 | start_position, |
| 3590 | 1, |
| 3591 | NULL, |
| 3592 | NULL, |
| 3593 | 0); |
| 3594 | } |
| 3595 | } else { |
| 3596 | si->query = *p; |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | si = NULL; |
| 3601 | start_position = -1; |
| 3602 | } else { |
| 3603 | stat = SCAN_COL2; |
| 3604 | } |
| 3605 | break; |
| 3606 | case GRN_OP_GET_REF: |
| 3607 | switch (stat) { |
| 3608 | case SCAN_START: |
| 3609 | if (!si) { |
| 3610 | SI_ALLOC(si, i, c - e->codes); |
| 3611 | } |
| 3612 | stat = SCAN_COL1; |
| 3613 | grn_scan_info_push_arg(ctx, si, c->value); |
| 3614 | break; |
| 3615 | default: |
| 3616 | break; |
| 3617 | } |
| 3618 | break; |
| 3619 | case GRN_OP_GET_MEMBER: |
| 3620 | { |
| 3621 | grn_obj *start_position_object; |
| 3622 | grn_obj buffer; |
| 3623 | start_position_object = si->args[--si->nargs]; |
| 3624 | GRN_INT32_INIT(&buffer, 0); |
| 3625 | grn_obj_cast(ctx, start_position_object, &buffer, false); |
| 3626 | start_position = GRN_INT32_VALUE(&buffer); |
| 3627 | GRN_OBJ_FIN(ctx, &buffer); |
| 3628 | } |
| 3629 | stat = SCAN_COL1; |
| 3630 | break; |
| 3631 | case GRN_OP_NOT: |
| 3632 | { |
| 3633 | bool valid = grn_scan_info_build_full_not(ctx, |
| 3634 | sis, |
| 3635 | &i, |
| 3636 | e->codes, |
| 3637 | c, |
| 3638 | ce, |
| 3639 | &next_code_op); |
| 3640 | if (!valid) { |
| 3641 | int j; |
| 3642 | for (j = 0; j < i; j++) { |
| 3643 | SI_FREE(sis[j]); |
| 3644 | } |
| 3645 | GRN_FREE(sis); |
| 3646 | return NULL; |
| 3647 | } |
| 3648 | } |
| 3649 | break; |
| 3650 | default: |
| 3651 | break; |
| 3652 | } |
| 3653 | } |
| 3654 | if (op == GRN_OP_OR && !record_exist) { |
| 3655 | // for debug |
| 3656 | if (!(sis[0]->flags & SCAN_PUSH) || (sis[0]->logical_op != op)) { |
| 3657 | int j; |
| 3658 | ERR(GRN_INVALID_ARGUMENT, "invalid expr"); |
| 3659 | for (j = 0; j < i; j++) { |
| 3660 | SI_FREE(sis[j]); |
| 3661 | } |
| 3662 | GRN_FREE(sis); |
| 3663 | return NULL; |
| 3664 | } else { |
| 3665 | sis[0]->flags &= ~SCAN_PUSH; |
| 3666 | sis[0]->logical_op = op; |
| 3667 | } |
| 3668 | } else { |
| 3669 | if (!put_logical_op(ctx, sis, &i, op, c - e->codes, 1.0)) { |
| 3670 | return NULL; |
| 3671 | } |
| 3672 | } |
| 3673 | *n = i; |
| 3674 | return sis; |
| 3675 | } |
| 3676 | |
| 3677 | static scan_info ** |
| 3678 | grn_scan_info_build_simple_open(grn_ctx *ctx, int *n, grn_operator logical_op) |
| 3679 | { |
| 3680 | scan_info **sis; |
| 3681 | scan_info *si; |
| 3682 | |
| 3683 | sis = GRN_MALLOCN(scan_info *, 1); |
| 3684 | if (!sis) { |
| 3685 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 3686 | "[scan_info][build] failed to allocate memory for scan_info **"); |
| 3687 | return NULL; |
| 3688 | } |
| 3689 | |
| 3690 | si = grn_scan_info_open(ctx, 0); |
| 3691 | if (!si) { |
| 3692 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 3693 | "[scan_info][build] failed to allocate memory for scan_info *"); |
| 3694 | GRN_FREE(sis); |
| 3695 | return NULL; |
| 3696 | } |
| 3697 | |
| 3698 | si->flags &= ~SCAN_PUSH; |
| 3699 | si->logical_op = logical_op; |
| 3700 | |
| 3701 | sis[0] = si; |
| 3702 | *n = 1; |
| 3703 | |
| 3704 | return sis; |
| 3705 | } |
| 3706 | |
| 3707 | static scan_info ** |
| 3708 | grn_scan_info_build_simple_value(grn_ctx *ctx, |
| 3709 | grn_obj *expr, |
| 3710 | int *n, |
| 3711 | grn_operator logical_op, |
| 3712 | bool record_exist) |
| 3713 | { |
| 3714 | grn_expr *e = (grn_expr *)expr; |
| 3715 | scan_info **sis; |
| 3716 | scan_info *si; |
| 3717 | grn_expr_code *target = e->codes; |
| 3718 | |
| 3719 | switch (target->op) { |
| 3720 | case GRN_OP_PUSH: |
| 3721 | case GRN_OP_GET_VALUE: |
| 3722 | break; |
| 3723 | default: |
| 3724 | return NULL; |
| 3725 | break; |
| 3726 | } |
| 3727 | |
| 3728 | sis = grn_scan_info_build_simple_open(ctx, n, logical_op); |
| 3729 | if (!sis) { |
| 3730 | return NULL; |
| 3731 | } |
| 3732 | |
| 3733 | si = sis[0]; |
| 3734 | si->end = 0; |
| 3735 | si->op = target->op; |
| 3736 | return sis; |
| 3737 | } |
| 3738 | |
| 3739 | static scan_info ** |
| 3740 | grn_scan_info_build_simple_operation(grn_ctx *ctx, |
| 3741 | grn_obj *expr, |
| 3742 | int *n, |
| 3743 | grn_operator logical_op, |
| 3744 | bool record_exist) |
| 3745 | { |
| 3746 | grn_expr *e = (grn_expr *)expr; |
| 3747 | grn_expr_code *target; |
| 3748 | grn_expr_code *constant; |
| 3749 | grn_expr_code *operator; |
| 3750 | scan_info **sis; |
| 3751 | scan_info *si; |
| 3752 | |
| 3753 | target = e->codes + 0; |
| 3754 | constant = e->codes + 1; |
| 3755 | operator= e->codes + 2; |
| 3756 | |
| 3757 | if (target->op != GRN_OP_GET_VALUE) { |
| 3758 | return NULL; |
| 3759 | } |
| 3760 | if (target->nargs != 1) { |
| 3761 | return NULL; |
| 3762 | } |
| 3763 | if (!target->value) { |
| 3764 | return NULL; |
| 3765 | } |
| 3766 | |
| 3767 | if (constant->op != GRN_OP_PUSH) { |
| 3768 | return NULL; |
| 3769 | } |
| 3770 | if (constant->nargs != 1) { |
| 3771 | return NULL; |
| 3772 | } |
| 3773 | if (!constant->value) { |
| 3774 | return NULL; |
| 3775 | } |
| 3776 | |
| 3777 | if (operator->nargs != 2) { |
| 3778 | return NULL; |
| 3779 | } |
| 3780 | switch (operator->op) { |
| 3781 | case GRN_OP_MATCH: |
| 3782 | case GRN_OP_NEAR: |
| 3783 | case GRN_OP_NEAR_PHRASE: |
| 3784 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 3785 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 3786 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 3787 | case GRN_OP_SIMILAR: |
| 3788 | case GRN_OP_PREFIX: |
| 3789 | case GRN_OP_SUFFIX: |
| 3790 | case GRN_OP_EQUAL: |
| 3791 | case GRN_OP_NOT_EQUAL: |
| 3792 | case GRN_OP_LESS: |
| 3793 | case GRN_OP_GREATER: |
| 3794 | case GRN_OP_LESS_EQUAL: |
| 3795 | case GRN_OP_GREATER_EQUAL: |
| 3796 | case GRN_OP_TERM_EXTRACT: |
| 3797 | case GRN_OP_REGEXP: |
| 3798 | case GRN_OP_QUORUM: |
| 3799 | break; |
| 3800 | default: |
| 3801 | return NULL; |
| 3802 | break; |
| 3803 | } |
| 3804 | |
| 3805 | sis = grn_scan_info_build_simple_open(ctx, n, logical_op); |
| 3806 | if (!sis) { |
| 3807 | return NULL; |
| 3808 | } |
| 3809 | |
| 3810 | si = sis[0]; |
| 3811 | si->end = 2; |
| 3812 | si->op = operator->op; |
| 3813 | grn_scan_info_push_arg(ctx, si, target->value); |
| 3814 | grn_scan_info_push_arg(ctx, si, constant->value); |
| 3815 | { |
| 3816 | float weight = grn_weight_bulk_get(ctx, operator->value); |
| 3817 | scan_info_build_match(ctx, si, -1, weight); |
| 3818 | if (ctx->rc != GRN_SUCCESS) { |
| 3819 | SI_FREE(sis[0]); |
| 3820 | GRN_FREE(sis); |
| 3821 | return NULL; |
| 3822 | } |
| 3823 | } |
| 3824 | return sis; |
| 3825 | } |
| 3826 | |
| 3827 | static scan_info ** |
| 3828 | grn_scan_info_build_simple_and_operations(grn_ctx *ctx, |
| 3829 | grn_obj *expr, |
| 3830 | int *n, |
| 3831 | grn_operator logical_op, |
| 3832 | bool record_exist) |
| 3833 | { |
| 3834 | grn_expr *e = (grn_expr *)expr; |
| 3835 | scan_info **sis = NULL; |
| 3836 | uint32_t n_sis = 0; |
| 3837 | uint32_t i; |
| 3838 | uint32_t nth_sis; |
| 3839 | |
| 3840 | for (i = 0, nth_sis = 0; i < e->codes_curr; i += 3, nth_sis++) { |
| 3841 | grn_expr_code *target = e->codes + i; |
| 3842 | grn_expr_code *constant = e->codes + i + 1; |
| 3843 | grn_expr_code *operator= e->codes + i + 2; |
| 3844 | |
| 3845 | if (target->op != GRN_OP_GET_VALUE) { |
| 3846 | return NULL; |
| 3847 | } |
| 3848 | if (target->nargs != 1) { |
| 3849 | return NULL; |
| 3850 | } |
| 3851 | if (!target->value) { |
| 3852 | return NULL; |
| 3853 | } |
| 3854 | |
| 3855 | if (constant->op != GRN_OP_PUSH) { |
| 3856 | return NULL; |
| 3857 | } |
| 3858 | if (constant->nargs != 1) { |
| 3859 | return NULL; |
| 3860 | } |
| 3861 | if (!constant->value) { |
| 3862 | return NULL; |
| 3863 | } |
| 3864 | |
| 3865 | if (operator->nargs != 2) { |
| 3866 | return NULL; |
| 3867 | } |
| 3868 | switch (operator->op) { |
| 3869 | case GRN_OP_MATCH: |
| 3870 | case GRN_OP_NEAR: |
| 3871 | case GRN_OP_NEAR_PHRASE: |
| 3872 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 3873 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 3874 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 3875 | case GRN_OP_SIMILAR: |
| 3876 | case GRN_OP_PREFIX: |
| 3877 | case GRN_OP_SUFFIX: |
| 3878 | case GRN_OP_EQUAL: |
| 3879 | case GRN_OP_NOT_EQUAL: |
| 3880 | case GRN_OP_LESS: |
| 3881 | case GRN_OP_GREATER: |
| 3882 | case GRN_OP_LESS_EQUAL: |
| 3883 | case GRN_OP_GREATER_EQUAL: |
| 3884 | case GRN_OP_TERM_EXTRACT: |
| 3885 | case GRN_OP_REGEXP: |
| 3886 | case GRN_OP_QUORUM: |
| 3887 | break; |
| 3888 | default: |
| 3889 | return NULL; |
| 3890 | break; |
| 3891 | } |
| 3892 | |
| 3893 | if (nth_sis > 0) { |
| 3894 | grn_expr_code *logical_operator = e->codes + i + 3; |
| 3895 | |
| 3896 | if (logical_operator->op != GRN_OP_AND) { |
| 3897 | return NULL; |
| 3898 | } |
| 3899 | if (logical_operator->nargs != 2) { |
| 3900 | return NULL; |
| 3901 | } |
| 3902 | |
| 3903 | i++; |
| 3904 | } |
| 3905 | } |
| 3906 | n_sis = nth_sis; |
| 3907 | |
| 3908 | bool need_temporary_result_set = true; |
| 3909 | switch (logical_op) { |
| 3910 | case GRN_OP_AND: |
| 3911 | case GRN_OP_OR: |
| 3912 | need_temporary_result_set = false; |
| 3913 | break; |
| 3914 | default: |
| 3915 | break; |
| 3916 | } |
| 3917 | |
| 3918 | if (need_temporary_result_set) { |
| 3919 | n_sis++; |
| 3920 | } |
| 3921 | |
| 3922 | sis = GRN_CALLOC(sizeof(scan_info *) * n_sis); |
| 3923 | if (!sis) { |
| 3924 | return NULL; |
| 3925 | } |
| 3926 | |
| 3927 | for (i = 0, nth_sis = 0; i < e->codes_curr; i += 3, nth_sis++) { |
| 3928 | grn_expr_code *target = e->codes + i; |
| 3929 | grn_expr_code *constant = e->codes + i + 1; |
| 3930 | grn_expr_code *operator= e->codes + i + 2; |
| 3931 | scan_info *si; |
| 3932 | |
| 3933 | sis[nth_sis] = si = grn_scan_info_open(ctx, i); |
| 3934 | if (!si) { |
| 3935 | goto exit; |
| 3936 | } |
| 3937 | grn_scan_info_push_arg(ctx, si, target->value); |
| 3938 | grn_scan_info_push_arg(ctx, si, constant->value); |
| 3939 | si->op = operator->op; |
| 3940 | si->end = i + 2; |
| 3941 | if (nth_sis == 0) { |
| 3942 | if (need_temporary_result_set) { |
| 3943 | si->logical_op = GRN_OP_OR; |
| 3944 | } else { |
| 3945 | si->flags &= ~SCAN_PUSH; |
| 3946 | si->logical_op = logical_op; |
| 3947 | } |
| 3948 | } else { |
| 3949 | si->flags &= ~SCAN_PUSH; |
| 3950 | si->logical_op = GRN_OP_AND; |
| 3951 | } |
| 3952 | { |
| 3953 | float weight = grn_weight_bulk_get(ctx, operator->value); |
| 3954 | scan_info_build_match(ctx, si, -1, weight); |
| 3955 | if (ctx->rc != GRN_SUCCESS) { |
| 3956 | goto exit; |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | if (nth_sis > 0) { |
| 3961 | i++; |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | if (need_temporary_result_set) { |
| 3966 | scan_info *si; |
| 3967 | sis[nth_sis] = si = grn_scan_info_open(ctx, i); |
| 3968 | if (!si) { |
| 3969 | goto exit; |
| 3970 | } |
| 3971 | si->end = i; |
| 3972 | si->op = GRN_OP_NOP; |
| 3973 | si->logical_op = logical_op; |
| 3974 | si->flags = SCAN_POP; |
| 3975 | } |
| 3976 | |
| 3977 | *n = n_sis; |
| 3978 | return sis; |
| 3979 | |
| 3980 | exit: |
| 3981 | if (n_sis > 0) { |
| 3982 | for (i = 0; i < n_sis; i++) { |
| 3983 | scan_info *si = sis[i]; |
| 3984 | if (si) { |
| 3985 | grn_scan_info_close(ctx, si); |
| 3986 | } |
| 3987 | } |
| 3988 | GRN_FREE(sis); |
| 3989 | } |
| 3990 | |
| 3991 | return NULL; |
| 3992 | } |
| 3993 | |
| 3994 | static scan_info ** |
| 3995 | grn_scan_info_build_simple(grn_ctx *ctx, |
| 3996 | grn_obj *expr, |
| 3997 | int *n, |
| 3998 | grn_operator logical_op, |
| 3999 | bool record_exist) |
| 4000 | { |
| 4001 | grn_expr *e = (grn_expr *)expr; |
| 4002 | |
| 4003 | if (e->codes_curr == 1) { |
| 4004 | return grn_scan_info_build_simple_value(ctx, |
| 4005 | expr, |
| 4006 | n, |
| 4007 | logical_op, |
| 4008 | record_exist); |
| 4009 | } else if (e->codes_curr == 3) { |
| 4010 | return grn_scan_info_build_simple_operation(ctx, |
| 4011 | expr, |
| 4012 | n, |
| 4013 | logical_op, |
| 4014 | record_exist); |
| 4015 | } else if (e->codes_curr % 4 == 3) { |
| 4016 | return grn_scan_info_build_simple_and_operations(ctx, |
| 4017 | expr, |
| 4018 | n, |
| 4019 | logical_op, |
| 4020 | record_exist); |
| 4021 | } |
| 4022 | |
| 4023 | return NULL; |
| 4024 | } |
| 4025 | |
| 4026 | scan_info ** |
| 4027 | grn_scan_info_build( |
| 4028 | grn_ctx *ctx, grn_obj *expr, int *n, grn_operator op, bool record_exist) |
| 4029 | { |
| 4030 | scan_info **sis; |
| 4031 | |
| 4032 | sis = grn_scan_info_build_simple(ctx, expr, n, op, record_exist); |
| 4033 | if (ctx->rc != GRN_SUCCESS) { |
| 4034 | return NULL; |
| 4035 | } |
| 4036 | #ifdef GRN_WITH_MRUBY |
| 4037 | if (!sis) { |
| 4038 | grn_ctx_impl_mrb_ensure_init(ctx); |
| 4039 | if (ctx->rc != GRN_SUCCESS) { |
| 4040 | return NULL; |
| 4041 | } |
| 4042 | if (ctx->impl->mrb.state) { |
| 4043 | return grn_mrb_scan_info_build(ctx, expr, n, op, record_exist); |
| 4044 | } |
| 4045 | } |
| 4046 | #endif |
| 4047 | if (!sis) { |
| 4048 | sis = grn_scan_info_build_full(ctx, expr, n, op, record_exist); |
| 4049 | } |
| 4050 | return sis; |
| 4051 | } |
| 4052 | |
| 4053 | void |
| 4054 | grn_inspect_scan_info_list(grn_ctx *ctx, |
| 4055 | grn_obj *buffer, |
| 4056 | scan_info **sis, |
| 4057 | int n) |
| 4058 | { |
| 4059 | int i; |
| 4060 | |
| 4061 | for (i = 0; i < n; i++) { |
| 4062 | scan_info *si = sis[i]; |
| 4063 | |
| 4064 | grn_text_printf(ctx, buffer, "[%d]\n", i); |
| 4065 | grn_text_printf(ctx, |
| 4066 | buffer, |
| 4067 | " op: <%s>\n", |
| 4068 | grn_operator_to_string(si->op)); |
| 4069 | grn_text_printf(ctx, |
| 4070 | buffer, |
| 4071 | " logical_op: <%s>\n", |
| 4072 | grn_operator_to_string(si->logical_op)); |
| 4073 | grn_text_printf(ctx, |
| 4074 | buffer, |
| 4075 | " weight_factor: <%f>\n", |
| 4076 | si->weight_factor); |
| 4077 | grn_text_printf(ctx, buffer, " sections: <"); |
| 4078 | grn_inspect(ctx, buffer, &(si->sections)); |
| 4079 | grn_text_printf(ctx, buffer, ">\n"); |
| 4080 | grn_text_printf(ctx, buffer, " weights: <"); |
| 4081 | grn_inspect(ctx, buffer, &(si->weights)); |
| 4082 | grn_text_printf(ctx, buffer, ">\n"); |
| 4083 | grn_text_printf(ctx, buffer, " start_positions: <"); |
| 4084 | grn_inspect(ctx, buffer, &(si->start_positions)); |
| 4085 | grn_text_printf(ctx, buffer, ">\n"); |
| 4086 | |
| 4087 | if (si->op == GRN_OP_CALL) { |
| 4088 | int i; |
| 4089 | for (i = 0; i < si->nargs; i++) { |
| 4090 | grn_text_printf(ctx, buffer, " args[%d]: <", i); |
| 4091 | grn_inspect(ctx, buffer, si->args[i]); |
| 4092 | GRN_TEXT_PUTS(ctx, buffer, ">\n"); |
| 4093 | } |
| 4094 | } else { |
| 4095 | GRN_TEXT_PUTS(ctx, buffer, " index: <"); |
| 4096 | grn_inspect(ctx, buffer, &(si->index)); |
| 4097 | GRN_TEXT_PUTS(ctx, buffer, ">\n"); |
| 4098 | |
| 4099 | GRN_TEXT_PUTS(ctx, buffer, " query: <"); |
| 4100 | grn_inspect(ctx, buffer, si->query); |
| 4101 | GRN_TEXT_PUTS(ctx, buffer, ">\n"); |
| 4102 | } |
| 4103 | |
| 4104 | grn_text_printf(ctx, |
| 4105 | buffer, |
| 4106 | " expr: <%d..%d>\n", |
| 4107 | si->start, |
| 4108 | si->end); |
| 4109 | } |
| 4110 | } |
| 4111 | |
| 4112 | void |
| 4113 | grn_p_scan_info_list(grn_ctx *ctx, scan_info **sis, int n) |
| 4114 | { |
| 4115 | grn_obj inspected; |
| 4116 | GRN_TEXT_INIT(&inspected, 0); |
| 4117 | grn_inspect_scan_info_list(ctx, &inspected, sis, n); |
| 4118 | printf("%.*s\n", (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); |
| 4119 | GRN_OBJ_FIN(ctx, &inspected); |
| 4120 | } |
| 4121 | |
| 4122 | grn_obj * |
| 4123 | grn_table_select(grn_ctx *ctx, |
| 4124 | grn_obj *table, |
| 4125 | grn_obj *expr, |
| 4126 | grn_obj *result_set, |
| 4127 | grn_operator op) |
| 4128 | { |
| 4129 | GRN_API_ENTER; |
| 4130 | grn_table_selector table_selector; |
| 4131 | grn_table_selector_init(ctx, &table_selector, table, expr, op); |
| 4132 | if (!result_set) { |
| 4133 | grn_table_selector_set_ensure_using_select_result(ctx, |
| 4134 | &table_selector, |
| 4135 | true); |
| 4136 | } |
| 4137 | result_set = grn_table_selector_select(ctx, &table_selector, result_set); |
| 4138 | grn_table_selector_fin(ctx, &table_selector); |
| 4139 | GRN_API_RETURN(result_set); |
| 4140 | } |
| 4141 | |
| 4142 | /* grn_expr_parse */ |
| 4143 | |
| 4144 | static grn_obj * |
| 4145 | grn_ptr_value_at(grn_obj *obj, int offset) |
| 4146 | { |
| 4147 | int size = GRN_BULK_VSIZE(obj) / sizeof(grn_obj *); |
| 4148 | if (offset < 0) { |
| 4149 | offset = size + offset; |
| 4150 | } |
| 4151 | return (0 <= offset && offset < size) |
| 4152 | ? (((grn_obj **)GRN_BULK_HEAD(obj))[offset]) |
| 4153 | : NULL; |
| 4154 | } |
| 4155 | |
| 4156 | static int32_t |
| 4157 | grn_int32_value_at(grn_obj *obj, int offset) |
| 4158 | { |
| 4159 | int size = GRN_BULK_VSIZE(obj) / sizeof(int32_t); |
| 4160 | if (offset < 0) { |
| 4161 | offset = size + offset; |
| 4162 | } |
| 4163 | return (0 <= offset && offset < size) ? GRN_INT32_VALUE_AT(obj, offset) : 0; |
| 4164 | } |
| 4165 | |
| 4166 | static float |
| 4167 | grn_float32_value_at(grn_obj *obj, int offset) |
| 4168 | { |
| 4169 | int size = GRN_BULK_VSIZE(obj) / sizeof(float); |
| 4170 | if (offset < 0) { |
| 4171 | offset = size + offset; |
| 4172 | } |
| 4173 | return (0 <= offset && offset < size) ? GRN_FLOAT32_VALUE_AT(obj, offset) : 0; |
| 4174 | } |
| 4175 | |
| 4176 | /* grn_expr_create_from_str */ |
| 4177 | |
| 4178 | #include "grn_snip.h" |
| 4179 | |
| 4180 | typedef struct { |
| 4181 | grn_ctx *ctx; |
| 4182 | void *parser; |
| 4183 | grn_obj *e; |
| 4184 | grn_obj *v; |
| 4185 | const char *str; |
| 4186 | const char *cur; |
| 4187 | const char *str_end; |
| 4188 | grn_obj *table; |
| 4189 | grn_obj *default_column; |
| 4190 | grn_obj buf; |
| 4191 | grn_obj token_stack; |
| 4192 | grn_obj column_stack; |
| 4193 | grn_obj op_stack; |
| 4194 | grn_obj mode_stack; |
| 4195 | grn_obj max_interval_stack; |
| 4196 | grn_obj additional_last_interval_stack; |
| 4197 | grn_obj max_element_intervals_stack; |
| 4198 | grn_obj min_interval_stack; |
| 4199 | grn_obj similarity_threshold_stack; |
| 4200 | grn_obj quorum_threshold_stack; |
| 4201 | grn_obj weight_stack; |
| 4202 | grn_operator default_op; |
| 4203 | grn_select_optarg opt; |
| 4204 | grn_operator default_mode; |
| 4205 | grn_expr_flags flags; |
| 4206 | grn_expr_flags default_flags; |
| 4207 | int escalation_threshold; |
| 4208 | int escalation_decaystep; |
| 4209 | int weight_offset; |
| 4210 | grn_hash *weight_set; |
| 4211 | snip_cond *snip_conds; |
| 4212 | int paren_depth; |
| 4213 | struct { |
| 4214 | const char *string; |
| 4215 | size_t string_length; |
| 4216 | int token; |
| 4217 | float weight; |
| 4218 | } pending_token; |
| 4219 | } efs_info; |
| 4220 | |
| 4221 | typedef struct { |
| 4222 | grn_operator op; |
| 4223 | float weight; |
| 4224 | } efs_op; |
| 4225 | |
| 4226 | static inline void |
| 4227 | skip_space(grn_ctx *ctx, efs_info *q) |
| 4228 | { |
| 4229 | unsigned int len; |
| 4230 | while (q->cur < q->str_end && grn_isspace(q->cur, ctx->encoding)) { |
| 4231 | /* null check and length check */ |
| 4232 | if (!(len = grn_charlen(ctx, q->cur, q->str_end))) { |
| 4233 | q->cur = q->str_end; |
| 4234 | break; |
| 4235 | } |
| 4236 | q->cur += len; |
| 4237 | } |
| 4238 | } |
| 4239 | |
| 4240 | typedef struct { |
| 4241 | grn_operator mode; |
| 4242 | union { |
| 4243 | struct { |
| 4244 | int32_t threshold; |
| 4245 | } similar; |
| 4246 | struct { |
| 4247 | int32_t max_interval; |
| 4248 | int32_t additional_last_interval; |
| 4249 | grn_obj *max_element_intervals; |
| 4250 | int32_t min_interval; |
| 4251 | /* near is defined by Visual C++. */ |
| 4252 | #ifdef near |
| 4253 | # undef near |
| 4254 | #endif |
| 4255 | } near; |
| 4256 | struct { |
| 4257 | int32_t policy; |
| 4258 | } term_extract; |
| 4259 | struct { |
| 4260 | int32_t threshold; |
| 4261 | } quorum; |
| 4262 | } options; |
| 4263 | } parse_query_op_data; |
| 4264 | |
| 4265 | static const char * |
| 4266 | parse_near_options(efs_info *q, const char *start, parse_query_op_data *data) |
| 4267 | { |
| 4268 | const char *end; |
| 4269 | data->options.near.min_interval = GRN_II_DEFAULT_NEAR_MIN_INTERVAL; |
| 4270 | data->options.near.max_interval = grn_atoi(start, q->str_end, &end); |
| 4271 | if (start == end) { |
| 4272 | data->options.near.max_interval = DEFAULT_MAX_INTERVAL; |
| 4273 | data->options.near.additional_last_interval = |
| 4274 | DEFAULT_ADDITIONAL_LAST_INTERVAL; |
| 4275 | return end; |
| 4276 | } |
| 4277 | |
| 4278 | if (!(data->mode == GRN_OP_NEAR || data->mode == GRN_OP_NEAR_NO_OFFSET)) { |
| 4279 | if (end < q->str_end && end[0] == ',') { |
| 4280 | const char *additional_last_interval_start = end + 1; |
| 4281 | data->options.near.additional_last_interval = |
| 4282 | grn_atoi(additional_last_interval_start, q->str_end, &end); |
| 4283 | if (additional_last_interval_start == end) { |
| 4284 | data->options.near.additional_last_interval = |
| 4285 | DEFAULT_ADDITIONAL_LAST_INTERVAL; |
| 4286 | return end; |
| 4287 | } |
| 4288 | } else { |
| 4289 | data->options.near.additional_last_interval = |
| 4290 | DEFAULT_ADDITIONAL_LAST_INTERVAL; |
| 4291 | } |
| 4292 | } |
| 4293 | |
| 4294 | if (end < q->str_end && end[0] == ',') { |
| 4295 | const char *max_element_interval_start; |
| 4296 | data->options.near.max_element_intervals = |
| 4297 | grn_obj_open(q->ctx, GRN_UVECTOR, 0, GRN_DB_INT32); |
| 4298 | for (max_element_interval_start = end + 1; |
| 4299 | max_element_interval_start < q->str_end; |
| 4300 | max_element_interval_start = end + 1) { |
| 4301 | int32_t max_element_interval = |
| 4302 | grn_atoi(max_element_interval_start, q->str_end, &end); |
| 4303 | if (max_element_interval_start == end) { |
| 4304 | break; |
| 4305 | } |
| 4306 | GRN_INT32_PUT(q->ctx, |
| 4307 | data->options.near.max_element_intervals, |
| 4308 | max_element_interval); |
| 4309 | if (end == q->str_end) { |
| 4310 | break; |
| 4311 | } |
| 4312 | if (end[0] != '|') { |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | } |
| 4317 | |
| 4318 | if (end < q->str_end && end[0] == ',') { |
| 4319 | const char *min_interval_start = end + 1; |
| 4320 | data->options.near.min_interval = |
| 4321 | grn_atoi(min_interval_start, q->str_end, &end); |
| 4322 | if (min_interval_start == end) { |
| 4323 | data->options.near.min_interval = GRN_II_DEFAULT_NEAR_MIN_INTERVAL; |
| 4324 | return end; |
| 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | return end; |
| 4329 | } |
| 4330 | |
| 4331 | static bool |
| 4332 | parse_query_op(efs_info *q, efs_op *op, parse_query_op_data *data) |
| 4333 | { |
| 4334 | bool found = true; |
| 4335 | const char *start, *end = q->cur; |
| 4336 | switch (*end) { |
| 4337 | case 'S': |
| 4338 | data->mode = GRN_OP_SIMILAR; |
| 4339 | start = ++end; |
| 4340 | data->options.similar.threshold = |
| 4341 | grn_atoi(start, q->str_end, (const char **)&end); |
| 4342 | if (start == end) { |
| 4343 | data->options.similar.threshold = DEFAULT_SIMILARITY_THRESHOLD; |
| 4344 | } |
| 4345 | q->cur = end; |
| 4346 | break; |
| 4347 | case 'N': |
| 4348 | case 'O': |
| 4349 | if (*end == 'N') { |
| 4350 | data->mode = GRN_OP_NEAR; |
| 4351 | start = ++end; |
| 4352 | if (start < q->str_end && start[0] == 'P') { |
| 4353 | start++; |
| 4354 | if (start < q->str_end && start[0] == 'P') { |
| 4355 | start++; |
| 4356 | data->mode = GRN_OP_NEAR_PHRASE_PRODUCT; |
| 4357 | } else { |
| 4358 | data->mode = GRN_OP_NEAR_PHRASE; |
| 4359 | } |
| 4360 | } |
| 4361 | } else { |
| 4362 | if (end + 2 < q->str_end && end[1] == 'N' && end[2] == 'P') { |
| 4363 | start = end + 3; |
| 4364 | if (start < q->str_end && start[0] == 'P') { |
| 4365 | start++; |
| 4366 | data->mode = GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT; |
| 4367 | } else { |
| 4368 | data->mode = GRN_OP_ORDERED_NEAR_PHRASE; |
| 4369 | } |
| 4370 | } else { |
| 4371 | found = false; |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | if (!found) { |
| 4376 | break; |
| 4377 | } |
| 4378 | |
| 4379 | q->cur = parse_near_options(q, start, data); |
| 4380 | break; |
| 4381 | case 'n': |
| 4382 | data->mode = GRN_OP_NEAR_NO_OFFSET; |
| 4383 | q->cur = parse_near_options(q, end + 1, data); |
| 4384 | break; |
| 4385 | case 'T': |
| 4386 | data->mode = GRN_OP_TERM_EXTRACT; |
| 4387 | start = ++end; |
| 4388 | data->options.term_extract.policy = |
| 4389 | grn_atoi(start, q->str_end, (const char **)&end); |
| 4390 | if (start == end) { |
| 4391 | data->options.term_extract.policy = DEFAULT_TERM_EXTRACT_POLICY; |
| 4392 | } |
| 4393 | q->cur = end; |
| 4394 | break; |
| 4395 | case 'X': /* force exact mode */ |
| 4396 | op->op = GRN_OP_AND; |
| 4397 | data->mode = GRN_OP_EXACT; |
| 4398 | start = ++end; |
| 4399 | q->cur = end; |
| 4400 | break; |
| 4401 | case 'Q': |
| 4402 | data->mode = GRN_OP_QUORUM; |
| 4403 | start = ++end; |
| 4404 | data->options.quorum.threshold = |
| 4405 | grn_atoi(start, q->str_end, (const char **)&end); |
| 4406 | if (start == end) { |
| 4407 | data->options.quorum.threshold = DEFAULT_QUORUM_THRESHOLD; |
| 4408 | } |
| 4409 | q->cur = end; |
| 4410 | break; |
| 4411 | default: |
| 4412 | found = false; |
| 4413 | break; |
| 4414 | } |
| 4415 | return found; |
| 4416 | } |
| 4417 | |
| 4418 | static bool |
| 4419 | parse_query_parse_weight(grn_ctx *ctx, |
| 4420 | efs_info *query, |
| 4421 | efs_op *op, |
| 4422 | float *weight) |
| 4423 | { |
| 4424 | char *end = (char *)(query->str_end); |
| 4425 | errno = 0; |
| 4426 | float value = strtof(query->cur, &end); |
| 4427 | if (errno == 0 && end != query->cur) { |
| 4428 | query->cur = end; |
| 4429 | *weight = value; |
| 4430 | return true; |
| 4431 | } else { |
| 4432 | return false; |
| 4433 | } |
| 4434 | } |
| 4435 | |
| 4436 | #define DISABLE_UNUSED_CODE 1 |
| 4437 | #ifndef DISABLE_UNUSED_CODE |
| 4438 | static const char * |
| 4439 | get_weight_vector(grn_ctx *ctx, efs_info *query, const char *source) |
| 4440 | { |
| 4441 | const char *p; |
| 4442 | |
| 4443 | if (!query->opt.weight_vector && !query->weight_set && |
| 4444 | !(query->opt.weight_vector = |
| 4445 | GRN_CALLOC(sizeof(int) * DEFAULT_WEIGHT_VECTOR_SIZE))) { |
| 4446 | GRN_LOG(ctx, GRN_LOG_ALERT, "get_weight_vector malloc fail"); |
| 4447 | return source; |
| 4448 | } |
| 4449 | for (p = source; p < query->str_end;) { |
| 4450 | unsigned int key; |
| 4451 | int value; |
| 4452 | |
| 4453 | /* key, key is not zero */ |
| 4454 | key = grn_atoui(p, query->str_end, &p); |
| 4455 | if (!key || key > GRN_ID_MAX) { |
| 4456 | break; |
| 4457 | } |
| 4458 | |
| 4459 | /* value */ |
| 4460 | if (*p == ':') { |
| 4461 | p++; |
| 4462 | value = grn_atoi(p, query->str_end, &p); |
| 4463 | } else { |
| 4464 | value = 1; |
| 4465 | } |
| 4466 | |
| 4467 | if (query->weight_set) { |
| 4468 | int *pval; |
| 4469 | if (grn_hash_add(ctx, |
| 4470 | query->weight_set, |
| 4471 | &key, |
| 4472 | sizeof(unsigned int), |
| 4473 | (void **)&pval, |
| 4474 | NULL)) { |
| 4475 | *pval = value; |
| 4476 | } |
| 4477 | } else if (key < DEFAULT_WEIGHT_VECTOR_SIZE) { |
| 4478 | query->opt.weight_vector[key - 1] = value; |
| 4479 | } else { |
| 4480 | GRN_FREE(query->opt.weight_vector); |
| 4481 | query->opt.weight_vector = NULL; |
| 4482 | if (!(query->weight_set = grn_hash_create(ctx, |
| 4483 | NULL, |
| 4484 | sizeof(unsigned int), |
| 4485 | sizeof(int), |
| 4486 | 0))) { |
| 4487 | return source; |
| 4488 | } |
| 4489 | p = source; /* reparse */ |
| 4490 | continue; |
| 4491 | } |
| 4492 | if (*p != ',') { |
| 4493 | break; |
| 4494 | } |
| 4495 | p++; |
| 4496 | } |
| 4497 | return p; |
| 4498 | } |
| 4499 | |
| 4500 | static void |
| 4501 | get_pragma(grn_ctx *ctx, efs_info *q) |
| 4502 | { |
| 4503 | const char *start, *end = q->cur; |
| 4504 | while (end < q->str_end && *end == GRN_QUERY_PREFIX) { |
| 4505 | if (++end >= q->str_end) { |
| 4506 | break; |
| 4507 | } |
| 4508 | switch (*end) { |
| 4509 | case 'E': |
| 4510 | start = ++end; |
| 4511 | q->escalation_threshold = |
| 4512 | grn_atoi(start, q->str_end, (const char **)&end); |
| 4513 | while (end < q->str_end && |
| 4514 | (('0' <= *end && *end <= '9') || *end == '-')) { |
| 4515 | end++; |
| 4516 | } |
| 4517 | if (*end == ',') { |
| 4518 | start = ++end; |
| 4519 | q->escalation_decaystep = |
| 4520 | grn_atoi(start, q->str_end, (const char **)&end); |
| 4521 | } |
| 4522 | q->cur = end; |
| 4523 | break; |
| 4524 | case 'D': |
| 4525 | start = ++end; |
| 4526 | while (end < q->str_end && *end != GRN_QUERY_PREFIX && |
| 4527 | !grn_isspace(end, ctx->encoding)) { |
| 4528 | end++; |
| 4529 | } |
| 4530 | if (end > start) { |
| 4531 | switch (*start) { |
| 4532 | case 'O': |
| 4533 | q->default_op = GRN_OP_OR; |
| 4534 | break; |
| 4535 | case GRN_QUERY_AND: |
| 4536 | q->default_op = GRN_OP_AND; |
| 4537 | break; |
| 4538 | case GRN_QUERY_AND_NOT: |
| 4539 | q->default_op = GRN_OP_AND_NOT; |
| 4540 | break; |
| 4541 | case GRN_QUERY_ADJ_INC: |
| 4542 | q->default_op = GRN_OP_ADJUST; |
| 4543 | break; |
| 4544 | } |
| 4545 | } |
| 4546 | q->cur = end; |
| 4547 | break; |
| 4548 | case 'W': |
| 4549 | start = ++end; |
| 4550 | end = (char *)get_weight_vector(ctx, q, start); |
| 4551 | q->cur = end; |
| 4552 | break; |
| 4553 | } |
| 4554 | } |
| 4555 | } |
| 4556 | |
| 4557 | static int |
| 4558 | section_weight_cb( |
| 4559 | grn_ctx *ctx, grn_hash *r, const void *rid, int sid, void *arg) |
| 4560 | { |
| 4561 | int *w; |
| 4562 | grn_hash *s = (grn_hash *)arg; |
| 4563 | if (s && grn_hash_get(ctx, s, &sid, sizeof(grn_id), (void **)&w)) { |
| 4564 | return *w; |
| 4565 | } else { |
| 4566 | return 0; |
| 4567 | } |
| 4568 | } |
| 4569 | #endif |
| 4570 | |
| 4571 | #define YYSTACKDEPTH 0 |
| 4572 | #include "grn_ecmascript.h" |
| 4573 | #include "grn_ecmascript.c" |
| 4574 | |
| 4575 | void * |
| 4576 | grn_expr_parser_open(grn_ctx *ctx) |
| 4577 | { |
| 4578 | return (void *)grn_expr_parserAlloc(malloc); |
| 4579 | } |
| 4580 | |
| 4581 | grn_rc |
| 4582 | grn_expr_parser_close(grn_ctx *ctx, void *expr_parser) |
| 4583 | { |
| 4584 | grn_expr_parserFree((yyParser *)expr_parser, free); |
| 4585 | return ctx->rc; |
| 4586 | } |
| 4587 | |
| 4588 | #define PARSE(token) grn_expr_parser((yyParser *)(q->parser), (token), 0, q) |
| 4589 | |
| 4590 | static void |
| 4591 | parse_query_accept_string(grn_ctx *ctx, |
| 4592 | efs_info *efsi, |
| 4593 | const char *str, |
| 4594 | unsigned int str_size) |
| 4595 | { |
| 4596 | grn_obj *column, *token; |
| 4597 | grn_operator mode; |
| 4598 | float weight; |
| 4599 | |
| 4600 | GRN_PTR_PUT(ctx, |
| 4601 | &efsi->token_stack, |
| 4602 | grn_expr_add_str(ctx, efsi->e, str, str_size)); |
| 4603 | { |
| 4604 | efs_info *q = efsi; |
| 4605 | PARSE(GRN_EXPR_TOKEN_QSTRING); |
| 4606 | } |
| 4607 | |
| 4608 | GRN_PTR_POP(&efsi->token_stack, token); |
| 4609 | column = grn_ptr_value_at(&efsi->column_stack, -1); |
| 4610 | grn_expr_append_const(efsi->ctx, efsi->e, column, GRN_OP_GET_VALUE, 1); |
| 4611 | grn_expr_append_obj(efsi->ctx, efsi->e, token, GRN_OP_PUSH, 1); |
| 4612 | |
| 4613 | mode = grn_int32_value_at(&efsi->mode_stack, -1); |
| 4614 | weight = grn_float32_value_at(&efsi->weight_stack, -1); |
| 4615 | switch (mode) { |
| 4616 | case GRN_OP_ASSIGN: |
| 4617 | grn_expr_append_op(efsi->ctx, efsi->e, mode, 2); |
| 4618 | break; |
| 4619 | case GRN_OP_NEAR: |
| 4620 | case GRN_OP_NEAR_NO_OFFSET: |
| 4621 | { |
| 4622 | int n_args = 2; |
| 4623 | int max_interval; |
| 4624 | max_interval = grn_int32_value_at(&efsi->max_interval_stack, -1); |
| 4625 | grn_expr_append_const_int32(efsi->ctx, |
| 4626 | efsi->e, |
| 4627 | max_interval, |
| 4628 | GRN_OP_PUSH, |
| 4629 | 1); |
| 4630 | n_args++; |
| 4631 | grn_obj *max_element_intervals; |
| 4632 | max_element_intervals = |
| 4633 | grn_ptr_value_at(&efsi->max_element_intervals_stack, -1); |
| 4634 | if (max_element_intervals) { |
| 4635 | grn_expr_append_obj(efsi->ctx, |
| 4636 | efsi->e, |
| 4637 | max_element_intervals, |
| 4638 | GRN_OP_PUSH, |
| 4639 | 1); |
| 4640 | grn_expr_take_obj(ctx, efsi->e, max_element_intervals); |
| 4641 | n_args++; |
| 4642 | int min_interval = grn_int32_value_at(&efsi->min_interval_stack, -1); |
| 4643 | grn_expr_append_const_int32(efsi->ctx, |
| 4644 | efsi->e, |
| 4645 | min_interval, |
| 4646 | GRN_OP_PUSH, |
| 4647 | 1); |
| 4648 | n_args++; |
| 4649 | } |
| 4650 | if (fpclassify(weight) == FP_ZERO) { |
| 4651 | grn_expr_append_op(efsi->ctx, efsi->e, mode, n_args); |
| 4652 | } else { |
| 4653 | grn_expr_append_const_float32(efsi->ctx, efsi->e, weight, mode, n_args); |
| 4654 | } |
| 4655 | } |
| 4656 | break; |
| 4657 | case GRN_OP_NEAR_PHRASE: |
| 4658 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 4659 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 4660 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 4661 | { |
| 4662 | int n_args = 2; |
| 4663 | int max_interval; |
| 4664 | max_interval = grn_int32_value_at(&efsi->max_interval_stack, -1); |
| 4665 | grn_expr_append_const_int32(efsi->ctx, |
| 4666 | efsi->e, |
| 4667 | max_interval, |
| 4668 | GRN_OP_PUSH, |
| 4669 | 1); |
| 4670 | n_args++; |
| 4671 | int additional_last_interval; |
| 4672 | additional_last_interval = |
| 4673 | grn_int32_value_at(&efsi->additional_last_interval_stack, -1); |
| 4674 | grn_expr_append_const_int32(efsi->ctx, |
| 4675 | efsi->e, |
| 4676 | additional_last_interval, |
| 4677 | GRN_OP_PUSH, |
| 4678 | 1); |
| 4679 | n_args++; |
| 4680 | grn_obj *max_element_intervals; |
| 4681 | max_element_intervals = |
| 4682 | grn_ptr_value_at(&efsi->max_element_intervals_stack, -1); |
| 4683 | if (max_element_intervals) { |
| 4684 | grn_expr_append_obj(efsi->ctx, |
| 4685 | efsi->e, |
| 4686 | max_element_intervals, |
| 4687 | GRN_OP_PUSH, |
| 4688 | 1); |
| 4689 | grn_expr_take_obj(ctx, efsi->e, max_element_intervals); |
| 4690 | n_args++; |
| 4691 | int min_interval = grn_int32_value_at(&efsi->min_interval_stack, -1); |
| 4692 | grn_expr_append_const_int32(efsi->ctx, |
| 4693 | efsi->e, |
| 4694 | min_interval, |
| 4695 | GRN_OP_PUSH, |
| 4696 | 1); |
| 4697 | n_args++; |
| 4698 | } |
| 4699 | if (fpclassify(weight) == FP_ZERO) { |
| 4700 | grn_expr_append_op(efsi->ctx, efsi->e, mode, n_args); |
| 4701 | } else { |
| 4702 | grn_expr_append_const_float32(efsi->ctx, efsi->e, weight, mode, n_args); |
| 4703 | } |
| 4704 | } |
| 4705 | break; |
| 4706 | case GRN_OP_SIMILAR: |
| 4707 | { |
| 4708 | int similarity_threshold; |
| 4709 | similarity_threshold = |
| 4710 | grn_int32_value_at(&efsi->similarity_threshold_stack, -1); |
| 4711 | grn_expr_append_const_int32(efsi->ctx, |
| 4712 | efsi->e, |
| 4713 | similarity_threshold, |
| 4714 | GRN_OP_PUSH, |
| 4715 | 1); |
| 4716 | if (fpclassify(weight) == FP_ZERO) { |
| 4717 | grn_expr_append_op(efsi->ctx, efsi->e, mode, 3); |
| 4718 | } else { |
| 4719 | grn_expr_append_const_float32(efsi->ctx, efsi->e, weight, mode, 3); |
| 4720 | } |
| 4721 | } |
| 4722 | break; |
| 4723 | case GRN_OP_QUORUM: |
| 4724 | { |
| 4725 | int quorum_threshold; |
| 4726 | quorum_threshold = grn_int32_value_at(&efsi->quorum_threshold_stack, -1); |
| 4727 | grn_expr_append_const_int32(efsi->ctx, |
| 4728 | efsi->e, |
| 4729 | quorum_threshold, |
| 4730 | GRN_OP_PUSH, |
| 4731 | 1); |
| 4732 | if (fpclassify(weight) == FP_ZERO) { |
| 4733 | grn_expr_append_op(efsi->ctx, efsi->e, mode, 3); |
| 4734 | } else { |
| 4735 | grn_expr_append_const_float32(efsi->ctx, efsi->e, weight, mode, 3); |
| 4736 | } |
| 4737 | } |
| 4738 | break; |
| 4739 | default: |
| 4740 | if (fpclassify(weight) == FP_ZERO) { |
| 4741 | grn_expr_append_op(efsi->ctx, efsi->e, mode, 2); |
| 4742 | } else { |
| 4743 | grn_expr_append_const_float32(efsi->ctx, efsi->e, weight, mode, 2); |
| 4744 | } |
| 4745 | break; |
| 4746 | } |
| 4747 | } |
| 4748 | |
| 4749 | static void |
| 4750 | parse_query_push_weight(grn_ctx *ctx, efs_info *efsi, float weight) |
| 4751 | { |
| 4752 | float last_weight = grn_float32_value_at(&(efsi->weight_stack), -1); |
| 4753 | weight += last_weight; |
| 4754 | GRN_FLOAT32_PUT(ctx, &(efsi->weight_stack), weight); |
| 4755 | } |
| 4756 | |
| 4757 | static void |
| 4758 | parse_query_flush_pending_token(grn_ctx *ctx, efs_info *q) |
| 4759 | { |
| 4760 | const char *cur_keep; |
| 4761 | |
| 4762 | if (!(q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR)) { |
| 4763 | return; |
| 4764 | } |
| 4765 | |
| 4766 | if (q->pending_token.string_length == 0) { |
| 4767 | return; |
| 4768 | } |
| 4769 | |
| 4770 | cur_keep = q->cur; |
| 4771 | q->cur = q->pending_token.string; |
| 4772 | if (q->pending_token.token == GRN_EXPR_TOKEN_ADJUST || |
| 4773 | q->pending_token.token == GRN_EXPR_TOKEN_NEGATIVE) { |
| 4774 | parse_query_push_weight(ctx, q, q->pending_token.weight); |
| 4775 | } |
| 4776 | PARSE(q->pending_token.token); |
| 4777 | q->cur = cur_keep; |
| 4778 | |
| 4779 | q->pending_token.string = NULL; |
| 4780 | q->pending_token.string_length = 0; |
| 4781 | q->pending_token.token = 0; |
| 4782 | q->pending_token.weight = 0; |
| 4783 | } |
| 4784 | |
| 4785 | static void |
| 4786 | parse_query_accept_logical_op(grn_ctx *ctx, |
| 4787 | efs_info *q, |
| 4788 | const char *string, |
| 4789 | unsigned int string_length, |
| 4790 | int token) |
| 4791 | { |
| 4792 | if (!(q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR)) { |
| 4793 | PARSE(token); |
| 4794 | return; |
| 4795 | } |
| 4796 | |
| 4797 | if (q->pending_token.string_length > 0) { |
| 4798 | parse_query_accept_string(ctx, |
| 4799 | q, |
| 4800 | q->pending_token.string, |
| 4801 | q->pending_token.string_length); |
| 4802 | } |
| 4803 | |
| 4804 | q->pending_token.string = string; |
| 4805 | q->pending_token.string_length = string_length; |
| 4806 | q->pending_token.token = token; |
| 4807 | } |
| 4808 | |
| 4809 | static void |
| 4810 | parse_query_accept_adjust(grn_ctx *ctx, |
| 4811 | efs_info *q, |
| 4812 | const char *string, |
| 4813 | unsigned int string_length, |
| 4814 | int token, |
| 4815 | float weight) |
| 4816 | { |
| 4817 | if (!(q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR)) { |
| 4818 | PARSE(token); |
| 4819 | parse_query_push_weight(ctx, q, weight); |
| 4820 | return; |
| 4821 | } |
| 4822 | |
| 4823 | parse_query_flush_pending_token(ctx, q); |
| 4824 | |
| 4825 | q->pending_token.string = string; |
| 4826 | q->pending_token.string_length = string_length; |
| 4827 | q->pending_token.token = token; |
| 4828 | q->pending_token.weight = weight; |
| 4829 | } |
| 4830 | |
| 4831 | static grn_rc |
| 4832 | parse_query_word(grn_ctx *ctx, efs_info *q) |
| 4833 | { |
| 4834 | const char *end; |
| 4835 | unsigned int len; |
| 4836 | bool need_mode_pop = false; |
| 4837 | GRN_BULK_REWIND(&q->buf); |
| 4838 | for (end = q->cur;;) { |
| 4839 | /* null check and length check */ |
| 4840 | if (!(len = grn_charlen(ctx, end, q->str_end))) { |
| 4841 | q->cur = q->str_end; |
| 4842 | break; |
| 4843 | } |
| 4844 | if (grn_isspace(end, ctx->encoding) || *end == GRN_QUERY_PARENL || |
| 4845 | *end == GRN_QUERY_PARENR) { |
| 4846 | q->cur = end; |
| 4847 | break; |
| 4848 | } |
| 4849 | if (q->flags & GRN_EXPR_ALLOW_COLUMN && *end == GRN_QUERY_COLUMN) { |
| 4850 | grn_operator mode; |
| 4851 | grn_obj *c = grn_obj_column(ctx, |
| 4852 | q->table, |
| 4853 | GRN_TEXT_VALUE(&q->buf), |
| 4854 | GRN_TEXT_LEN(&q->buf)); |
| 4855 | if (c && end + 1 < q->str_end) { |
| 4856 | switch (end[1]) { |
| 4857 | case '!': |
| 4858 | mode = GRN_OP_NOT_EQUAL; |
| 4859 | q->cur = end + 2; |
| 4860 | break; |
| 4861 | case '=': |
| 4862 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 4863 | mode = GRN_OP_ASSIGN; |
| 4864 | q->cur = end + 2; |
| 4865 | } else { |
| 4866 | mode = GRN_OP_EQUAL; |
| 4867 | q->cur = end + 1; |
| 4868 | } |
| 4869 | break; |
| 4870 | case '<': |
| 4871 | if (end + 2 < q->str_end && end[2] == '=') { |
| 4872 | mode = GRN_OP_LESS_EQUAL; |
| 4873 | q->cur = end + 3; |
| 4874 | } else { |
| 4875 | mode = GRN_OP_LESS; |
| 4876 | q->cur = end + 2; |
| 4877 | } |
| 4878 | break; |
| 4879 | case '>': |
| 4880 | if (end + 2 < q->str_end && end[2] == '=') { |
| 4881 | mode = GRN_OP_GREATER_EQUAL; |
| 4882 | q->cur = end + 3; |
| 4883 | } else { |
| 4884 | mode = GRN_OP_GREATER; |
| 4885 | q->cur = end + 2; |
| 4886 | } |
| 4887 | break; |
| 4888 | case '@': |
| 4889 | mode = GRN_OP_MATCH; |
| 4890 | q->cur = end + 2; |
| 4891 | break; |
| 4892 | case '^': |
| 4893 | if (q->flags & GRN_EXPR_DISABLE_PREFIX_SEARCH) { |
| 4894 | mode = GRN_OP_EQUAL; |
| 4895 | q->cur = end + 1; |
| 4896 | } else { |
| 4897 | mode = GRN_OP_PREFIX; |
| 4898 | q->cur = end + 2; |
| 4899 | } |
| 4900 | break; |
| 4901 | case '$': |
| 4902 | mode = GRN_OP_SUFFIX; |
| 4903 | q->cur = end + 2; |
| 4904 | break; |
| 4905 | case '~': |
| 4906 | mode = GRN_OP_REGEXP; |
| 4907 | q->cur = end + 2; |
| 4908 | break; |
| 4909 | default: |
| 4910 | mode = GRN_OP_EQUAL; |
| 4911 | q->cur = end + 1; |
| 4912 | break; |
| 4913 | } |
| 4914 | } else if (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR) { |
| 4915 | GRN_TEXT_PUT(ctx, &q->buf, end, len); |
| 4916 | end += len; |
| 4917 | continue; |
| 4918 | } else { |
| 4919 | ERR(GRN_INVALID_ARGUMENT, "column lookup failed"); |
| 4920 | q->cur = q->str_end; |
| 4921 | return ctx->rc; |
| 4922 | } |
| 4923 | parse_query_flush_pending_token(ctx, q); |
| 4924 | PARSE(GRN_EXPR_TOKEN_IDENTIFIER); |
| 4925 | PARSE(GRN_EXPR_TOKEN_RELATIVE_OP); |
| 4926 | |
| 4927 | grn_expr_take_obj(ctx, q->e, c); |
| 4928 | GRN_PTR_PUT(ctx, &q->column_stack, c); |
| 4929 | GRN_INT32_PUT(ctx, &q->mode_stack, mode); |
| 4930 | |
| 4931 | return GRN_SUCCESS; |
| 4932 | } else if (!(q->flags & GRN_EXPR_DISABLE_PREFIX_SEARCH) && |
| 4933 | GRN_TEXT_LEN(&q->buf) > 0 && *end == GRN_QUERY_PREFIX) { |
| 4934 | q->cur = end + 1; |
| 4935 | GRN_INT32_PUT(ctx, &q->mode_stack, GRN_OP_PREFIX); |
| 4936 | need_mode_pop = true; |
| 4937 | break; |
| 4938 | } else if (*end == GRN_QUERY_ESCAPE) { |
| 4939 | end += len; |
| 4940 | if (!(len = grn_charlen(ctx, end, q->str_end))) { |
| 4941 | q->cur = q->str_end; |
| 4942 | break; |
| 4943 | } |
| 4944 | } |
| 4945 | GRN_TEXT_PUT(ctx, &q->buf, end, len); |
| 4946 | end += len; |
| 4947 | } |
| 4948 | parse_query_flush_pending_token(ctx, q); |
| 4949 | parse_query_accept_string(ctx, |
| 4950 | q, |
| 4951 | GRN_TEXT_VALUE(&q->buf), |
| 4952 | GRN_TEXT_LEN(&q->buf)); |
| 4953 | if (need_mode_pop) { |
| 4954 | int32_t mode; |
| 4955 | GRN_INT32_POP(&(q->mode_stack), mode); |
| 4956 | } |
| 4957 | |
| 4958 | return GRN_SUCCESS; |
| 4959 | } |
| 4960 | |
| 4961 | static grn_rc |
| 4962 | parse_query(grn_ctx *ctx, efs_info *q) |
| 4963 | { |
| 4964 | efs_op op_, *op = &op_; |
| 4965 | bool first_token = true; |
| 4966 | bool only_first_and = false; |
| 4967 | bool block_started = false; |
| 4968 | |
| 4969 | op->op = q->default_op; |
| 4970 | op->weight = DEFAULT_WEIGHT; |
| 4971 | while (!ctx->rc) { |
| 4972 | skip_space(ctx, q); |
| 4973 | |
| 4974 | if (q->cur >= q->str_end) { |
| 4975 | goto exit; |
| 4976 | } |
| 4977 | if (*q->cur == '\0') { |
| 4978 | goto exit; |
| 4979 | } |
| 4980 | |
| 4981 | switch (*q->cur) { |
| 4982 | case GRN_QUERY_PARENR: |
| 4983 | if (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR) { |
| 4984 | if (q->paren_depth == 0) { |
| 4985 | const char parenr = GRN_QUERY_PARENR; |
| 4986 | parse_query_flush_pending_token(ctx, q); |
| 4987 | parse_query_accept_string(ctx, q, &parenr, 1); |
| 4988 | q->cur++; |
| 4989 | break; |
| 4990 | } |
| 4991 | if (first_token) { |
| 4992 | const char parenl = GRN_QUERY_PARENL; |
| 4993 | const char parenr = GRN_QUERY_PARENR; |
| 4994 | parse_query_flush_pending_token(ctx, q); |
| 4995 | parse_query_accept_string(ctx, q, &parenl, 1); |
| 4996 | parse_query_accept_string(ctx, q, &parenr, 1); |
| 4997 | } |
| 4998 | if (only_first_and) { |
| 4999 | const char query_and[] = {GRN_QUERY_AND}; |
| 5000 | parse_query_flush_pending_token(ctx, q); |
| 5001 | parse_query_accept_string(ctx, q, query_and, 1); |
| 5002 | } |
| 5003 | } |
| 5004 | parse_query_flush_pending_token(ctx, q); |
| 5005 | PARSE(GRN_EXPR_TOKEN_PARENR); |
| 5006 | q->paren_depth--; |
| 5007 | q->cur++; |
| 5008 | break; |
| 5009 | case GRN_QUERY_QUOTEL: |
| 5010 | q->cur++; |
| 5011 | |
| 5012 | { |
| 5013 | bool closed = false; |
| 5014 | const char *start, *s; |
| 5015 | start = s = q->cur; |
| 5016 | GRN_BULK_REWIND(&q->buf); |
| 5017 | while (1) { |
| 5018 | unsigned int len; |
| 5019 | if (s >= q->str_end) { |
| 5020 | q->cur = s; |
| 5021 | break; |
| 5022 | } |
| 5023 | len = grn_charlen(ctx, s, q->str_end); |
| 5024 | if (len == 0) { |
| 5025 | /* invalid string containing malformed multibyte char */ |
| 5026 | goto exit; |
| 5027 | } else if (len == 1) { |
| 5028 | if (*s == GRN_QUERY_QUOTER) { |
| 5029 | q->cur = s + 1; |
| 5030 | closed = true; |
| 5031 | break; |
| 5032 | } else if (*s == GRN_QUERY_ESCAPE && s + 1 < q->str_end) { |
| 5033 | s++; |
| 5034 | len = grn_charlen(ctx, s, q->str_end); |
| 5035 | } |
| 5036 | } |
| 5037 | GRN_TEXT_PUT(ctx, &q->buf, s, len); |
| 5038 | s += len; |
| 5039 | } |
| 5040 | if (!closed && (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR)) { |
| 5041 | q->cur = start - 1; |
| 5042 | parse_query_word(ctx, q); |
| 5043 | } else { |
| 5044 | parse_query_flush_pending_token(ctx, q); |
| 5045 | parse_query_accept_string(ctx, |
| 5046 | q, |
| 5047 | GRN_TEXT_VALUE(&q->buf), |
| 5048 | GRN_TEXT_LEN(&q->buf)); |
| 5049 | } |
| 5050 | } |
| 5051 | |
| 5052 | break; |
| 5053 | case GRN_QUERY_PREFIX: |
| 5054 | q->cur++; |
| 5055 | { |
| 5056 | parse_query_op_data data = {0}; |
| 5057 | if (parse_query_op(q, op, &data)) { |
| 5058 | switch (data.mode) { |
| 5059 | case GRN_OP_NEAR: |
| 5060 | case GRN_OP_NEAR_NO_OFFSET: |
| 5061 | GRN_INT32_PUT(ctx, |
| 5062 | &q->max_interval_stack, |
| 5063 | data.options.near.max_interval); |
| 5064 | GRN_PTR_PUT(ctx, |
| 5065 | &q->max_element_intervals_stack, |
| 5066 | data.options.near.max_element_intervals); |
| 5067 | GRN_INT32_PUT(ctx, |
| 5068 | &q->min_interval_stack, |
| 5069 | data.options.near.min_interval); |
| 5070 | break; |
| 5071 | case GRN_OP_NEAR_PHRASE: |
| 5072 | case GRN_OP_ORDERED_NEAR_PHRASE: |
| 5073 | case GRN_OP_NEAR_PHRASE_PRODUCT: |
| 5074 | case GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT: |
| 5075 | GRN_INT32_PUT(ctx, |
| 5076 | &q->max_interval_stack, |
| 5077 | data.options.near.max_interval); |
| 5078 | GRN_INT32_PUT(ctx, |
| 5079 | &q->additional_last_interval_stack, |
| 5080 | data.options.near.additional_last_interval); |
| 5081 | GRN_PTR_PUT(ctx, |
| 5082 | &q->max_element_intervals_stack, |
| 5083 | data.options.near.max_element_intervals); |
| 5084 | GRN_INT32_PUT(ctx, |
| 5085 | &q->min_interval_stack, |
| 5086 | data.options.near.min_interval); |
| 5087 | break; |
| 5088 | case GRN_OP_SIMILAR: |
| 5089 | GRN_INT32_PUT(ctx, |
| 5090 | &q->similarity_threshold_stack, |
| 5091 | data.options.similar.threshold); |
| 5092 | break; |
| 5093 | case GRN_OP_QUORUM: |
| 5094 | GRN_INT32_PUT(ctx, |
| 5095 | &q->quorum_threshold_stack, |
| 5096 | data.options.quorum.threshold); |
| 5097 | break; |
| 5098 | default: |
| 5099 | break; |
| 5100 | } |
| 5101 | GRN_INT32_PUT(ctx, &q->mode_stack, data.mode); |
| 5102 | parse_query_flush_pending_token(ctx, q); |
| 5103 | PARSE(GRN_EXPR_TOKEN_RELATIVE_OP); |
| 5104 | } else { |
| 5105 | q->cur--; |
| 5106 | parse_query_word(ctx, q); |
| 5107 | } |
| 5108 | } |
| 5109 | break; |
| 5110 | case GRN_QUERY_AND: |
| 5111 | if (first_token) { |
| 5112 | only_first_and = true; |
| 5113 | } else { |
| 5114 | op->op = GRN_OP_AND; |
| 5115 | parse_query_accept_logical_op(ctx, |
| 5116 | q, |
| 5117 | q->cur, |
| 5118 | 1, |
| 5119 | GRN_EXPR_TOKEN_LOGICAL_AND); |
| 5120 | } |
| 5121 | q->cur++; |
| 5122 | break; |
| 5123 | case GRN_QUERY_AND_NOT: |
| 5124 | if (q->flags & GRN_EXPR_DISABLE_AND_NOT) { |
| 5125 | parse_query_word(ctx, q); |
| 5126 | } else { |
| 5127 | if (first_token) { |
| 5128 | if (q->flags & GRN_EXPR_ALLOW_LEADING_NOT) { |
| 5129 | grn_obj *all_records = grn_ctx_get(ctx, "all_records", 11); |
| 5130 | if (all_records) { |
| 5131 | /* dummy token */ |
| 5132 | PARSE(GRN_EXPR_TOKEN_QSTRING); |
| 5133 | grn_expr_append_obj(ctx, q->e, all_records, GRN_OP_PUSH, 1); |
| 5134 | grn_expr_append_op(ctx, q->e, GRN_OP_CALL, 0); |
| 5135 | } |
| 5136 | } else if (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR) { |
| 5137 | parse_query_flush_pending_token(ctx, q); |
| 5138 | parse_query_accept_string(ctx, q, q->cur, 1); |
| 5139 | q->cur++; |
| 5140 | break; |
| 5141 | } |
| 5142 | } |
| 5143 | op->op = GRN_OP_AND_NOT; |
| 5144 | parse_query_accept_logical_op(ctx, |
| 5145 | q, |
| 5146 | q->cur, |
| 5147 | 1, |
| 5148 | GRN_EXPR_TOKEN_LOGICAL_AND_NOT); |
| 5149 | q->cur++; |
| 5150 | } |
| 5151 | break; |
| 5152 | case GRN_QUERY_ADJ_INC: |
| 5153 | { |
| 5154 | const char *cur = q->cur; |
| 5155 | uint32_t adjust_mark_length = 1; |
| 5156 | q->cur++; |
| 5157 | float weight; |
| 5158 | if (!parse_query_parse_weight(ctx, q, op, &weight)) { |
| 5159 | if (op->weight < 127) { |
| 5160 | op->weight++; |
| 5161 | } |
| 5162 | weight = op->weight; |
| 5163 | } |
| 5164 | op->op = GRN_OP_ADJUST; |
| 5165 | parse_query_accept_adjust(ctx, |
| 5166 | q, |
| 5167 | cur, |
| 5168 | adjust_mark_length, |
| 5169 | GRN_EXPR_TOKEN_ADJUST, |
| 5170 | weight); |
| 5171 | } |
| 5172 | break; |
| 5173 | case GRN_QUERY_ADJ_DEC: |
| 5174 | { |
| 5175 | const char *cur = q->cur; |
| 5176 | uint32_t adjust_mark_length = 1; |
| 5177 | q->cur++; |
| 5178 | float weight; |
| 5179 | if (parse_query_parse_weight(ctx, q, op, &weight)) { |
| 5180 | weight = -weight; |
| 5181 | } else { |
| 5182 | if (op->weight > -128) { |
| 5183 | op->weight--; |
| 5184 | } |
| 5185 | weight = op->weight; |
| 5186 | } |
| 5187 | op->op = GRN_OP_ADJUST; |
| 5188 | parse_query_accept_adjust(ctx, |
| 5189 | q, |
| 5190 | cur, |
| 5191 | adjust_mark_length, |
| 5192 | GRN_EXPR_TOKEN_ADJUST, |
| 5193 | weight); |
| 5194 | } |
| 5195 | break; |
| 5196 | case GRN_QUERY_ADJ_NEG: |
| 5197 | if (first_token) { |
| 5198 | parse_query_flush_pending_token(ctx, q); |
| 5199 | parse_query_accept_string(ctx, q, q->cur, 1); |
| 5200 | } else { |
| 5201 | const char *cur = q->cur; |
| 5202 | uint32_t adjust_mark_length = 1; |
| 5203 | q->cur++; |
| 5204 | float weight; |
| 5205 | if (parse_query_parse_weight(ctx, q, op, &weight)) { |
| 5206 | weight = -1 - weight; |
| 5207 | } else { |
| 5208 | weight = -DEFAULT_WEIGHT; |
| 5209 | } |
| 5210 | op->op = GRN_OP_ADJUST; |
| 5211 | parse_query_accept_adjust(ctx, |
| 5212 | q, |
| 5213 | cur, |
| 5214 | adjust_mark_length, |
| 5215 | GRN_EXPR_TOKEN_NEGATIVE, |
| 5216 | weight); |
| 5217 | } |
| 5218 | break; |
| 5219 | case GRN_QUERY_PARENL: |
| 5220 | parse_query_flush_pending_token(ctx, q); |
| 5221 | PARSE(GRN_EXPR_TOKEN_PARENL); |
| 5222 | q->cur++; |
| 5223 | q->paren_depth++; |
| 5224 | block_started = true; |
| 5225 | break; |
| 5226 | case 'O': |
| 5227 | if (q->cur + 2 < q->str_end && q->cur[1] == 'R' && q->cur[2] == ' ') { |
| 5228 | if (first_token && (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR)) { |
| 5229 | parse_query_flush_pending_token(ctx, q); |
| 5230 | parse_query_accept_string(ctx, q, q->cur, 2); |
| 5231 | } else { |
| 5232 | parse_query_accept_logical_op(ctx, |
| 5233 | q, |
| 5234 | q->cur, |
| 5235 | 2, |
| 5236 | GRN_EXPR_TOKEN_LOGICAL_OR); |
| 5237 | } |
| 5238 | q->cur += 2; |
| 5239 | break; |
| 5240 | } |
| 5241 | /* fallthru */ |
| 5242 | default: |
| 5243 | parse_query_word(ctx, q); |
| 5244 | break; |
| 5245 | } |
| 5246 | if (!first_token) { |
| 5247 | only_first_and = false; |
| 5248 | } |
| 5249 | first_token = block_started; |
| 5250 | block_started = false; |
| 5251 | } |
| 5252 | exit: |
| 5253 | if (q->flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR) { |
| 5254 | if (q->pending_token.string_length > 0) { |
| 5255 | parse_query_accept_string(ctx, |
| 5256 | q, |
| 5257 | q->pending_token.string, |
| 5258 | q->pending_token.string_length); |
| 5259 | } else if (only_first_and) { |
| 5260 | const char query_and[] = {GRN_QUERY_AND}; |
| 5261 | parse_query_accept_string(ctx, q, query_and, 1); |
| 5262 | } |
| 5263 | if (q->paren_depth > 0) { |
| 5264 | int paren_depth = q->paren_depth; |
| 5265 | while (paren_depth > 0) { |
| 5266 | const char parenl = GRN_QUERY_PARENL; |
| 5267 | parse_query_accept_string(ctx, q, &parenl, 1); |
| 5268 | PARSE(GRN_EXPR_TOKEN_PARENR); |
| 5269 | paren_depth--; |
| 5270 | } |
| 5271 | } |
| 5272 | } |
| 5273 | PARSE(0); |
| 5274 | return GRN_SUCCESS; |
| 5275 | } |
| 5276 | |
| 5277 | static grn_rc |
| 5278 | get_string(grn_ctx *ctx, efs_info *q, char quote) |
| 5279 | { |
| 5280 | const char *s; |
| 5281 | unsigned int len; |
| 5282 | grn_rc rc = GRN_END_OF_DATA; |
| 5283 | GRN_BULK_REWIND(&q->buf); |
| 5284 | for (s = q->cur + 1; s < q->str_end; s += len) { |
| 5285 | if (!(len = grn_charlen(ctx, s, q->str_end))) { |
| 5286 | break; |
| 5287 | } |
| 5288 | if (len == 1) { |
| 5289 | if (*s == quote) { |
| 5290 | s++; |
| 5291 | rc = GRN_SUCCESS; |
| 5292 | break; |
| 5293 | } |
| 5294 | if (*s == GRN_QUERY_ESCAPE && s + 1 < q->str_end) { |
| 5295 | s++; |
| 5296 | if (!(len = grn_charlen(ctx, s, q->str_end))) { |
| 5297 | break; |
| 5298 | } |
| 5299 | } |
| 5300 | } |
| 5301 | GRN_TEXT_PUT(ctx, &q->buf, s, len); |
| 5302 | } |
| 5303 | q->cur = s; |
| 5304 | return rc; |
| 5305 | } |
| 5306 | |
| 5307 | static grn_obj * |
| 5308 | resolve_top_level_name(grn_ctx *ctx, const char *name, unsigned int name_size) |
| 5309 | { |
| 5310 | unsigned int i; |
| 5311 | unsigned int first_delimiter_position = 0; |
| 5312 | unsigned int n_delimiters = 0; |
| 5313 | grn_obj *top_level_object; |
| 5314 | grn_obj *object; |
| 5315 | |
| 5316 | for (i = 0; i < name_size; i++) { |
| 5317 | if (name[i] != GRN_DB_DELIMITER) { |
| 5318 | continue; |
| 5319 | } |
| 5320 | |
| 5321 | if (n_delimiters == 0) { |
| 5322 | first_delimiter_position = i; |
| 5323 | } |
| 5324 | n_delimiters++; |
| 5325 | } |
| 5326 | |
| 5327 | if (n_delimiters < 2) { |
| 5328 | return grn_ctx_get(ctx, name, name_size); |
| 5329 | } |
| 5330 | |
| 5331 | top_level_object = grn_ctx_get(ctx, name, first_delimiter_position); |
| 5332 | if (!top_level_object) { |
| 5333 | return NULL; |
| 5334 | } |
| 5335 | object = grn_obj_column(ctx, |
| 5336 | top_level_object, |
| 5337 | name + first_delimiter_position + 1, |
| 5338 | name_size - first_delimiter_position - 1); |
| 5339 | grn_obj_unlink(ctx, top_level_object); |
| 5340 | return object; |
| 5341 | } |
| 5342 | |
| 5343 | static grn_rc |
| 5344 | get_identifier(grn_ctx *ctx, efs_info *q, grn_obj *name_resolve_context) |
| 5345 | { |
| 5346 | const char *s; |
| 5347 | unsigned int len; |
| 5348 | grn_rc rc = GRN_SUCCESS; |
| 5349 | for (s = q->cur; s < q->str_end; s += len) { |
| 5350 | if (!(len = grn_charlen(ctx, s, q->str_end))) { |
| 5351 | rc = GRN_END_OF_DATA; |
| 5352 | goto exit; |
| 5353 | } |
| 5354 | if (grn_isspace(s, ctx->encoding)) { |
| 5355 | goto done; |
| 5356 | } |
| 5357 | if (len == 1) { |
| 5358 | switch (*s) { |
| 5359 | case '\0': |
| 5360 | case '(': |
| 5361 | case ')': |
| 5362 | case '{': |
| 5363 | case '}': |
| 5364 | case '[': |
| 5365 | case ']': |
| 5366 | case ',': |
| 5367 | case ':': |
| 5368 | case '@': |
| 5369 | case '?': |
| 5370 | case '"': |
| 5371 | case '*': |
| 5372 | case '+': |
| 5373 | case '-': |
| 5374 | case '|': |
| 5375 | case '/': |
| 5376 | case '%': |
| 5377 | case '!': |
| 5378 | case '^': |
| 5379 | case '&': |
| 5380 | case '>': |
| 5381 | case '<': |
| 5382 | case '=': |
| 5383 | case '~': |
| 5384 | /* case '.' : */ |
| 5385 | goto done; |
| 5386 | break; |
| 5387 | } |
| 5388 | } |
| 5389 | } |
| 5390 | done: |
| 5391 | len = s - q->cur; |
| 5392 | switch (*q->cur) { |
| 5393 | case 'd': |
| 5394 | if (len == 6 && !memcmp(q->cur, "delete", 6)) { |
| 5395 | PARSE(GRN_EXPR_TOKEN_DELETE); |
| 5396 | goto exit; |
| 5397 | } |
| 5398 | break; |
| 5399 | case 'f': |
| 5400 | if (len == 5 && !memcmp(q->cur, "false", 5)) { |
| 5401 | grn_obj buf; |
| 5402 | PARSE(GRN_EXPR_TOKEN_BOOLEAN); |
| 5403 | GRN_BOOL_INIT(&buf, 0); |
| 5404 | GRN_BOOL_SET(ctx, &buf, 0); |
| 5405 | grn_expr_append_const(ctx, q->e, &buf, GRN_OP_PUSH, 1); |
| 5406 | GRN_OBJ_FIN(ctx, &buf); |
| 5407 | goto exit; |
| 5408 | } |
| 5409 | break; |
| 5410 | case 'i': |
| 5411 | if (len == 2 && !memcmp(q->cur, "in", 2)) { |
| 5412 | PARSE(GRN_EXPR_TOKEN_IN); |
| 5413 | goto exit; |
| 5414 | } |
| 5415 | break; |
| 5416 | case 'n': |
| 5417 | if (len == 4 && !memcmp(q->cur, "null", 4)) { |
| 5418 | grn_obj buf; |
| 5419 | PARSE(GRN_EXPR_TOKEN_NULL); |
| 5420 | GRN_VOID_INIT(&buf); |
| 5421 | grn_expr_append_const(ctx, q->e, &buf, GRN_OP_PUSH, 1); |
| 5422 | GRN_OBJ_FIN(ctx, &buf); |
| 5423 | goto exit; |
| 5424 | } |
| 5425 | break; |
| 5426 | case 't': |
| 5427 | if (len == 4 && !memcmp(q->cur, "true", 4)) { |
| 5428 | grn_obj buf; |
| 5429 | PARSE(GRN_EXPR_TOKEN_BOOLEAN); |
| 5430 | GRN_BOOL_INIT(&buf, 0); |
| 5431 | GRN_BOOL_SET(ctx, &buf, 1); |
| 5432 | grn_expr_append_const(ctx, q->e, &buf, GRN_OP_PUSH, 1); |
| 5433 | GRN_OBJ_FIN(ctx, &buf); |
| 5434 | goto exit; |
| 5435 | } |
| 5436 | break; |
| 5437 | } |
| 5438 | { |
| 5439 | grn_obj *obj; |
| 5440 | const char *name = q->cur; |
| 5441 | unsigned int name_size = s - q->cur; |
| 5442 | if (name_resolve_context) { |
| 5443 | if ((obj = grn_obj_column(ctx, name_resolve_context, name, name_size))) { |
| 5444 | grn_expr_take_obj(ctx, q->e, obj); |
| 5445 | PARSE(GRN_EXPR_TOKEN_IDENTIFIER); |
| 5446 | grn_expr_append_obj(ctx, q->e, obj, GRN_OP_GET_VALUE, 2); |
| 5447 | goto exit; |
| 5448 | } |
| 5449 | } |
| 5450 | if ((obj = grn_expr_get_var(ctx, q->e, name, name_size))) { |
| 5451 | PARSE(GRN_EXPR_TOKEN_IDENTIFIER); |
| 5452 | grn_expr_append_obj(ctx, q->e, obj, GRN_OP_PUSH, 1); |
| 5453 | goto exit; |
| 5454 | } |
| 5455 | if ((obj = grn_obj_column(ctx, q->table, name, name_size))) { |
| 5456 | grn_expr_take_obj(ctx, q->e, obj); |
| 5457 | PARSE(GRN_EXPR_TOKEN_IDENTIFIER); |
| 5458 | grn_expr_append_obj(ctx, q->e, obj, GRN_OP_GET_VALUE, 1); |
| 5459 | goto exit; |
| 5460 | } |
| 5461 | if ((obj = resolve_top_level_name(ctx, name, name_size))) { |
| 5462 | grn_expr_take_obj(ctx, q->e, obj); |
| 5463 | PARSE(GRN_EXPR_TOKEN_IDENTIFIER); |
| 5464 | grn_expr_append_obj(ctx, q->e, obj, GRN_OP_PUSH, 1); |
| 5465 | goto exit; |
| 5466 | } |
| 5467 | if (q->flags & GRN_EXPR_SYNTAX_OUTPUT_COLUMNS || |
| 5468 | q->flags & GRN_EXPR_SYNTAX_SORT_KEYS) { |
| 5469 | const char *type = |
| 5470 | (q->flags & GRN_EXPR_SYNTAX_OUTPUT_COLUMNS) ? "column" : "key"; |
| 5471 | const char *identifier = name; |
| 5472 | int identifier_size = name_size; |
| 5473 | { |
| 5474 | GRN_DEFINE_NAME(q->table); |
| 5475 | GRN_LOG(ctx, |
| 5476 | GRN_WARN, |
| 5477 | "[expr][parse] ignore invalid %s: <%.*s>: " |
| 5478 | "table:<%.*s> keys:<%.*s>", |
| 5479 | type, |
| 5480 | identifier_size, |
| 5481 | identifier, |
| 5482 | name_size, |
| 5483 | name, |
| 5484 | (int)(q->str_end - q->str), |
| 5485 | q->str); |
| 5486 | } |
| 5487 | PARSE(GRN_EXPR_TOKEN_NONEXISTENT_COLUMN); |
| 5488 | } else { |
| 5489 | rc = GRN_SYNTAX_ERROR; |
| 5490 | ERR(rc, "[expr][parse] unknown identifier: <%.*s>", (int)name_size, name); |
| 5491 | } |
| 5492 | } |
| 5493 | exit: |
| 5494 | q->cur = s; |
| 5495 | return rc; |
| 5496 | } |
| 5497 | |
| 5498 | static void |
| 5499 | set_tos_minor_to_curr(grn_ctx *ctx, efs_info *q) |
| 5500 | { |
| 5501 | yyParser *parser = q->parser; |
| 5502 | yyStackEntry *yytos = parser->yytos; |
| 5503 | yytos->minor.yy0 = ((grn_expr *)(q->e))->codes_curr; |
| 5504 | } |
| 5505 | |
| 5506 | static grn_obj * |
| 5507 | parse_script_extract_name_resolve_context(grn_ctx *ctx, efs_info *q) |
| 5508 | { |
| 5509 | grn_expr *expr = (grn_expr *)(q->e); |
| 5510 | grn_expr_code *code_start; |
| 5511 | grn_expr_code *code_last; |
| 5512 | |
| 5513 | if (expr->codes_curr == 0) { |
| 5514 | return NULL; |
| 5515 | } |
| 5516 | |
| 5517 | code_start = expr->codes; |
| 5518 | code_last = code_start + (expr->codes_curr - 1); |
| 5519 | switch (code_last->op) { |
| 5520 | case GRN_OP_GET_MEMBER: |
| 5521 | { |
| 5522 | unsigned int n_used_codes_for_key; |
| 5523 | grn_expr_code *code_key; |
| 5524 | grn_expr_code *code_receiver; |
| 5525 | |
| 5526 | code_key = code_last - 1; |
| 5527 | if (code_key < code_start) { |
| 5528 | return NULL; |
| 5529 | } |
| 5530 | |
| 5531 | n_used_codes_for_key = |
| 5532 | grn_expr_code_n_used_codes(ctx, code_start, code_key); |
| 5533 | if (n_used_codes_for_key == 0) { |
| 5534 | return NULL; |
| 5535 | } |
| 5536 | code_receiver = code_key - n_used_codes_for_key; |
| 5537 | if (code_receiver < code_start) { |
| 5538 | return NULL; |
| 5539 | } |
| 5540 | return code_receiver->value; |
| 5541 | } |
| 5542 | break; |
| 5543 | default: |
| 5544 | /* TODO: Support other operators. */ |
| 5545 | return NULL; |
| 5546 | break; |
| 5547 | } |
| 5548 | } |
| 5549 | |
| 5550 | static grn_rc |
| 5551 | parse_script(grn_ctx *ctx, efs_info *q) |
| 5552 | { |
| 5553 | grn_rc rc = GRN_SUCCESS; |
| 5554 | grn_obj *name_resolve_context = NULL; |
| 5555 | for (;;) { |
| 5556 | grn_obj *current_name_resolve_context = name_resolve_context; |
| 5557 | name_resolve_context = NULL; |
| 5558 | skip_space(ctx, q); |
| 5559 | if (q->cur >= q->str_end) { |
| 5560 | rc = GRN_END_OF_DATA; |
| 5561 | goto exit; |
| 5562 | } |
| 5563 | switch (*q->cur) { |
| 5564 | case '\0': |
| 5565 | rc = GRN_END_OF_DATA; |
| 5566 | goto exit; |
| 5567 | break; |
| 5568 | case '(': |
| 5569 | PARSE(GRN_EXPR_TOKEN_PARENL); |
| 5570 | q->cur++; |
| 5571 | break; |
| 5572 | case ')': |
| 5573 | PARSE(GRN_EXPR_TOKEN_PARENR); |
| 5574 | q->cur++; |
| 5575 | break; |
| 5576 | case '{': |
| 5577 | PARSE(GRN_EXPR_TOKEN_BRACEL); |
| 5578 | q->cur++; |
| 5579 | break; |
| 5580 | case '}': |
| 5581 | PARSE(GRN_EXPR_TOKEN_BRACER); |
| 5582 | q->cur++; |
| 5583 | break; |
| 5584 | case '[': |
| 5585 | PARSE(GRN_EXPR_TOKEN_BRACKETL); |
| 5586 | q->cur++; |
| 5587 | break; |
| 5588 | case ']': |
| 5589 | PARSE(GRN_EXPR_TOKEN_BRACKETR); |
| 5590 | q->cur++; |
| 5591 | break; |
| 5592 | case ',': |
| 5593 | PARSE(GRN_EXPR_TOKEN_COMMA); |
| 5594 | q->cur++; |
| 5595 | break; |
| 5596 | case '.': |
| 5597 | PARSE(GRN_EXPR_TOKEN_DOT); |
| 5598 | name_resolve_context = parse_script_extract_name_resolve_context(ctx, q); |
| 5599 | q->cur++; |
| 5600 | break; |
| 5601 | case ':': |
| 5602 | PARSE(GRN_EXPR_TOKEN_COLON); |
| 5603 | q->cur++; |
| 5604 | set_tos_minor_to_curr(ctx, q); |
| 5605 | grn_expr_append_op(ctx, q->e, GRN_OP_JUMP, 0); |
| 5606 | break; |
| 5607 | case '@': |
| 5608 | switch (q->cur[1]) { |
| 5609 | case '^': |
| 5610 | if (q->flags & GRN_EXPR_DISABLE_PREFIX_SEARCH) { |
| 5611 | PARSE(GRN_EXPR_TOKEN_MATCH); |
| 5612 | q->cur++; |
| 5613 | } else { |
| 5614 | PARSE(GRN_EXPR_TOKEN_PREFIX); |
| 5615 | q->cur += 2; |
| 5616 | } |
| 5617 | break; |
| 5618 | case '$': |
| 5619 | PARSE(GRN_EXPR_TOKEN_SUFFIX); |
| 5620 | q->cur += 2; |
| 5621 | break; |
| 5622 | case '~': |
| 5623 | PARSE(GRN_EXPR_TOKEN_REGEXP); |
| 5624 | q->cur += 2; |
| 5625 | break; |
| 5626 | default: |
| 5627 | PARSE(GRN_EXPR_TOKEN_MATCH); |
| 5628 | q->cur++; |
| 5629 | break; |
| 5630 | } |
| 5631 | break; |
| 5632 | case '~': |
| 5633 | PARSE(GRN_EXPR_TOKEN_BITWISE_NOT); |
| 5634 | q->cur++; |
| 5635 | break; |
| 5636 | case '?': |
| 5637 | PARSE(GRN_EXPR_TOKEN_QUESTION); |
| 5638 | q->cur++; |
| 5639 | set_tos_minor_to_curr(ctx, q); |
| 5640 | grn_expr_append_op(ctx, q->e, GRN_OP_CJUMP, 0); |
| 5641 | break; |
| 5642 | case '"': |
| 5643 | if ((rc = get_string(ctx, q, '"'))) { |
| 5644 | goto exit; |
| 5645 | } |
| 5646 | PARSE(GRN_EXPR_TOKEN_STRING); |
| 5647 | grn_expr_append_const(ctx, q->e, &q->buf, GRN_OP_PUSH, 1); |
| 5648 | break; |
| 5649 | case '\'': |
| 5650 | if ((rc = get_string(ctx, q, '\''))) { |
| 5651 | goto exit; |
| 5652 | } |
| 5653 | PARSE(GRN_EXPR_TOKEN_STRING); |
| 5654 | grn_expr_append_const(ctx, q->e, &q->buf, GRN_OP_PUSH, 1); |
| 5655 | break; |
| 5656 | case '*': |
| 5657 | { |
| 5658 | bool processed = true; |
| 5659 | switch (q->cur[1]) { |
| 5660 | case 'n': |
| 5661 | case 'N': |
| 5662 | case 'O': |
| 5663 | { |
| 5664 | const char *next_start = NULL; |
| 5665 | parse_query_op_data data = {0}; |
| 5666 | int token; |
| 5667 | if (q->cur[1] == 'n') { |
| 5668 | next_start = q->cur + 2; |
| 5669 | data.mode = GRN_OP_NEAR_NO_OFFSET; |
| 5670 | token = GRN_EXPR_TOKEN_NEAR_NO_OFFSET; |
| 5671 | } else if (q->cur[1] == 'N') { |
| 5672 | next_start = q->cur + 2; |
| 5673 | data.mode = GRN_OP_NEAR; |
| 5674 | token = GRN_EXPR_TOKEN_NEAR; |
| 5675 | if (next_start < q->str_end && next_start[0] == 'P') { |
| 5676 | next_start++; |
| 5677 | if (next_start < q->str_end && next_start[0] == 'P') { |
| 5678 | next_start++; |
| 5679 | data.mode = GRN_OP_NEAR_PHRASE_PRODUCT; |
| 5680 | token = GRN_EXPR_TOKEN_NEAR_PHRASE_PRODUCT; |
| 5681 | } else { |
| 5682 | data.mode = GRN_OP_NEAR_PHRASE; |
| 5683 | token = GRN_EXPR_TOKEN_NEAR_PHRASE; |
| 5684 | } |
| 5685 | } |
| 5686 | } else { |
| 5687 | if (q->cur + 4 < q->str_end && q->cur[2] == 'N' && |
| 5688 | q->cur[3] == 'P') { |
| 5689 | next_start = q->cur + 4; |
| 5690 | if (next_start < q->str_end && next_start[0] == 'P') { |
| 5691 | next_start++; |
| 5692 | data.mode = GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT; |
| 5693 | token = GRN_EXPR_TOKEN_ORDERED_NEAR_PHRASE_PRODUCT; |
| 5694 | } else { |
| 5695 | data.mode = GRN_OP_ORDERED_NEAR_PHRASE; |
| 5696 | token = GRN_EXPR_TOKEN_ORDERED_NEAR_PHRASE; |
| 5697 | } |
| 5698 | } else { |
| 5699 | processed = false; |
| 5700 | } |
| 5701 | } |
| 5702 | if (!processed) { |
| 5703 | break; |
| 5704 | } |
| 5705 | |
| 5706 | next_start = parse_near_options(q, next_start, &data); |
| 5707 | GRN_INT32_PUT(ctx, |
| 5708 | &q->max_interval_stack, |
| 5709 | data.options.near.max_interval); |
| 5710 | if (!(data.mode == GRN_OP_NEAR || |
| 5711 | data.mode == GRN_OP_NEAR_NO_OFFSET)) { |
| 5712 | GRN_INT32_PUT(ctx, |
| 5713 | &q->additional_last_interval_stack, |
| 5714 | data.options.near.additional_last_interval); |
| 5715 | } |
| 5716 | GRN_PTR_PUT(ctx, |
| 5717 | &q->max_element_intervals_stack, |
| 5718 | data.options.near.max_element_intervals); |
| 5719 | GRN_INT32_PUT(ctx, |
| 5720 | &q->min_interval_stack, |
| 5721 | data.options.near.min_interval); |
| 5722 | PARSE(token); |
| 5723 | q->cur = next_start; |
| 5724 | } |
| 5725 | break; |
| 5726 | case 'S': |
| 5727 | { |
| 5728 | const char *next_start = q->cur + 2; |
| 5729 | const char *end; |
| 5730 | int similarity_threshold; |
| 5731 | similarity_threshold = grn_atoi(next_start, q->str_end, &end); |
| 5732 | if (end == next_start) { |
| 5733 | similarity_threshold = DEFAULT_SIMILARITY_THRESHOLD; |
| 5734 | } else { |
| 5735 | next_start = end; |
| 5736 | } |
| 5737 | GRN_INT32_PUT(ctx, |
| 5738 | &q->similarity_threshold_stack, |
| 5739 | similarity_threshold); |
| 5740 | PARSE(GRN_EXPR_TOKEN_SIMILAR); |
| 5741 | q->cur = next_start; |
| 5742 | } |
| 5743 | break; |
| 5744 | case 'T': |
| 5745 | PARSE(GRN_EXPR_TOKEN_TERM_EXTRACT); |
| 5746 | q->cur += 2; |
| 5747 | break; |
| 5748 | case 'Q': |
| 5749 | { |
| 5750 | const char *next_start = q->cur + 2; |
| 5751 | const char *end; |
| 5752 | int quorum_threshold; |
| 5753 | quorum_threshold = grn_atoi(next_start, q->str_end, &end); |
| 5754 | if (end == next_start) { |
| 5755 | quorum_threshold = DEFAULT_QUORUM_THRESHOLD; |
| 5756 | } else { |
| 5757 | next_start = end; |
| 5758 | } |
| 5759 | GRN_INT32_PUT(ctx, &q->quorum_threshold_stack, quorum_threshold); |
| 5760 | PARSE(GRN_EXPR_TOKEN_QUORUM); |
| 5761 | q->cur = next_start; |
| 5762 | } |
| 5763 | break; |
| 5764 | case '>': |
| 5765 | q->cur += 2; |
| 5766 | { |
| 5767 | float weight; |
| 5768 | if (!parse_query_parse_weight(ctx, q, NULL, &weight)) { |
| 5769 | weight = 2; |
| 5770 | } |
| 5771 | parse_query_push_weight(ctx, q, weight); |
| 5772 | } |
| 5773 | PARSE(GRN_EXPR_TOKEN_ADJUST); |
| 5774 | break; |
| 5775 | case '<': |
| 5776 | q->cur += 2; |
| 5777 | { |
| 5778 | float weight; |
| 5779 | if (parse_query_parse_weight(ctx, q, NULL, &weight)) { |
| 5780 | weight = -weight; |
| 5781 | } else { |
| 5782 | weight = 0.5; |
| 5783 | } |
| 5784 | parse_query_push_weight(ctx, q, weight); |
| 5785 | } |
| 5786 | PARSE(GRN_EXPR_TOKEN_ADJUST); |
| 5787 | break; |
| 5788 | case '~': |
| 5789 | q->cur += 2; |
| 5790 | { |
| 5791 | float weight; |
| 5792 | if (parse_query_parse_weight(ctx, q, NULL, &weight)) { |
| 5793 | weight = -weight; |
| 5794 | } else { |
| 5795 | weight = -1; |
| 5796 | } |
| 5797 | parse_query_push_weight(ctx, q, weight); |
| 5798 | } |
| 5799 | PARSE(GRN_EXPR_TOKEN_NEGATIVE); |
| 5800 | break; |
| 5801 | case '=': |
| 5802 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5803 | PARSE(GRN_EXPR_TOKEN_STAR_ASSIGN); |
| 5804 | q->cur += 2; |
| 5805 | } else { |
| 5806 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5807 | "'*=' is not allowed: <%.*s>", |
| 5808 | (int)(q->str_end - q->str), |
| 5809 | q->str); |
| 5810 | } |
| 5811 | break; |
| 5812 | default: |
| 5813 | processed = false; |
| 5814 | break; |
| 5815 | } |
| 5816 | if (!processed) { |
| 5817 | PARSE(GRN_EXPR_TOKEN_STAR); |
| 5818 | q->cur++; |
| 5819 | } |
| 5820 | break; |
| 5821 | } |
| 5822 | case '+': |
| 5823 | switch (q->cur[1]) { |
| 5824 | case '+': |
| 5825 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5826 | PARSE(GRN_EXPR_TOKEN_INCR); |
| 5827 | q->cur += 2; |
| 5828 | } else { |
| 5829 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5830 | "'++' is not allowed: <%.*s>", |
| 5831 | (int)(q->str_end - q->str), |
| 5832 | q->str); |
| 5833 | } |
| 5834 | break; |
| 5835 | case '=': |
| 5836 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5837 | PARSE(GRN_EXPR_TOKEN_PLUS_ASSIGN); |
| 5838 | q->cur += 2; |
| 5839 | } else { |
| 5840 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5841 | "'+=' is not allowed: <%.*s>", |
| 5842 | (int)(q->str_end - q->str), |
| 5843 | q->str); |
| 5844 | } |
| 5845 | break; |
| 5846 | default: |
| 5847 | PARSE(GRN_EXPR_TOKEN_PLUS); |
| 5848 | q->cur++; |
| 5849 | break; |
| 5850 | } |
| 5851 | break; |
| 5852 | case '-': |
| 5853 | switch (q->cur[1]) { |
| 5854 | case '-': |
| 5855 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5856 | PARSE(GRN_EXPR_TOKEN_DECR); |
| 5857 | q->cur += 2; |
| 5858 | } else { |
| 5859 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5860 | "'--' is not allowed: <%.*s>", |
| 5861 | (int)(q->str_end - q->str), |
| 5862 | q->str); |
| 5863 | } |
| 5864 | break; |
| 5865 | case '=': |
| 5866 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5867 | PARSE(GRN_EXPR_TOKEN_MINUS_ASSIGN); |
| 5868 | q->cur += 2; |
| 5869 | } else { |
| 5870 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5871 | "'-=' is not allowed: <%.*s>", |
| 5872 | (int)(q->str_end - q->str), |
| 5873 | q->str); |
| 5874 | } |
| 5875 | break; |
| 5876 | default: |
| 5877 | PARSE(GRN_EXPR_TOKEN_MINUS); |
| 5878 | q->cur++; |
| 5879 | break; |
| 5880 | } |
| 5881 | break; |
| 5882 | case '|': |
| 5883 | switch (q->cur[1]) { |
| 5884 | case '|': |
| 5885 | PARSE(GRN_EXPR_TOKEN_LOGICAL_OR); |
| 5886 | q->cur += 2; |
| 5887 | break; |
| 5888 | case '=': |
| 5889 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5890 | PARSE(GRN_EXPR_TOKEN_OR_ASSIGN); |
| 5891 | q->cur += 2; |
| 5892 | } else { |
| 5893 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5894 | "'|=' is not allowed: <%.*s>", |
| 5895 | (int)(q->str_end - q->str), |
| 5896 | q->str); |
| 5897 | } |
| 5898 | break; |
| 5899 | default: |
| 5900 | PARSE(GRN_EXPR_TOKEN_BITWISE_OR); |
| 5901 | q->cur++; |
| 5902 | break; |
| 5903 | } |
| 5904 | break; |
| 5905 | case '/': |
| 5906 | switch (q->cur[1]) { |
| 5907 | case '=': |
| 5908 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5909 | PARSE(GRN_EXPR_TOKEN_SLASH_ASSIGN); |
| 5910 | q->cur += 2; |
| 5911 | } else { |
| 5912 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5913 | "'/=' is not allowed: <%.*s>", |
| 5914 | (int)(q->str_end - q->str), |
| 5915 | q->str); |
| 5916 | } |
| 5917 | break; |
| 5918 | default: |
| 5919 | PARSE(GRN_EXPR_TOKEN_SLASH); |
| 5920 | q->cur++; |
| 5921 | break; |
| 5922 | } |
| 5923 | break; |
| 5924 | case '%': |
| 5925 | switch (q->cur[1]) { |
| 5926 | case '=': |
| 5927 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5928 | PARSE(GRN_EXPR_TOKEN_MOD_ASSIGN); |
| 5929 | q->cur += 2; |
| 5930 | } else { |
| 5931 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5932 | "'%%=' is not allowed: <%.*s>", |
| 5933 | (int)(q->str_end - q->str), |
| 5934 | q->str); |
| 5935 | } |
| 5936 | break; |
| 5937 | default: |
| 5938 | PARSE(GRN_EXPR_TOKEN_MOD); |
| 5939 | q->cur++; |
| 5940 | break; |
| 5941 | } |
| 5942 | break; |
| 5943 | case '!': |
| 5944 | switch (q->cur[1]) { |
| 5945 | case '=': |
| 5946 | PARSE(GRN_EXPR_TOKEN_NOT_EQUAL); |
| 5947 | q->cur += 2; |
| 5948 | break; |
| 5949 | default: |
| 5950 | PARSE(GRN_EXPR_TOKEN_NOT); |
| 5951 | q->cur++; |
| 5952 | break; |
| 5953 | } |
| 5954 | break; |
| 5955 | case '^': |
| 5956 | switch (q->cur[1]) { |
| 5957 | case '=': |
| 5958 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5959 | q->cur += 2; |
| 5960 | PARSE(GRN_EXPR_TOKEN_XOR_ASSIGN); |
| 5961 | } else { |
| 5962 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5963 | "'^=' is not allowed: <%.*s>", |
| 5964 | (int)(q->str_end - q->str), |
| 5965 | q->str); |
| 5966 | } |
| 5967 | break; |
| 5968 | default: |
| 5969 | PARSE(GRN_EXPR_TOKEN_BITWISE_XOR); |
| 5970 | q->cur++; |
| 5971 | break; |
| 5972 | } |
| 5973 | break; |
| 5974 | case '&': |
| 5975 | switch (q->cur[1]) { |
| 5976 | case '&': |
| 5977 | PARSE(GRN_EXPR_TOKEN_LOGICAL_AND); |
| 5978 | q->cur += 2; |
| 5979 | break; |
| 5980 | case '=': |
| 5981 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 5982 | PARSE(GRN_EXPR_TOKEN_AND_ASSIGN); |
| 5983 | q->cur += 2; |
| 5984 | } else { |
| 5985 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 5986 | "'&=' is not allowed: <%.*s>", |
| 5987 | (int)(q->str_end - q->str), |
| 5988 | q->str); |
| 5989 | } |
| 5990 | break; |
| 5991 | case '!': |
| 5992 | if (q->flags & GRN_EXPR_DISABLE_AND_NOT) { |
| 5993 | ERR(GRN_INVALID_ARGUMENT, |
| 5994 | "[expr][parse][script] '&!' is disabled: <%.*s>", |
| 5995 | (int)(q->str_end - q->str), |
| 5996 | q->str); |
| 5997 | } else { |
| 5998 | PARSE(GRN_EXPR_TOKEN_LOGICAL_AND_NOT); |
| 5999 | q->cur += 2; |
| 6000 | } |
| 6001 | break; |
| 6002 | default: |
| 6003 | PARSE(GRN_EXPR_TOKEN_BITWISE_AND); |
| 6004 | q->cur++; |
| 6005 | break; |
| 6006 | } |
| 6007 | break; |
| 6008 | case '>': |
| 6009 | switch (q->cur[1]) { |
| 6010 | case '>': |
| 6011 | switch (q->cur[2]) { |
| 6012 | case '>': |
| 6013 | switch (q->cur[3]) { |
| 6014 | case '=': |
| 6015 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 6016 | PARSE(GRN_EXPR_TOKEN_SHIFTRR_ASSIGN); |
| 6017 | q->cur += 4; |
| 6018 | } else { |
| 6019 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 6020 | "'>>>=' is not allowed: <%.*s>", |
| 6021 | (int)(q->str_end - q->str), |
| 6022 | q->str); |
| 6023 | } |
| 6024 | break; |
| 6025 | default: |
| 6026 | PARSE(GRN_EXPR_TOKEN_SHIFTRR); |
| 6027 | q->cur += 3; |
| 6028 | break; |
| 6029 | } |
| 6030 | break; |
| 6031 | case '=': |
| 6032 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 6033 | PARSE(GRN_EXPR_TOKEN_SHIFTR_ASSIGN); |
| 6034 | q->cur += 3; |
| 6035 | } else { |
| 6036 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 6037 | "'>>=' is not allowed: <%.*s>", |
| 6038 | (int)(q->str_end - q->str), |
| 6039 | q->str); |
| 6040 | } |
| 6041 | break; |
| 6042 | default: |
| 6043 | PARSE(GRN_EXPR_TOKEN_SHIFTR); |
| 6044 | q->cur += 2; |
| 6045 | break; |
| 6046 | } |
| 6047 | break; |
| 6048 | case '=': |
| 6049 | PARSE(GRN_EXPR_TOKEN_GREATER_EQUAL); |
| 6050 | q->cur += 2; |
| 6051 | break; |
| 6052 | default: |
| 6053 | PARSE(GRN_EXPR_TOKEN_GREATER); |
| 6054 | q->cur++; |
| 6055 | break; |
| 6056 | } |
| 6057 | break; |
| 6058 | case '<': |
| 6059 | switch (q->cur[1]) { |
| 6060 | case '<': |
| 6061 | switch (q->cur[2]) { |
| 6062 | case '=': |
| 6063 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 6064 | PARSE(GRN_EXPR_TOKEN_SHIFTL_ASSIGN); |
| 6065 | q->cur += 3; |
| 6066 | } else { |
| 6067 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 6068 | "'<<=' is not allowed: <%.*s>", |
| 6069 | (int)(q->str_end - q->str), |
| 6070 | q->str); |
| 6071 | } |
| 6072 | break; |
| 6073 | default: |
| 6074 | PARSE(GRN_EXPR_TOKEN_SHIFTL); |
| 6075 | q->cur += 2; |
| 6076 | break; |
| 6077 | } |
| 6078 | break; |
| 6079 | case '=': |
| 6080 | PARSE(GRN_EXPR_TOKEN_LESS_EQUAL); |
| 6081 | q->cur += 2; |
| 6082 | break; |
| 6083 | default: |
| 6084 | PARSE(GRN_EXPR_TOKEN_LESS); |
| 6085 | q->cur++; |
| 6086 | break; |
| 6087 | } |
| 6088 | break; |
| 6089 | case '=': |
| 6090 | switch (q->cur[1]) { |
| 6091 | case '=': |
| 6092 | PARSE(GRN_EXPR_TOKEN_EQUAL); |
| 6093 | q->cur += 2; |
| 6094 | break; |
| 6095 | default: |
| 6096 | if (q->flags & GRN_EXPR_ALLOW_UPDATE) { |
| 6097 | PARSE(GRN_EXPR_TOKEN_ASSIGN); |
| 6098 | q->cur++; |
| 6099 | } else { |
| 6100 | ERR(GRN_UPDATE_NOT_ALLOWED, |
| 6101 | "'=' is not allowed: <%.*s>", |
| 6102 | (int)(q->str_end - q->str), |
| 6103 | q->str); |
| 6104 | } |
| 6105 | break; |
| 6106 | } |
| 6107 | break; |
| 6108 | case '0': |
| 6109 | case '1': |
| 6110 | case '2': |
| 6111 | case '3': |
| 6112 | case '4': |
| 6113 | case '5': |
| 6114 | case '6': |
| 6115 | case '7': |
| 6116 | case '8': |
| 6117 | case '9': |
| 6118 | { |
| 6119 | const char *rest; |
| 6120 | int64_t int64 = grn_atoll(q->cur, q->str_end, &rest); |
| 6121 | // checks to see grn_atoll was appropriate |
| 6122 | // (NOTE: *q->cur begins with a digit. Thus, grn_atoll parses at least |
| 6123 | // one char.) |
| 6124 | if (q->str_end != rest && |
| 6125 | (*rest == '.' || *rest == 'e' || *rest == 'E' || |
| 6126 | (*rest >= '0' && *rest <= '9'))) { |
| 6127 | grn_obj buffer; |
| 6128 | char *rest_float; |
| 6129 | double d; |
| 6130 | grn_obj floatbuf; |
| 6131 | GRN_TEXT_INIT(&buffer, 0); |
| 6132 | GRN_TEXT_SET(ctx, &buffer, q->cur, q->str_end - q->cur); |
| 6133 | GRN_TEXT_PUTC(ctx, &buffer, '\0'); |
| 6134 | errno = 0; |
| 6135 | d = strtod(GRN_TEXT_VALUE(&buffer), &rest_float); |
| 6136 | rest = q->cur + (rest_float - GRN_TEXT_VALUE(&buffer)); |
| 6137 | GRN_OBJ_FIN(ctx, &buffer); |
| 6138 | GRN_FLOAT_INIT(&floatbuf, 0); |
| 6139 | GRN_FLOAT_SET(ctx, &floatbuf, d); |
| 6140 | grn_expr_append_const(ctx, q->e, &floatbuf, GRN_OP_PUSH, 1); |
| 6141 | } else { |
| 6142 | const char *rest64 = rest; |
| 6143 | grn_atoui(q->cur, q->str_end, &rest); |
| 6144 | // checks to see grn_atoi failed (see above NOTE) |
| 6145 | if ((int64 > UINT32_MAX) || |
| 6146 | (q->str_end != rest && *rest >= '0' && *rest <= '9')) { |
| 6147 | grn_obj int64buf; |
| 6148 | GRN_INT64_INIT(&int64buf, 0); |
| 6149 | GRN_INT64_SET(ctx, &int64buf, int64); |
| 6150 | grn_expr_append_const(ctx, q->e, &int64buf, GRN_OP_PUSH, 1); |
| 6151 | rest = rest64; |
| 6152 | } else if (int64 > INT32_MAX || int64 < INT32_MIN) { |
| 6153 | grn_obj int64buf; |
| 6154 | GRN_INT64_INIT(&int64buf, 0); |
| 6155 | GRN_INT64_SET(ctx, &int64buf, int64); |
| 6156 | grn_expr_append_const(ctx, q->e, &int64buf, GRN_OP_PUSH, 1); |
| 6157 | } else { |
| 6158 | grn_obj int32buf; |
| 6159 | GRN_INT32_INIT(&int32buf, 0); |
| 6160 | GRN_INT32_SET(ctx, &int32buf, (int32_t)int64); |
| 6161 | grn_expr_append_const(ctx, q->e, &int32buf, GRN_OP_PUSH, 1); |
| 6162 | } |
| 6163 | } |
| 6164 | PARSE(GRN_EXPR_TOKEN_DECIMAL); |
| 6165 | q->cur = rest; |
| 6166 | } |
| 6167 | break; |
| 6168 | default: |
| 6169 | if ((rc = get_identifier(ctx, q, current_name_resolve_context))) { |
| 6170 | goto exit; |
| 6171 | } |
| 6172 | break; |
| 6173 | } |
| 6174 | if (ctx->rc) { |
| 6175 | rc = ctx->rc; |
| 6176 | break; |
| 6177 | } |
| 6178 | } |
| 6179 | exit: |
| 6180 | PARSE(0); |
| 6181 | return rc; |
| 6182 | } |
| 6183 | |
| 6184 | grn_rc |
| 6185 | grn_expr_parse(grn_ctx *ctx, |
| 6186 | grn_obj *expr, |
| 6187 | const char *str, |
| 6188 | unsigned int str_size, |
| 6189 | grn_obj *default_column, |
| 6190 | grn_operator default_mode, |
| 6191 | grn_operator default_op, |
| 6192 | grn_expr_flags flags) |
| 6193 | { |
| 6194 | const char *tag = "[expr][parse]"; |
| 6195 | |
| 6196 | void *parser = grn_ctx_expr_parser_pull(ctx); |
| 6197 | if (!parser) { |
| 6198 | return ctx->rc; |
| 6199 | } |
| 6200 | |
| 6201 | GRN_API_ENTER; |
| 6202 | efs_info efsi; |
| 6203 | efsi.ctx = ctx; |
| 6204 | efsi.parser = parser; |
| 6205 | efsi.str = str; |
| 6206 | |
| 6207 | efsi.v = grn_expr_get_var_by_offset(ctx, expr, 0); |
| 6208 | if (!efsi.v) { |
| 6209 | grn_obj inspected; |
| 6210 | GRN_TEXT_INIT(&inspected, 0); |
| 6211 | grn_inspect(ctx, &inspected, expr); |
| 6212 | ERR(GRN_INVALID_ARGUMENT, |
| 6213 | "%s variable is not defined: <%.*s>", |
| 6214 | tag, |
| 6215 | (int)GRN_TEXT_LEN(&inspected), |
| 6216 | GRN_TEXT_VALUE(&inspected)); |
| 6217 | GRN_OBJ_FIN(ctx, &inspected); |
| 6218 | goto exit; |
| 6219 | } |
| 6220 | |
| 6221 | efsi.table = grn_ctx_at(ctx, efsi.v->header.domain); |
| 6222 | if (!efsi.table) { |
| 6223 | grn_obj inspected; |
| 6224 | GRN_TEXT_INIT(&inspected, 0); |
| 6225 | grn_inspect(ctx, &inspected, expr); |
| 6226 | ERR(GRN_INVALID_ARGUMENT, |
| 6227 | "%s variable refers to a nonexistent table: <%u>: <%.*s>", |
| 6228 | tag, |
| 6229 | efsi.v->header.domain, |
| 6230 | (int)GRN_TEXT_LEN(&inspected), |
| 6231 | GRN_TEXT_VALUE(&inspected)); |
| 6232 | GRN_OBJ_FIN(ctx, &inspected); |
| 6233 | goto exit; |
| 6234 | } |
| 6235 | |
| 6236 | GRN_TEXT_INIT(&efsi.buf, 0); |
| 6237 | GRN_INT32_INIT(&efsi.op_stack, GRN_OBJ_VECTOR); |
| 6238 | GRN_INT32_INIT(&efsi.mode_stack, GRN_OBJ_VECTOR); |
| 6239 | GRN_INT32_INIT(&efsi.max_interval_stack, GRN_OBJ_VECTOR); |
| 6240 | GRN_INT32_INIT(&efsi.additional_last_interval_stack, GRN_OBJ_VECTOR); |
| 6241 | GRN_PTR_INIT(&efsi.max_element_intervals_stack, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 6242 | GRN_INT32_INIT(&efsi.min_interval_stack, GRN_OBJ_VECTOR); |
| 6243 | GRN_INT32_INIT(&efsi.similarity_threshold_stack, GRN_OBJ_VECTOR); |
| 6244 | GRN_INT32_INIT(&efsi.quorum_threshold_stack, GRN_OBJ_VECTOR); |
| 6245 | GRN_FLOAT32_INIT(&efsi.weight_stack, GRN_OBJ_VECTOR); |
| 6246 | GRN_PTR_INIT(&efsi.column_stack, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 6247 | GRN_PTR_INIT(&efsi.token_stack, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 6248 | efsi.e = expr; |
| 6249 | efsi.str = str; |
| 6250 | efsi.cur = str; |
| 6251 | efsi.str_end = str + str_size; |
| 6252 | efsi.default_column = default_column; |
| 6253 | GRN_PTR_PUT(ctx, &efsi.column_stack, default_column); |
| 6254 | GRN_INT32_PUT(ctx, &efsi.op_stack, default_op); |
| 6255 | GRN_INT32_PUT(ctx, &efsi.mode_stack, default_mode); |
| 6256 | GRN_FLOAT32_PUT(ctx, &efsi.weight_stack, 0); |
| 6257 | efsi.default_flags = efsi.flags = flags; |
| 6258 | efsi.escalation_threshold = GRN_DEFAULT_MATCH_ESCALATION_THRESHOLD; |
| 6259 | efsi.escalation_decaystep = DEFAULT_DECAYSTEP; |
| 6260 | efsi.weight_offset = 0; |
| 6261 | memset(&(efsi.opt), 0, sizeof(grn_select_optarg)); |
| 6262 | efsi.opt.weight_vector = NULL; |
| 6263 | efsi.weight_set = NULL; |
| 6264 | efsi.paren_depth = 0; |
| 6265 | efsi.pending_token.string = NULL; |
| 6266 | efsi.pending_token.string_length = 0; |
| 6267 | efsi.pending_token.token = 0; |
| 6268 | |
| 6269 | if (flags & (GRN_EXPR_SYNTAX_SCRIPT | GRN_EXPR_SYNTAX_OUTPUT_COLUMNS | |
| 6270 | GRN_EXPR_SYNTAX_ADJUSTER | GRN_EXPR_SYNTAX_SORT_KEYS | |
| 6271 | GRN_EXPR_SYNTAX_OPTIONS)) { |
| 6272 | efs_info *q = &efsi; |
| 6273 | if (flags & GRN_EXPR_SYNTAX_OUTPUT_COLUMNS) { |
| 6274 | PARSE(GRN_EXPR_TOKEN_START_OUTPUT_COLUMNS); |
| 6275 | } else if (flags & GRN_EXPR_SYNTAX_ADJUSTER) { |
| 6276 | PARSE(GRN_EXPR_TOKEN_START_ADJUSTER); |
| 6277 | } else if (flags & GRN_EXPR_SYNTAX_SORT_KEYS) { |
| 6278 | PARSE(GRN_EXPR_TOKEN_START_SORT_KEYS); |
| 6279 | } else if (flags & GRN_EXPR_SYNTAX_OPTIONS) { |
| 6280 | PARSE(GRN_EXPR_TOKEN_START_OPTIONS); |
| 6281 | } |
| 6282 | parse_script(ctx, q); |
| 6283 | } else { |
| 6284 | efs_info *q = &efsi; |
| 6285 | if (flags & GRN_EXPR_QUERY_NO_SYNTAX_ERROR) { |
| 6286 | PARSE(GRN_EXPR_TOKEN_START_QUERY_NO_SYNTAX_ERROR); |
| 6287 | } |
| 6288 | parse_query(ctx, q); |
| 6289 | } |
| 6290 | |
| 6291 | /* |
| 6292 | grn_obj strbuf; |
| 6293 | GRN_TEXT_INIT(&strbuf, 0); |
| 6294 | grn_expr_inspect_internal(ctx, &strbuf, expr); |
| 6295 | GRN_TEXT_PUTC(ctx, &strbuf, '\0'); |
| 6296 | GRN_LOG(ctx, GRN_LOG_NOTICE, "query=(%s)", GRN_TEXT_VALUE(&strbuf)); |
| 6297 | GRN_OBJ_FIN(ctx, &strbuf); |
| 6298 | */ |
| 6299 | |
| 6300 | /* |
| 6301 | efsi.opt.vector_size = DEFAULT_WEIGHT_VECTOR_SIZE; |
| 6302 | efsi.opt.func = efsi.weight_set ? section_weight_cb : NULL; |
| 6303 | efsi.opt.func_arg = efsi.weight_set; |
| 6304 | efsi.snip_conds = NULL; |
| 6305 | */ |
| 6306 | GRN_OBJ_FIN(ctx, &efsi.op_stack); |
| 6307 | GRN_OBJ_FIN(ctx, &efsi.mode_stack); |
| 6308 | GRN_OBJ_FIN(ctx, &efsi.max_interval_stack); |
| 6309 | GRN_OBJ_FIN(ctx, &efsi.additional_last_interval_stack); |
| 6310 | GRN_OBJ_FIN(ctx, &efsi.max_element_intervals_stack); |
| 6311 | GRN_OBJ_FIN(ctx, &efsi.min_interval_stack); |
| 6312 | GRN_OBJ_FIN(ctx, &efsi.similarity_threshold_stack); |
| 6313 | GRN_OBJ_FIN(ctx, &efsi.quorum_threshold_stack); |
| 6314 | GRN_OBJ_FIN(ctx, &efsi.weight_stack); |
| 6315 | GRN_OBJ_FIN(ctx, &efsi.column_stack); |
| 6316 | GRN_OBJ_FIN(ctx, &efsi.token_stack); |
| 6317 | GRN_OBJ_FIN(ctx, &efsi.buf); |
| 6318 | grn_obj_unref(ctx, efsi.table); |
| 6319 | |
| 6320 | exit: |
| 6321 | grn_ctx_expr_parser_release(ctx, parser); |
| 6322 | GRN_API_RETURN(ctx->rc); |
| 6323 | } |
| 6324 | |
| 6325 | uint32_t |
| 6326 | grn_expr_get_n_codes(grn_ctx *ctx, grn_obj *expr) |
| 6327 | { |
| 6328 | return ((grn_expr *)expr)->codes_curr; |
| 6329 | } |
| 6330 | |
| 6331 | typedef grn_rc (*grn_expr_syntax_expand_term_func)(grn_ctx *ctx, |
| 6332 | const char *term, |
| 6333 | size_t term_len, |
| 6334 | grn_operator mode, |
| 6335 | grn_obj *substituted_term, |
| 6336 | void *data); |
| 6337 | |
| 6338 | typedef struct { |
| 6339 | const char *query; |
| 6340 | size_t query_size; |
| 6341 | grn_expr_flags flags; |
| 6342 | grn_obj *expanded_query; |
| 6343 | grn_expr_syntax_expand_term_func expand_term_func; |
| 6344 | void *expand_term_func_data; |
| 6345 | } grn_expr_syntax_expand_query_data; |
| 6346 | |
| 6347 | static grn_rc |
| 6348 | grn_expr_syntax_expand_term_by_func(grn_ctx *ctx, |
| 6349 | const char *term, |
| 6350 | size_t term_len, |
| 6351 | grn_operator mode, |
| 6352 | grn_obj *expanded_term, |
| 6353 | void *data) |
| 6354 | { |
| 6355 | grn_rc rc; |
| 6356 | grn_obj *expander = data; |
| 6357 | grn_obj grn_term; |
| 6358 | grn_obj grn_mode; |
| 6359 | grn_obj *caller; |
| 6360 | grn_obj *rc_object; |
| 6361 | int nargs = 0; |
| 6362 | |
| 6363 | GRN_TEXT_INIT(&grn_term, GRN_OBJ_DO_SHALLOW_COPY); |
| 6364 | GRN_TEXT_SET(ctx, &grn_term, term, term_len); |
| 6365 | grn_ctx_push(ctx, &grn_term); |
| 6366 | nargs++; |
| 6367 | grn_ctx_push(ctx, expanded_term); |
| 6368 | nargs++; |
| 6369 | GRN_UINT32_INIT(&grn_mode, 0); |
| 6370 | GRN_UINT32_SET(ctx, &grn_mode, mode); |
| 6371 | grn_ctx_push(ctx, &grn_mode); |
| 6372 | nargs++; |
| 6373 | |
| 6374 | caller = grn_expr_create(ctx, NULL, 0); |
| 6375 | rc = grn_proc_call(ctx, expander, nargs, caller); |
| 6376 | GRN_OBJ_FIN(ctx, &grn_mode); |
| 6377 | GRN_OBJ_FIN(ctx, &grn_term); |
| 6378 | rc_object = grn_ctx_pop(ctx); |
| 6379 | rc = GRN_INT32_VALUE(rc_object); |
| 6380 | grn_obj_unlink(ctx, caller); |
| 6381 | |
| 6382 | return rc; |
| 6383 | } |
| 6384 | |
| 6385 | static void |
| 6386 | grn_expr_syntax_expand_term_phrase_product(grn_ctx *ctx, |
| 6387 | const char *term, |
| 6388 | size_t term_len, |
| 6389 | bool have_last, |
| 6390 | grn_obj *expanded_term) |
| 6391 | { |
| 6392 | /* In quote and paren: |
| 6393 | * *NPP:"(...HERE...) (...HERE...) ..." |
| 6394 | * *NPPP:"(...HERE...) (...HERE...) ..." |
| 6395 | */ |
| 6396 | const char target_characters[] = { |
| 6397 | GRN_QUERY_PARENL, |
| 6398 | GRN_QUERY_PARENR, |
| 6399 | GRN_QUERY_QUOTEL, |
| 6400 | GRN_QUERY_ESCAPE, |
| 6401 | '\0', |
| 6402 | }; |
| 6403 | grn_expr_syntax_escape(ctx, |
| 6404 | term, |
| 6405 | term_len, |
| 6406 | target_characters, |
| 6407 | GRN_QUERY_ESCAPE, |
| 6408 | expanded_term); |
| 6409 | if (have_last) { |
| 6410 | GRN_TEXT_PUTC(ctx, expanded_term, '$'); |
| 6411 | } |
| 6412 | } |
| 6413 | |
| 6414 | static grn_rc |
| 6415 | grn_expr_syntax_expand_term_by_text_vector(grn_ctx *ctx, |
| 6416 | grn_obj *vector, |
| 6417 | grn_operator mode, |
| 6418 | bool have_last, |
| 6419 | grn_obj *expanded_term) |
| 6420 | { |
| 6421 | bool is_phrase_product = (mode == GRN_OP_NEAR_PHRASE_PRODUCT || |
| 6422 | mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT); |
| 6423 | uint32_t i, n; |
| 6424 | n = grn_vector_size(ctx, vector); |
| 6425 | uint32_t target_n = 0; |
| 6426 | for (i = 0; i < n; i++) { |
| 6427 | const char *value; |
| 6428 | unsigned int length; |
| 6429 | grn_id domain; |
| 6430 | length = grn_vector_get_element(ctx, vector, i, &value, NULL, &domain); |
| 6431 | if (grn_type_id_is_text_family(ctx, domain)) { |
| 6432 | target_n++; |
| 6433 | } |
| 6434 | } |
| 6435 | if (!is_phrase_product && target_n > 1) { |
| 6436 | GRN_TEXT_PUTC(ctx, expanded_term, '('); |
| 6437 | } |
| 6438 | uint32_t target_i = 0; |
| 6439 | for (i = 0; i < n; i++) { |
| 6440 | const char *value; |
| 6441 | unsigned int length; |
| 6442 | grn_id domain; |
| 6443 | length = grn_vector_get_element(ctx, vector, i, &value, NULL, &domain); |
| 6444 | if (!grn_type_id_is_text_family(ctx, domain)) { |
| 6445 | continue; |
| 6446 | } |
| 6447 | if (target_i > 0) { |
| 6448 | if (is_phrase_product) { |
| 6449 | GRN_TEXT_PUTS(ctx, expanded_term, " "); |
| 6450 | } else { |
| 6451 | GRN_TEXT_PUTS(ctx, expanded_term, " OR "); |
| 6452 | } |
| 6453 | } |
| 6454 | if (!is_phrase_product && target_n > 1) { |
| 6455 | GRN_TEXT_PUTC(ctx, expanded_term, '('); |
| 6456 | } |
| 6457 | if (is_phrase_product) { |
| 6458 | grn_expr_syntax_expand_term_phrase_product(ctx, |
| 6459 | value, |
| 6460 | length, |
| 6461 | have_last, |
| 6462 | expanded_term); |
| 6463 | } else { |
| 6464 | GRN_TEXT_PUT(ctx, expanded_term, value, length); |
| 6465 | } |
| 6466 | if (!is_phrase_product && target_n > 1) { |
| 6467 | GRN_TEXT_PUTC(ctx, expanded_term, ')'); |
| 6468 | } |
| 6469 | target_i++; |
| 6470 | } |
| 6471 | if (!is_phrase_product && target_n > 1) { |
| 6472 | GRN_TEXT_PUTC(ctx, expanded_term, ')'); |
| 6473 | } |
| 6474 | return target_i == 0 ? GRN_END_OF_DATA : GRN_SUCCESS; |
| 6475 | } |
| 6476 | |
| 6477 | typedef struct { |
| 6478 | grn_obj *table; |
| 6479 | grn_obj *column; |
| 6480 | grn_obj *representative_column; |
| 6481 | uint32_t representative_column_section; |
| 6482 | grn_obj *group_column; |
| 6483 | uint32_t group_column_section; |
| 6484 | } grn_expr_syntax_expand_term_by_column_data; |
| 6485 | |
| 6486 | static grn_rc |
| 6487 | grn_expr_syntax_expand_term_by_column(grn_ctx *ctx, |
| 6488 | const char *term, |
| 6489 | size_t term_len, |
| 6490 | grn_operator mode, |
| 6491 | grn_obj *expanded_term, |
| 6492 | void *data) |
| 6493 | { |
| 6494 | grn_expr_syntax_expand_term_by_column_data *column_data = data; |
| 6495 | |
| 6496 | bool is_phrase_product = (mode == GRN_OP_NEAR_PHRASE_PRODUCT || |
| 6497 | mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT); |
| 6498 | bool have_last = (is_phrase_product && term[term_len - 1] == '$'); |
| 6499 | if (have_last) { |
| 6500 | term_len--; |
| 6501 | } |
| 6502 | grn_obj *table = column_data->table; |
| 6503 | grn_id id = grn_table_get(ctx, table, term, term_len); |
| 6504 | if (id == GRN_ID_NIL) { |
| 6505 | return GRN_END_OF_DATA; |
| 6506 | } |
| 6507 | |
| 6508 | grn_obj *column = column_data->column; |
| 6509 | if (!column) { |
| 6510 | grn_obj *value = |
| 6511 | (grn_obj *)(grn_hash_get_value_(ctx, (grn_hash *)table, id, NULL)); |
| 6512 | if (!value) { |
| 6513 | return GRN_END_OF_DATA; |
| 6514 | } |
| 6515 | if (grn_obj_is_vector(ctx, value)) { |
| 6516 | return grn_expr_syntax_expand_term_by_text_vector(ctx, |
| 6517 | value, |
| 6518 | mode, |
| 6519 | have_last, |
| 6520 | expanded_term); |
| 6521 | } else if (grn_obj_is_text_family_bulk(ctx, value)) { |
| 6522 | GRN_TEXT_SET(ctx, |
| 6523 | expanded_term, |
| 6524 | GRN_TEXT_VALUE(value), |
| 6525 | GRN_TEXT_LEN(value)); |
| 6526 | return GRN_SUCCESS; |
| 6527 | } else { |
| 6528 | return GRN_END_OF_DATA; |
| 6529 | } |
| 6530 | } else if (grn_obj_is_vector_column(ctx, column)) { |
| 6531 | grn_obj values; |
| 6532 | GRN_TEXT_INIT(&values, GRN_OBJ_VECTOR); |
| 6533 | grn_obj_get_value(ctx, column, id, &values); |
| 6534 | grn_rc rc = grn_expr_syntax_expand_term_by_text_vector(ctx, |
| 6535 | &values, |
| 6536 | mode, |
| 6537 | have_last, |
| 6538 | expanded_term); |
| 6539 | GRN_OBJ_FIN(ctx, &values); |
| 6540 | return rc; |
| 6541 | } else if (grn_obj_is_index_column(ctx, column)) { |
| 6542 | if (column_data->representative_column) { |
| 6543 | grn_obj representative_term; |
| 6544 | GRN_RECORD_INIT(&representative_term, 0, DB_OBJ(column)->range); |
| 6545 | grn_obj_get_value(ctx, |
| 6546 | column_data->representative_column, |
| 6547 | id, |
| 6548 | &representative_term); |
| 6549 | grn_id representative_term_id = GRN_ID_NIL; |
| 6550 | if (GRN_BULK_VSIZE(&(representative_term)) > 0) { |
| 6551 | representative_term_id = GRN_RECORD_VALUE(&representative_term); |
| 6552 | } |
| 6553 | GRN_OBJ_FIN(ctx, &representative_term); |
| 6554 | if (representative_term_id == GRN_ID_NIL) { |
| 6555 | return GRN_END_OF_DATA; |
| 6556 | } |
| 6557 | grn_obj *index_cursor = |
| 6558 | grn_index_cursor_open(ctx, NULL, column, GRN_ID_NIL, GRN_ID_MAX, 0); |
| 6559 | if (!index_cursor) { |
| 6560 | return ctx->rc; |
| 6561 | } |
| 6562 | grn_rc rc = |
| 6563 | grn_index_cursor_set_term_id(ctx, index_cursor, representative_term_id); |
| 6564 | if (rc != GRN_SUCCESS) { |
| 6565 | grn_obj_close(ctx, index_cursor); |
| 6566 | return rc; |
| 6567 | } |
| 6568 | rc = grn_index_cursor_set_section_id( |
| 6569 | ctx, |
| 6570 | index_cursor, |
| 6571 | column_data->representative_column_section); |
| 6572 | if (rc != GRN_SUCCESS) { |
| 6573 | grn_obj_close(ctx, index_cursor); |
| 6574 | return rc; |
| 6575 | } |
| 6576 | grn_posting *posting; |
| 6577 | grn_id term_id; |
| 6578 | grn_obj synonym; |
| 6579 | GRN_TEXT_INIT(&synonym, 0); |
| 6580 | grn_obj synonyms; |
| 6581 | GRN_TEXT_INIT(&synonyms, GRN_OBJ_VECTOR); |
| 6582 | while ((posting = grn_index_cursor_next(ctx, index_cursor, &term_id))) { |
| 6583 | GRN_BULK_REWIND(&synonym); |
| 6584 | grn_table_get_key2(ctx, table, posting->rid, &synonym); |
| 6585 | grn_vector_add_element_float(ctx, |
| 6586 | &synonyms, |
| 6587 | GRN_TEXT_VALUE(&synonym), |
| 6588 | GRN_TEXT_LEN(&synonym), |
| 6589 | 0.0, |
| 6590 | synonym.header.domain); |
| 6591 | } |
| 6592 | GRN_OBJ_FIN(ctx, &synonym); |
| 6593 | grn_obj_close(ctx, index_cursor); |
| 6594 | rc = grn_expr_syntax_expand_term_by_text_vector(ctx, |
| 6595 | &synonyms, |
| 6596 | mode, |
| 6597 | have_last, |
| 6598 | expanded_term); |
| 6599 | GRN_OBJ_FIN(ctx, &synonyms); |
| 6600 | return rc; |
| 6601 | } else { |
| 6602 | grn_obj *index_cursor = |
| 6603 | grn_index_cursor_open(ctx, NULL, column, GRN_ID_NIL, GRN_ID_MAX, 0); |
| 6604 | if (!index_cursor) { |
| 6605 | return ctx->rc; |
| 6606 | } |
| 6607 | grn_rc rc = grn_index_cursor_set_term_id(ctx, index_cursor, id); |
| 6608 | if (rc != GRN_SUCCESS) { |
| 6609 | grn_obj_close(ctx, index_cursor); |
| 6610 | return rc; |
| 6611 | } |
| 6612 | rc = grn_index_cursor_set_section_id(ctx, |
| 6613 | index_cursor, |
| 6614 | column_data->group_column_section); |
| 6615 | if (rc != GRN_SUCCESS) { |
| 6616 | grn_obj_close(ctx, index_cursor); |
| 6617 | return rc; |
| 6618 | } |
| 6619 | grn_posting *posting; |
| 6620 | grn_id term_id; |
| 6621 | grn_obj synonyms; |
| 6622 | GRN_TEXT_INIT(&synonyms, GRN_OBJ_VECTOR); |
| 6623 | while ((posting = grn_index_cursor_next(ctx, index_cursor, &term_id))) { |
| 6624 | grn_obj_get_value(ctx, |
| 6625 | column_data->group_column, |
| 6626 | posting->rid, |
| 6627 | &synonyms); |
| 6628 | } |
| 6629 | grn_obj_close(ctx, index_cursor); |
| 6630 | rc = grn_expr_syntax_expand_term_by_text_vector(ctx, |
| 6631 | &synonyms, |
| 6632 | mode, |
| 6633 | have_last, |
| 6634 | expanded_term); |
| 6635 | GRN_OBJ_FIN(ctx, &synonyms); |
| 6636 | return rc; |
| 6637 | } |
| 6638 | } else { |
| 6639 | if (is_phrase_product) { |
| 6640 | grn_obj value; |
| 6641 | GRN_TEXT_INIT(&value, 0); |
| 6642 | grn_obj_get_value(ctx, column, id, &value); |
| 6643 | grn_expr_syntax_expand_term_phrase_product(ctx, |
| 6644 | GRN_TEXT_VALUE(&value), |
| 6645 | GRN_TEXT_LEN(&value), |
| 6646 | have_last, |
| 6647 | expanded_term); |
| 6648 | GRN_OBJ_FIN(ctx, &value); |
| 6649 | } else { |
| 6650 | grn_obj_get_value(ctx, column, id, expanded_term); |
| 6651 | } |
| 6652 | return GRN_SUCCESS; |
| 6653 | } |
| 6654 | } |
| 6655 | |
| 6656 | typedef struct { |
| 6657 | grn_obj *table; |
| 6658 | grn_obj *term_column; |
| 6659 | grn_obj *expanded_term_column; |
| 6660 | } grn_expr_syntax_expand_term_by_table_data; |
| 6661 | |
| 6662 | static grn_rc |
| 6663 | grn_expr_syntax_expand_term_by_table(grn_ctx *ctx, |
| 6664 | const char *term, |
| 6665 | size_t term_len, |
| 6666 | grn_operator mode, |
| 6667 | grn_obj *expanded_term, |
| 6668 | void *data) |
| 6669 | { |
| 6670 | grn_rc rc = GRN_END_OF_DATA; |
| 6671 | grn_expr_syntax_expand_term_by_table_data *table_data = data; |
| 6672 | grn_obj *table; |
| 6673 | grn_obj *term_column; |
| 6674 | grn_obj *expanded_term_column; |
| 6675 | grn_obj *expression; |
| 6676 | grn_obj *variable; |
| 6677 | grn_obj *found_terms; |
| 6678 | int n_terms; |
| 6679 | |
| 6680 | table = table_data->table; |
| 6681 | term_column = table_data->term_column; |
| 6682 | expanded_term_column = table_data->expanded_term_column; |
| 6683 | |
| 6684 | GRN_EXPR_CREATE_FOR_QUERY(ctx, table, expression, variable); |
| 6685 | if (ctx->rc != GRN_SUCCESS) { |
| 6686 | char errbuf[GRN_CTX_MSGSIZE]; |
| 6687 | grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 6688 | ERR(ctx->rc, |
| 6689 | "[query][expand][table] " |
| 6690 | "failed to create expression: <%s>", |
| 6691 | errbuf); |
| 6692 | return ctx->rc; |
| 6693 | } |
| 6694 | grn_expr_append_const(ctx, expression, term_column, GRN_OP_GET_VALUE, 1); |
| 6695 | grn_expr_append_const_str(ctx, expression, term, term_len, GRN_OP_PUSH, 1); |
| 6696 | grn_expr_append_op(ctx, expression, GRN_OP_EQUAL, 2); |
| 6697 | if (ctx->rc != GRN_SUCCESS) { |
| 6698 | char errbuf[GRN_CTX_MSGSIZE]; |
| 6699 | grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 6700 | grn_obj_close(ctx, expression); |
| 6701 | ERR(ctx->rc, |
| 6702 | "[query][expand][table] " |
| 6703 | "failed to build expression: <%s>", |
| 6704 | errbuf); |
| 6705 | return ctx->rc; |
| 6706 | } |
| 6707 | |
| 6708 | found_terms = grn_table_select(ctx, table, expression, NULL, GRN_OP_OR); |
| 6709 | grn_obj_close(ctx, expression); |
| 6710 | if (!found_terms) { |
| 6711 | char errbuf[GRN_CTX_MSGSIZE]; |
| 6712 | grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf); |
| 6713 | ERR(ctx->rc, |
| 6714 | "[query][expand][table] " |
| 6715 | "failed to find term: <%.*s>: <%s>", |
| 6716 | (int)term_len, |
| 6717 | term, |
| 6718 | errbuf); |
| 6719 | return ctx->rc; |
| 6720 | } |
| 6721 | |
| 6722 | n_terms = grn_table_size(ctx, found_terms); |
| 6723 | if (n_terms == 0) { |
| 6724 | grn_obj_close(ctx, found_terms); |
| 6725 | return rc; |
| 6726 | } |
| 6727 | |
| 6728 | { |
| 6729 | int nth_term; |
| 6730 | |
| 6731 | GRN_TEXT_PUTC(ctx, expanded_term, '('); |
| 6732 | nth_term = 0; |
| 6733 | GRN_TABLE_EACH_BEGIN(ctx, found_terms, cursor, id) |
| 6734 | { |
| 6735 | void *key; |
| 6736 | grn_id record_id; |
| 6737 | |
| 6738 | grn_table_cursor_get_key(ctx, cursor, &key); |
| 6739 | record_id = *((grn_id *)key); |
| 6740 | if (grn_obj_is_vector_column(ctx, expanded_term_column)) { |
| 6741 | unsigned int j, n_values; |
| 6742 | grn_obj values; |
| 6743 | GRN_TEXT_INIT(&values, GRN_OBJ_VECTOR); |
| 6744 | grn_obj_get_value(ctx, expanded_term_column, record_id, &values); |
| 6745 | n_values = grn_vector_size(ctx, &values); |
| 6746 | n_terms += n_values - 1; |
| 6747 | for (j = 0; j < n_values; j++) { |
| 6748 | const char *value; |
| 6749 | unsigned int length; |
| 6750 | if (nth_term > 0) { |
| 6751 | GRN_TEXT_PUTS(ctx, expanded_term, " OR "); |
| 6752 | } |
| 6753 | if (n_terms > 1) { |
| 6754 | GRN_TEXT_PUTC(ctx, expanded_term, '('); |
| 6755 | } |
| 6756 | length = grn_vector_get_element(ctx, &values, j, &value, NULL, NULL); |
| 6757 | GRN_TEXT_PUT(ctx, expanded_term, value, length); |
| 6758 | if (n_terms > 1) { |
| 6759 | GRN_TEXT_PUTC(ctx, expanded_term, ')'); |
| 6760 | } |
| 6761 | nth_term++; |
| 6762 | } |
| 6763 | GRN_OBJ_FIN(ctx, &values); |
| 6764 | } else { |
| 6765 | if (nth_term > 0) { |
| 6766 | GRN_TEXT_PUTS(ctx, expanded_term, " OR "); |
| 6767 | } |
| 6768 | if (n_terms > 1) { |
| 6769 | GRN_TEXT_PUTC(ctx, expanded_term, '('); |
| 6770 | } |
| 6771 | grn_obj_get_value(ctx, expanded_term_column, record_id, expanded_term); |
| 6772 | if (n_terms > 1) { |
| 6773 | GRN_TEXT_PUTC(ctx, expanded_term, ')'); |
| 6774 | } |
| 6775 | nth_term++; |
| 6776 | } |
| 6777 | } |
| 6778 | GRN_TABLE_EACH_END(ctx, cursor); |
| 6779 | GRN_TEXT_PUTC(ctx, expanded_term, ')'); |
| 6780 | } |
| 6781 | rc = GRN_SUCCESS; |
| 6782 | grn_obj_close(ctx, found_terms); |
| 6783 | |
| 6784 | return rc; |
| 6785 | } |
| 6786 | |
| 6787 | static void |
| 6788 | grn_expr_syntax_expand_query_term(grn_ctx *ctx, |
| 6789 | grn_expr_syntax_expand_query_data *data, |
| 6790 | grn_operator mode, |
| 6791 | const char *term, |
| 6792 | size_t term_len, |
| 6793 | const char *fallback_term, |
| 6794 | size_t fallback_term_len) |
| 6795 | { |
| 6796 | grn_rc rc = data->expand_term_func(ctx, |
| 6797 | term, |
| 6798 | term_len, |
| 6799 | mode, |
| 6800 | data->expanded_query, |
| 6801 | data->expand_term_func_data); |
| 6802 | if (rc == GRN_SUCCESS) { |
| 6803 | return; |
| 6804 | } |
| 6805 | GRN_TEXT_PUT(ctx, data->expanded_query, fallback_term, fallback_term_len); |
| 6806 | } |
| 6807 | |
| 6808 | static grn_rc |
| 6809 | grn_expr_syntax_expand_query_terms(grn_ctx *ctx, |
| 6810 | grn_expr_syntax_expand_query_data *data) |
| 6811 | { |
| 6812 | grn_obj buf; |
| 6813 | grn_operator mode = GRN_OP_MATCH; |
| 6814 | unsigned int len; |
| 6815 | const char *start; |
| 6816 | const char *cur = data->query; |
| 6817 | const char *query_end = data->query + data->query_size; |
| 6818 | GRN_TEXT_INIT(&buf, 0); |
| 6819 | for (;;) { |
| 6820 | while (cur < query_end && grn_isspace(cur, ctx->encoding)) { |
| 6821 | if (!(len = grn_charlen(ctx, cur, query_end))) { |
| 6822 | goto exit; |
| 6823 | } |
| 6824 | GRN_TEXT_PUT(ctx, data->expanded_query, cur, len); |
| 6825 | cur += len; |
| 6826 | } |
| 6827 | if (query_end <= cur) { |
| 6828 | break; |
| 6829 | } |
| 6830 | switch (cur[0]) { |
| 6831 | case '\0': |
| 6832 | goto exit; |
| 6833 | break; |
| 6834 | case GRN_QUERY_AND: |
| 6835 | case GRN_QUERY_AND_NOT: |
| 6836 | case GRN_QUERY_PARENL: |
| 6837 | case GRN_QUERY_PARENR: |
| 6838 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6839 | cur++; |
| 6840 | mode = GRN_OP_MATCH; |
| 6841 | break; |
| 6842 | case GRN_QUERY_PREFIX: |
| 6843 | mode = GRN_OP_MATCH; |
| 6844 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6845 | cur++; |
| 6846 | if (cur < query_end && grn_charlen(ctx, cur, query_end) == 1) { |
| 6847 | switch (cur[0]) { |
| 6848 | case 'N': |
| 6849 | mode = GRN_OP_NEAR; |
| 6850 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6851 | cur++; |
| 6852 | if (cur < query_end && grn_charlen(ctx, cur, query_end) == 1 && |
| 6853 | cur[0] == 'P') { |
| 6854 | mode = GRN_OP_NEAR_PHRASE; |
| 6855 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6856 | cur++; |
| 6857 | if (cur < query_end && grn_charlen(ctx, cur, query_end) == 1 && |
| 6858 | cur[0] == 'P') { |
| 6859 | mode = GRN_OP_NEAR_PHRASE_PRODUCT; |
| 6860 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6861 | cur++; |
| 6862 | } |
| 6863 | } |
| 6864 | break; |
| 6865 | case 'O': |
| 6866 | if (cur + 2 < query_end && |
| 6867 | grn_charlen(ctx, cur + 1, query_end) == 1 && |
| 6868 | grn_charlen(ctx, cur + 2, query_end) == 1 && cur[1] == 'N' && |
| 6869 | cur[2] == 'P') { |
| 6870 | mode = GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT; |
| 6871 | GRN_TEXT_PUT(ctx, data->expanded_query, cur, 3); |
| 6872 | cur += 3; |
| 6873 | if (cur < query_end && grn_charlen(ctx, cur, query_end) == 1 && |
| 6874 | cur[0] == 'P') { |
| 6875 | mode = GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT; |
| 6876 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6877 | cur++; |
| 6878 | } |
| 6879 | } |
| 6880 | break; |
| 6881 | default: |
| 6882 | break; |
| 6883 | } |
| 6884 | } |
| 6885 | if (mode != GRN_OP_MATCH) { |
| 6886 | while (cur < query_end && grn_charlen(ctx, cur, query_end) == 1 && |
| 6887 | /* Loose. Do we need to process strictly here? */ |
| 6888 | (('0' <= cur[0] && cur[0] < '9') || cur[0] == '-' || |
| 6889 | cur[0] == '+' || cur[0] == ',' || cur[0] == '|')) { |
| 6890 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6891 | cur++; |
| 6892 | } |
| 6893 | } |
| 6894 | break; |
| 6895 | case GRN_QUERY_ADJ_INC: |
| 6896 | case GRN_QUERY_ADJ_DEC: |
| 6897 | case GRN_QUERY_ADJ_NEG: |
| 6898 | { |
| 6899 | char *end = (char *)query_end; |
| 6900 | strtof(cur + 1, &end); |
| 6901 | GRN_TEXT_PUT(ctx, data->expanded_query, cur, end - cur); |
| 6902 | cur = end; |
| 6903 | } |
| 6904 | break; |
| 6905 | case GRN_QUERY_QUOTEL: |
| 6906 | GRN_BULK_REWIND(&buf); |
| 6907 | if (mode == GRN_OP_NEAR_PHRASE_PRODUCT || |
| 6908 | mode == GRN_OP_ORDERED_NEAR_PHRASE_PRODUCT) { |
| 6909 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6910 | cur++; |
| 6911 | for (start = cur; cur < query_end; cur += len) { |
| 6912 | len = grn_charlen(ctx, cur, query_end); |
| 6913 | if (len == 0) { |
| 6914 | goto exit; |
| 6915 | } |
| 6916 | int space_char_len = grn_isspace(cur, ctx->encoding); |
| 6917 | if (space_char_len > 0) { |
| 6918 | if (GRN_TEXT_LEN(&buf) > 0) { |
| 6919 | grn_expr_syntax_expand_query_term(ctx, |
| 6920 | data, |
| 6921 | mode, |
| 6922 | GRN_TEXT_VALUE(&buf), |
| 6923 | GRN_TEXT_LEN(&buf), |
| 6924 | GRN_TEXT_VALUE(&buf), |
| 6925 | GRN_TEXT_LEN(&buf)); |
| 6926 | GRN_BULK_REWIND(&buf); |
| 6927 | } |
| 6928 | GRN_TEXT_PUT(ctx, data->expanded_query, cur, space_char_len); |
| 6929 | len = space_char_len; |
| 6930 | continue; |
| 6931 | } |
| 6932 | if (len == 1) { |
| 6933 | if (cur[0] == GRN_QUERY_QUOTER) { |
| 6934 | if (GRN_TEXT_LEN(&buf) > 0) { |
| 6935 | grn_expr_syntax_expand_query_term(ctx, |
| 6936 | data, |
| 6937 | mode, |
| 6938 | GRN_TEXT_VALUE(&buf), |
| 6939 | GRN_TEXT_LEN(&buf), |
| 6940 | GRN_TEXT_VALUE(&buf), |
| 6941 | GRN_TEXT_LEN(&buf)); |
| 6942 | GRN_BULK_REWIND(&buf); |
| 6943 | } |
| 6944 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6945 | cur++; |
| 6946 | break; |
| 6947 | } else if (cur[0] == GRN_QUERY_ESCAPE && cur + 1 < query_end) { |
| 6948 | cur++; |
| 6949 | len = grn_charlen(ctx, cur, query_end); |
| 6950 | } else if (cur[0] == GRN_QUERY_PARENL) { |
| 6951 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6952 | continue; |
| 6953 | } else if (cur[0] == GRN_QUERY_PARENR) { |
| 6954 | if (GRN_TEXT_LEN(&buf) > 0) { |
| 6955 | grn_expr_syntax_expand_query_term(ctx, |
| 6956 | data, |
| 6957 | mode, |
| 6958 | GRN_TEXT_VALUE(&buf), |
| 6959 | GRN_TEXT_LEN(&buf), |
| 6960 | GRN_TEXT_VALUE(&buf), |
| 6961 | GRN_TEXT_LEN(&buf)); |
| 6962 | GRN_BULK_REWIND(&buf); |
| 6963 | } |
| 6964 | GRN_TEXT_PUTC(ctx, data->expanded_query, cur[0]); |
| 6965 | continue; |
| 6966 | } |
| 6967 | } |
| 6968 | GRN_TEXT_PUT(ctx, &buf, cur, len); |
| 6969 | } |
| 6970 | } else { |
| 6971 | for (start = cur++; cur < query_end; cur += len) { |
| 6972 | len = grn_charlen(ctx, cur, query_end); |
| 6973 | if (len == 0) { |
| 6974 | goto exit; |
| 6975 | } |
| 6976 | if (len == 1) { |
| 6977 | if (cur[0] == GRN_QUERY_QUOTER) { |
| 6978 | cur++; |
| 6979 | break; |
| 6980 | } else if (cur[0] == GRN_QUERY_ESCAPE && cur + 1 < query_end) { |
| 6981 | cur++; |
| 6982 | len = grn_charlen(ctx, cur, query_end); |
| 6983 | } |
| 6984 | } |
| 6985 | GRN_TEXT_PUT(ctx, &buf, cur, len); |
| 6986 | } |
| 6987 | grn_expr_syntax_expand_query_term(ctx, |
| 6988 | data, |
| 6989 | mode, |
| 6990 | GRN_TEXT_VALUE(&buf), |
| 6991 | GRN_TEXT_LEN(&buf), |
| 6992 | start, |
| 6993 | cur - start); |
| 6994 | } |
| 6995 | mode = GRN_OP_MATCH; |
| 6996 | break; |
| 6997 | case 'O': |
| 6998 | if (cur + 2 <= query_end && grn_charlen(ctx, cur + 1, query_end) == 1 && |
| 6999 | cur[1] == 'R' && |
| 7000 | (cur + 2 == query_end || grn_isspace(cur + 2, ctx->encoding) > 0)) { |
| 7001 | GRN_TEXT_PUT(ctx, data->expanded_query, cur, 2); |
| 7002 | cur += 2; |
| 7003 | mode = GRN_OP_MATCH; |
| 7004 | break; |
| 7005 | } |
| 7006 | /* fallthru */ |
| 7007 | default: |
| 7008 | for (start = cur; cur < query_end; cur += len) { |
| 7009 | if (!(len = grn_charlen(ctx, cur, query_end))) { |
| 7010 | goto exit; |
| 7011 | } else if (grn_isspace(cur, ctx->encoding)) { |
| 7012 | break; |
| 7013 | } else if (len == 1) { |
| 7014 | if (cur[0] == GRN_QUERY_PARENL || cur[0] == GRN_QUERY_PARENR || |
| 7015 | cur[0] == GRN_QUERY_PREFIX || cur[0] == GRN_QUERY_QUOTEL) { |
| 7016 | break; |
| 7017 | } else if (data->flags & GRN_EXPR_ALLOW_COLUMN && |
| 7018 | cur[0] == GRN_QUERY_COLUMN) { |
| 7019 | if (cur + 1 < query_end) { |
| 7020 | switch (cur[1]) { |
| 7021 | case '!': |
| 7022 | case '@': |
| 7023 | case '^': |
| 7024 | case '$': |
| 7025 | cur += 2; |
| 7026 | break; |
| 7027 | case '=': |
| 7028 | cur += (data->flags & GRN_EXPR_ALLOW_UPDATE) ? 2 : 1; |
| 7029 | break; |
| 7030 | case '<': |
| 7031 | case '>': |
| 7032 | cur += (cur + 2 < query_end && cur[2] == '=') ? 3 : 2; |
| 7033 | break; |
| 7034 | default: |
| 7035 | cur += 1; |
| 7036 | break; |
| 7037 | } |
| 7038 | } else { |
| 7039 | cur += 1; |
| 7040 | } |
| 7041 | GRN_TEXT_PUT(ctx, data->expanded_query, start, cur - start); |
| 7042 | start = cur; |
| 7043 | break; |
| 7044 | } |
| 7045 | } |
| 7046 | } |
| 7047 | if (start < cur) { |
| 7048 | grn_expr_syntax_expand_query_term(ctx, |
| 7049 | data, |
| 7050 | mode, |
| 7051 | start, |
| 7052 | cur - start, |
| 7053 | start, |
| 7054 | cur - start); |
| 7055 | mode = GRN_OP_MATCH; |
| 7056 | } |
| 7057 | break; |
| 7058 | } |
| 7059 | } |
| 7060 | exit: |
| 7061 | GRN_OBJ_FIN(ctx, &buf); |
| 7062 | return GRN_SUCCESS; |
| 7063 | } |
| 7064 | |
| 7065 | grn_rc |
| 7066 | grn_expr_syntax_expand_query(grn_ctx *ctx, |
| 7067 | const char *query, |
| 7068 | int query_size, |
| 7069 | grn_expr_flags flags, |
| 7070 | grn_obj *expander, |
| 7071 | grn_obj *expanded_query) |
| 7072 | { |
| 7073 | GRN_API_ENTER; |
| 7074 | |
| 7075 | grn_expr_syntax_expand_query_data data; |
| 7076 | data.query = query; |
| 7077 | if (query_size < 0) { |
| 7078 | data.query_size = strlen(query); |
| 7079 | } else { |
| 7080 | data.query_size = query_size; |
| 7081 | } |
| 7082 | data.flags = flags; |
| 7083 | data.expanded_query = expanded_query; |
| 7084 | |
| 7085 | switch (expander->header.type) { |
| 7086 | case GRN_PROC: |
| 7087 | if (((grn_proc *)expander)->type != GRN_PROC_FUNCTION) { |
| 7088 | char name[GRN_TABLE_MAX_KEY_SIZE]; |
| 7089 | int name_size; |
| 7090 | name_size = grn_obj_name(ctx, expander, name, GRN_TABLE_MAX_KEY_SIZE); |
| 7091 | ERR(GRN_INVALID_ARGUMENT, |
| 7092 | "[query][expand][proc] " |
| 7093 | "proc query expander must be a function proc: <%.*s>", |
| 7094 | name_size, |
| 7095 | name); |
| 7096 | goto exit; |
| 7097 | } |
| 7098 | data.expand_term_func = grn_expr_syntax_expand_term_by_func; |
| 7099 | data.expand_term_func_data = expander; |
| 7100 | grn_expr_syntax_expand_query_terms(ctx, &data); |
| 7101 | break; |
| 7102 | case GRN_COLUMN_FIX_SIZE: |
| 7103 | case GRN_COLUMN_VAR_SIZE: |
| 7104 | case GRN_COLUMN_INDEX: |
| 7105 | { |
| 7106 | grn_obj *expansion_table = grn_column_table(ctx, expander); |
| 7107 | if (!expansion_table) { |
| 7108 | GRN_DEFINE_NAME(expander); |
| 7109 | ERR(GRN_INVALID_ARGUMENT, |
| 7110 | "[query][expand][column] " |
| 7111 | "failed to get table of query expansion column: <%.*s>", |
| 7112 | name_size, |
| 7113 | name); |
| 7114 | goto exit; |
| 7115 | } |
| 7116 | |
| 7117 | data.expand_term_func = grn_expr_syntax_expand_term_by_column; |
| 7118 | grn_expr_syntax_expand_term_by_column_data column_data; |
| 7119 | data.expand_term_func_data = &column_data; |
| 7120 | column_data.table = expansion_table; |
| 7121 | column_data.column = expander; |
| 7122 | column_data.representative_column = NULL; |
| 7123 | column_data.representative_column_section = 0; |
| 7124 | column_data.group_column = NULL; |
| 7125 | column_data.group_column_section = 0; |
| 7126 | if (expander->header.type == GRN_COLUMN_INDEX) { |
| 7127 | grn_obj source_ids; |
| 7128 | GRN_RECORD_INIT(&source_ids, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 7129 | grn_obj_get_info(ctx, expander, GRN_INFO_SOURCE, &source_ids); |
| 7130 | size_t n = GRN_RECORD_VECTOR_SIZE(&source_ids); |
| 7131 | size_t i; |
| 7132 | for (i = 0; i < n; i++) { |
| 7133 | grn_id source_id = GRN_RECORD_VALUE_AT(&source_ids, i); |
| 7134 | grn_obj *source = grn_ctx_at(ctx, source_id); |
| 7135 | if (!source) { |
| 7136 | continue; |
| 7137 | } |
| 7138 | if (grn_obj_is_table(ctx, source)) { |
| 7139 | grn_obj_unref(ctx, source); |
| 7140 | continue; |
| 7141 | } |
| 7142 | if (source->header.domain == DB_OBJ(expansion_table)->id) { |
| 7143 | column_data.representative_column = source; |
| 7144 | column_data.representative_column_section = i + 1; |
| 7145 | break; |
| 7146 | } else if (grn_obj_is_text_family_vector_column(ctx, source)) { |
| 7147 | /* TODO: Add support for multiple group columns. */ |
| 7148 | column_data.group_column = source; |
| 7149 | column_data.group_column_section = i + 1; |
| 7150 | break; |
| 7151 | } |
| 7152 | } |
| 7153 | if (!column_data.representative_column && !column_data.group_column) { |
| 7154 | GRN_DEFINE_NAME(expander); |
| 7155 | ERR(GRN_INVALID_ARGUMENT, |
| 7156 | "[query][expand][column][index] " |
| 7157 | "must have a reference scalar column source that refers " |
| 7158 | "lexicon itself or a text vector column source: <%.*s>", |
| 7159 | name_size, |
| 7160 | name); |
| 7161 | goto exit; |
| 7162 | } |
| 7163 | } |
| 7164 | |
| 7165 | grn_expr_syntax_expand_query_terms(ctx, &data); |
| 7166 | if (column_data.representative_column) { |
| 7167 | grn_obj_unref(ctx, column_data.representative_column); |
| 7168 | } |
| 7169 | } |
| 7170 | break; |
| 7171 | case GRN_TABLE_HASH_KEY: |
| 7172 | { |
| 7173 | /* Object literal for query(): |
| 7174 | * query("...", {"expander": {"key": ["v1", "v2", ...], ...}}) */ |
| 7175 | if (!(DB_OBJ(expander)->range == GRN_ID_NIL && |
| 7176 | ((grn_hash *)expander)->value_size == sizeof(grn_obj))) { |
| 7177 | ERR(GRN_INVALID_ARGUMENT, |
| 7178 | "[query][expand][table] value must be grn_obj"); |
| 7179 | goto exit; |
| 7180 | } |
| 7181 | |
| 7182 | data.expand_term_func = grn_expr_syntax_expand_term_by_column; |
| 7183 | grn_expr_syntax_expand_term_by_column_data column_data; |
| 7184 | data.expand_term_func_data = &column_data; |
| 7185 | column_data.table = expander; |
| 7186 | column_data.column = NULL; |
| 7187 | column_data.representative_column = NULL; |
| 7188 | column_data.representative_column_section = 0; |
| 7189 | column_data.group_column = NULL; |
| 7190 | column_data.group_column_section = 0; |
| 7191 | grn_expr_syntax_expand_query_terms(ctx, &data); |
| 7192 | } |
| 7193 | break; |
| 7194 | default: |
| 7195 | { |
| 7196 | GRN_DEFINE_NAME(expander); |
| 7197 | grn_obj type_name; |
| 7198 | GRN_TEXT_INIT(&type_name, 0); |
| 7199 | grn_inspect_type(ctx, &type_name, expander->header.type); |
| 7200 | ERR(GRN_INVALID_ARGUMENT, |
| 7201 | "[query][expand] " |
| 7202 | "query expander must be a column or function proc: <%.*s>(%.*s)", |
| 7203 | name_size, |
| 7204 | name, |
| 7205 | (int)GRN_TEXT_LEN(&type_name), |
| 7206 | GRN_TEXT_VALUE(&type_name)); |
| 7207 | GRN_OBJ_FIN(ctx, &type_name); |
| 7208 | goto exit; |
| 7209 | } |
| 7210 | break; |
| 7211 | } |
| 7212 | |
| 7213 | exit: |
| 7214 | GRN_API_RETURN(ctx->rc); |
| 7215 | } |
| 7216 | |
| 7217 | grn_rc |
| 7218 | grn_expr_syntax_expand_query_by_table(grn_ctx *ctx, |
| 7219 | const char *query, |
| 7220 | int query_size, |
| 7221 | grn_expr_flags flags, |
| 7222 | grn_obj *term_column, |
| 7223 | grn_obj *expanded_term_column, |
| 7224 | grn_obj *expanded_query) |
| 7225 | { |
| 7226 | grn_obj *table; |
| 7227 | bool term_column_is_key; |
| 7228 | |
| 7229 | GRN_API_ENTER; |
| 7230 | |
| 7231 | grn_expr_syntax_expand_query_data data; |
| 7232 | data.query = query; |
| 7233 | if (query_size < 0) { |
| 7234 | data.query_size = strlen(query); |
| 7235 | } else { |
| 7236 | data.query_size = query_size; |
| 7237 | } |
| 7238 | data.flags = flags; |
| 7239 | data.expanded_query = expanded_query; |
| 7240 | |
| 7241 | if (!grn_obj_is_column(ctx, expanded_term_column)) { |
| 7242 | grn_obj inspected; |
| 7243 | GRN_TEXT_INIT(&inspected, 0); |
| 7244 | grn_inspect(ctx, &inspected, expanded_term_column); |
| 7245 | ERR(GRN_INVALID_ARGUMENT, |
| 7246 | "[query][expand][table] " |
| 7247 | "expanded term column must be a column: <%.*s>", |
| 7248 | (int)GRN_TEXT_LEN(&inspected), |
| 7249 | GRN_TEXT_VALUE(&inspected)); |
| 7250 | GRN_OBJ_FIN(ctx, &inspected); |
| 7251 | GRN_API_RETURN(ctx->rc); |
| 7252 | } |
| 7253 | table = grn_column_table(ctx, expanded_term_column); |
| 7254 | |
| 7255 | if (!term_column) { |
| 7256 | term_column_is_key = true; |
| 7257 | } else { |
| 7258 | if (grn_obj_is_key_accessor(ctx, term_column)) { |
| 7259 | term_column_is_key = true; |
| 7260 | } else if (grn_obj_is_data_column(ctx, term_column)) { |
| 7261 | term_column_is_key = false; |
| 7262 | } else { |
| 7263 | grn_obj inspected; |
| 7264 | GRN_TEXT_INIT(&inspected, 0); |
| 7265 | grn_inspect(ctx, &inspected, term_column); |
| 7266 | ERR(GRN_INVALID_ARGUMENT, |
| 7267 | "[query][expand][table] " |
| 7268 | "term column must be NULL, _key or a data column: <%.*s>", |
| 7269 | (int)GRN_TEXT_LEN(&inspected), |
| 7270 | GRN_TEXT_VALUE(&inspected)); |
| 7271 | GRN_OBJ_FIN(ctx, &inspected); |
| 7272 | GRN_API_RETURN(ctx->rc); |
| 7273 | } |
| 7274 | if (term_column->header.domain != expanded_term_column->header.domain) { |
| 7275 | grn_obj inspected_term_column; |
| 7276 | grn_obj inspected_expanded_term_column; |
| 7277 | GRN_TEXT_INIT(&inspected_term_column, 0); |
| 7278 | GRN_TEXT_INIT(&inspected_expanded_term_column, 0); |
| 7279 | grn_inspect(ctx, &inspected_term_column, term_column); |
| 7280 | grn_inspect(ctx, &inspected_expanded_term_column, expanded_term_column); |
| 7281 | ERR(GRN_INVALID_ARGUMENT, |
| 7282 | "[query][expand][table] " |
| 7283 | "term column and expanded term column must belong to the same table: " |
| 7284 | "term column: <%.*s>, " |
| 7285 | "expanded term column: <%*.s>", |
| 7286 | (int)GRN_TEXT_LEN(&inspected_term_column), |
| 7287 | GRN_TEXT_VALUE(&inspected_term_column), |
| 7288 | (int)GRN_TEXT_LEN(&inspected_expanded_term_column), |
| 7289 | GRN_TEXT_VALUE(&inspected_expanded_term_column)); |
| 7290 | GRN_OBJ_FIN(ctx, &inspected_term_column); |
| 7291 | GRN_OBJ_FIN(ctx, &inspected_expanded_term_column); |
| 7292 | GRN_API_RETURN(ctx->rc); |
| 7293 | } |
| 7294 | } |
| 7295 | |
| 7296 | if (term_column_is_key) { |
| 7297 | data.expand_term_func = grn_expr_syntax_expand_term_by_column; |
| 7298 | grn_expr_syntax_expand_term_by_column_data column_data; |
| 7299 | data.expand_term_func_data = &column_data; |
| 7300 | column_data.table = table; |
| 7301 | column_data.column = expanded_term_column; |
| 7302 | grn_expr_syntax_expand_query_terms(ctx, &data); |
| 7303 | } else { |
| 7304 | data.expand_term_func = grn_expr_syntax_expand_term_by_table; |
| 7305 | grn_expr_syntax_expand_term_by_table_data table_data; |
| 7306 | data.expand_term_func_data = &table_data; |
| 7307 | table_data.table = table; |
| 7308 | table_data.term_column = term_column; |
| 7309 | table_data.expanded_term_column = expanded_term_column; |
| 7310 | grn_expr_syntax_expand_query_terms(ctx, &data); |
| 7311 | } |
| 7312 | |
| 7313 | GRN_API_RETURN(ctx->rc); |
| 7314 | } |
| 7315 | |
| 7316 | /* |
| 7317 | * TODO: It's very loose implementations. It just splits regexp by |
| 7318 | * meta characters to extract keywords. For example, "r.*nga" has "r" |
| 7319 | * and "nga" keywords. |
| 7320 | */ |
| 7321 | static void |
| 7322 | grn_expr_get_keywords_regexp(grn_ctx *ctx, grn_obj *keywords, grn_obj *regexp) |
| 7323 | { |
| 7324 | const char *all_off_options = "?-mix:"; |
| 7325 | size_t all_off_options_length = strlen(all_off_options); |
| 7326 | const char *regexp_raw; |
| 7327 | const char *regexp_raw_end; |
| 7328 | bool escaping = false; |
| 7329 | grn_obj keyword; |
| 7330 | |
| 7331 | regexp_raw = GRN_TEXT_VALUE(regexp); |
| 7332 | regexp_raw_end = regexp_raw + GRN_TEXT_LEN(regexp); |
| 7333 | |
| 7334 | GRN_TEXT_INIT(&keyword, 0); |
| 7335 | while (regexp_raw < regexp_raw_end) { |
| 7336 | unsigned int char_len; |
| 7337 | |
| 7338 | char_len = grn_charlen(ctx, regexp_raw, regexp_raw_end); |
| 7339 | |
| 7340 | if (char_len == 1) { |
| 7341 | if (escaping) { |
| 7342 | escaping = false; |
| 7343 | switch (regexp_raw[0]) { |
| 7344 | case 'A': |
| 7345 | case 'z': |
| 7346 | if (GRN_TEXT_LEN(&keyword) > 0) { |
| 7347 | grn_vector_add_element(ctx, |
| 7348 | keywords, |
| 7349 | GRN_TEXT_VALUE(&keyword), |
| 7350 | GRN_TEXT_LEN(&keyword), |
| 7351 | 0, |
| 7352 | GRN_DB_TEXT); |
| 7353 | GRN_BULK_REWIND(&keyword); |
| 7354 | } |
| 7355 | break; |
| 7356 | default: |
| 7357 | GRN_TEXT_PUTC(ctx, &keyword, regexp_raw[0]); |
| 7358 | break; |
| 7359 | } |
| 7360 | } else { |
| 7361 | switch (regexp_raw[0]) { |
| 7362 | case '(': |
| 7363 | regexp_raw += all_off_options_length; |
| 7364 | break; |
| 7365 | case ')': |
| 7366 | break; |
| 7367 | case '.': |
| 7368 | break; |
| 7369 | case '*': |
| 7370 | if (GRN_TEXT_LEN(&keyword) > 0) { |
| 7371 | grn_vector_add_element(ctx, |
| 7372 | keywords, |
| 7373 | GRN_TEXT_VALUE(&keyword), |
| 7374 | GRN_TEXT_LEN(&keyword), |
| 7375 | 0, |
| 7376 | GRN_DB_TEXT); |
| 7377 | GRN_BULK_REWIND(&keyword); |
| 7378 | } |
| 7379 | break; |
| 7380 | case '\\': |
| 7381 | escaping = true; |
| 7382 | break; |
| 7383 | default: |
| 7384 | GRN_TEXT_PUTC(ctx, &keyword, regexp_raw[0]); |
| 7385 | break; |
| 7386 | } |
| 7387 | } |
| 7388 | } else { |
| 7389 | escaping = false; |
| 7390 | GRN_TEXT_PUT(ctx, &keyword, regexp_raw, char_len); |
| 7391 | } |
| 7392 | |
| 7393 | regexp_raw += char_len; |
| 7394 | } |
| 7395 | |
| 7396 | if (GRN_TEXT_LEN(&keyword) > 0) { |
| 7397 | grn_vector_add_element(ctx, |
| 7398 | keywords, |
| 7399 | GRN_TEXT_VALUE(&keyword), |
| 7400 | GRN_TEXT_LEN(&keyword), |
| 7401 | 0, |
| 7402 | GRN_DB_TEXT); |
| 7403 | } |
| 7404 | |
| 7405 | GRN_OBJ_FIN(ctx, &keyword); |
| 7406 | } |
| 7407 | |
| 7408 | grn_rc |
| 7409 | grn_expr_get_keywords(grn_ctx *ctx, grn_obj *expr, grn_obj *keywords) |
| 7410 | { |
| 7411 | int i, n; |
| 7412 | scan_info **sis, *si; |
| 7413 | GRN_API_ENTER; |
| 7414 | if ((sis = grn_scan_info_build(ctx, expr, &n, GRN_OP_OR, false))) { |
| 7415 | int butp = 0, nparens = 0, npbut = 0; |
| 7416 | grn_obj but_stack; |
| 7417 | GRN_UINT32_INIT(&but_stack, GRN_OBJ_VECTOR); |
| 7418 | for (i = n; i--;) { |
| 7419 | si = sis[i]; |
| 7420 | if (si->flags & SCAN_POP) { |
| 7421 | nparens++; |
| 7422 | if (si->logical_op == GRN_OP_AND_NOT) { |
| 7423 | GRN_UINT32_PUT(ctx, &but_stack, npbut); |
| 7424 | npbut = nparens; |
| 7425 | butp = 1 - butp; |
| 7426 | } |
| 7427 | } else { |
| 7428 | if (butp == (si->logical_op == GRN_OP_AND_NOT) && |
| 7429 | grn_obj_is_text_family_bulk(ctx, si->query)) { |
| 7430 | switch (si->op) { |
| 7431 | case GRN_OP_MATCH: |
| 7432 | case GRN_OP_PREFIX: |
| 7433 | if (keywords->header.type == GRN_PVECTOR) { |
| 7434 | GRN_PTR_PUT(ctx, keywords, si->query); |
| 7435 | } else { |
| 7436 | grn_vector_add_element(ctx, |
| 7437 | keywords, |
| 7438 | GRN_TEXT_VALUE(si->query), |
| 7439 | GRN_TEXT_LEN(si->query), |
| 7440 | 0, |
| 7441 | GRN_DB_TEXT); |
| 7442 | } |
| 7443 | break; |
| 7444 | case GRN_OP_REGEXP: |
| 7445 | /* TODO: It should be refined. */ |
| 7446 | if (is_index_searchable_regexp(ctx, si->query)) { |
| 7447 | if (keywords->header.type == GRN_PVECTOR) { |
| 7448 | GRN_PTR_PUT(ctx, keywords, si->query); |
| 7449 | } else { |
| 7450 | grn_expr_get_keywords_regexp(ctx, keywords, si->query); |
| 7451 | } |
| 7452 | } |
| 7453 | break; |
| 7454 | case GRN_OP_SIMILAR: |
| 7455 | case GRN_OP_QUORUM: |
| 7456 | if (keywords->header.type == GRN_VECTOR && |
| 7457 | GRN_BULK_VSIZE(&(si->index)) > 0) { |
| 7458 | grn_token_cursor *token_cursor; |
| 7459 | unsigned int token_flags = 0; |
| 7460 | grn_obj *index = GRN_PTR_VALUE(&(si->index)); |
| 7461 | grn_obj *lexicon; |
| 7462 | |
| 7463 | lexicon = grn_ctx_at(ctx, index->header.domain); |
| 7464 | token_cursor = grn_token_cursor_open(ctx, |
| 7465 | lexicon, |
| 7466 | GRN_TEXT_VALUE(si->query), |
| 7467 | GRN_TEXT_LEN(si->query), |
| 7468 | GRN_TOKENIZE_GET, |
| 7469 | token_flags); |
| 7470 | if (token_cursor) { |
| 7471 | grn_obj *source_table; |
| 7472 | uint32_t n_records_threshold; |
| 7473 | source_table = grn_ctx_at(ctx, grn_obj_get_range(ctx, index)); |
| 7474 | n_records_threshold = grn_table_size(ctx, source_table) / 2; |
| 7475 | while (token_cursor->status != GRN_TOKEN_CURSOR_DONE) { |
| 7476 | grn_id token_id; |
| 7477 | uint32_t n_estimated_records; |
| 7478 | token_id = grn_token_cursor_next(ctx, token_cursor); |
| 7479 | if (token_id == GRN_ID_NIL) { |
| 7480 | continue; |
| 7481 | } |
| 7482 | n_estimated_records = |
| 7483 | grn_ii_estimate_size(ctx, (grn_ii *)index, token_id); |
| 7484 | if (n_estimated_records >= n_records_threshold) { |
| 7485 | continue; |
| 7486 | } |
| 7487 | grn_vector_add_element(ctx, |
| 7488 | keywords, |
| 7489 | token_cursor->curr, |
| 7490 | token_cursor->curr_size, |
| 7491 | 0, |
| 7492 | GRN_DB_TEXT); |
| 7493 | } |
| 7494 | grn_token_cursor_close(ctx, token_cursor); |
| 7495 | } |
| 7496 | } |
| 7497 | break; |
| 7498 | default: |
| 7499 | break; |
| 7500 | } |
| 7501 | } |
| 7502 | if (si->flags & SCAN_PUSH) { |
| 7503 | if (nparens == npbut) { |
| 7504 | butp = 1 - butp; |
| 7505 | GRN_UINT32_POP(&but_stack, npbut); |
| 7506 | } |
| 7507 | nparens--; |
| 7508 | } |
| 7509 | } |
| 7510 | } |
| 7511 | GRN_OBJ_FIN(ctx, &but_stack); |
| 7512 | for (i = n; i--;) { |
| 7513 | SI_FREE(sis[i]); |
| 7514 | } |
| 7515 | GRN_FREE(sis); |
| 7516 | } |
| 7517 | GRN_API_RETURN(GRN_SUCCESS); |
| 7518 | } |
| 7519 | |
| 7520 | grn_rc |
| 7521 | grn_expr_snip_add_conditions(grn_ctx *ctx, |
| 7522 | grn_obj *expr, |
| 7523 | grn_obj *snip, |
| 7524 | unsigned int n_tags, |
| 7525 | const char **opentags, |
| 7526 | unsigned int *opentag_lens, |
| 7527 | const char **closetags, |
| 7528 | unsigned int *closetag_lens) |
| 7529 | { |
| 7530 | grn_rc rc; |
| 7531 | grn_obj keywords; |
| 7532 | |
| 7533 | GRN_API_ENTER; |
| 7534 | |
| 7535 | GRN_PTR_INIT(&keywords, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 7536 | rc = grn_expr_get_keywords(ctx, expr, &keywords); |
| 7537 | if (rc != GRN_SUCCESS) { |
| 7538 | GRN_OBJ_FIN(ctx, &keywords); |
| 7539 | GRN_API_RETURN(rc); |
| 7540 | } |
| 7541 | |
| 7542 | if (n_tags) { |
| 7543 | int i; |
| 7544 | for (i = 0;; i = (i + 1) % n_tags) { |
| 7545 | grn_obj *keyword; |
| 7546 | GRN_PTR_POP(&keywords, keyword); |
| 7547 | if (!keyword) { |
| 7548 | break; |
| 7549 | } |
| 7550 | grn_snip_add_cond(ctx, |
| 7551 | snip, |
| 7552 | GRN_TEXT_VALUE(keyword), |
| 7553 | GRN_TEXT_LEN(keyword), |
| 7554 | opentags[i], |
| 7555 | opentag_lens[i], |
| 7556 | closetags[i], |
| 7557 | closetag_lens[i]); |
| 7558 | } |
| 7559 | } else { |
| 7560 | for (;;) { |
| 7561 | grn_obj *keyword; |
| 7562 | GRN_PTR_POP(&keywords, keyword); |
| 7563 | if (!keyword) { |
| 7564 | break; |
| 7565 | } |
| 7566 | grn_snip_add_cond(ctx, |
| 7567 | snip, |
| 7568 | GRN_TEXT_VALUE(keyword), |
| 7569 | GRN_TEXT_LEN(keyword), |
| 7570 | NULL, |
| 7571 | 0, |
| 7572 | NULL, |
| 7573 | 0); |
| 7574 | } |
| 7575 | } |
| 7576 | GRN_OBJ_FIN(ctx, &keywords); |
| 7577 | |
| 7578 | GRN_API_RETURN(GRN_SUCCESS); |
| 7579 | } |
| 7580 | |
| 7581 | grn_obj * |
| 7582 | grn_expr_snip(grn_ctx *ctx, |
| 7583 | grn_obj *expr, |
| 7584 | int flags, |
| 7585 | unsigned int width, |
| 7586 | unsigned int max_results, |
| 7587 | unsigned int n_tags, |
| 7588 | const char **opentags, |
| 7589 | unsigned int *opentag_lens, |
| 7590 | const char **closetags, |
| 7591 | unsigned int *closetag_lens, |
| 7592 | grn_snip_mapping *mapping) |
| 7593 | { |
| 7594 | grn_obj *res = NULL; |
| 7595 | GRN_API_ENTER; |
| 7596 | if ((res = grn_snip_open(ctx, |
| 7597 | flags, |
| 7598 | width, |
| 7599 | max_results, |
| 7600 | NULL, |
| 7601 | 0, |
| 7602 | NULL, |
| 7603 | 0, |
| 7604 | mapping))) { |
| 7605 | grn_expr_snip_add_conditions(ctx, |
| 7606 | expr, |
| 7607 | res, |
| 7608 | n_tags, |
| 7609 | opentags, |
| 7610 | opentag_lens, |
| 7611 | closetags, |
| 7612 | closetag_lens); |
| 7613 | } |
| 7614 | GRN_API_RETURN(res); |
| 7615 | } |
| 7616 | |
| 7617 | grn_rc |
| 7618 | grn_expr_syntax_escape(grn_ctx *ctx, |
| 7619 | const char *query, |
| 7620 | int query_size, |
| 7621 | const char *target_characters, |
| 7622 | char escape_character, |
| 7623 | grn_obj *escaped_query) |
| 7624 | { |
| 7625 | grn_rc rc = GRN_SUCCESS; |
| 7626 | const char *current, *query_end; |
| 7627 | |
| 7628 | if (!query) { |
| 7629 | return GRN_INVALID_ARGUMENT; |
| 7630 | } |
| 7631 | |
| 7632 | GRN_API_ENTER; |
| 7633 | if (query_size < 0) { |
| 7634 | query_size = strlen(query); |
| 7635 | } |
| 7636 | query_end = query + query_size; |
| 7637 | |
| 7638 | current = query; |
| 7639 | while (current < query_end) { |
| 7640 | unsigned int char_size; |
| 7641 | char_size = grn_charlen(ctx, current, query_end); |
| 7642 | switch (char_size) { |
| 7643 | case 0: |
| 7644 | /* string includes malformed multibyte character. */ |
| 7645 | return GRN_INVALID_ARGUMENT; |
| 7646 | break; |
| 7647 | case 1: |
| 7648 | if (strchr(target_characters, *current)) { |
| 7649 | GRN_TEXT_PUTC(ctx, escaped_query, escape_character); |
| 7650 | } |
| 7651 | GRN_TEXT_PUT(ctx, escaped_query, current, char_size); |
| 7652 | current += char_size; |
| 7653 | break; |
| 7654 | default: |
| 7655 | GRN_TEXT_PUT(ctx, escaped_query, current, char_size); |
| 7656 | current += char_size; |
| 7657 | break; |
| 7658 | } |
| 7659 | } |
| 7660 | |
| 7661 | GRN_API_RETURN(rc); |
| 7662 | } |
| 7663 | |
| 7664 | grn_rc |
| 7665 | grn_expr_syntax_escape_query(grn_ctx *ctx, |
| 7666 | const char *query, |
| 7667 | int query_size, |
| 7668 | grn_obj *escaped_query) |
| 7669 | { |
| 7670 | const char target_characters[] = { |
| 7671 | GRN_QUERY_AND, |
| 7672 | GRN_QUERY_AND_NOT, |
| 7673 | GRN_QUERY_ADJ_INC, |
| 7674 | GRN_QUERY_ADJ_DEC, |
| 7675 | GRN_QUERY_ADJ_NEG, |
| 7676 | GRN_QUERY_PREFIX, |
| 7677 | GRN_QUERY_PARENL, |
| 7678 | GRN_QUERY_PARENR, |
| 7679 | GRN_QUERY_QUOTEL, |
| 7680 | GRN_QUERY_ESCAPE, |
| 7681 | GRN_QUERY_COLUMN, |
| 7682 | '\0', |
| 7683 | }; |
| 7684 | return grn_expr_syntax_escape(ctx, |
| 7685 | query, |
| 7686 | query_size, |
| 7687 | target_characters, |
| 7688 | GRN_QUERY_ESCAPE, |
| 7689 | escaped_query); |
| 7690 | } |
| 7691 | |
| 7692 | grn_rc |
| 7693 | grn_expr_dump_plan(grn_ctx *ctx, grn_obj *expr, grn_obj *buffer) |
| 7694 | { |
| 7695 | grn_scanner *scanner; |
| 7696 | |
| 7697 | GRN_API_ENTER; |
| 7698 | scanner = grn_scanner_open(ctx, expr, GRN_OP_OR, false); |
| 7699 | if (scanner) { |
| 7700 | grn_inspect_scan_info_list(ctx, buffer, scanner->sis, scanner->n_sis); |
| 7701 | grn_scanner_close(ctx, scanner); |
| 7702 | } else { |
| 7703 | GRN_TEXT_PUTS(ctx, buffer, "sequential search\n"); |
| 7704 | } |
| 7705 | GRN_API_RETURN(GRN_SUCCESS); |
| 7706 | } |
| 7707 | |
| 7708 | static unsigned int |
| 7709 | grn_expr_estimate_size_raw(grn_ctx *ctx, grn_obj *expr, grn_obj *table) |
| 7710 | { |
| 7711 | return grn_table_size(ctx, table); |
| 7712 | } |
| 7713 | |
| 7714 | unsigned int |
| 7715 | grn_expr_estimate_size(grn_ctx *ctx, grn_obj *expr) |
| 7716 | { |
| 7717 | grn_obj *table; |
| 7718 | grn_obj *variable; |
| 7719 | unsigned int size; |
| 7720 | |
| 7721 | variable = grn_expr_get_var_by_offset(ctx, expr, 0); |
| 7722 | if (!variable) { |
| 7723 | ERR(GRN_INVALID_ARGUMENT, "at least one variable must be defined"); |
| 7724 | return 0; |
| 7725 | } |
| 7726 | |
| 7727 | table = grn_ctx_at(ctx, variable->header.domain); |
| 7728 | if (!table) { |
| 7729 | ERR(GRN_INVALID_ARGUMENT, |
| 7730 | "variable refers unknown domain: <%u>", |
| 7731 | variable->header.domain); |
| 7732 | return 0; |
| 7733 | } |
| 7734 | |
| 7735 | GRN_API_ENTER; |
| 7736 | #ifdef GRN_WITH_MRUBY |
| 7737 | grn_ctx_impl_mrb_ensure_init(ctx); |
| 7738 | if (ctx->rc != GRN_SUCCESS) { |
| 7739 | GRN_API_RETURN(0); |
| 7740 | } |
| 7741 | if (ctx->impl->mrb.state) { |
| 7742 | size = grn_mrb_expr_estimate_size(ctx, expr, table); |
| 7743 | } else { |
| 7744 | size = grn_expr_estimate_size_raw(ctx, expr, table); |
| 7745 | } |
| 7746 | #else |
| 7747 | size = grn_expr_estimate_size_raw(ctx, expr, table); |
| 7748 | #endif |
| 7749 | GRN_API_RETURN(size); |
| 7750 | } |
| 7751 | |
| 7752 | bool |
| 7753 | grn_expr_is_simple_function_call(grn_ctx *ctx, grn_obj *expr) |
| 7754 | { |
| 7755 | grn_expr *e = (grn_expr *)expr; |
| 7756 | grn_expr_code *codes = e->codes; |
| 7757 | grn_expr_code *codes_end = codes + e->codes_curr; |
| 7758 | |
| 7759 | if (codes == codes_end) { |
| 7760 | return false; |
| 7761 | } |
| 7762 | |
| 7763 | for (; codes < codes_end; codes++) { |
| 7764 | switch (codes[0].op) { |
| 7765 | case GRN_OP_PUSH: |
| 7766 | break; |
| 7767 | case GRN_OP_CALL: |
| 7768 | if (codes + 1 != codes_end) { |
| 7769 | return false; |
| 7770 | } |
| 7771 | break; |
| 7772 | default: |
| 7773 | return false; |
| 7774 | } |
| 7775 | } |
| 7776 | |
| 7777 | return true; |
| 7778 | } |
| 7779 | |
| 7780 | grn_obj * |
| 7781 | grn_expr_simple_function_call_get_function(grn_ctx *ctx, grn_obj *expr) |
| 7782 | { |
| 7783 | grn_expr *e = (grn_expr *)expr; |
| 7784 | |
| 7785 | return e->codes[0].value; |
| 7786 | } |
| 7787 | |
| 7788 | grn_rc |
| 7789 | grn_expr_simple_function_call_get_arguments(grn_ctx *ctx, |
| 7790 | grn_obj *expr, |
| 7791 | grn_obj *arguments) |
| 7792 | { |
| 7793 | grn_expr *e = (grn_expr *)expr; |
| 7794 | grn_expr_code *codes = e->codes; |
| 7795 | grn_expr_code *codes_end = codes + e->codes_curr; |
| 7796 | |
| 7797 | for (codes++; codes < codes_end - 1; codes++) { |
| 7798 | grn_obj *value = codes[0].value; |
| 7799 | switch (codes[0].op) { |
| 7800 | case GRN_OP_PUSH: |
| 7801 | grn_vector_add_element(ctx, |
| 7802 | arguments, |
| 7803 | GRN_BULK_HEAD(value), |
| 7804 | GRN_BULK_VSIZE(value), |
| 7805 | 0, |
| 7806 | value->header.domain); |
| 7807 | break; |
| 7808 | default: |
| 7809 | return GRN_INVALID_ARGUMENT; |
| 7810 | break; |
| 7811 | } |
| 7812 | } |
| 7813 | |
| 7814 | return GRN_SUCCESS; |
| 7815 | } |
| 7816 | |
| 7817 | bool |
| 7818 | grn_expr_is_module_list(grn_ctx *ctx, grn_obj *expr) |
| 7819 | { |
| 7820 | grn_expr *e = (grn_expr *)expr; |
| 7821 | grn_expr_code *codes = e->codes; |
| 7822 | grn_expr_code *codes_end = codes + e->codes_curr; |
| 7823 | |
| 7824 | if (codes == codes_end) { |
| 7825 | return false; |
| 7826 | } |
| 7827 | |
| 7828 | for (; codes < codes_end; codes++) { |
| 7829 | switch (codes[0].op) { |
| 7830 | case GRN_OP_PUSH: |
| 7831 | break; |
| 7832 | case GRN_OP_CALL: |
| 7833 | break; |
| 7834 | case GRN_OP_COMMA: |
| 7835 | break; |
| 7836 | default: |
| 7837 | return false; |
| 7838 | } |
| 7839 | } |
| 7840 | |
| 7841 | return true; |
| 7842 | } |
| 7843 | |
| 7844 | unsigned int |
| 7845 | grn_expr_module_list_get_n_modules(grn_ctx *ctx, grn_obj *expr) |
| 7846 | { |
| 7847 | grn_expr *e = (grn_expr *)expr; |
| 7848 | grn_expr_code *codes = e->codes; |
| 7849 | grn_expr_code *codes_end = codes + e->codes_curr; |
| 7850 | unsigned int n = 1; |
| 7851 | |
| 7852 | for (; codes < codes_end; codes++) { |
| 7853 | if (codes[0].op == GRN_OP_COMMA) { |
| 7854 | n++; |
| 7855 | } |
| 7856 | } |
| 7857 | |
| 7858 | return n; |
| 7859 | } |
| 7860 | |
| 7861 | static void |
| 7862 | grn_expr_module_list_detect_module(grn_ctx *ctx, |
| 7863 | grn_obj *expr, |
| 7864 | unsigned int i, |
| 7865 | grn_expr_code **module_start, |
| 7866 | grn_expr_code **module_end) |
| 7867 | { |
| 7868 | grn_expr *e = (grn_expr *)expr; |
| 7869 | grn_expr_code *codes = e->codes; |
| 7870 | grn_expr_code *codes_end = codes + e->codes_curr; |
| 7871 | unsigned int j = 0; |
| 7872 | |
| 7873 | *module_start = codes; |
| 7874 | *module_end = codes_end; |
| 7875 | |
| 7876 | if (i == 0) { |
| 7877 | for (codes = e->codes; codes < codes_end; codes++) { |
| 7878 | switch (codes[0].op) { |
| 7879 | case GRN_OP_CALL: |
| 7880 | if ((codes + 1) < codes_end && codes[1].op == GRN_OP_COMMA) { |
| 7881 | /* |
| 7882 | * A,B(X): |
| 7883 | * * push: A <- module_start |
| 7884 | * * push: B <- module_end |
| 7885 | * * push: X |
| 7886 | * * call |
| 7887 | * * comma |
| 7888 | */ |
| 7889 | *module_start = codes - codes[0].nargs - 1; |
| 7890 | *module_end = codes - codes[0].nargs; |
| 7891 | } else { |
| 7892 | /* |
| 7893 | * A(X): |
| 7894 | * * push: A <- module_start |
| 7895 | * * push: X |
| 7896 | * * call <- module_end |
| 7897 | */ |
| 7898 | *module_start = codes - codes[0].nargs; |
| 7899 | *module_end = codes; |
| 7900 | } |
| 7901 | return; |
| 7902 | case GRN_OP_COMMA: |
| 7903 | /* |
| 7904 | * A(X): |
| 7905 | * * push: A <- module_start |
| 7906 | * * push: B <- module_end |
| 7907 | * * comma |
| 7908 | */ |
| 7909 | *module_start = codes - 2; |
| 7910 | *module_end = codes - 1; |
| 7911 | return; |
| 7912 | default: |
| 7913 | break; |
| 7914 | } |
| 7915 | } |
| 7916 | return; |
| 7917 | } else { |
| 7918 | for (codes = e->codes; codes < codes_end; codes++) { |
| 7919 | if (codes[0].op != GRN_OP_COMMA) { |
| 7920 | continue; |
| 7921 | } |
| 7922 | j++; |
| 7923 | if (i == j) { |
| 7924 | if (codes > e->codes && codes[-1].op == GRN_OP_CALL) { |
| 7925 | /* |
| 7926 | * i = 1 |
| 7927 | * A,B(X): |
| 7928 | * * push: A |
| 7929 | * * push: B <- module_start |
| 7930 | * * push: X |
| 7931 | * * call <- module_end |
| 7932 | * * comma |
| 7933 | */ |
| 7934 | *module_start = codes - codes[-1].nargs - 1; |
| 7935 | *module_end = codes - 1; |
| 7936 | } else { |
| 7937 | /* |
| 7938 | * i = 1 |
| 7939 | * A(X),B: |
| 7940 | * * push: A |
| 7941 | * * push: X |
| 7942 | * * call |
| 7943 | * * push: B <- module_start |
| 7944 | * * comma <- module_end |
| 7945 | */ |
| 7946 | *module_start = codes - 1; |
| 7947 | *module_end = codes; |
| 7948 | } |
| 7949 | return; |
| 7950 | } |
| 7951 | } |
| 7952 | } |
| 7953 | |
| 7954 | *module_start = NULL; |
| 7955 | *module_end = NULL; |
| 7956 | } |
| 7957 | |
| 7958 | grn_obj * |
| 7959 | grn_expr_module_list_get_function(grn_ctx *ctx, grn_obj *expr, unsigned int i) |
| 7960 | { |
| 7961 | grn_expr_code *module_start; |
| 7962 | grn_expr_code *module_end; |
| 7963 | |
| 7964 | grn_expr_module_list_detect_module(ctx, expr, i, &module_start, &module_end); |
| 7965 | |
| 7966 | if (module_start) { |
| 7967 | return module_start[0].value; |
| 7968 | } else { |
| 7969 | return NULL; |
| 7970 | } |
| 7971 | } |
| 7972 | |
| 7973 | grn_rc |
| 7974 | grn_expr_module_list_get_arguments(grn_ctx *ctx, |
| 7975 | grn_obj *expr, |
| 7976 | unsigned int i, |
| 7977 | grn_obj *arguments) |
| 7978 | { |
| 7979 | grn_expr_code *codes; |
| 7980 | grn_expr_code *module_start; |
| 7981 | grn_expr_code *module_end; |
| 7982 | |
| 7983 | grn_expr_module_list_detect_module(ctx, expr, i, &module_start, &module_end); |
| 7984 | |
| 7985 | for (codes = module_start + 1; codes < module_end; codes++) { |
| 7986 | grn_obj *value = codes[0].value; |
| 7987 | switch (codes[0].op) { |
| 7988 | case GRN_OP_PUSH: |
| 7989 | grn_vector_add_element(ctx, |
| 7990 | arguments, |
| 7991 | GRN_BULK_HEAD(value), |
| 7992 | GRN_BULK_VSIZE(value), |
| 7993 | 0, |
| 7994 | value->header.domain); |
| 7995 | break; |
| 7996 | default: |
| 7997 | return GRN_INVALID_ARGUMENT; |
| 7998 | break; |
| 7999 | } |
| 8000 | } |
| 8001 | |
| 8002 | return GRN_SUCCESS; |
| 8003 | } |
| 8004 | |
| 8005 | grn_rc |
| 8006 | grn_expr_match_columns_split(grn_ctx *ctx, |
| 8007 | grn_obj *expr, |
| 8008 | grn_obj *splitted_match_columns) |
| 8009 | { |
| 8010 | grn_expr *e = (grn_expr *)expr; |
| 8011 | uint32_t match_columns_start = 0; |
| 8012 | uint32_t i; |
| 8013 | for (i = 0; i < e->codes_curr; i++) { |
| 8014 | switch (e->codes[i].op) { |
| 8015 | case GRN_OP_STAR: |
| 8016 | { |
| 8017 | grn_obj *match_column = |
| 8018 | grn_expr_slice(ctx, expr, match_columns_start, i + 1); |
| 8019 | GRN_PTR_PUT(ctx, splitted_match_columns, match_column); |
| 8020 | match_columns_start = i + 1; |
| 8021 | } |
| 8022 | break; |
| 8023 | case GRN_OP_OR: |
| 8024 | if (match_columns_start == i) { |
| 8025 | match_columns_start++; |
| 8026 | } else { |
| 8027 | grn_obj *match_column = |
| 8028 | grn_expr_slice(ctx, expr, match_columns_start, i); |
| 8029 | GRN_PTR_PUT(ctx, splitted_match_columns, match_column); |
| 8030 | match_columns_start = i + 1; |
| 8031 | } |
| 8032 | break; |
| 8033 | default: |
| 8034 | if (e->codes[i].modify != 0 && |
| 8035 | e->codes[i + e->codes[i].modify].op == GRN_OP_OR) { |
| 8036 | grn_obj *match_column = |
| 8037 | grn_expr_slice(ctx, expr, match_columns_start, i + 1); |
| 8038 | GRN_PTR_PUT(ctx, splitted_match_columns, match_column); |
| 8039 | match_columns_start = i + 1; |
| 8040 | } |
| 8041 | break; |
| 8042 | } |
| 8043 | } |
| 8044 | |
| 8045 | if (match_columns_start < e->codes_curr) { |
| 8046 | uint32_t match_columns_end = e->codes_curr; |
| 8047 | if (match_columns_start < match_columns_end - 1) { |
| 8048 | if (e->codes[match_columns_end - 1].op == GRN_OP_OR) { |
| 8049 | match_columns_end--; |
| 8050 | } |
| 8051 | } |
| 8052 | grn_obj *match_column = |
| 8053 | grn_expr_slice(ctx, expr, match_columns_start, match_columns_end); |
| 8054 | GRN_PTR_PUT(ctx, splitted_match_columns, match_column); |
| 8055 | } |
| 8056 | |
| 8057 | return GRN_SUCCESS; |
| 8058 | } |
| 8059 | |
| 8060 | grn_rc |
| 8061 | grn_expr_set_query_log_tag_prefix(grn_ctx *ctx, |
| 8062 | grn_obj *expr, |
| 8063 | const char *prefix, |
| 8064 | int prefix_len) |
| 8065 | { |
| 8066 | GRN_API_ENTER; |
| 8067 | if (prefix_len < 0) { |
| 8068 | if (prefix) { |
| 8069 | prefix_len = strlen(prefix); |
| 8070 | } else { |
| 8071 | prefix_len = 0; |
| 8072 | } |
| 8073 | } |
| 8074 | |
| 8075 | grn_expr *e = (grn_expr *)expr; |
| 8076 | if (prefix_len == 0) { |
| 8077 | GRN_BULK_REWIND(&(e->query_log_tag_prefix)); |
| 8078 | } else { |
| 8079 | GRN_TEXT_SET(ctx, &(e->query_log_tag_prefix), prefix, prefix_len); |
| 8080 | } |
| 8081 | GRN_TEXT_PUTC(ctx, &(e->query_log_tag_prefix), '\0'); |
| 8082 | |
| 8083 | GRN_API_RETURN(GRN_SUCCESS); |
| 8084 | } |
| 8085 | |
| 8086 | const char * |
| 8087 | grn_expr_get_query_log_tag_prefix(grn_ctx *ctx, grn_obj *expr) |
| 8088 | { |
| 8089 | GRN_API_ENTER; |
| 8090 | grn_expr *e = (grn_expr *)expr; |
| 8091 | const char *prefix = GRN_TEXT_VALUE(&(e->query_log_tag_prefix)); |
| 8092 | GRN_API_RETURN(prefix); |
| 8093 | } |
| 8094 | |
| 8095 | grn_rc |
| 8096 | grn_expr_set_parent(grn_ctx *ctx, grn_obj *expr, grn_obj *parent) |
| 8097 | { |
| 8098 | GRN_API_ENTER; |
| 8099 | grn_expr *e = (grn_expr *)expr; |
| 8100 | e->parent = parent; |
| 8101 | GRN_API_RETURN(GRN_SUCCESS); |
| 8102 | } |
| 8103 | |
| 8104 | grn_obj * |
| 8105 | grn_expr_get_parent(grn_ctx *ctx, grn_obj *expr) |
| 8106 | { |
| 8107 | GRN_API_ENTER; |
| 8108 | grn_expr *e = (grn_expr *)expr; |
| 8109 | GRN_API_RETURN(e->parent); |
| 8110 | } |
| 8111 | |
| 8112 | grn_rc |
| 8113 | grn_expr_set_condition(grn_ctx *ctx, grn_obj *expr, grn_obj *condition) |
| 8114 | { |
| 8115 | GRN_API_ENTER; |
| 8116 | grn_expr *e = (grn_expr *)expr; |
| 8117 | e->condition = condition; |
| 8118 | GRN_API_RETURN(GRN_SUCCESS); |
| 8119 | } |
| 8120 | |
| 8121 | grn_obj * |
| 8122 | grn_expr_get_condition(grn_ctx *ctx, grn_obj *expr) |
| 8123 | { |
| 8124 | GRN_API_ENTER; |
| 8125 | grn_expr *e = (grn_expr *)expr; |
| 8126 | GRN_API_RETURN(e->condition); |
| 8127 | } |
| 8128 | |
| 8129 | static bool |
| 8130 | grn_expr_is_options(grn_ctx *ctx, grn_obj *obj) |
| 8131 | { |
| 8132 | return obj->header.type == GRN_TABLE_HASH_KEY && |
| 8133 | grn_obj_is_temporary(ctx, obj) && |
| 8134 | ((grn_hash *)obj)->value_size == sizeof(grn_obj); |
| 8135 | } |
| 8136 | |
| 8137 | static grn_obj * |
| 8138 | grn_expr_copy_options(grn_ctx *ctx, grn_obj *expr, grn_obj *options) |
| 8139 | { |
| 8140 | grn_hash *source = (grn_hash *)options; |
| 8141 | grn_hash *copied = grn_hash_create(ctx, |
| 8142 | NULL, |
| 8143 | source->key_size, |
| 8144 | source->value_size, |
| 8145 | source->obj.header.flags); |
| 8146 | if (!copied) { |
| 8147 | ERR(GRN_NO_MEMORY_AVAILABLE, "[expr][copy-options] failed to create hash"); |
| 8148 | return NULL; |
| 8149 | } |
| 8150 | copied->obj.header.domain = source->obj.header.domain; |
| 8151 | GRN_HASH_EACH_BEGIN(ctx, source, cursor, id) |
| 8152 | { |
| 8153 | void *key; |
| 8154 | unsigned int key_size; |
| 8155 | void *value; |
| 8156 | grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value); |
| 8157 | void *copied_value; |
| 8158 | grn_id copied_id = |
| 8159 | grn_hash_add(ctx, copied, key, key_size, &copied_value, NULL); |
| 8160 | if (copied_id == GRN_ID_NIL) { |
| 8161 | ERR(GRN_NO_MEMORY_AVAILABLE, "[expr][copy-options] failed to add key"); |
| 8162 | break; |
| 8163 | } |
| 8164 | grn_obj *entry = (grn_obj *)value; |
| 8165 | grn_obj *copied_entry = (grn_obj *)copied_value; |
| 8166 | switch (entry->header.type) { |
| 8167 | case GRN_PTR: |
| 8168 | { |
| 8169 | grn_obj *pointer = GRN_PTR_VALUE(entry); |
| 8170 | if (pointer && grn_expr_is_options(ctx, pointer)) { |
| 8171 | pointer = grn_expr_copy_options(ctx, expr, pointer); |
| 8172 | if (!pointer) { |
| 8173 | break; |
| 8174 | } |
| 8175 | grn_expr_take_obj(ctx, expr, pointer); |
| 8176 | } |
| 8177 | GRN_PTR_INIT(copied_entry, 0, GRN_ID_NIL); |
| 8178 | GRN_PTR_SET(ctx, copied_entry, pointer); |
| 8179 | } |
| 8180 | break; |
| 8181 | case GRN_VECTOR: |
| 8182 | GRN_OBJ_INIT(copied_entry, entry->header.type, 0, entry->header.domain); |
| 8183 | grn_vector_copy(ctx, entry, copied_entry); |
| 8184 | break; |
| 8185 | default: |
| 8186 | GRN_OBJ_INIT(copied_entry, entry->header.type, 0, entry->header.domain); |
| 8187 | GRN_TEXT_PUT(ctx, |
| 8188 | copied_entry, |
| 8189 | GRN_TEXT_VALUE(entry), |
| 8190 | GRN_TEXT_LEN(entry)); |
| 8191 | break; |
| 8192 | } |
| 8193 | } |
| 8194 | GRN_HASH_EACH_END(ctx, cursor); |
| 8195 | if (ctx->rc != GRN_SUCCESS) { |
| 8196 | GRN_HASH_EACH_BEGIN(ctx, copied, cleanup_cursor, cleanup_id) |
| 8197 | { |
| 8198 | void *value; |
| 8199 | grn_hash_cursor_get_value(ctx, cleanup_cursor, &value); |
| 8200 | GRN_OBJ_FIN(ctx, (grn_obj *)value); |
| 8201 | } |
| 8202 | GRN_HASH_EACH_END(ctx, cleanup_cursor); |
| 8203 | grn_hash_close(ctx, copied); |
| 8204 | return NULL; |
| 8205 | } |
| 8206 | return (grn_obj *)copied; |
| 8207 | } |
| 8208 | |
| 8209 | grn_obj * |
| 8210 | grn_expr_slice(grn_ctx *ctx, |
| 8211 | grn_obj *expr, |
| 8212 | uint32_t code_start_offset, |
| 8213 | uint32_t code_end_offset) |
| 8214 | { |
| 8215 | GRN_API_ENTER; |
| 8216 | grn_expr *e = (grn_expr *)expr; |
| 8217 | if (code_start_offset >= e->codes_curr) { |
| 8218 | ERR(GRN_INVALID_ARGUMENT, |
| 8219 | "[expr][slice] code start offset is out of range: [0..%u): <%u>", |
| 8220 | e->codes_curr, |
| 8221 | code_start_offset); |
| 8222 | GRN_API_RETURN(NULL); |
| 8223 | } |
| 8224 | if (code_end_offset > e->codes_curr) { |
| 8225 | ERR(GRN_INVALID_ARGUMENT, |
| 8226 | "[expr][slice] code end offset is out of range: [0..%u]: <%u>", |
| 8227 | e->codes_curr, |
| 8228 | code_end_offset); |
| 8229 | GRN_API_RETURN(NULL); |
| 8230 | } |
| 8231 | if (code_start_offset >= code_end_offset) { |
| 8232 | ERR(GRN_INVALID_ARGUMENT, |
| 8233 | "[expr][slice] code start offset must be less than code end offset: " |
| 8234 | "<%u>:<%u>", |
| 8235 | code_start_offset, |
| 8236 | code_end_offset); |
| 8237 | GRN_API_RETURN(NULL); |
| 8238 | } |
| 8239 | grn_obj *sliced_expr = grn_expr_create(ctx, NULL, 0); |
| 8240 | if (!sliced_expr) { |
| 8241 | GRN_API_RETURN(NULL); |
| 8242 | } |
| 8243 | uint32_t n_vars; |
| 8244 | grn_hash *vars = grn_expr_get_vars(ctx, expr, &n_vars); |
| 8245 | if (n_vars > 0) { |
| 8246 | GRN_HASH_EACH_BEGIN(ctx, vars, cursor, id) |
| 8247 | { |
| 8248 | void *key; |
| 8249 | unsigned int key_size; |
| 8250 | void *value; |
| 8251 | grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value); |
| 8252 | grn_obj *original_value = value; |
| 8253 | grn_obj *copied_value = grn_expr_add_var(ctx, sliced_expr, key, key_size); |
| 8254 | grn_bulk_copy(ctx, original_value, copied_value); |
| 8255 | } |
| 8256 | GRN_HASH_EACH_END(ctx, cursor); |
| 8257 | } |
| 8258 | grn_expr_code *codes_start = e->codes + code_start_offset; |
| 8259 | grn_expr_code *codes_end = e->codes + code_end_offset; |
| 8260 | grn_expr_code *code; |
| 8261 | for (code = codes_start; code < codes_end; code++) { |
| 8262 | if (code->value) { |
| 8263 | grn_obj *value = code->value; |
| 8264 | if (code->value->header.impl_flags & GRN_OBJ_EXPRCONST) { |
| 8265 | grn_expr_append_const(ctx, sliced_expr, value, code->op, code->nargs); |
| 8266 | } else { |
| 8267 | if (grn_obj_is_accessor(ctx, value)) { |
| 8268 | value = grn_accessor_copy(ctx, value); |
| 8269 | } else if (grn_expr_is_options(ctx, value)) { |
| 8270 | value = grn_expr_copy_options(ctx, sliced_expr, value); |
| 8271 | if (!value) { |
| 8272 | grn_expr_close(ctx, sliced_expr); |
| 8273 | GRN_API_RETURN(NULL); |
| 8274 | } |
| 8275 | } else { |
| 8276 | grn_obj_refer(ctx, value); |
| 8277 | } |
| 8278 | grn_expr_take_obj(ctx, sliced_expr, value); |
| 8279 | grn_expr_append_obj(ctx, sliced_expr, value, code->op, code->nargs); |
| 8280 | } |
| 8281 | } else { |
| 8282 | int nargs = code->nargs; |
| 8283 | if (code->op == GRN_OP_CALL) { |
| 8284 | nargs--; |
| 8285 | } |
| 8286 | grn_expr_append_op(ctx, sliced_expr, code->op, nargs); |
| 8287 | } |
| 8288 | } |
| 8289 | GRN_API_RETURN(sliced_expr); |
| 8290 | } |
| 8291 | |
| 8292 | grn_rc |
| 8293 | grn_expr_to_script_syntax(grn_ctx *ctx, grn_obj *expr, grn_obj *buffer) |
| 8294 | { |
| 8295 | grn_obj push_buffer; |
| 8296 | GRN_TEXT_INIT(&push_buffer, 0); |
| 8297 | grn_obj substring_buffer; |
| 8298 | GRN_TEXT_INIT(&substring_buffer, 0); |
| 8299 | grn_obj substring_stack; |
| 8300 | GRN_TEXT_INIT(&substring_stack, GRN_OBJ_VECTOR); |
| 8301 | grn_expr *e = (grn_expr *)expr; |
| 8302 | grn_expr_code *code = e->codes; |
| 8303 | grn_expr_code *code_end = e->codes + e->codes_curr; |
| 8304 | |
| 8305 | #define PUSH_SUBSTRING(substring, substring_length) \ |
| 8306 | do { \ |
| 8307 | grn_vector_add_element(ctx, \ |
| 8308 | &substring_stack, \ |
| 8309 | substring, \ |
| 8310 | substring_length, \ |
| 8311 | 0, \ |
| 8312 | GRN_DB_TEXT); \ |
| 8313 | } while (false) |
| 8314 | |
| 8315 | #define PUSH_OBJECT(object) \ |
| 8316 | do { \ |
| 8317 | GRN_BULK_REWIND(&push_buffer); \ |
| 8318 | grn_obj_to_script_syntax(ctx, object, &push_buffer); \ |
| 8319 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&push_buffer), GRN_TEXT_LEN(&push_buffer)); \ |
| 8320 | } while (false) |
| 8321 | |
| 8322 | #define POP(substring, substring_length) \ |
| 8323 | do { \ |
| 8324 | float weight; \ |
| 8325 | grn_id domain; \ |
| 8326 | substring_length = grn_vector_pop_element_float(ctx, \ |
| 8327 | &substring_stack, \ |
| 8328 | &substring, \ |
| 8329 | &weight, \ |
| 8330 | &domain); \ |
| 8331 | } while (false) |
| 8332 | |
| 8333 | #define DROP() \ |
| 8334 | do { \ |
| 8335 | const char *substring; \ |
| 8336 | uint32_t substring_length; \ |
| 8337 | POP(substring, substring_length); \ |
| 8338 | } while (false) |
| 8339 | |
| 8340 | while (code < code_end) { |
| 8341 | switch (code->op) { |
| 8342 | case GRN_OP_NOP: |
| 8343 | code++; |
| 8344 | break; |
| 8345 | case GRN_OP_PUSH: |
| 8346 | PUSH_OBJECT(code->value); |
| 8347 | code++; |
| 8348 | break; |
| 8349 | case GRN_OP_POP: |
| 8350 | DROP(); |
| 8351 | code++; |
| 8352 | break; |
| 8353 | case GRN_OP_GET_REF: |
| 8354 | { |
| 8355 | grn_obj *column = NULL; |
| 8356 | if (code->nargs == 1) { |
| 8357 | if (code->value) { |
| 8358 | column = code->value; |
| 8359 | } else { |
| 8360 | /* TODO: can't resolve column name */ |
| 8361 | DROP(); |
| 8362 | } |
| 8363 | } else { |
| 8364 | if (code->value) { |
| 8365 | column = code->value; |
| 8366 | DROP(); |
| 8367 | } else { |
| 8368 | /* TODO: can't resolve column name */ |
| 8369 | DROP(); |
| 8370 | DROP(); |
| 8371 | } |
| 8372 | } |
| 8373 | if (grn_obj_is_text_family_bulk(ctx, column)) { |
| 8374 | PUSH_SUBSTRING(GRN_TEXT_VALUE(column), GRN_TEXT_LEN(column)); |
| 8375 | } else if (grn_obj_is_column(ctx, column)) { |
| 8376 | PUSH_OBJECT(column); |
| 8377 | } else { |
| 8378 | GRN_LOG(ctx, |
| 8379 | GRN_LOG_DEBUG, |
| 8380 | "[expr][to-string] failed to resolve column"); |
| 8381 | goto exit; |
| 8382 | } |
| 8383 | code++; |
| 8384 | } |
| 8385 | break; |
| 8386 | case GRN_OP_CALL: |
| 8387 | { |
| 8388 | grn_obj args; |
| 8389 | GRN_TEXT_INIT(&args, GRN_OBJ_VECTOR); |
| 8390 | int32_t i; |
| 8391 | for (i = 0; i < code->nargs; i++) { |
| 8392 | const char *arg; |
| 8393 | int arg_length; |
| 8394 | POP(arg, arg_length); |
| 8395 | grn_vector_add_element(ctx, &args, arg, arg_length, 0, GRN_DB_TEXT); |
| 8396 | } |
| 8397 | const char *name; |
| 8398 | char name_buffer[GRN_TABLE_MAX_KEY_SIZE]; |
| 8399 | int name_length; |
| 8400 | if (code->value) { |
| 8401 | name_length = |
| 8402 | grn_obj_name(ctx, code->value, name_buffer, GRN_TABLE_MAX_KEY_SIZE); |
| 8403 | name = name_buffer; |
| 8404 | } else { |
| 8405 | name_length = grn_vector_pop_element(ctx, &args, &name, NULL, NULL); |
| 8406 | } |
| 8407 | GRN_BULK_REWIND(&substring_buffer); |
| 8408 | GRN_TEXT_PUT(ctx, &substring_buffer, name, name_length); |
| 8409 | GRN_TEXT_PUTC(ctx, &substring_buffer, '('); |
| 8410 | int32_t n_args = grn_vector_size(ctx, &args); |
| 8411 | for (i = 0; i < n_args; i++) { |
| 8412 | const char *arg; |
| 8413 | int arg_length; |
| 8414 | arg_length = grn_vector_pop_element(ctx, &args, &arg, NULL, NULL); |
| 8415 | if (i > 0) { |
| 8416 | GRN_TEXT_PUTS(ctx, &substring_buffer, ", "); |
| 8417 | } |
| 8418 | GRN_TEXT_PUT(ctx, &substring_buffer, arg, arg_length); |
| 8419 | } |
| 8420 | GRN_TEXT_PUTC(ctx, &substring_buffer, ')'); |
| 8421 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8422 | GRN_TEXT_LEN(&substring_buffer)); |
| 8423 | GRN_OBJ_FIN(ctx, &args); |
| 8424 | } |
| 8425 | code++; |
| 8426 | break; |
| 8427 | case GRN_OP_INTERN: |
| 8428 | case GRN_OP_TABLE_CREATE: |
| 8429 | case GRN_OP_EXPR_GET_VAR: |
| 8430 | case GRN_OP_ASSIGN: |
| 8431 | case GRN_OP_STAR_ASSIGN: |
| 8432 | case GRN_OP_SLASH_ASSIGN: |
| 8433 | case GRN_OP_MOD_ASSIGN: |
| 8434 | case GRN_OP_PLUS_ASSIGN: |
| 8435 | case GRN_OP_MINUS_ASSIGN: |
| 8436 | case GRN_OP_SHIFTL_ASSIGN: |
| 8437 | case GRN_OP_SHIFTR_ASSIGN: |
| 8438 | case GRN_OP_SHIFTRR_ASSIGN: |
| 8439 | case GRN_OP_AND_ASSIGN: |
| 8440 | case GRN_OP_OR_ASSIGN: |
| 8441 | case GRN_OP_XOR_ASSIGN: |
| 8442 | case GRN_OP_JUMP: |
| 8443 | case GRN_OP_CJUMP: |
| 8444 | GRN_LOG(ctx, |
| 8445 | GRN_LOG_DEBUG, |
| 8446 | "[expr][to-string] unsupported operator: %s", |
| 8447 | grn_operator_to_string(code->op)); |
| 8448 | goto exit; |
| 8449 | break; |
| 8450 | case GRN_OP_GET_VALUE: |
| 8451 | if (code->value) { |
| 8452 | PUSH_OBJECT(code->value); |
| 8453 | } else { |
| 8454 | GRN_LOG(ctx, |
| 8455 | GRN_LOG_DEBUG, |
| 8456 | "[expr][to-string] unsupported operator: %s", |
| 8457 | grn_operator_to_string(code->op)); |
| 8458 | goto exit; |
| 8459 | } |
| 8460 | code++; |
| 8461 | break; |
| 8462 | case GRN_OP_OBJ_SEARCH: |
| 8463 | case GRN_OP_TABLE_SELECT: |
| 8464 | case GRN_OP_TABLE_SORT: |
| 8465 | case GRN_OP_TABLE_GROUP: |
| 8466 | case GRN_OP_JSON_PUT: |
| 8467 | GRN_LOG(ctx, |
| 8468 | GRN_LOG_DEBUG, |
| 8469 | "[expr][to-string] unsupported operator: %s", |
| 8470 | grn_operator_to_string(code->op)); |
| 8471 | goto exit; |
| 8472 | break; |
| 8473 | case GRN_OP_AND: |
| 8474 | case GRN_OP_OR: |
| 8475 | case GRN_OP_AND_NOT: |
| 8476 | { |
| 8477 | const char *x_substring; |
| 8478 | int x_substring_length; |
| 8479 | const char *y_substring; |
| 8480 | int y_substring_length; |
| 8481 | POP(y_substring, y_substring_length); |
| 8482 | POP(x_substring, x_substring_length); |
| 8483 | GRN_BULK_REWIND(&substring_buffer); |
| 8484 | grn_text_printf(ctx, |
| 8485 | &substring_buffer, |
| 8486 | "(%.*s %s %.*s)", |
| 8487 | x_substring_length, |
| 8488 | x_substring, |
| 8489 | grn_operator_to_script_syntax(code->op), |
| 8490 | y_substring_length, |
| 8491 | y_substring); |
| 8492 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8493 | GRN_TEXT_LEN(&substring_buffer)); |
| 8494 | } |
| 8495 | code++; |
| 8496 | break; |
| 8497 | case GRN_OP_ADJUST: |
| 8498 | GRN_LOG(ctx, |
| 8499 | GRN_LOG_DEBUG, |
| 8500 | "[expr][to-string] unsupported operator: %s", |
| 8501 | grn_operator_to_string(code->op)); |
| 8502 | goto exit; |
| 8503 | break; |
| 8504 | case GRN_OP_MATCH: |
| 8505 | case GRN_OP_EQUAL: |
| 8506 | case GRN_OP_NOT_EQUAL: |
| 8507 | case GRN_OP_PREFIX: |
| 8508 | case GRN_OP_SUFFIX: |
| 8509 | case GRN_OP_LESS: |
| 8510 | case GRN_OP_GREATER: |
| 8511 | case GRN_OP_LESS_EQUAL: |
| 8512 | case GRN_OP_GREATER_EQUAL: |
| 8513 | { |
| 8514 | const char *x_substring; |
| 8515 | int x_substring_length; |
| 8516 | const char *y_substring; |
| 8517 | int y_substring_length; |
| 8518 | POP(y_substring, y_substring_length); |
| 8519 | POP(x_substring, x_substring_length); |
| 8520 | GRN_BULK_REWIND(&substring_buffer); |
| 8521 | grn_text_printf(ctx, |
| 8522 | &substring_buffer, |
| 8523 | "(%.*s %s %.*s)", |
| 8524 | x_substring_length, |
| 8525 | x_substring, |
| 8526 | grn_operator_to_script_syntax(code->op), |
| 8527 | y_substring_length, |
| 8528 | y_substring); |
| 8529 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8530 | GRN_TEXT_LEN(&substring_buffer)); |
| 8531 | } |
| 8532 | code++; |
| 8533 | break; |
| 8534 | case GRN_OP_GEO_DISTANCE1: |
| 8535 | case GRN_OP_GEO_DISTANCE2: |
| 8536 | case GRN_OP_GEO_DISTANCE3: |
| 8537 | case GRN_OP_GEO_DISTANCE4: |
| 8538 | case GRN_OP_GEO_WITHINP5: |
| 8539 | case GRN_OP_GEO_WITHINP6: |
| 8540 | case GRN_OP_GEO_WITHINP8: |
| 8541 | GRN_LOG(ctx, |
| 8542 | GRN_LOG_DEBUG, |
| 8543 | "[expr][to-string] unsupported operator: %s", |
| 8544 | grn_operator_to_string(code->op)); |
| 8545 | goto exit; |
| 8546 | break; |
| 8547 | case GRN_OP_PLUS: |
| 8548 | { |
| 8549 | const char *x_substring; |
| 8550 | int x_substring_length; |
| 8551 | const char *y_substring; |
| 8552 | int y_substring_length; |
| 8553 | POP(y_substring, y_substring_length); |
| 8554 | POP(x_substring, x_substring_length); |
| 8555 | GRN_BULK_REWIND(&substring_buffer); |
| 8556 | grn_text_printf(ctx, |
| 8557 | &substring_buffer, |
| 8558 | "(%.*s %s %.*s)", |
| 8559 | x_substring_length, |
| 8560 | x_substring, |
| 8561 | grn_operator_to_script_syntax(code->op), |
| 8562 | y_substring_length, |
| 8563 | y_substring); |
| 8564 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8565 | GRN_TEXT_LEN(&substring_buffer)); |
| 8566 | } |
| 8567 | code++; |
| 8568 | break; |
| 8569 | case GRN_OP_MINUS: |
| 8570 | if (code->nargs == 1) { |
| 8571 | const char *substring; |
| 8572 | int substring_length; |
| 8573 | POP(substring, substring_length); |
| 8574 | GRN_BULK_REWIND(&substring_buffer); |
| 8575 | grn_text_printf(ctx, |
| 8576 | &substring_buffer, |
| 8577 | "-%.*s", |
| 8578 | substring_length, |
| 8579 | substring); |
| 8580 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8581 | GRN_TEXT_LEN(&substring_buffer)); |
| 8582 | } else { |
| 8583 | const char *x_substring; |
| 8584 | int x_substring_length; |
| 8585 | const char *y_substring; |
| 8586 | int y_substring_length; |
| 8587 | POP(y_substring, y_substring_length); |
| 8588 | POP(x_substring, x_substring_length); |
| 8589 | GRN_BULK_REWIND(&substring_buffer); |
| 8590 | grn_text_printf(ctx, |
| 8591 | &substring_buffer, |
| 8592 | "(%.*s %s %.*s)", |
| 8593 | x_substring_length, |
| 8594 | x_substring, |
| 8595 | grn_operator_to_script_syntax(code->op), |
| 8596 | y_substring_length, |
| 8597 | y_substring); |
| 8598 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8599 | GRN_TEXT_LEN(&substring_buffer)); |
| 8600 | } |
| 8601 | code++; |
| 8602 | break; |
| 8603 | case GRN_OP_STAR: |
| 8604 | case GRN_OP_SLASH: |
| 8605 | case GRN_OP_MOD: |
| 8606 | case GRN_OP_BITWISE_NOT: |
| 8607 | case GRN_OP_BITWISE_OR: |
| 8608 | case GRN_OP_BITWISE_XOR: |
| 8609 | case GRN_OP_BITWISE_AND: |
| 8610 | case GRN_OP_SHIFTL: |
| 8611 | case GRN_OP_SHIFTR: |
| 8612 | case GRN_OP_SHIFTRR: |
| 8613 | { |
| 8614 | const char *x_substring; |
| 8615 | int x_substring_length; |
| 8616 | const char *y_substring; |
| 8617 | int y_substring_length; |
| 8618 | POP(y_substring, y_substring_length); |
| 8619 | POP(x_substring, x_substring_length); |
| 8620 | GRN_BULK_REWIND(&substring_buffer); |
| 8621 | grn_text_printf(ctx, |
| 8622 | &substring_buffer, |
| 8623 | "(%.*s %s %.*s)", |
| 8624 | x_substring_length, |
| 8625 | x_substring, |
| 8626 | grn_operator_to_script_syntax(code->op), |
| 8627 | y_substring_length, |
| 8628 | y_substring); |
| 8629 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8630 | GRN_TEXT_LEN(&substring_buffer)); |
| 8631 | } |
| 8632 | code++; |
| 8633 | break; |
| 8634 | case GRN_OP_INCR: |
| 8635 | case GRN_OP_DECR: |
| 8636 | { |
| 8637 | const char *substring; |
| 8638 | int substring_length; |
| 8639 | POP(substring, substring_length); |
| 8640 | GRN_BULK_REWIND(&substring_buffer); |
| 8641 | grn_text_printf(ctx, |
| 8642 | &substring_buffer, |
| 8643 | "%s%.*s", |
| 8644 | grn_operator_to_script_syntax(code->op), |
| 8645 | substring_length, |
| 8646 | substring); |
| 8647 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8648 | GRN_TEXT_LEN(&substring_buffer)); |
| 8649 | } |
| 8650 | code++; |
| 8651 | break; |
| 8652 | case GRN_OP_INCR_POST: |
| 8653 | case GRN_OP_DECR_POST: |
| 8654 | { |
| 8655 | const char *substring; |
| 8656 | int substring_length; |
| 8657 | POP(substring, substring_length); |
| 8658 | GRN_BULK_REWIND(&substring_buffer); |
| 8659 | grn_text_printf(ctx, |
| 8660 | &substring_buffer, |
| 8661 | "%.*s%s", |
| 8662 | substring_length, |
| 8663 | substring, |
| 8664 | grn_operator_to_script_syntax(code->op)); |
| 8665 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8666 | GRN_TEXT_LEN(&substring_buffer)); |
| 8667 | } |
| 8668 | code++; |
| 8669 | break; |
| 8670 | case GRN_OP_NOT: |
| 8671 | { |
| 8672 | const char *substring; |
| 8673 | int substring_length; |
| 8674 | POP(substring, substring_length); |
| 8675 | GRN_BULK_REWIND(&substring_buffer); |
| 8676 | grn_text_printf(ctx, |
| 8677 | &substring_buffer, |
| 8678 | "!%.*s", |
| 8679 | substring_length, |
| 8680 | substring); |
| 8681 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8682 | GRN_TEXT_LEN(&substring_buffer)); |
| 8683 | } |
| 8684 | code++; |
| 8685 | break; |
| 8686 | case GRN_OP_GET_MEMBER: |
| 8687 | { |
| 8688 | const char *receiver; |
| 8689 | int receiver_length; |
| 8690 | const char *index; |
| 8691 | int index_length; |
| 8692 | POP(receiver, receiver_length); |
| 8693 | POP(index, index_length); |
| 8694 | GRN_BULK_REWIND(&substring_buffer); |
| 8695 | grn_text_printf(ctx, |
| 8696 | &substring_buffer, |
| 8697 | "%.*s[%.*s]", |
| 8698 | receiver_length, |
| 8699 | receiver, |
| 8700 | index_length, |
| 8701 | index); |
| 8702 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8703 | GRN_TEXT_LEN(&substring_buffer)); |
| 8704 | } |
| 8705 | code++; |
| 8706 | goto exit; |
| 8707 | case GRN_OP_REGEXP: |
| 8708 | { |
| 8709 | const char *x_substring; |
| 8710 | int x_substring_length; |
| 8711 | const char *y_substring; |
| 8712 | int y_substring_length; |
| 8713 | POP(y_substring, y_substring_length); |
| 8714 | POP(x_substring, x_substring_length); |
| 8715 | GRN_BULK_REWIND(&substring_buffer); |
| 8716 | grn_text_printf(ctx, |
| 8717 | &substring_buffer, |
| 8718 | "(%.*s %s %.*s)", |
| 8719 | x_substring_length, |
| 8720 | x_substring, |
| 8721 | grn_operator_to_script_syntax(code->op), |
| 8722 | y_substring_length, |
| 8723 | y_substring); |
| 8724 | PUSH_SUBSTRING(GRN_TEXT_VALUE(&substring_buffer), |
| 8725 | GRN_TEXT_LEN(&substring_buffer)); |
| 8726 | } |
| 8727 | code++; |
| 8728 | break; |
| 8729 | default: |
| 8730 | GRN_LOG(ctx, |
| 8731 | GRN_LOG_DEBUG, |
| 8732 | "[expr][to-string] unsupported operator: %s", |
| 8733 | grn_operator_to_string(code->op)); |
| 8734 | goto exit; |
| 8735 | break; |
| 8736 | } |
| 8737 | } |
| 8738 | if (grn_vector_size(ctx, &substring_stack) > 0) { |
| 8739 | const char *substring; |
| 8740 | uint32_t substring_length; |
| 8741 | POP(substring, substring_length); |
| 8742 | GRN_TEXT_PUT(ctx, buffer, substring, substring_length); |
| 8743 | } |
| 8744 | |
| 8745 | #undef DROP |
| 8746 | #undef POP |
| 8747 | #undef PUSH |
| 8748 | |
| 8749 | exit: |
| 8750 | GRN_OBJ_FIN(ctx, &substring_stack); |
| 8751 | GRN_OBJ_FIN(ctx, &substring_buffer); |
| 8752 | GRN_OBJ_FIN(ctx, &push_buffer); |
| 8753 | return ctx->rc; |
| 8754 | } |
| 8755 | |
| 8756 | void |
| 8757 | grn_expr_get_range_info(grn_ctx *ctx, |
| 8758 | grn_obj *expr, |
| 8759 | grn_id *range_id, |
| 8760 | grn_obj_flags *range_flags) |
| 8761 | { |
| 8762 | grn_obj range_id_stack; |
| 8763 | GRN_UINT32_INIT(&range_id_stack, GRN_OBJ_VECTOR); |
| 8764 | grn_obj range_flags_stack; |
| 8765 | GRN_UINT16_INIT(&range_flags_stack, GRN_OBJ_VECTOR); |
| 8766 | grn_expr *e = (grn_expr *)expr; |
| 8767 | grn_expr_code *code = e->codes; |
| 8768 | grn_expr_code *code_end = e->codes + e->codes_curr; |
| 8769 | |
| 8770 | #define PUSH(range_id_, range_flags_) \ |
| 8771 | do { \ |
| 8772 | GRN_UINT32_PUT(ctx, &range_id_stack, range_id_); \ |
| 8773 | GRN_UINT16_PUT(ctx, &range_flags_stack, range_flags_); \ |
| 8774 | } while (false) |
| 8775 | |
| 8776 | #define POP(range_id_, range_flags_) \ |
| 8777 | do { \ |
| 8778 | GRN_UINT32_POP(&range_id_stack, range_id_); \ |
| 8779 | GRN_UINT16_POP(&range_flags_stack, range_flags_); \ |
| 8780 | } while (false) |
| 8781 | |
| 8782 | #define DROP() \ |
| 8783 | do { \ |
| 8784 | grn_id unused_range_id; \ |
| 8785 | grn_obj_flags unused_range_flags; \ |
| 8786 | POP(unused_range_id, unused_range_flags); \ |
| 8787 | } while (false) |
| 8788 | |
| 8789 | while (code < code_end) { |
| 8790 | switch (code->op) { |
| 8791 | case GRN_OP_NOP: |
| 8792 | code++; |
| 8793 | break; |
| 8794 | case GRN_OP_PUSH: |
| 8795 | { |
| 8796 | grn_id range_id = code->value->header.domain; |
| 8797 | grn_obj_flags range_flags_ = 0; |
| 8798 | if (grn_obj_is_vector(ctx, code->value) || |
| 8799 | grn_obj_is_uvector(ctx, code->value)) { |
| 8800 | range_flags_ = GRN_OBJ_VECTOR; |
| 8801 | } |
| 8802 | if (range_id == GRN_ID_NIL && grn_obj_is_vector(ctx, code->value) && |
| 8803 | grn_vector_size(ctx, code->value) > 0) { |
| 8804 | const char *content; |
| 8805 | grn_vector_get_element(ctx, |
| 8806 | code->value, |
| 8807 | 0, |
| 8808 | &content, |
| 8809 | NULL, |
| 8810 | &range_id); |
| 8811 | } |
| 8812 | PUSH(range_id, range_flags_); |
| 8813 | } |
| 8814 | code++; |
| 8815 | break; |
| 8816 | case GRN_OP_POP: |
| 8817 | DROP(); |
| 8818 | code++; |
| 8819 | break; |
| 8820 | case GRN_OP_GET_REF: |
| 8821 | { |
| 8822 | grn_obj *column = NULL; |
| 8823 | if (code->nargs == 1) { |
| 8824 | if (code->value) { |
| 8825 | column = code->value; |
| 8826 | } else { |
| 8827 | /* TODO: can't resolve column name */ |
| 8828 | DROP(); |
| 8829 | } |
| 8830 | } else { |
| 8831 | if (code->value) { |
| 8832 | column = code->value; |
| 8833 | DROP(); |
| 8834 | } else { |
| 8835 | /* TODO: can't resolve column name */ |
| 8836 | DROP(); |
| 8837 | DROP(); |
| 8838 | } |
| 8839 | } |
| 8840 | if (grn_obj_is_text_family_bulk(ctx, column)) { |
| 8841 | /* TODO: resolve column */ |
| 8842 | column = NULL; |
| 8843 | } |
| 8844 | if (column) { |
| 8845 | grn_id range_id_; |
| 8846 | grn_obj_flags range_flags_; |
| 8847 | grn_obj_get_range_info(ctx, column, &range_id_, &range_flags_); |
| 8848 | PUSH(range_id_, range_flags_); |
| 8849 | } else { |
| 8850 | GRN_LOG(ctx, |
| 8851 | GRN_LOG_DEBUG, |
| 8852 | "[expr][get-range-info] failed to resolve column"); |
| 8853 | goto exit; |
| 8854 | } |
| 8855 | code++; |
| 8856 | } |
| 8857 | break; |
| 8858 | case GRN_OP_CALL: |
| 8859 | { |
| 8860 | grn_obj *proc = NULL; |
| 8861 | if (code->value) { |
| 8862 | proc = code->value; |
| 8863 | } else { |
| 8864 | GRN_LOG(ctx, |
| 8865 | GRN_LOG_DEBUG, |
| 8866 | "[expr][get-range-info] unsupported operator: %s", |
| 8867 | grn_operator_to_string(code->op)); |
| 8868 | goto exit; |
| 8869 | } |
| 8870 | grn_id range_id_; |
| 8871 | grn_obj_flags range_flags_; |
| 8872 | grn_obj_get_range_info(ctx, proc, &range_id_, &range_flags_); |
| 8873 | PUSH(range_id_, range_flags_); |
| 8874 | } |
| 8875 | code++; |
| 8876 | break; |
| 8877 | case GRN_OP_INTERN: |
| 8878 | case GRN_OP_TABLE_CREATE: |
| 8879 | case GRN_OP_EXPR_GET_VAR: |
| 8880 | case GRN_OP_ASSIGN: |
| 8881 | case GRN_OP_STAR_ASSIGN: |
| 8882 | case GRN_OP_SLASH_ASSIGN: |
| 8883 | case GRN_OP_MOD_ASSIGN: |
| 8884 | case GRN_OP_PLUS_ASSIGN: |
| 8885 | case GRN_OP_MINUS_ASSIGN: |
| 8886 | case GRN_OP_SHIFTL_ASSIGN: |
| 8887 | case GRN_OP_SHIFTR_ASSIGN: |
| 8888 | case GRN_OP_SHIFTRR_ASSIGN: |
| 8889 | case GRN_OP_AND_ASSIGN: |
| 8890 | case GRN_OP_OR_ASSIGN: |
| 8891 | case GRN_OP_XOR_ASSIGN: |
| 8892 | case GRN_OP_JUMP: |
| 8893 | case GRN_OP_CJUMP: |
| 8894 | GRN_LOG(ctx, |
| 8895 | GRN_LOG_DEBUG, |
| 8896 | "[expr][get-range-info] unsupported operator: %s", |
| 8897 | grn_operator_to_string(code->op)); |
| 8898 | goto exit; |
| 8899 | break; |
| 8900 | case GRN_OP_GET_VALUE: |
| 8901 | if (code->value) { |
| 8902 | grn_id range_id_; |
| 8903 | grn_obj_flags range_flags_; |
| 8904 | grn_obj_get_range_info(ctx, code->value, &range_id_, &range_flags_); |
| 8905 | PUSH(range_id_, range_flags_); |
| 8906 | } else { |
| 8907 | GRN_LOG(ctx, |
| 8908 | GRN_LOG_DEBUG, |
| 8909 | "[expr][get-range-info] unsupported operator: %s", |
| 8910 | grn_operator_to_string(code->op)); |
| 8911 | goto exit; |
| 8912 | } |
| 8913 | code++; |
| 8914 | break; |
| 8915 | case GRN_OP_OBJ_SEARCH: |
| 8916 | case GRN_OP_TABLE_SELECT: |
| 8917 | case GRN_OP_TABLE_SORT: |
| 8918 | case GRN_OP_TABLE_GROUP: |
| 8919 | case GRN_OP_JSON_PUT: |
| 8920 | GRN_LOG(ctx, |
| 8921 | GRN_LOG_DEBUG, |
| 8922 | "[expr][get-range-info] unsupported operator: %s", |
| 8923 | grn_operator_to_string(code->op)); |
| 8924 | goto exit; |
| 8925 | break; |
| 8926 | case GRN_OP_AND: |
| 8927 | case GRN_OP_OR: |
| 8928 | { |
| 8929 | grn_id x_range_id; |
| 8930 | grn_obj_flags x_range_flags; |
| 8931 | grn_id y_range_id; |
| 8932 | grn_obj_flags y_range_flags; |
| 8933 | POP(y_range_id, y_range_flags); |
| 8934 | POP(x_range_id, x_range_flags); |
| 8935 | if (x_range_id == y_range_id) { |
| 8936 | PUSH(x_range_id, x_range_flags); |
| 8937 | } else if (grn_type_id_is_text_family(ctx, x_range_id) && |
| 8938 | grn_type_id_is_text_family(ctx, y_range_id)) { |
| 8939 | if (x_range_id >= y_range_id) { |
| 8940 | PUSH(x_range_id, x_range_flags); |
| 8941 | } else { |
| 8942 | PUSH(y_range_id, y_range_flags); |
| 8943 | } |
| 8944 | } else { |
| 8945 | grn_obj *x_range = grn_ctx_at(ctx, x_range_id); |
| 8946 | grn_obj *y_range = grn_ctx_at(ctx, y_range_id); |
| 8947 | GRN_DEFINE_NAME_CUSTOM(x_range, x_range_name); |
| 8948 | GRN_DEFINE_NAME_CUSTOM(y_range, y_range_name); |
| 8949 | GRN_LOG(ctx, |
| 8950 | GRN_LOG_DEBUG, |
| 8951 | "[expr][get-range-info] can't chose range: " |
| 8952 | "<%.*s> <%s> <%.*s>", |
| 8953 | x_range_name_size, |
| 8954 | x_range_name, |
| 8955 | grn_operator_to_string(code->op), |
| 8956 | y_range_name_size, |
| 8957 | y_range_name); |
| 8958 | if (x_range) { |
| 8959 | grn_obj_unref(ctx, x_range); |
| 8960 | } |
| 8961 | if (y_range) { |
| 8962 | grn_obj_unref(ctx, y_range); |
| 8963 | } |
| 8964 | goto exit; |
| 8965 | } |
| 8966 | } |
| 8967 | code++; |
| 8968 | break; |
| 8969 | case GRN_OP_AND_NOT: |
| 8970 | case GRN_OP_ADJUST: |
| 8971 | GRN_LOG(ctx, |
| 8972 | GRN_LOG_DEBUG, |
| 8973 | "[expr][get-range-info] unsupported operator: %s", |
| 8974 | grn_operator_to_string(code->op)); |
| 8975 | goto exit; |
| 8976 | break; |
| 8977 | case GRN_OP_MATCH: |
| 8978 | case GRN_OP_EQUAL: |
| 8979 | case GRN_OP_NOT_EQUAL: |
| 8980 | case GRN_OP_PREFIX: |
| 8981 | case GRN_OP_SUFFIX: |
| 8982 | case GRN_OP_LESS: |
| 8983 | case GRN_OP_GREATER: |
| 8984 | case GRN_OP_LESS_EQUAL: |
| 8985 | case GRN_OP_GREATER_EQUAL: |
| 8986 | DROP(); |
| 8987 | DROP(); |
| 8988 | PUSH(GRN_DB_BOOL, 0); |
| 8989 | code++; |
| 8990 | break; |
| 8991 | case GRN_OP_GEO_DISTANCE1: |
| 8992 | case GRN_OP_GEO_DISTANCE2: |
| 8993 | case GRN_OP_GEO_DISTANCE3: |
| 8994 | case GRN_OP_GEO_DISTANCE4: |
| 8995 | DROP(); |
| 8996 | DROP(); |
| 8997 | DROP(); |
| 8998 | DROP(); |
| 8999 | PUSH(GRN_DB_FLOAT, 0); |
| 9000 | code++; |
| 9001 | break; |
| 9002 | case GRN_OP_GEO_WITHINP5: |
| 9003 | case GRN_OP_GEO_WITHINP6: |
| 9004 | case GRN_OP_GEO_WITHINP8: |
| 9005 | GRN_LOG(ctx, |
| 9006 | GRN_LOG_DEBUG, |
| 9007 | "[expr][get-range-info] unsupported operator: %s", |
| 9008 | grn_operator_to_string(code->op)); |
| 9009 | goto exit; |
| 9010 | break; |
| 9011 | case GRN_OP_PLUS: |
| 9012 | { |
| 9013 | grn_id x_range_id; |
| 9014 | grn_obj_flags x_range_flags; |
| 9015 | grn_id y_range_id; |
| 9016 | grn_obj_flags y_range_flags; |
| 9017 | POP(y_range_id, y_range_flags); |
| 9018 | POP(x_range_id, x_range_flags); |
| 9019 | /* TODO: Use larger range */ |
| 9020 | PUSH(x_range_id, x_range_flags); |
| 9021 | } |
| 9022 | code++; |
| 9023 | break; |
| 9024 | case GRN_OP_MINUS: |
| 9025 | if (code->nargs == 1) { |
| 9026 | /* no range change */ |
| 9027 | } else { |
| 9028 | grn_id x_range_id; |
| 9029 | grn_obj_flags x_range_flags; |
| 9030 | grn_id y_range_id; |
| 9031 | grn_obj_flags y_range_flags; |
| 9032 | POP(y_range_id, y_range_flags); |
| 9033 | POP(x_range_id, x_range_flags); |
| 9034 | /* TODO: Use larger range */ |
| 9035 | PUSH(x_range_id, x_range_flags); |
| 9036 | } |
| 9037 | code++; |
| 9038 | break; |
| 9039 | case GRN_OP_STAR: |
| 9040 | case GRN_OP_SLASH: |
| 9041 | case GRN_OP_MOD: |
| 9042 | case GRN_OP_BITWISE_NOT: |
| 9043 | case GRN_OP_BITWISE_OR: |
| 9044 | case GRN_OP_BITWISE_XOR: |
| 9045 | case GRN_OP_BITWISE_AND: |
| 9046 | case GRN_OP_SHIFTL: |
| 9047 | case GRN_OP_SHIFTR: |
| 9048 | case GRN_OP_SHIFTRR: |
| 9049 | { |
| 9050 | grn_id x_range_id; |
| 9051 | grn_obj_flags x_range_flags; |
| 9052 | grn_id y_range_id; |
| 9053 | grn_obj_flags y_range_flags; |
| 9054 | POP(y_range_id, y_range_flags); |
| 9055 | POP(x_range_id, x_range_flags); |
| 9056 | /* TODO: Use larger range */ |
| 9057 | PUSH(x_range_id, x_range_flags); |
| 9058 | } |
| 9059 | code++; |
| 9060 | break; |
| 9061 | case GRN_OP_INCR: |
| 9062 | case GRN_OP_DECR: |
| 9063 | case GRN_OP_INCR_POST: |
| 9064 | case GRN_OP_DECR_POST: |
| 9065 | /* no range change */ |
| 9066 | code++; |
| 9067 | break; |
| 9068 | case GRN_OP_NOT: |
| 9069 | DROP(); |
| 9070 | PUSH(GRN_DB_BOOL, 0); |
| 9071 | code++; |
| 9072 | break; |
| 9073 | case GRN_OP_GET_MEMBER: |
| 9074 | { |
| 9075 | grn_id index_range_id; |
| 9076 | grn_obj_flags index_range_flags; |
| 9077 | POP(index_range_id, index_range_flags); |
| 9078 | grn_id column_range_id; |
| 9079 | grn_obj_flags column_range_flags; |
| 9080 | POP(column_range_id, column_range_flags); |
| 9081 | PUSH(column_range_id, 0); |
| 9082 | code++; |
| 9083 | } |
| 9084 | break; |
| 9085 | case GRN_OP_REGEXP: |
| 9086 | DROP(); |
| 9087 | DROP(); |
| 9088 | PUSH(GRN_DB_BOOL, 0); |
| 9089 | code++; |
| 9090 | break; |
| 9091 | default: |
| 9092 | GRN_LOG(ctx, |
| 9093 | GRN_LOG_DEBUG, |
| 9094 | "[expr][get-range-info] unsupported operator: %s", |
| 9095 | grn_operator_to_string(code->op)); |
| 9096 | goto exit; |
| 9097 | break; |
| 9098 | } |
| 9099 | } |
| 9100 | if (GRN_UINT32_VECTOR_SIZE(&range_id_stack) > 0) { |
| 9101 | grn_id range_id_; |
| 9102 | grn_obj_flags range_flags_; |
| 9103 | POP(range_id_, range_flags_); |
| 9104 | *range_id = range_id_; |
| 9105 | *range_flags = range_flags_; |
| 9106 | } |
| 9107 | |
| 9108 | #undef DROP |
| 9109 | #undef POP |
| 9110 | #undef PUSH |
| 9111 | |
| 9112 | exit: |
| 9113 | GRN_OBJ_FIN(ctx, &range_id_stack); |
| 9114 | GRN_OBJ_FIN(ctx, &range_flags_stack); |
| 9115 | return; |
| 9116 | } |
| 9117 | |
| 9118 | uint32_t |
| 9119 | grn_expr_get_n_logical_ops(grn_ctx *ctx, grn_obj *expr) |
| 9120 | { |
| 9121 | GRN_API_ENTER; |
| 9122 | uint32_t n = 0; |
| 9123 | grn_expr *e = (grn_expr *)expr; |
| 9124 | grn_expr_code *code; |
| 9125 | grn_expr_code *code_end = e->codes + e->codes_curr; |
| 9126 | for (code = e->codes; code < code_end; code++) { |
| 9127 | switch (code->op) { |
| 9128 | case GRN_OP_AND: |
| 9129 | case GRN_OP_OR: |
| 9130 | case GRN_OP_AND_NOT: |
| 9131 | case GRN_OP_ADJUST: |
| 9132 | n++; |
| 9133 | break; |
| 9134 | default: |
| 9135 | break; |
| 9136 | } |
| 9137 | } |
| 9138 | GRN_API_RETURN(n); |
| 9139 | } |
| 9140 | |
| 9141 | /* |
| 9142 | * In command version 1: |
| 9143 | * |
| 9144 | * * We accept only column name and accessor as a column. |
| 9145 | * |
| 9146 | * * We accept one or more spaces and commas (",,," is OK) as a |
| 9147 | * separator. |
| 9148 | */ |
| 9149 | bool |
| 9150 | grn_expr_is_v1_format(grn_ctx *ctx, |
| 9151 | const char *raw_text, |
| 9152 | ssize_t raw_text_len, |
| 9153 | grn_expr_v1_format_type type) |
| 9154 | { |
| 9155 | |
| 9156 | if (raw_text_len < 0) { |
| 9157 | raw_text_len = strlen(raw_text); |
| 9158 | } |
| 9159 | const char *current = raw_text; |
| 9160 | const char *end = current + raw_text_len; |
| 9161 | bool in_identifier = false; |
| 9162 | bool separator_is_comma = false; |
| 9163 | size_t n_identifiers = 0; |
| 9164 | while (current < end) { |
| 9165 | int char_length; |
| 9166 | |
| 9167 | char_length = grn_charlen(ctx, current, end); |
| 9168 | if (char_length != 1) { |
| 9169 | return false; |
| 9170 | } |
| 9171 | |
| 9172 | switch (current[0]) { |
| 9173 | case ' ': |
| 9174 | if (!separator_is_comma) { |
| 9175 | in_identifier = false; |
| 9176 | n_identifiers = 0; |
| 9177 | } else { |
| 9178 | if (in_identifier) { |
| 9179 | n_identifiers++; |
| 9180 | } |
| 9181 | } |
| 9182 | break; |
| 9183 | case ',': |
| 9184 | in_identifier = false; |
| 9185 | n_identifiers = 0; |
| 9186 | separator_is_comma = true; |
| 9187 | break; |
| 9188 | case '_': |
| 9189 | in_identifier = true; |
| 9190 | break; |
| 9191 | case '.': |
| 9192 | case '#': |
| 9193 | case '@': |
| 9194 | if (!in_identifier) { |
| 9195 | return false; |
| 9196 | } |
| 9197 | break; |
| 9198 | case '-': |
| 9199 | if (!in_identifier) { |
| 9200 | if (type == GRN_EXPR_V1_FORMAT_TYPE_SORT_KEYS) { |
| 9201 | /* "A -" isn't v1 format. */ |
| 9202 | if (current == end) { |
| 9203 | return false; |
| 9204 | } |
| 9205 | /* "-${NO_IDENTIFIER}" isn't v1 format. */ |
| 9206 | if (!(('a' <= current[1] && current[1] <= 'z') || |
| 9207 | ('Z' <= current[1] && current[1] <= 'Z') || |
| 9208 | current[1] == '_')) { |
| 9209 | return false; |
| 9210 | } |
| 9211 | } else { |
| 9212 | return false; |
| 9213 | } |
| 9214 | } else { |
| 9215 | if (n_identifiers >= 1) { |
| 9216 | return false; |
| 9217 | } |
| 9218 | } |
| 9219 | break; |
| 9220 | case '+': |
| 9221 | if (type == GRN_EXPR_V1_FORMAT_TYPE_SORT_KEYS) { |
| 9222 | /* "A +" isn't v1 format. */ |
| 9223 | if (current == end) { |
| 9224 | return false; |
| 9225 | } |
| 9226 | /* "+${NO_IDENTIFIER}" isn't v1 format. */ |
| 9227 | if (!(('a' <= current[1] && current[1] <= 'z') || |
| 9228 | ('Z' <= current[1] && current[1] <= 'Z') || current[1] == '_')) { |
| 9229 | return false; |
| 9230 | } |
| 9231 | } else { |
| 9232 | return false; |
| 9233 | } |
| 9234 | break; |
| 9235 | case '*': |
| 9236 | if (type == GRN_EXPR_V1_FORMAT_TYPE_OUTPUT_COLUMNS) { |
| 9237 | if (in_identifier) { |
| 9238 | return false; |
| 9239 | } |
| 9240 | } else { |
| 9241 | return false; |
| 9242 | } |
| 9243 | break; |
| 9244 | default: |
| 9245 | if ('a' <= current[0] && current[0] <= 'z') { |
| 9246 | in_identifier = true; |
| 9247 | break; |
| 9248 | } else if ('A' <= current[0] && current[0] <= 'Z') { |
| 9249 | in_identifier = true; |
| 9250 | break; |
| 9251 | } else if ('0' <= current[0] && current[0] <= '9') { |
| 9252 | in_identifier = true; |
| 9253 | break; |
| 9254 | } else { |
| 9255 | return false; |
| 9256 | } |
| 9257 | } |
| 9258 | |
| 9259 | current += char_length; |
| 9260 | } |
| 9261 | |
| 9262 | return true; |
| 9263 | } |