Debian Codegraph @

lib/expr_executor.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2010-2018 Brazil
3 Copyright (C) 2020-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"
21 #include "grn_bulk.h"
22 #include "grn_ctx_impl.h"
23 #include "grn_expr.h"
24 #include "grn_expr_code.h"
25 #include "grn_expr_executor.h"
26 #include "grn_geo.h"
27 #include "grn_normalizer.h"
28
29 #ifdef GRN_SUPPORT_REGEXP
30 # include "grn_onigmo.h"
31 #endif
32
33 #include <groonga.hpp>
34
35 static void
36 grn_expr_executor_init_general(grn_ctx *ctx, grn_expr_executor *executor)
37 {
38 }
39
40 #define EXPRVP(x) ((x)->header.impl_flags & GRN_OBJ_EXPRVALUE)
41
42 #define GEO_RESOLUTION 3600000
43 #define GEO_RADIOUS 6357303
44 #define GEO_BES_C1 6334834
45 #define GEO_BES_C2 6377397
46 #define GEO_BES_C3 0.006674
47 #define GEO_GRS_C1 6335439
48 #define GEO_GRS_C2 6378137
49 #define GEO_GRS_C3 0.006694
50
51 #define CHECK(expression) \
52 do { \
53 if (!(expression)) { \
54 return false; \
55 } \
56 } while (false)
57
58 namespace {
59 struct ExecuteData {
60 grn_expr *e;
61 grn_obj **s_;
62 grn_obj *s0;
63 grn_obj *s1;
64 grn_obj **sp;
65 grn_obj *vp;
66 grn_obj *res;
67 grn_obj *v0;
68 grn_expr_code *code;
69 grn_expr_code *ce;
70 };
71
72 template <typename BLOCK>
73 void
74 with_spsave(grn_ctx *ctx, ExecuteData &data, BLOCK block)
75 {
76 ctx->impl->stack_curr = data.sp - ctx->impl->stack;
77 data.e->values_curr = data.vp - data.e->values;
78 block();
79 data.vp = data.e->values + data.e->values_curr;
80 data.sp = ctx->impl->stack + ctx->impl->stack_curr;
81 data.s_ = ctx->impl->stack;
82 data.s0 = (data.sp > data.s_) ? data.sp[-1] : nullptr;
83 data.s1 = (data.sp > data.s_ + 1) ? data.sp[-2] : nullptr;
84 }
85
86 inline void
87 var_set_value(grn_ctx *ctx, grn_obj *var, grn_obj *value)
88 {
89 if (GRN_DB_OBJP(value)) {
90 var->header.type = GRN_PTR;
91 var->header.domain = DB_OBJ(value)->id;
92 GRN_PTR_SET(ctx, var, value);
93 } else {
94 var->header.type = value->header.type;
95 var->header.domain = value->header.domain;
96 GRN_TEXT_SET(ctx, var, GRN_TEXT_VALUE(value), GRN_TEXT_LEN(value));
97 }
98 }
99
100 inline bool
101 push1(grn_ctx *ctx, ExecuteData &data, grn_obj *v)
102 {
103 if (EXPRVP(v)) {
104 data.vp++;
105 if (data.vp - data.e->values > data.e->values_tail) {
106 data.e->values_tail = data.vp - data.e->values;
107 }
108 }
109 data.s1 = data.s0;
110 if (data.sp >= data.s_ + ctx->impl->stack_size) {
111 if (grn_ctx_expand_stack(ctx) != GRN_SUCCESS) {
112 return false;
113 }
114 data.sp += (ctx->impl->stack - data.s_);
115 data.s_ = ctx->impl->stack;
116 }
117 *data.sp = data.s0 = v;
118 data.sp++;
119 return true;
120 }
121
122 inline bool
123 pop1(grn_ctx *ctx, ExecuteData &data, grn_obj *&v)
124 {
125 if (EXPRVP(data.s0)) {
126 data.vp--;
127 }
128 v = data.s0;
129 data.s0 = data.s1;
130 data.sp--;
131 if (data.sp < data.s_) {
132 ERR(GRN_INVALID_ARGUMENT, "stack underflow");
133 return false;
134 }
135 data.s1 = (data.sp > data.s_ + 1) ? data.sp[-2] : nullptr;
136 return true;
137 }
138
139 inline bool
140 alloc1(grn_ctx *ctx, ExecuteData &data, grn_obj *&value)
141 {
142 data.s1 = data.s0;
143 if (data.sp >= data.s_ + ctx->impl->stack_size) {
144 if (grn_ctx_expand_stack(ctx) != GRN_SUCCESS) {
145 return false;
146 }
147 data.sp += (ctx->impl->stack - data.s_);
148 data.s_ = ctx->impl->stack;
149 }
150 *(data.sp) = data.s0 = value = data.vp++;
151 data.sp++;
152 if (data.vp - data.e->values > data.e->values_tail) {
153 data.e->values_tail = data.vp - data.e->values;
154 }
155 return true;
156 }
157
158 inline bool
159 pop1alloc1(grn_ctx *ctx, ExecuteData &data, grn_obj *&arg, grn_obj *&value)
160 {
161 arg = data.s0;
162 if (EXPRVP(data.s0)) {
163 value = data.s0;
164 } else {
165 if (data.sp < data.s_ + 1) {
166 ERR(GRN_INVALID_ARGUMENT, "stack underflow");
167 return false;
168 }
169 data.sp[-1] = data.s0 = value = data.vp++;
170 if (data.vp - data.e->values > data.e->values_tail) {
171 data.e->values_tail = data.vp - data.e->values;
172 }
173 data.s0->header.impl_flags |= GRN_OBJ_EXPRVALUE;
174 }
175 return true;
176 }
177
178 inline bool
179 pop2alloc1(grn_ctx *ctx,
180 ExecuteData &data,
181 grn_obj *&arg1,
182 grn_obj *&arg2,
183 grn_obj *&value)
184 {
185 if (EXPRVP(data.s0)) {
186 data.vp--;
187 }
188 if (EXPRVP(data.s1)) {
189 data.vp--;
190 }
191 arg2 = data.s0;
192 arg1 = data.s1;
193 data.sp--;
194 if (data.sp < data.s_ + 1) {
195 ERR(GRN_INVALID_ARGUMENT, "stack underflow");
196 return false;
197 }
198 data.s1 = (data.sp > data.s_ + 1) ? data.sp[-2] : NULL;
199 data.sp[-1] = data.s0 = value = data.vp++;
200 if (data.vp - data.e->values > data.e->values_tail) {
201 data.e->values_tail = data.vp - data.e->values;
202 }
203 data.s0->header.impl_flags |= GRN_OBJ_EXPRVALUE;
204 return true;
205 }
206
207 inline bool
208 text_arithmetic_binary_operation(
209 grn_ctx *ctx, grn_operator op, grn_obj *x, grn_obj *y, grn_obj *result)
210 {
211 grn_obj casted_value;
212 GRN_INT64_INIT(&casted_value, 0);
213 grn_obj_cast(ctx, x, &casted_value, false);
214 auto x_int64 = GRN_BULK_VSIZE(&casted_value) < sizeof(int64_t)
215 ? 0
216 : GRN_INT64_VALUE(&casted_value);
217 GRN_BULK_REWIND(&casted_value);
218 grn_obj_cast(ctx, y, &casted_value, false);
219 auto y_int64 = GRN_BULK_VSIZE(&casted_value) < sizeof(int64_t)
220 ? 0
221 : GRN_INT64_VALUE(&casted_value);
222 GRN_OBJ_FIN(ctx, &casted_value);
223
224 result->header.domain = GRN_DB_INT64;
225 switch (op) {
226 case GRN_OP_BITWISE_OR:
227 GRN_INT64_SET(ctx, result, x_int64 | y_int64);
228 return true;
229 case GRN_OP_BITWISE_XOR:
230 GRN_INT64_SET(ctx, result, x_int64 ^ y_int64);
231 return true;
232 case GRN_OP_BITWISE_AND:
233 GRN_INT64_SET(ctx, result, x_int64 & y_int64);
234 return true;
235 case GRN_OP_SHIFTL:
236 GRN_INT64_SET(ctx, result, x_int64 << y_int64);
237 return true;
238 case GRN_OP_SHIFTR:
239 GRN_INT64_SET(ctx, result, x_int64 >> y_int64);
240 return true;
241 case GRN_OP_SHIFTRR:
242 GRN_INT64_SET(ctx,
243 result,
244 static_cast<uint64_t>(x_int64) >>
245 static_cast<uint64_t>(y_int64));
246 return true;
247 default:
248 ERR(GRN_INVALID_ARGUMENT,
249 "[expr-executor] \"string\" %s \"string\" isn't supported",
250 grn_operator_to_string(op));
251 return false;
252 }
253 }
254
255 /*
256 * X / -1 as -X.
257 * Since UInt cannot express negative values, it is expressed by changing its
258 * type.
259 *
260 * - uint8_t -> int16_t
261 * - uint16_t -> int32_t
262 * - uint32_t -> int64_t
263 * - uint64_t -> int64_t (When X does not over INT64_MAX + 1)
264 * - uint64_t -> double (When X over INT64_MAX + 1)
265 */
266 template <typename RESULT_TYPE, typename X, typename Y>
267 std::enable_if_t<
268 !(std::is_same_v<X, uint8_t> || std::is_same_v<X, uint16_t> ||
269 std::is_same_v<X, uint32_t> || std::is_same_v<X, uint64_t>) &&
270 std::is_signed_v<Y> && std::is_integral_v<Y>,
271 bool>
272 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
273 X x,
274 Y y,
275 grn_obj *result)
276 {
277 if (y == -1) {
278 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(-x));
279 } else {
280 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
281 }
282 return true;
283 }
284
285 template <typename RESULT_TYPE, typename X, typename Y>
286 std::enable_if_t<(std::is_same_v<X, uint8_t>) && std::is_signed_v<Y> &&
287 std::is_integral_v<Y>,
288 bool>
289 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
290 X x,
291 Y y,
292 grn_obj *result)
293 {
294 if (y == -1) {
295 grn_obj_reinit(ctx, result, GRN_DB_INT16, 0);
296 grn::bulk::set<int16_t>(ctx, result, -static_cast<int16_t>(x));
297 } else if (y < -1) {
298 grn_obj_reinit(ctx, result, GRN_DB_INT16, 0);
299 grn::bulk::set<int16_t>(ctx, result, static_cast<int16_t>(x) / y);
300 } else {
301 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
302 }
303 return true;
304 }
305
306 template <typename RESULT_TYPE, typename X, typename Y>
307 std::enable_if_t<(std::is_same_v<X, uint16_t>) && std::is_signed_v<Y> &&
308 std::is_integral_v<Y>,
309 bool>
310 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
311 X x,
312 Y y,
313 grn_obj *result)
314 {
315 if (y == -1) {
316 grn_obj_reinit(ctx, result, GRN_DB_INT32, 0);
317 grn::bulk::set<int32_t>(ctx, result, -static_cast<int32_t>(x));
318 } else if (y < -1) {
319 grn_obj_reinit(ctx, result, GRN_DB_INT32, 0);
320 grn::bulk::set<int32_t>(ctx, result, static_cast<int32_t>(x) / y);
321 } else {
322 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
323 }
324 return true;
325 }
326
327 template <typename RESULT_TYPE, typename X, typename Y>
328 std::enable_if_t<(std::is_same_v<X, uint32_t>) && std::is_signed_v<Y> &&
329 std::is_integral_v<Y>,
330 bool>
331 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
332 X x,
333 Y y,
334 grn_obj *result)
335 {
336 if (y == -1) {
337 grn_obj_reinit(ctx, result, GRN_DB_INT64, 0);
338 grn::bulk::set<int64_t>(ctx, result, -static_cast<int64_t>(x));
339 } else if (y < -1) {
340 grn_obj_reinit(ctx, result, GRN_DB_INT64, 0);
341 grn::bulk::set<int64_t>(ctx, result, static_cast<int64_t>(x) / y);
342 } else {
343 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
344 }
345 return true;
346 }
347
348 template <typename RESULT_TYPE, typename X, typename Y>
349 std::enable_if_t<(std::is_same_v<X, uint64_t>) && std::is_signed_v<Y> &&
350 std::is_integral_v<Y>,
351 bool>
352 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
353 X x,
354 Y y,
355 grn_obj *result)
356 {
357 if (y == -1) {
358 /* int64_t max: 9223372036854775807
359 * int64_t min: -9223372036854775808
360 * uint64_t's 9223372036854775808 as int64_t's -9223372036854775808
361 */
362 auto threshold =
363 static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1;
364 if (x > threshold) {
365 grn_obj_reinit(ctx, result, GRN_DB_FLOAT, 0);
366 grn::bulk::set<double>(ctx, result, -static_cast<double>(x));
367 } else if (x == threshold) {
368 grn_obj_reinit(ctx, result, GRN_DB_INT64, 0);
369 grn::bulk::set<int64_t>(ctx,
370 result,
371 std::numeric_limits<int64_t>::min());
372 } else {
373 grn_obj_reinit(ctx, result, GRN_DB_INT64, 0);
374 grn::bulk::set<int64_t>(ctx, result, -static_cast<int64_t>(x));
375 }
376 } else if (y < -1) {
377 /* `y` is less than -2 because `std::is_integral_v`.
378 * Therefore, `x / y` is never smaller than the minimum value of int64_t.
379 */
380 grn_obj_reinit(ctx, result, GRN_DB_INT64, 0);
381 grn::bulk::set<int64_t>(ctx, result, static_cast<double>(x) / y);
382 } else {
383 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
384 }
385 return true;
386 }
387
388 template <typename RESULT_TYPE, typename X, typename Y>
389 std::enable_if_t<!std::is_signed_v<Y>, bool>
390 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
391 X x,
392 Y y,
393 grn_obj *result)
394 {
395 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
396 return true;
397 }
398
399 template <typename RESULT_TYPE, typename X, typename Y>
400 std::enable_if_t<std::is_floating_point_v<Y>, bool>
401 numeric_arithmetic_binary_operation_execute_slash(grn_ctx *ctx,
402 X x,
403 Y y,
404 grn_obj *result)
405 {
406 // y == -1
407 if (grn::numeric::is_zero(y + 1)) {
408 grn::bulk::set<RESULT_TYPE>(ctx, result, -static_cast<RESULT_TYPE>(x));
409 } else {
410 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x / y));
411 }
412 return true;
413 }
414
415 template <typename RESULT_TYPE, typename X, typename Y>
416 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
417 numeric_arithmetic_binary_operation_execute_mod(grn_ctx *ctx,
418 X x,
419 Y y,
420 grn_obj *result)
421 {
422 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x % y));
423 return true;
424 }
425
426 template <typename RESULT_TYPE, typename X, typename Y>
427 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
428 bool>
429 numeric_arithmetic_binary_operation_execute_mod(grn_ctx *ctx,
430 X x,
431 Y y,
432 grn_obj *result)
433 {
434 grn::bulk::set<RESULT_TYPE>(ctx,
435 result,
436 static_cast<RESULT_TYPE>(fmod(x, y)));
437 return true;
438 }
439
440 template <typename RESULT_TYPE, typename X, typename Y>
441 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
442 numeric_arithmetic_binary_operation_execute_shiftl(grn_ctx *ctx,
443 X x,
444 Y y,
445 grn_obj *result)
446 {
447 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x << y));
448 return true;
449 }
450
451 template <typename RESULT_TYPE, typename X, typename Y>
452 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
453 bool>
454 numeric_arithmetic_binary_operation_execute_shiftl(grn_ctx *ctx,
455 X x,
456 Y y,
457 grn_obj *result)
458 {
459 grn::bulk::set<RESULT_TYPE>(
460 ctx,
461 result,
462 static_cast<RESULT_TYPE>(static_cast<int64_t>(x)
463 << static_cast<int64_t>(y)));
464 return true;
465 }
466
467 template <typename RESULT_TYPE, typename X, typename Y>
468 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
469 numeric_arithmetic_binary_operation_execute_shiftr(grn_ctx *ctx,
470 X x,
471 Y y,
472 grn_obj *result)
473 {
474 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x >> y));
475 return true;
476 }
477
478 template <typename RESULT_TYPE, typename X, typename Y>
479 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
480 bool>
481 numeric_arithmetic_binary_operation_execute_shiftr(grn_ctx *ctx,
482 X x,
483 Y y,
484 grn_obj *result)
485 {
486 grn::bulk::set<RESULT_TYPE>(
487 ctx,
488 result,
489 static_cast<RESULT_TYPE>(static_cast<int64_t>(x) >>
490 static_cast<int64_t>(y)));
491 return true;
492 }
493
494 template <typename RESULT_TYPE, typename X, typename Y>
495 std::enable_if_t<(std::is_same_v<X, int8_t> || std::is_same_v<X, uint8_t>) &&
496 !std::is_floating_point_v<Y>,
497 bool>
498 numeric_arithmetic_binary_operation_execute_shiftrr(grn_ctx *ctx,
499 X x,
500 Y y,
501 grn_obj *result)
502 {
503 grn::bulk::set<RESULT_TYPE>(
504 ctx,
505 result,
506 static_cast<RESULT_TYPE>(static_cast<uint8_t>(x) >> y));
507 return true;
508 }
509
510 template <typename RESULT_TYPE, typename X, typename Y>
511 std::enable_if_t<(std::is_same_v<X, int16_t> ||
512 std::is_same_v<X, uint16_t>) &&
513 !std::is_floating_point_v<Y>,
514 bool>
515 numeric_arithmetic_binary_operation_execute_shiftrr(grn_ctx *ctx,
516 X x,
517 Y y,
518 grn_obj *result)
519 {
520 grn::bulk::set<RESULT_TYPE>(
521 ctx,
522 result,
523 static_cast<RESULT_TYPE>(static_cast<uint16_t>(x) >> y));
524 return true;
525 }
526
527 template <typename RESULT_TYPE, typename X, typename Y>
528 std::enable_if_t<(std::is_same_v<X, int32_t> ||
529 std::is_same_v<X, uint32_t>) &&
530 !std::is_floating_point_v<Y>,
531 bool>
532 numeric_arithmetic_binary_operation_execute_shiftrr(grn_ctx *ctx,
533 X x,
534 Y y,
535 grn_obj *result)
536 {
537 grn::bulk::set<RESULT_TYPE>(
538 ctx,
539 result,
540 static_cast<RESULT_TYPE>(static_cast<uint32_t>(x) >> y));
541 return true;
542 }
543
544 template <typename RESULT_TYPE, typename X, typename Y>
545 std::enable_if_t<(std::is_same_v<X, int64_t> ||
546 std::is_same_v<X, uint64_t>) &&
547 !std::is_floating_point_v<Y>,
548 bool>
549 numeric_arithmetic_binary_operation_execute_shiftrr(grn_ctx *ctx,
550 X x,
551 Y y,
552 grn_obj *result)
553 {
554 grn::bulk::set<RESULT_TYPE>(
555 ctx,
556 result,
557 static_cast<RESULT_TYPE>(static_cast<uint64_t>(x) >> y));
558 return true;
559 }
560
561 template <typename RESULT_TYPE, typename X, typename Y>
562 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
563 bool>
564 numeric_arithmetic_binary_operation_execute_shiftrr(grn_ctx *ctx,
565 X x,
566 Y y,
567 grn_obj *result)
568 {
569 grn::bulk::set<RESULT_TYPE>(
570 ctx,
571 result,
572 static_cast<RESULT_TYPE>(static_cast<int64_t>(x) >>
573 static_cast<int64_t>(y)));
574 return true;
575 }
576
577 template <typename RESULT_TYPE, typename X, typename Y>
578 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
579 numeric_arithmetic_binary_operation_execute_bitwise_or(grn_ctx *ctx,
580 X x,
581 Y y,
582 grn_obj *result)
583 {
584 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x | y));
585 return true;
586 }
587
588 template <typename RESULT_TYPE, typename X, typename Y>
589 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
590 bool>
591 numeric_arithmetic_binary_operation_execute_bitwise_or(grn_ctx *ctx,
592 X x,
593 Y y,
594 grn_obj *result)
595 {
596 grn::bulk::set<RESULT_TYPE>(
597 ctx,
598 result,
599 static_cast<RESULT_TYPE>(static_cast<int64_t>(x) |
600 static_cast<int64_t>(y)));
601 return true;
602 }
603
604 template <typename RESULT_TYPE, typename X, typename Y>
605 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
606 numeric_arithmetic_binary_operation_execute_bitwise_xor(grn_ctx *ctx,
607 X x,
608 Y y,
609 grn_obj *result)
610 {
611 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x ^ y));
612 return true;
613 }
614
615 template <typename RESULT_TYPE, typename X, typename Y>
616 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
617 bool>
618 numeric_arithmetic_binary_operation_execute_bitwise_xor(grn_ctx *ctx,
619 X x,
620 Y y,
621 grn_obj *result)
622 {
623 grn::bulk::set<RESULT_TYPE>(
624 ctx,
625 result,
626 static_cast<RESULT_TYPE>(static_cast<int64_t>(x) ^
627 static_cast<int64_t>(y)));
628 return true;
629 }
630
631 template <typename RESULT_TYPE, typename X, typename Y>
632 std::enable_if_t<std::is_integral_v<X> && std::is_integral_v<Y>, bool>
633 numeric_arithmetic_binary_operation_execute_bitwise_and(grn_ctx *ctx,
634 X x,
635 Y y,
636 grn_obj *result)
637 {
638 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x & y));
639 return true;
640 }
641
642 template <typename RESULT_TYPE, typename X, typename Y>
643 std::enable_if_t<std::is_floating_point_v<X> || std::is_floating_point_v<Y>,
644 bool>
645 numeric_arithmetic_binary_operation_execute_bitwise_and(grn_ctx *ctx,
646 X x,
647 Y y,
648 grn_obj *result)
649 {
650 grn::bulk::set<RESULT_TYPE>(
651 ctx,
652 result,
653 static_cast<RESULT_TYPE>(static_cast<int64_t>(x) &
654 static_cast<int64_t>(y)));
655 return true;
656 }
657
658 template <typename RESULT_TYPE, typename X, typename Y>
659 bool
660 numeric_arithmetic_binary_operation_execute(
661 grn_ctx *ctx, grn_operator op, X x, Y y, grn_obj *result)
662 {
663 switch (op) {
664 case GRN_OP_PLUS:
665 case GRN_OP_PLUS_ASSIGN:
666 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x + y));
667 return true;
668 case GRN_OP_MINUS:
669 case GRN_OP_MINUS_ASSIGN:
670 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x - y));
671 return true;
672 case GRN_OP_STAR:
673 case GRN_OP_STAR_ASSIGN:
674 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(x * y));
675 return true;
676 case GRN_OP_SLASH:
677 case GRN_OP_SLASH_ASSIGN:
678 if (grn::numeric::is_zero(y)) {
679 ERR(GRN_INVALID_ARGUMENT, "divisor should not be 0");
680 return false;
681 }
682 return numeric_arithmetic_binary_operation_execute_slash<RESULT_TYPE>(
683 ctx,
684 x,
685 y,
686 result);
687 return true;
688 case GRN_OP_MOD:
689 case GRN_OP_MOD_ASSIGN:
690 if (grn::numeric::is_zero(y)) {
691 ERR(GRN_INVALID_ARGUMENT, "divisor should not be 0");
692 return false;
693 }
694 return numeric_arithmetic_binary_operation_execute_mod<RESULT_TYPE>(
695 ctx,
696 x,
697 y,
698 result);
699 case GRN_OP_SHIFTL:
700 case GRN_OP_SHIFTL_ASSIGN:
701 return numeric_arithmetic_binary_operation_execute_shiftl<RESULT_TYPE>(
702 ctx,
703 x,
704 y,
705 result);
706 case GRN_OP_SHIFTR:
707 case GRN_OP_SHIFTR_ASSIGN:
708 return numeric_arithmetic_binary_operation_execute_shiftr<RESULT_TYPE>(
709 ctx,
710 x,
711 y,
712 result);
713 case GRN_OP_SHIFTRR:
714 case GRN_OP_SHIFTRR_ASSIGN:
715 return numeric_arithmetic_binary_operation_execute_shiftrr<RESULT_TYPE>(
716 ctx,
717 x,
718 y,
719 result);
720 case GRN_OP_BITWISE_OR:
721 case GRN_OP_OR_ASSIGN:
722 return numeric_arithmetic_binary_operation_execute_bitwise_or<
723 RESULT_TYPE>(ctx, x, y, result);
724 case GRN_OP_BITWISE_XOR:
725 case GRN_OP_XOR_ASSIGN:
726 return numeric_arithmetic_binary_operation_execute_bitwise_xor<
727 RESULT_TYPE>(ctx, x, y, result);
728 case GRN_OP_BITWISE_AND:
729 case GRN_OP_AND_ASSIGN:
730 return numeric_arithmetic_binary_operation_execute_bitwise_and<
731 RESULT_TYPE>(ctx, x, y, result);
732 default:
733 ERR(GRN_INVALID_ARGUMENT,
734 "[expr-executor] unsupported operator: %s",
735 grn_operator_to_script_syntax(op));
736 return false;
737 }
738 }
739
740 template <typename RESULT_TYPE, typename X>
741 bool
742 numeric_arithmetic_binary_operation_dispatch(grn_ctx *ctx,
743 grn_operator op,
744 X x_raw,
745 grn_obj *y,
746 grn_obj *res,
747 grn_id res_domain)
748 {
749 switch (y->header.domain) {
750 case GRN_DB_BOOL:
751 {
752 uint8_t y_raw = GRN_BOOL_VALUE(y) ? 1 : 0;
753 grn_obj_reinit(ctx, res, res_domain, 0);
754 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
755 op,
756 x_raw,
757 y_raw,
758 res);
759 }
760 case GRN_DB_INT8:
761 {
762 auto y_raw = GRN_INT8_VALUE(y);
763 grn_obj_reinit(ctx, res, res_domain, 0);
764 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
765 op,
766 x_raw,
767 y_raw,
768 res);
769 }
770 case GRN_DB_UINT8:
771 {
772 auto y_raw = GRN_UINT8_VALUE(y);
773 grn_obj_reinit(ctx, res, res_domain, 0);
774 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
775 op,
776 x_raw,
777 y_raw,
778 res);
779 }
780 case GRN_DB_INT16:
781 {
782 auto y_raw = GRN_INT16_VALUE(y);
783 grn_obj_reinit(ctx, res, res_domain, 0);
784 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
785 op,
786 x_raw,
787 y_raw,
788 res);
789 }
790 case GRN_DB_UINT16:
791 {
792 auto y_raw = GRN_UINT16_VALUE(y);
793 grn_obj_reinit(ctx, res, res_domain, 0);
794 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
795 op,
796 x_raw,
797 y_raw,
798 res);
799 }
800 case GRN_DB_INT32:
801 {
802 auto y_raw = GRN_INT32_VALUE(y);
803 grn_obj_reinit(ctx, res, res_domain, 0);
804 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
805 op,
806 x_raw,
807 y_raw,
808 res);
809 }
810 case GRN_DB_UINT32:
811 {
812 auto y_raw = GRN_UINT32_VALUE(y);
813 grn_obj_reinit(ctx, res, res_domain, 0);
814 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
815 op,
816 x_raw,
817 y_raw,
818 res);
819 }
820 case GRN_DB_TIME:
821 {
822 auto y_raw = GRN_TIME_VALUE(y);
823 grn_obj_reinit(ctx, res, res_domain, 0);
824 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
825 op,
826 x_raw,
827 y_raw,
828 res);
829 }
830 case GRN_DB_INT64:
831 {
832 auto y_raw = GRN_INT64_VALUE(y);
833 grn_obj_reinit(ctx, res, res_domain, 0);
834 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
835 op,
836 x_raw,
837 y_raw,
838 res);
839 }
840 case GRN_DB_UINT64:
841 {
842 auto y_raw = GRN_UINT64_VALUE(y);
843 grn_obj_reinit(ctx, res, res_domain, 0);
844 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(ctx,
845 op,
846 x_raw,
847 y_raw,
848 res);
849 }
850 case GRN_DB_FLOAT32:
851 {
852 auto y_raw = GRN_FLOAT32_VALUE(y);
853 grn_obj_reinit(ctx, res, GRN_DB_FLOAT32, 0);
854 return numeric_arithmetic_binary_operation_execute<float>(ctx,
855 op,
856 x_raw,
857 y_raw,
858 res);
859 }
860 case GRN_DB_FLOAT:
861 {
862 auto y_raw = GRN_FLOAT_VALUE(y);
863 grn_obj_reinit(ctx, res, GRN_DB_FLOAT, 0);
864 return numeric_arithmetic_binary_operation_execute<double>(ctx,
865 op,
866 x_raw,
867 y_raw,
868 res);
869 }
870 case GRN_DB_SHORT_TEXT:
871 case GRN_DB_TEXT:
872 case GRN_DB_LONG_TEXT:
873 grn_obj_reinit(ctx, res, res_domain, 0);
874 if (grn_obj_cast(ctx, y, res, false)) {
875 ERR(GRN_INVALID_ARGUMENT,
876 "not a numerical format: <%.*s>",
877 (int)GRN_TEXT_LEN(y),
878 GRN_TEXT_VALUE(y));
879 return false;
880 }
881 return numeric_arithmetic_binary_operation_execute<RESULT_TYPE>(
882 ctx,
883 op,
884 x_raw,
885 grn::bulk::get<RESULT_TYPE>(ctx, res, 0),
886 res);
887 default:
888 {
889 grn::TextBulk y_inspected(ctx);
890 grn_inspect(ctx, *y_inspected, y);
891 ERR(GRN_INVALID_ARGUMENT,
892 "[expr-executor] unsupported right type: %.*s(%s)",
893 static_cast<int>(GRN_TEXT_LEN(*y_inspected)),
894 GRN_TEXT_VALUE(*y_inspected),
895 grn_obj_type_to_string(y->header.type));
896 return false;
897 }
898 }
899 }
900
901 inline bool
902 text_binary_operation(
903 grn_ctx *ctx, grn_operator op, grn_obj *x, grn_obj *y, grn_obj *result)
904 {
905 switch (op) {
906 case GRN_OP_STAR_ASSIGN:
907 ERR(GRN_INVALID_ARGUMENT,
908 "[expr-executor] variable *= \"string\" isn't supported");
909 return false;
910 case GRN_OP_SLASH_ASSIGN:
911 ERR(GRN_INVALID_ARGUMENT,
912 "[expr-executor] variable /= \"string\" isn't supported");
913 return false;
914 case GRN_OP_MOD_ASSIGN:
915 ERR(GRN_INVALID_ARGUMENT,
916 "[expr-executor] variable %%= \"string\" isn't supported");
917 return false;
918 case GRN_OP_PLUS_ASSIGN:
919 ERR(GRN_INVALID_ARGUMENT,
920 "[expr-executor] variable += \"string\" isn't supported");
921 return false;
922 case GRN_OP_MINUS_ASSIGN:
923 ERR(GRN_INVALID_ARGUMENT,
924 "[expr-executor] variable -= \"string\" isn't supported");
925 return false;
926 case GRN_OP_SHIFTL_ASSIGN:
927 ERR(GRN_INVALID_ARGUMENT,
928 "[expr-executor] variable <<= \"string\" isn't supported");
929 return false;
930 case GRN_OP_SHIFTR_ASSIGN:
931 ERR(GRN_INVALID_ARGUMENT,
932 "[expr-executor] variable >>= \"string\" isn't supported");
933 return false;
934 case GRN_OP_SHIFTRR_ASSIGN:
935 ERR(GRN_INVALID_ARGUMENT,
936 "[expr-executor] variable >>>= \"string\" isn't supported");
937 return false;
938 case GRN_OP_AND_ASSIGN:
939 ERR(GRN_INVALID_ARGUMENT,
940 "[expr-executor] variable &= \"string\" isn't supported");
941 return false;
942 case GRN_OP_XOR_ASSIGN:
943 ERR(GRN_INVALID_ARGUMENT,
944 "[expr-executor] variable ^= \"string\" isn't supported");
945 return false;
946 case GRN_OP_OR_ASSIGN:
947 ERR(GRN_INVALID_ARGUMENT,
948 "[expr-executor] variable |= \"string\" isn't supported");
949 return false;
950 case GRN_OP_BITWISE_OR:
951 case GRN_OP_BITWISE_XOR:
952 case GRN_OP_BITWISE_AND:
953 case GRN_OP_SHIFTL:
954 case GRN_OP_SHIFTR:
955 case GRN_OP_SHIFTRR:
956 return text_arithmetic_binary_operation(ctx, op, x, y, result);
957 case GRN_OP_PLUS:
958 if (x == result) {
959 grn_obj_cast(ctx, y, result, false);
960 } else if (y == result) {
961 grn::TextBulk buffer(ctx);
962 GRN_TEXT_INIT(*buffer, 0);
963 grn_obj_cast(ctx, x, *buffer, false);
964 grn_obj_cast(ctx, y, *buffer, false);
965 GRN_BULK_REWIND(result);
966 grn_obj_reinit(ctx, result, GRN_DB_TEXT, 0);
967 grn_obj_cast(ctx, *buffer, result, false);
968 } else {
969 grn_obj_reinit(ctx, result, GRN_DB_TEXT, 0);
970 grn_obj_cast(ctx, x, result, false);
971 grn_obj_cast(ctx, y, result, false);
972 }
973 return true;
974 case GRN_OP_MINUS:
975 ERR(GRN_INVALID_ARGUMENT,
976 "[expr-executor] \"string\" - \"string\" isn't supported");
977 return false;
978 case GRN_OP_STAR:
979 ERR(GRN_INVALID_ARGUMENT,
980 "[expr-executor] \"string\" * \"string\" isn't supported");
981 return false;
982 case GRN_OP_SLASH:
983 ERR(GRN_INVALID_ARGUMENT,
984 "[expr-executor] \"string\" / \"string\" isn't supported");
985 return false;
986 case GRN_OP_MOD:
987 ERR(GRN_INVALID_ARGUMENT,
988 "[expr-executor] \"string\" %% \"string\" isn't supported");
989 return false;
990 default:
991 {
992 grn::TextBulk x_inspected(ctx);
993 grn_inspect(ctx, *x_inspected, x);
994 grn::TextBulk y_inspected(ctx);
995 grn_inspect(ctx, *y_inspected, y);
996 ERR(GRN_INVALID_ARGUMENT,
997 "[expr-executor] unsupported text operation: %.*s %s %.*s",
998 static_cast<int>(GRN_TEXT_LEN(*x_inspected)),
999 GRN_TEXT_VALUE(*x_inspected),
1000 grn_operator_to_string(op),
1001 static_cast<int>(GRN_TEXT_LEN(*y_inspected)),
1002 GRN_TEXT_VALUE(*y_inspected));
1003 return false;
1004 }
1005 }
1006 }
1007
1008 inline bool
1009 arithmetic_binary_operation_dispatch(grn_ctx *ctx,
1010 ExecuteData &data,
1011 grn_operator op,
1012 grn_obj *x,
1013 grn_obj *y,
1014 grn_obj *res)
1015 {
1016 switch (x->header.domain) {
1017 case GRN_DB_BOOL:
1018 {
1019 uint8_t x_ = GRN_BOOL_VALUE(x) ? 1 : 0;
1020 CHECK(
1021 numeric_arithmetic_binary_operation_dispatch<uint8_t>(ctx,
1022 op,
1023 x_,
1024 y,
1025 res,
1026 GRN_DB_UINT8));
1027 }
1028 break;
1029 case GRN_DB_INT8:
1030 {
1031 auto x_ = GRN_INT8_VALUE(x);
1032 CHECK(
1033 numeric_arithmetic_binary_operation_dispatch<int8_t>(ctx,
1034 op,
1035 x_,
1036 y,
1037 res,
1038 GRN_DB_INT8));
1039 }
1040 break;
1041 case GRN_DB_UINT8:
1042 {
1043 auto x_ = GRN_UINT8_VALUE(x);
1044 CHECK(
1045 numeric_arithmetic_binary_operation_dispatch<uint8_t>(ctx,
1046 op,
1047 x_,
1048 y,
1049 res,
1050 GRN_DB_UINT8));
1051 }
1052 break;
1053 case GRN_DB_INT16:
1054 {
1055 auto x_ = GRN_INT16_VALUE(x);
1056 CHECK(
1057 numeric_arithmetic_binary_operation_dispatch<int16_t>(ctx,
1058 op,
1059 x_,
1060 y,
1061 res,
1062 GRN_DB_INT16));
1063 }
1064 break;
1065 case GRN_DB_UINT16:
1066 {
1067 auto x_ = GRN_UINT16_VALUE(x);
1068 CHECK(numeric_arithmetic_binary_operation_dispatch<
1069 uint16_t>(ctx, op, x_, y, res, GRN_DB_UINT16));
1070 }
1071 break;
1072 case GRN_DB_INT32:
1073 {
1074 auto x_ = GRN_INT32_VALUE(x);
1075 CHECK(
1076 numeric_arithmetic_binary_operation_dispatch<int32_t>(ctx,
1077 op,
1078 x_,
1079 y,
1080 res,
1081 GRN_DB_INT32));
1082 }
1083 break;
1084 case GRN_DB_UINT32:
1085 {
1086 auto x_ = GRN_UINT32_VALUE(x);
1087 CHECK(numeric_arithmetic_binary_operation_dispatch<
1088 uint32_t>(ctx, op, x_, y, res, GRN_DB_UINT32));
1089 }
1090 break;
1091 case GRN_DB_INT64:
1092 {
1093 auto x_ = GRN_INT64_VALUE(x);
1094 CHECK(
1095 numeric_arithmetic_binary_operation_dispatch<int64_t>(ctx,
1096 op,
1097 x_,
1098 y,
1099 res,
1100 GRN_DB_INT64));
1101 }
1102 break;
1103 case GRN_DB_TIME:
1104 {
1105 auto x_ = GRN_TIME_VALUE(x);
1106 CHECK(
1107 numeric_arithmetic_binary_operation_dispatch<int64_t>(ctx,
1108 op,
1109 x_,
1110 y,
1111 res,
1112 GRN_DB_TIME));
1113 }
1114 break;
1115 case GRN_DB_UINT64:
1116 {
1117 auto x_ = GRN_UINT64_VALUE(x);
1118 CHECK(numeric_arithmetic_binary_operation_dispatch<
1119 uint64_t>(ctx, op, x_, y, res, GRN_DB_UINT64));
1120 }
1121 break;
1122 case GRN_DB_FLOAT32:
1123 {
1124 auto x_ = GRN_FLOAT32_VALUE(x);
1125 CHECK(
1126 numeric_arithmetic_binary_operation_dispatch<float>(ctx,
1127 op,
1128 x_,
1129 y,
1130 res,
1131 GRN_DB_FLOAT32));
1132 }
1133 break;
1134 case GRN_DB_FLOAT:
1135 {
1136 auto x_ = GRN_FLOAT_VALUE(x);
1137 CHECK(
1138 numeric_arithmetic_binary_operation_dispatch<double>(ctx,
1139 op,
1140 x_,
1141 y,
1142 res,
1143 GRN_DB_FLOAT));
1144 }
1145 break;
1146 case GRN_DB_SHORT_TEXT:
1147 case GRN_DB_TEXT:
1148 case GRN_DB_LONG_TEXT:
1149 CHECK(text_binary_operation(ctx, op, x, y, res));
1150 break;
1151 default:
1152 break;
1153 }
1154 data.code++;
1155 return true;
1156 }
1157
1158 inline bool
1159 arithmetic_binary_operation_dispatch(grn_ctx *ctx,
1160 ExecuteData &data,
1161 grn_operator op)
1162 {
1163 grn_obj *x = NULL;
1164 grn_obj *y = NULL;
1165
1166 CHECK(pop2alloc1(ctx, data, x, y, data.res));
1167 if (x->header.type == GRN_VECTOR || y->header.type == GRN_VECTOR) {
1168 grn_obj inspected_x;
1169 grn_obj inspected_y;
1170 GRN_TEXT_INIT(&inspected_x, 0);
1171 GRN_TEXT_INIT(&inspected_y, 0);
1172 grn_inspect(ctx, &inspected_x, x);
1173 grn_inspect(ctx, &inspected_y, y);
1174 ERR(GRN_INVALID_ARGUMENT,
1175 "<%s> doesn't support vector: <%.*s> %s <%.*s>",
1176 grn_operator_to_script_syntax(op),
1177 (int)GRN_TEXT_LEN(&inspected_x),
1178 GRN_TEXT_VALUE(&inspected_x),
1179 grn_operator_to_script_syntax(op),
1180 (int)GRN_TEXT_LEN(&inspected_y),
1181 GRN_TEXT_VALUE(&inspected_y));
1182 GRN_OBJ_FIN(ctx, &inspected_x);
1183 GRN_OBJ_FIN(ctx, &inspected_y);
1184 return false;
1185 }
1186 return arithmetic_binary_operation_dispatch(ctx, data, op, x, y, data.res);
1187 }
1188
1189 template <typename RESULT_TYPE, typename X>
1190 std::enable_if_t<std::is_integral_v<X>, bool>
1191 numeric_arithmetic_unary_operation_execute_bitwise_not(grn_ctx *ctx,
1192 X x,
1193 grn_obj *result)
1194 {
1195 grn::bulk::set<RESULT_TYPE>(ctx, result, static_cast<RESULT_TYPE>(~x));
1196 return true;
1197 }
1198
1199 template <typename RESULT_TYPE, typename X>
1200 std::enable_if_t<std::is_floating_point_v<X>, bool>
1201 numeric_arithmetic_unary_operation_execute_bitwise_not(grn_ctx *ctx,
1202 X x,
1203 grn_obj *result)
1204 {
1205 grn::bulk::set<RESULT_TYPE>(
1206 ctx,
1207 result,
1208 static_cast<RESULT_TYPE>(~static_cast<int64_t>(x)));
1209 return true;
1210 }
1211
1212 template <typename RESULT_TYPE, typename X>
1213 bool
1214 numeric_arithmetic_unary_operation_execute(grn_ctx *ctx,
1215 grn_operator op,
1216 X x_raw,
1217 grn_obj *result,
1218 grn_id result_domain)
1219 {
1220 switch (op) {
1221 case GRN_OP_MINUS:
1222 grn_obj_reinit(ctx, result, result_domain, 0);
1223 grn::bulk::set<RESULT_TYPE>(ctx,
1224 result,
1225 static_cast<RESULT_TYPE>(-x_raw));
1226 return true;
1227 case GRN_OP_BITWISE_NOT:
1228 grn_obj_reinit(ctx, result, result_domain, 0);
1229 return numeric_arithmetic_unary_operation_execute_bitwise_not<
1230 RESULT_TYPE>(ctx, x_raw, result);
1231 default:
1232 ERR(GRN_INVALID_ARGUMENT,
1233 "[expr-executor] unsupported unary operator: %s",
1234 grn_operator_to_script_syntax(op));
1235 return false;
1236 }
1237 }
1238
1239 inline bool
1240 arithmetic_unary_operation_dispatch(grn_ctx *ctx,
1241 ExecuteData &data,
1242 grn_operator op)
1243 {
1244 grn_obj *x = nullptr;
1245 CHECK(pop1alloc1(ctx, data, x, data.res));
1246 switch (x->header.domain) {
1247 case GRN_DB_INT8:
1248 {
1249 auto x_raw = GRN_INT8_VALUE(x);
1250 CHECK(numeric_arithmetic_unary_operation_execute<int8_t>(ctx,
1251 op,
1252 x_raw,
1253 data.res,
1254 GRN_DB_INT8));
1255 }
1256 break;
1257 case GRN_DB_UINT8:
1258 {
1259 int16_t x_raw = GRN_UINT8_VALUE(x);
1260 CHECK(
1261 numeric_arithmetic_unary_operation_execute<int16_t>(ctx,
1262 op,
1263 x_raw,
1264 data.res,
1265 GRN_DB_INT16));
1266 }
1267 break;
1268 case GRN_DB_INT16:
1269 {
1270 auto x_raw = GRN_INT16_VALUE(x);
1271 CHECK(
1272 numeric_arithmetic_unary_operation_execute<int16_t>(ctx,
1273 op,
1274 x_raw,
1275 data.res,
1276 GRN_DB_INT16));
1277 }
1278 break;
1279 case GRN_DB_UINT16:
1280 {
1281 int32_t x_raw = GRN_UINT16_VALUE(x);
1282 CHECK(
1283 numeric_arithmetic_unary_operation_execute<int32_t>(ctx,
1284 op,
1285 x_raw,
1286 data.res,
1287 GRN_DB_INT32));
1288 }
1289 break;
1290 case GRN_DB_INT32:
1291 {
1292 auto x_raw = GRN_INT32_VALUE(x);
1293 CHECK(
1294 numeric_arithmetic_unary_operation_execute<int32_t>(ctx,
1295 op,
1296 x_raw,
1297 data.res,
1298 GRN_DB_INT32));
1299 }
1300 break;
1301 case GRN_DB_UINT32:
1302 {
1303 int64_t x_raw = GRN_UINT32_VALUE(x);
1304 CHECK(
1305 numeric_arithmetic_unary_operation_execute<int64_t>(ctx,
1306 op,
1307 x_raw,
1308 data.res,
1309 GRN_DB_INT64));
1310 }
1311 break;
1312 case GRN_DB_INT64:
1313 {
1314 auto x_raw = GRN_INT64_VALUE(x);
1315 CHECK(
1316 numeric_arithmetic_unary_operation_execute<int64_t>(ctx,
1317 op,
1318 x_raw,
1319 data.res,
1320 GRN_DB_INT64));
1321 }
1322 break;
1323 case GRN_DB_TIME:
1324 {
1325 auto x_raw = GRN_TIME_VALUE(x);
1326 CHECK(numeric_arithmetic_unary_operation_execute<int64_t>(ctx,
1327 op,
1328 x_raw,
1329 data.res,
1330 GRN_DB_TIME));
1331 }
1332 break;
1333 case GRN_DB_UINT64:
1334 {
1335 auto x_raw = GRN_UINT64_VALUE(x);
1336 if (x_raw >
1337 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
1338 ERR(GRN_INVALID_ARGUMENT,
1339 "too large UInt64 value to inverse sign: "
1340 "<%" GRN_FMT_INT64U ">",
1341 x_raw);
1342 return false;
1343 } else {
1344 int64_t signed_x_raw;
1345 signed_x_raw = x_raw;
1346 CHECK(
1347 numeric_arithmetic_unary_operation_execute<int64_t>(ctx,
1348 op,
1349 signed_x_raw,
1350 data.res,
1351 GRN_DB_INT64));
1352 }
1353 }
1354 break;
1355 case GRN_DB_FLOAT32:
1356 {
1357 auto x_raw = GRN_FLOAT32_VALUE(x);
1358 CHECK(
1359 numeric_arithmetic_unary_operation_execute<float>(ctx,
1360 op,
1361 x_raw,
1362 data.res,
1363 GRN_DB_FLOAT32));
1364 }
1365 break;
1366 case GRN_DB_FLOAT:
1367 {
1368 auto x_raw = GRN_FLOAT_VALUE(x);
1369 CHECK(numeric_arithmetic_unary_operation_execute<double>(ctx,
1370 op,
1371 x_raw,
1372 data.res,
1373 GRN_DB_FLOAT));
1374 }
1375 break;
1376 case GRN_DB_SHORT_TEXT:
1377 case GRN_DB_TEXT:
1378 case GRN_DB_LONG_TEXT:
1379 {
1380 grn_obj x_casted;
1381 GRN_INT64_INIT(&x_casted, 0);
1382 grn_obj_cast(ctx, x, &x_casted, false);
1383 auto x_casted_raw = GRN_INT64_VALUE(&x_casted);
1384 GRN_OBJ_FIN(ctx, &x_casted);
1385 CHECK(
1386 numeric_arithmetic_unary_operation_execute<int64_t>(ctx,
1387 op,
1388 x_casted_raw,
1389 data.res,
1390 GRN_DB_INT64));
1391 break;
1392 }
1393 default:
1394 {
1395 grn::TextBulk x_inspected(ctx);
1396 grn_inspect(ctx, *x_inspected, x);
1397 ERR(GRN_INVALID_ARGUMENT,
1398 "[expr-executor] unsupported unary operation target type: %.*s(%s)",
1399 static_cast<int>(GRN_TEXT_LEN(*x_inspected)),
1400 GRN_TEXT_VALUE(*x_inspected),
1401 grn_obj_type_to_string(x->header.type));
1402 return false;
1403 }
1404 }
1405 data.code++;
1406 return true;
1407 }
1408
1409 inline bool
1410 unary_operation_and_assign_dispatch(grn_ctx *ctx,
1411 ExecuteData &data,
1412 grn_operator op)
1413 {
1414 grn_obj *variable = nullptr;
1415 CHECK(pop1alloc1(ctx, data, variable, data.res));
1416 if (variable->header.type != GRN_PTR) {
1417 ERR(GRN_INVALID_ARGUMENT,
1418 "invalid variable type: %s(0x%0x)",
1419 grn_obj_type_to_string(variable->header.type),
1420 variable->header.type);
1421 return false;
1422 }
1423 if (GRN_BULK_VSIZE(variable) != (sizeof(grn_obj *) + sizeof(grn_id))) {
1424 ERR(GRN_INVALID_ARGUMENT,
1425 "invalid variable size: "
1426 "expected: %" GRN_FMT_SIZE "actual: %" GRN_FMT_SIZE,
1427 (sizeof(grn_obj *) + sizeof(grn_id)),
1428 GRN_BULK_VSIZE(variable));
1429 return false;
1430 }
1431 auto column = GRN_PTR_VALUE(variable);
1432 auto rid =
1433 *reinterpret_cast<grn_id *>(GRN_BULK_HEAD(variable) + sizeof(grn_obj *));
1434 data.res->header.type = GRN_VOID;
1435 data.res->header.domain = DB_OBJ(column)->range;
1436 grn_obj value;
1437 int32_t delta = 1;
1438 int set_flags = (op == GRN_OP_INCR || op == GRN_OP_INCR_POST)
1439 ? GRN_OBJ_INCR
1440 : GRN_OBJ_DECR;
1441 switch (DB_OBJ(column)->range) {
1442 case GRN_DB_INT8:
1443 GRN_INT8_INIT(&value, 0);
1444 GRN_INT8_SET(ctx, &value, delta);
1445 break;
1446 case GRN_DB_UINT8:
1447 GRN_UINT8_INIT(&value, 0);
1448 GRN_UINT8_SET(ctx, &value, delta);
1449 break;
1450 case GRN_DB_INT16:
1451 GRN_INT16_INIT(&value, 0);
1452 GRN_INT16_SET(ctx, &value, delta);
1453 break;
1454 case GRN_DB_UINT16:
1455 GRN_UINT16_INIT(&value, 0);
1456 GRN_UINT16_SET(ctx, &value, delta);
1457 break;
1458 case GRN_DB_INT32:
1459 GRN_INT32_INIT(&value, 0);
1460 GRN_INT32_SET(ctx, &value, delta);
1461 break;
1462 case GRN_DB_UINT32:
1463 GRN_UINT32_INIT(&value, 0);
1464 GRN_UINT32_SET(ctx, &value, delta);
1465 break;
1466 case GRN_DB_INT64:
1467 GRN_INT64_INIT(&value, 0);
1468 GRN_INT64_SET(ctx, &value, delta);
1469 break;
1470 case GRN_DB_UINT64:
1471 GRN_UINT64_INIT(&value, 0);
1472 GRN_UINT64_SET(ctx, &value, delta);
1473 break;
1474 case GRN_DB_FLOAT32:
1475 GRN_FLOAT32_INIT(&value, 0);
1476 GRN_FLOAT32_SET(ctx, &value, delta);
1477 break;
1478 case GRN_DB_FLOAT:
1479 GRN_FLOAT_INIT(&value, 0);
1480 GRN_FLOAT_SET(ctx, &value, delta);
1481 break;
1482 case GRN_DB_TIME:
1483 GRN_TIME_INIT(&value, 0);
1484 GRN_TIME_SET(ctx, &value, GRN_TIME_PACK(delta, 0));
1485 break;
1486 default:
1487 ERR(GRN_INVALID_ARGUMENT,
1488 "invalid %s target type: %d "
1489 "(FIXME: type name is needed)",
1490 grn_operator_to_script_syntax(op),
1491 DB_OBJ(column)->range);
1492 return false;
1493 }
1494 if (op == GRN_OP_INCR || op == GRN_OP_DECR) {
1495 grn_obj_set_value(ctx, column, rid, &value, set_flags);
1496 grn_obj_get_value(ctx, column, rid, data.res);
1497 } else {
1498 grn_obj_get_value(ctx, column, rid, data.res);
1499 grn_obj_set_value(ctx, column, rid, &value, set_flags);
1500 }
1501 data.code++;
1502 return true;
1503 }
1504
1505 inline bool
1506 operation_and_assign_dispatch(grn_ctx *ctx,
1507 ExecuteData &data,
1508 grn_operator op)
1509 {
1510 grn_obj *value = nullptr;
1511 grn_obj *variable = nullptr;
1512 grn_obj *result = nullptr;
1513 if (data.code->value) {
1514 value = data.code->value;
1515 CHECK(pop1alloc1(ctx, data, variable, result));
1516 } else {
1517 CHECK(pop2alloc1(ctx, data, variable, value, result));
1518 }
1519 if (!(variable->header.type == GRN_PTR &&
1520 GRN_BULK_VSIZE(variable) == (sizeof(grn_obj *) + sizeof(grn_id)))) {
1521 ERR(GRN_INVALID_ARGUMENT, "left hand expression isn't column.");
1522 CHECK(pop1(ctx, data, result));
1523 }
1524
1525 auto column = GRN_PTR_VALUE(variable);
1526 auto rid =
1527 *reinterpret_cast<grn_id *>(GRN_BULK_HEAD(variable) + sizeof(grn_obj *));
1528 value = GRN_OBJ_RESOLVE(ctx, value);
1529 auto domain = grn_obj_get_range(ctx, column);
1530
1531 grn_obj variable_value;
1532 GRN_OBJ_INIT(&variable_value, GRN_BULK, 0, domain);
1533 grn_obj_get_value(ctx, column, rid, &variable_value);
1534
1535 grn_obj casted_value;
1536 GRN_OBJ_INIT(&casted_value, GRN_BULK, 0, domain);
1537 if (grn_obj_cast(ctx, value, &casted_value, false) != GRN_SUCCESS) {
1538 ERR(GRN_INVALID_ARGUMENT, "invalid value: string");
1539 GRN_OBJ_FIN(ctx, &variable_value);
1540 GRN_OBJ_FIN(ctx, &casted_value);
1541 CHECK(pop1(ctx, data, result));
1542 return false;
1543 }
1544 auto success = arithmetic_binary_operation_dispatch(ctx,
1545 data,
1546 op,
1547 &variable_value,
1548 &casted_value,
1549 result);
1550 GRN_OBJ_FIN(ctx, &variable_value);
1551 GRN_OBJ_FIN(ctx, &casted_value);
1552 if (!success) {
1553 return false;
1554 }
1555 grn_obj_set_value(ctx, column, rid, result, GRN_OBJ_SET);
1556 return true;
1557 }
1558 }; // namespace
1559
1560 static inline void
1561 grn_expr_exec_get_member_vector(grn_ctx *ctx,
1562 grn_obj *expr,
1563 grn_obj *column_and_record_id,
1564 grn_obj *index,
1565 grn_obj *result)
1566 {
1567 grn_obj *column;
1568 grn_id record_id;
1569 grn_obj values;
1570 uint32_t i;
1571
1572 column = GRN_PTR_VALUE(column_and_record_id);
1573 record_id = *((grn_id *)(&(GRN_PTR_VALUE_AT(column_and_record_id, 1))));
1574 GRN_TEXT_INIT(&values, 0);
1575 grn_obj_get_value(ctx, column, record_id, &values);
1576
1577 i = GRN_UINT32_VALUE(index);
1578 if (values.header.type == GRN_UVECTOR) {
1579 uint32_t n_elements = 0;
1580 grn_obj *range;
1581 grn_id range_id = DB_OBJ(column)->range;
1582
1583 grn_obj_reinit(ctx, result, range_id, 0);
1584 range = grn_ctx_at(ctx, range_id);
1585 if (range) {
1586 switch (range->header.type) {
1587 case GRN_TYPE:
1588 n_elements = GRN_BULK_VSIZE(&values) / grn_type_size(ctx, range);
1589 break;
1590 case GRN_TABLE_HASH_KEY:
1591 case GRN_TABLE_PAT_KEY:
1592 case GRN_TABLE_DAT_KEY:
1593 case GRN_TABLE_NO_KEY:
1594 n_elements = GRN_BULK_VSIZE(&values) / sizeof(grn_id);
1595 break;
1596 }
1597 }
1598 if (n_elements > i) {
1599 #define GET_UVECTOR_ELEMENT_AS(type) \
1600 do { \
1601 GRN_##type##_SET(ctx, result, GRN_##type##_VALUE_AT(&values, i)); \
1602 } while (false)
1603 switch (values.header.domain) {
1604 case GRN_DB_BOOL:
1605 GET_UVECTOR_ELEMENT_AS(BOOL);
1606 break;
1607 case GRN_DB_INT8:
1608 GET_UVECTOR_ELEMENT_AS(INT8);
1609 break;
1610 case GRN_DB_UINT8:
1611 GET_UVECTOR_ELEMENT_AS(UINT8);
1612 break;
1613 case GRN_DB_INT16:
1614 GET_UVECTOR_ELEMENT_AS(INT16);
1615 break;
1616 case GRN_DB_UINT16:
1617 GET_UVECTOR_ELEMENT_AS(UINT16);
1618 break;
1619 case GRN_DB_INT32:
1620 GET_UVECTOR_ELEMENT_AS(INT32);
1621 break;
1622 case GRN_DB_UINT32:
1623 GET_UVECTOR_ELEMENT_AS(UINT32);
1624 break;
1625 case GRN_DB_INT64:
1626 GET_UVECTOR_ELEMENT_AS(INT64);
1627 break;
1628 case GRN_DB_UINT64:
1629 GET_UVECTOR_ELEMENT_AS(UINT64);
1630 break;
1631 case GRN_DB_FLOAT32:
1632 GET_UVECTOR_ELEMENT_AS(FLOAT32);
1633 break;
1634 case GRN_DB_FLOAT:
1635 GET_UVECTOR_ELEMENT_AS(FLOAT);
1636 break;
1637 case GRN_DB_TIME:
1638 GET_UVECTOR_ELEMENT_AS(TIME);
1639 break;
1640 default:
1641 GET_UVECTOR_ELEMENT_AS(RECORD);
1642 break;
1643 }
1644 #undef GET_UVECTOR_ELEMENT_AS
1645 }
1646 } else {
1647 if (values.u.v.n_sections > i) {
1648 const char *content;
1649 unsigned int content_length;
1650 grn_id domain;
1651
1652 content_length =
1653 grn_vector_get_element(ctx, &values, i, &content, NULL, &domain);
1654 grn_obj_reinit(ctx, result, domain, 0);
1655 grn_bulk_write(ctx, result, content, content_length);
1656 }
1657 }
1658
1659 GRN_OBJ_FIN(ctx, &values);
1660 }
1661
1662 static inline void
1663 grn_expr_exec_get_member_table(
1664 grn_ctx *ctx, grn_obj *expr, grn_obj *table, grn_obj *key, grn_obj *result)
1665 {
1666 grn_id id;
1667
1668 if (table->header.domain == key->header.domain) {
1669 id = grn_table_get(ctx, table, GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));
1670 } else {
1671 grn_obj casted_key;
1672 GRN_OBJ_INIT(&casted_key, GRN_BULK, 0, table->header.domain);
1673 if (grn_obj_cast(ctx, key, &casted_key, false) == GRN_SUCCESS) {
1674 id = grn_table_get(ctx,
1675 table,
1676 GRN_BULK_HEAD(&casted_key),
1677 GRN_BULK_VSIZE(&casted_key));
1678 } else {
1679 id = GRN_ID_NIL;
1680 }
1681 GRN_OBJ_FIN(ctx, &casted_key);
1682 }
1683
1684 grn_obj_reinit(ctx, result, DB_OBJ(table)->id, 0);
1685 GRN_RECORD_SET(ctx, result, id);
1686 }
1687
1688 static bool
1689 expr_exec_internal(grn_ctx *ctx, grn_obj *expr)
1690 {
1691 uint32_t stack_curr = ctx->impl->stack_curr;
1692 ExecuteData data;
1693 data.e = reinterpret_cast<grn_expr *>(expr);
1694 data.s_ = ctx->impl->stack;
1695 data.s0 = nullptr;
1696 data.s1 = nullptr;
1697 data.sp = nullptr;
1698 data.vp = data.e->values;
1699 data.res = nullptr;
1700 data.v0 = grn_expr_get_var_by_offset(ctx, expr, 0);
1701 data.code = data.e->codes;
1702 data.ce = &(data.e->codes[data.e->codes_curr]);
1703 data.sp = data.s_ + stack_curr;
1704 while (data.code < data.ce) {
1705 switch (data.code->op) {
1706 case GRN_OP_NOP:
1707 data.code++;
1708 break;
1709 case GRN_OP_PUSH:
1710 CHECK(push1(ctx, data, data.code->value));
1711 data.code++;
1712 break;
1713 case GRN_OP_POP:
1714 {
1715 grn_obj *obj = NULL;
1716 CHECK(pop1(ctx, data, obj));
1717 data.code++;
1718 }
1719 break;
1720 case GRN_OP_GET_REF:
1721 {
1722 grn_obj *col = NULL;
1723 grn_obj *rec = NULL;
1724 if (data.code->nargs == 1) {
1725 rec = data.v0;
1726 if (data.code->value) {
1727 col = data.code->value;
1728 CHECK(alloc1(ctx, data, data.res));
1729 } else {
1730 CHECK(pop1alloc1(ctx, data, col, data.res));
1731 }
1732 } else {
1733 if (data.code->value) {
1734 col = data.code->value;
1735 CHECK(pop1alloc1(ctx, data, rec, data.res));
1736 } else {
1737 CHECK(pop2alloc1(ctx, data, rec, col, data.res));
1738 }
1739 }
1740 if (col->header.type == GRN_BULK) {
1741 grn_obj *table = grn_ctx_at(ctx, GRN_OBJ_GET_DOMAIN(rec));
1742 col =
1743 grn_obj_column(ctx, table, GRN_BULK_HEAD(col), GRN_BULK_VSIZE(col));
1744 if (col) {
1745 grn_expr_take_obj(ctx, expr, col);
1746 }
1747 }
1748 if (col) {
1749 data.res->header.type = GRN_PTR;
1750 data.res->header.domain = GRN_ID_NIL;
1751 GRN_PTR_SET(ctx, data.res, col);
1752 GRN_UINT32_PUT(ctx, data.res, GRN_RECORD_VALUE(rec));
1753 } else {
1754 ERR(GRN_INVALID_ARGUMENT, "col resolve failed");
1755 return false;
1756 }
1757 data.code++;
1758 }
1759 break;
1760 case GRN_OP_CALL:
1761 {
1762 grn_obj *proc = NULL;
1763 if (data.code->value) {
1764 if (data.sp < data.s_ + data.code->nargs - 1) {
1765 ERR(GRN_INVALID_ARGUMENT, "stack error");
1766 return false;
1767 }
1768 proc = data.code->value;
1769 with_spsave(ctx, data, [&]() {
1770 grn_proc_call(ctx, proc, data.code->nargs - 1, expr);
1771 });
1772 } else {
1773 int offset = data.code->nargs;
1774 if (data.sp < data.s_ + offset) {
1775 ERR(GRN_INVALID_ARGUMENT, "stack error");
1776 return false;
1777 }
1778 proc = data.sp[-offset];
1779 if (grn_obj_is_window_function_proc(ctx, proc)) {
1780 grn_obj inspected;
1781 GRN_TEXT_INIT(&inspected, 0);
1782 grn_inspect(ctx, &inspected, proc);
1783 ERR(GRN_INVALID_ARGUMENT,
1784 "window function can't be executed for each record: %.*s",
1785 (int)GRN_TEXT_LEN(&inspected),
1786 GRN_TEXT_VALUE(&inspected));
1787 GRN_OBJ_FIN(ctx, &inspected);
1788 return false;
1789 } else {
1790 with_spsave(ctx, data, [&]() {
1791 grn_proc_call(ctx, proc, data.code->nargs - 1, expr);
1792 });
1793 }
1794 if (ctx->rc) {
1795 return false;
1796 }
1797 CHECK(pop1(ctx, data, data.res));
1798 {
1799 grn_obj *proc_;
1800 CHECK(pop1(ctx, data, proc_));
1801 if (proc != proc_) {
1802 GRN_LOG(ctx, GRN_LOG_WARNING, "stack may be corrupt");
1803 }
1804 }
1805 CHECK(push1(ctx, data, data.res));
1806 }
1807 }
1808 data.code++;
1809 break;
1810 case GRN_OP_INTERN:
1811 {
1812 grn_obj *obj = NULL;
1813 CHECK(pop1(ctx, data, obj));
1814 obj = GRN_OBJ_RESOLVE(ctx, obj);
1815 data.res =
1816 grn_expr_get_var(ctx, expr, GRN_TEXT_VALUE(obj), GRN_TEXT_LEN(obj));
1817 if (!data.res) {
1818 data.res = grn_ctx_get(ctx, GRN_TEXT_VALUE(obj), GRN_TEXT_LEN(obj));
1819 }
1820 if (!data.res) {
1821 ERR(GRN_INVALID_ARGUMENT, "intern failed");
1822 return false;
1823 }
1824 CHECK(push1(ctx, data, data.res));
1825 }
1826 data.code++;
1827 break;
1828 case GRN_OP_TABLE_CREATE:
1829 {
1830 grn_obj *value_type, *key_type, *flags, *name;
1831 CHECK(pop1(ctx, data, value_type));
1832 value_type = GRN_OBJ_RESOLVE(ctx, value_type);
1833 CHECK(pop1(ctx, data, key_type));
1834 key_type = GRN_OBJ_RESOLVE(ctx, key_type);
1835 CHECK(pop1(ctx, data, flags));
1836 flags = GRN_OBJ_RESOLVE(ctx, flags);
1837 CHECK(pop1(ctx, data, name));
1838 name = GRN_OBJ_RESOLVE(ctx, name);
1839 data.res = grn_table_create(ctx,
1840 GRN_TEXT_VALUE(name),
1841 GRN_TEXT_LEN(name),
1842 NULL,
1843 GRN_UINT32_VALUE(flags),
1844 key_type,
1845 value_type);
1846 CHECK(push1(ctx, data, data.res));
1847 }
1848 data.code++;
1849 break;
1850 case GRN_OP_EXPR_GET_VAR:
1851 {
1852 grn_obj *name = NULL;
1853 grn_obj *expr = NULL;
1854 CHECK(pop1(ctx, data, name));
1855 name = GRN_OBJ_RESOLVE(ctx, name);
1856 CHECK(pop1(ctx, data, expr));
1857 expr = GRN_OBJ_RESOLVE(ctx, expr);
1858 switch (name->header.domain) {
1859 case GRN_DB_INT32:
1860 data.res =
1861 grn_expr_get_var_by_offset(ctx,
1862 expr,
1863 (unsigned int)GRN_INT32_VALUE(name));
1864 break;
1865 case GRN_DB_UINT32:
1866 data.res =
1867 grn_expr_get_var_by_offset(ctx,
1868 expr,
1869 (unsigned int)GRN_UINT32_VALUE(name));
1870 break;
1871 case GRN_DB_INT64:
1872 data.res =
1873 grn_expr_get_var_by_offset(ctx,
1874 expr,
1875 (unsigned int)GRN_INT64_VALUE(name));
1876 break;
1877 case GRN_DB_UINT64:
1878 data.res =
1879 grn_expr_get_var_by_offset(ctx,
1880 expr,
1881 (unsigned int)GRN_UINT64_VALUE(name));
1882 break;
1883 case GRN_DB_SHORT_TEXT:
1884 case GRN_DB_TEXT:
1885 case GRN_DB_LONG_TEXT:
1886 data.res = grn_expr_get_var(ctx,
1887 expr,
1888 GRN_TEXT_VALUE(name),
1889 GRN_TEXT_LEN(name));
1890 break;
1891 default:
1892 ERR(GRN_INVALID_ARGUMENT, "invalid type");
1893 return false;
1894 }
1895 CHECK(push1(ctx, data, data.res));
1896 }
1897 data.code++;
1898 break;
1899 case GRN_OP_ASSIGN:
1900 {
1901 grn_obj *value = NULL;
1902 grn_obj *var = NULL;
1903 if (data.code->value) {
1904 value = data.code->value;
1905 } else {
1906 CHECK(pop1(ctx, data, value));
1907 }
1908 value = GRN_OBJ_RESOLVE(ctx, value);
1909 CHECK(pop1(ctx, data, var));
1910 // var = GRN_OBJ_RESOLVE(ctx, var);
1911 if (var->header.type == GRN_PTR &&
1912 GRN_BULK_VSIZE(var) == (sizeof(grn_obj *) + sizeof(grn_id))) {
1913 grn_obj *col = GRN_PTR_VALUE(var);
1914 grn_id rid = *(grn_id *)(GRN_BULK_HEAD(var) + sizeof(grn_obj *));
1915 grn_obj_set_value(ctx, col, rid, value, GRN_OBJ_SET);
1916 } else {
1917 var_set_value(ctx, var, value);
1918 }
1919 CHECK(push1(ctx, data, value));
1920 }
1921 data.code++;
1922 break;
1923 case GRN_OP_STAR_ASSIGN:
1924 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_STAR_ASSIGN));
1925 break;
1926 case GRN_OP_SLASH_ASSIGN:
1927 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_SLASH_ASSIGN));
1928 break;
1929 case GRN_OP_MOD_ASSIGN:
1930 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_MOD_ASSIGN));
1931 break;
1932 case GRN_OP_PLUS_ASSIGN:
1933 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_PLUS_ASSIGN));
1934 break;
1935 case GRN_OP_MINUS_ASSIGN:
1936 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_MINUS_ASSIGN));
1937 break;
1938 case GRN_OP_SHIFTL_ASSIGN:
1939 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_SHIFTL_ASSIGN));
1940 break;
1941 case GRN_OP_SHIFTR_ASSIGN:
1942 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_SHIFTR));
1943 break;
1944 case GRN_OP_SHIFTRR_ASSIGN:
1945 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_SHIFTRR));
1946 break;
1947 case GRN_OP_AND_ASSIGN:
1948 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_AND_ASSIGN));
1949 break;
1950 case GRN_OP_OR_ASSIGN:
1951 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_OR_ASSIGN));
1952 break;
1953 case GRN_OP_XOR_ASSIGN:
1954 CHECK(operation_and_assign_dispatch(ctx, data, GRN_OP_BITWISE_XOR));
1955 break;
1956 case GRN_OP_JUMP:
1957 data.code += data.code->nargs + 1;
1958 break;
1959 case GRN_OP_CJUMP:
1960 {
1961 grn_obj *v = NULL;
1962 CHECK(pop1(ctx, data, v));
1963 if (!grn_obj_is_true(ctx, v)) {
1964 data.code += data.code->nargs;
1965 }
1966 }
1967 data.code++;
1968 break;
1969 case GRN_OP_GET_VALUE:
1970 {
1971 grn_obj *col = NULL;
1972 grn_obj *rec = NULL;
1973 do {
1974 if (data.code->nargs == 1) {
1975 rec = data.v0;
1976 if (data.code->value) {
1977 col = data.code->value;
1978 CHECK(alloc1(ctx, data, data.res));
1979 } else {
1980 CHECK(pop1alloc1(ctx, data, col, data.res));
1981 }
1982 } else {
1983 if (data.code->value) {
1984 col = data.code->value;
1985 CHECK(pop1alloc1(ctx, data, rec, data.res));
1986 } else {
1987 CHECK(pop2alloc1(ctx, data, rec, col, data.res));
1988 }
1989 }
1990 if (col->header.type == GRN_BULK) {
1991 grn_obj *table = grn_ctx_at(ctx, GRN_OBJ_GET_DOMAIN(rec));
1992 col = grn_obj_column(ctx,
1993 table,
1994 GRN_BULK_HEAD(col),
1995 GRN_BULK_VSIZE(col));
1996 if (col) {
1997 grn_expr_take_obj(ctx, expr, col);
1998 }
1999 }
2000 if (!col) {
2001 ERR(GRN_INVALID_ARGUMENT, "col resolve failed");
2002 return false;
2003 }
2004 grn_obj_reinit_for(ctx, data.res, col);
2005 grn_obj_get_value(ctx, col, GRN_RECORD_VALUE(rec), data.res);
2006 data.code++;
2007 } while (data.code < data.ce && data.code->op == GRN_OP_GET_VALUE);
2008 }
2009 break;
2010 case GRN_OP_OBJ_SEARCH:
2011 {
2012 grn_obj *op = NULL;
2013 grn_obj *query = NULL;
2014 grn_obj *index = NULL;
2015 // todo : grn_search_optarg optarg;
2016 CHECK(pop1(ctx, data, op));
2017 op = GRN_OBJ_RESOLVE(ctx, op);
2018 CHECK(pop1(ctx, data, data.res));
2019 data.res = GRN_OBJ_RESOLVE(ctx, data.res);
2020 CHECK(pop1(ctx, data, query));
2021 query = GRN_OBJ_RESOLVE(ctx, query);
2022 CHECK(pop1(ctx, data, index));
2023 index = GRN_OBJ_RESOLVE(ctx, index);
2024 grn_obj_search(ctx,
2025 index,
2026 query,
2027 data.res,
2028 (grn_operator)GRN_UINT32_VALUE(op),
2029 NULL);
2030 }
2031 data.code++;
2032 break;
2033 case GRN_OP_TABLE_SELECT:
2034 {
2035 grn_obj *op = NULL;
2036 grn_obj *res = NULL;
2037 grn_obj *expr = NULL;
2038 grn_obj *table = NULL;
2039 CHECK(pop1(ctx, data, op));
2040 op = GRN_OBJ_RESOLVE(ctx, op);
2041 CHECK(pop1(ctx, data, res));
2042 res = GRN_OBJ_RESOLVE(ctx, res);
2043 CHECK(pop1(ctx, data, expr));
2044 expr = GRN_OBJ_RESOLVE(ctx, expr);
2045 CHECK(pop1(ctx, data, table));
2046 table = GRN_OBJ_RESOLVE(ctx, table);
2047 with_spsave(ctx, data, [&]() {
2048 grn_table_select(ctx,
2049 table,
2050 expr,
2051 res,
2052 static_cast<grn_operator>(GRN_UINT32_VALUE(op)));
2053 });
2054 CHECK(push1(ctx, data, res));
2055 }
2056 data.code++;
2057 break;
2058 case GRN_OP_TABLE_SORT:
2059 {
2060 grn_obj *keys_ = NULL;
2061 grn_obj *res = NULL;
2062 grn_obj *limit = NULL;
2063 grn_obj *table = NULL;
2064 CHECK(pop1(ctx, data, keys_));
2065 keys_ = GRN_OBJ_RESOLVE(ctx, keys_);
2066 CHECK(pop1(ctx, data, res));
2067 res = GRN_OBJ_RESOLVE(ctx, res);
2068 CHECK(pop1(ctx, data, limit));
2069 limit = GRN_OBJ_RESOLVE(ctx, limit);
2070 CHECK(pop1(ctx, data, table));
2071 table = GRN_OBJ_RESOLVE(ctx, table);
2072 {
2073 grn_table_sort_key *keys;
2074 const char *p = GRN_BULK_HEAD(keys_), *tokbuf[256];
2075 int n = grn_str_tok(p, GRN_BULK_VSIZE(keys_), ' ', tokbuf, 256, NULL);
2076 if ((keys = GRN_MALLOCN(grn_table_sort_key, n))) {
2077 int i, n_keys = 0;
2078 for (i = 0; i < n; i++) {
2079 uint32_t len = (uint32_t)(tokbuf[i] - p);
2080 grn_obj *col = grn_obj_column(ctx, table, p, len);
2081 if (col) {
2082 keys[n_keys].key = col;
2083 keys[n_keys].flags = GRN_TABLE_SORT_ASC;
2084 keys[n_keys].offset = 0;
2085 n_keys++;
2086 } else {
2087 if (p[0] == ':' && p[1] == 'd' && len == 2 && n_keys) {
2088 keys[n_keys - 1].flags |= GRN_TABLE_SORT_DESC;
2089 }
2090 }
2091 p = tokbuf[i] + 1;
2092 }
2093 with_spsave(ctx, data, [&]() {
2094 grn_table_sort(ctx,
2095 table,
2096 0,
2097 GRN_INT32_VALUE(limit),
2098 res,
2099 keys,
2100 n_keys);
2101 });
2102 for (i = 0; i < n_keys; i++) {
2103 grn_obj_unlink(ctx, keys[i].key);
2104 }
2105 GRN_FREE(keys);
2106 }
2107 }
2108 }
2109 data.code++;
2110 break;
2111 case GRN_OP_TABLE_GROUP:
2112 {
2113 grn_obj *res = NULL;
2114 grn_obj *keys_ = NULL;
2115 grn_obj *table = NULL;
2116 CHECK(pop1(ctx, data, res));
2117 res = GRN_OBJ_RESOLVE(ctx, res);
2118 CHECK(pop1(ctx, data, keys_));
2119 keys_ = GRN_OBJ_RESOLVE(ctx, keys_);
2120 CHECK(pop1(ctx, data, table));
2121 table = GRN_OBJ_RESOLVE(ctx, table);
2122 {
2123 grn_table_sort_key *keys;
2124 grn_table_group_result results;
2125 const char *p = GRN_BULK_HEAD(keys_), *tokbuf[256];
2126 int n = grn_str_tok(p, GRN_BULK_VSIZE(keys_), ' ', tokbuf, 256, NULL);
2127 if ((keys = GRN_MALLOCN(grn_table_sort_key, n))) {
2128 int i, n_keys = 0;
2129 for (i = 0; i < n; i++) {
2130 uint32_t len = (uint32_t)(tokbuf[i] - p);
2131 grn_obj *col = grn_obj_column(ctx, table, p, len);
2132 if (col) {
2133 keys[n_keys].key = col;
2134 keys[n_keys].flags = GRN_TABLE_SORT_ASC;
2135 keys[n_keys].offset = 0;
2136 n_keys++;
2137 } else if (n_keys) {
2138 if (p[0] == ':' && p[1] == 'd' && len == 2) {
2139 keys[n_keys - 1].flags |= GRN_TABLE_SORT_DESC;
2140 } else {
2141 keys[n_keys - 1].offset = grn_atoi(p, p + len, NULL);
2142 }
2143 }
2144 p = tokbuf[i] + 1;
2145 }
2146 /* todo : support multi-results */
2147 results.table = res;
2148 results.key_begin = 0;
2149 results.key_end = 0;
2150 results.limit = 0;
2151 results.flags = 0;
2152 results.op = GRN_OP_OR;
2153 with_spsave(ctx, data, [&]() {
2154 grn_table_group(ctx, table, keys, n_keys, &results, 1);
2155 });
2156 for (i = 0; i < n_keys; i++) {
2157 grn_obj_unlink(ctx, keys[i].key);
2158 }
2159 GRN_FREE(keys);
2160 }
2161 }
2162 }
2163 data.code++;
2164 break;
2165 case GRN_OP_JSON_PUT:
2166 {
2167 grn_obj_format format;
2168 grn_obj *res = NULL;
2169 grn_obj *str = NULL;
2170 grn_obj *table = NULL;
2171 CHECK(pop1(ctx, data, res));
2172 res = GRN_OBJ_RESOLVE(ctx, res);
2173 CHECK(pop1(ctx, data, str));
2174 str = GRN_OBJ_RESOLVE(ctx, str);
2175 CHECK(pop1(ctx, data, table));
2176 table = GRN_OBJ_RESOLVE(ctx, table);
2177 GRN_OBJ_FORMAT_INIT(&format, grn_table_size(ctx, table), 0, -1, 0);
2178 format.flags = 0;
2179 grn_obj_columns(ctx,
2180 table,
2181 GRN_TEXT_VALUE(str),
2182 GRN_TEXT_LEN(str),
2183 &format.columns);
2184 grn_text_otoj(ctx, res, table, &format);
2185 grn_obj_format_fin(ctx, &format);
2186 }
2187 data.code++;
2188 break;
2189 case GRN_OP_AND:
2190 {
2191 grn_obj *x = NULL;
2192 grn_obj *y = NULL;
2193 grn_obj *result = NULL;
2194 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2195 if (grn_obj_is_true(ctx, x)) {
2196 if (grn_obj_is_true(ctx, y)) {
2197 result = y;
2198 }
2199 }
2200 if (result) {
2201 if (data.res != result) {
2202 grn_bulk_copy(ctx, result, data.res);
2203 }
2204 } else {
2205 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2206 GRN_BOOL_SET(ctx, data.res, false);
2207 }
2208 }
2209 data.code++;
2210 break;
2211 case GRN_OP_OR:
2212 {
2213 grn_obj *x = NULL;
2214 grn_obj *y = NULL;
2215 grn_obj *result = NULL;
2216 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2217 if (grn_obj_is_true(ctx, x)) {
2218 result = x;
2219 } else {
2220 if (grn_obj_is_true(ctx, y)) {
2221 result = y;
2222 } else {
2223 result = NULL;
2224 }
2225 }
2226 if (result) {
2227 if (data.res != result) {
2228 grn_bulk_copy(ctx, result, data.res);
2229 }
2230 } else {
2231 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2232 GRN_BOOL_SET(ctx, data.res, false);
2233 }
2234 }
2235 data.code++;
2236 break;
2237 case GRN_OP_AND_NOT:
2238 {
2239 grn_obj *x = NULL;
2240 grn_obj *y = NULL;
2241 bool is_true = false;
2242 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2243 if (!grn_obj_is_true(ctx, x) || grn_obj_is_true(ctx, y)) {
2244 is_true = false;
2245 } else {
2246 is_true = true;
2247 }
2248 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2249 GRN_BOOL_SET(ctx, data.res, is_true);
2250 }
2251 data.code++;
2252 break;
2253 case GRN_OP_ADJUST:
2254 {
2255 /* todo */
2256 }
2257 data.code++;
2258 break;
2259 case GRN_OP_MATCH:
2260 {
2261 grn_obj *x = NULL;
2262 grn_obj *y = NULL;
2263 bool matched;
2264 CHECK(pop1(ctx, data, y));
2265 CHECK(pop1(ctx, data, x));
2266 with_spsave(ctx, data, [&]() {
2267 matched = grn_operator_exec_match(ctx, x, y);
2268 });
2269 CHECK(alloc1(ctx, data, data.res));
2270 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2271 GRN_BOOL_SET(ctx, data.res, matched);
2272 }
2273 data.code++;
2274 break;
2275 case GRN_OP_EQUAL:
2276 {
2277 grn_obj *x = NULL;
2278 grn_obj *y = NULL;
2279 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2280 bool is_equal = grn_operator_exec_equal(ctx, x, y);
2281 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2282 GRN_BOOL_SET(ctx, data.res, is_equal);
2283 }
2284 data.code++;
2285 break;
2286 case GRN_OP_NOT_EQUAL:
2287 {
2288 grn_obj *x = NULL;
2289 grn_obj *y = NULL;
2290 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2291 bool is_not_equal = grn_operator_exec_not_equal(ctx, x, y);
2292 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2293 GRN_BOOL_SET(ctx, data.res, is_not_equal);
2294 }
2295 data.code++;
2296 break;
2297 case GRN_OP_PREFIX:
2298 {
2299 grn_obj *x = NULL;
2300 grn_obj *y = NULL;
2301 bool matched = false;
2302 CHECK(pop1(ctx, data, y));
2303 CHECK(pop1(ctx, data, x));
2304 with_spsave(ctx, data, [&]() {
2305 matched = grn_operator_exec_prefix(ctx, x, y);
2306 });
2307 CHECK(alloc1(ctx, data, data.res));
2308 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2309 GRN_BOOL_SET(ctx, data.res, matched);
2310 }
2311 data.code++;
2312 break;
2313 case GRN_OP_SUFFIX:
2314 {
2315 grn_obj *x = NULL;
2316 grn_obj *y = NULL;
2317 bool matched = false;
2318 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2319 if (GRN_TEXT_LEN(x) >= GRN_TEXT_LEN(y) &&
2320 !memcmp(GRN_TEXT_VALUE(x) + GRN_TEXT_LEN(x) - GRN_TEXT_LEN(y),
2321 GRN_TEXT_VALUE(y),
2322 GRN_TEXT_LEN(y))) {
2323 matched = true;
2324 }
2325 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2326 GRN_BOOL_SET(ctx, data.res, matched);
2327 }
2328 data.code++;
2329 break;
2330 case GRN_OP_LESS:
2331 {
2332 grn_obj *x = NULL;
2333 grn_obj *y = NULL;
2334 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2335 bool r = grn_operator_exec_less(ctx, x, y);
2336 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2337 GRN_BOOL_SET(ctx, data.res, r);
2338 }
2339 data.code++;
2340 break;
2341 case GRN_OP_GREATER:
2342 {
2343 grn_obj *x = NULL;
2344 grn_obj *y = NULL;
2345 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2346 bool r = grn_operator_exec_greater(ctx, x, y);
2347 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2348 GRN_BOOL_SET(ctx, data.res, r);
2349 }
2350 data.code++;
2351 break;
2352 case GRN_OP_LESS_EQUAL:
2353 {
2354 grn_obj *x = NULL;
2355 grn_obj *y = NULL;
2356 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2357 bool r = grn_operator_exec_less_equal(ctx, x, y);
2358 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2359 GRN_BOOL_SET(ctx, data.res, r);
2360 }
2361 data.code++;
2362 break;
2363 case GRN_OP_GREATER_EQUAL:
2364 {
2365 grn_obj *x = NULL;
2366 grn_obj *y = NULL;
2367 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2368 bool r = grn_operator_exec_greater_equal(ctx, x, y);
2369 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2370 GRN_BOOL_SET(ctx, data.res, r);
2371 }
2372 data.code++;
2373 break;
2374 case GRN_OP_GEO_DISTANCE1:
2375 {
2376 grn_obj *value = NULL;
2377 double lng1, lat1, lng2, lat2, x, y, d;
2378 CHECK(pop1(ctx, data, value));
2379 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2380 CHECK(pop1(ctx, data, value));
2381 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2382 CHECK(pop1(ctx, data, value));
2383 lng2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2384 CHECK(pop1alloc1(ctx, data, value, data.res));
2385 lat2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2386 x = (lng2 - lng1) * cos((lat1 + lat2) * 0.5);
2387 y = (lat2 - lat1);
2388 d = sqrt((x * x) + (y * y)) * GEO_RADIOUS;
2389 data.res->header.type = GRN_BULK;
2390 data.res->header.domain = GRN_DB_FLOAT;
2391 GRN_FLOAT_SET(ctx, data.res, d);
2392 }
2393 data.code++;
2394 break;
2395 case GRN_OP_GEO_DISTANCE2:
2396 {
2397 grn_obj *value = NULL;
2398 double lng1, lat1, lng2, lat2, x, y, d;
2399 CHECK(pop1(ctx, data, value));
2400 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2401 CHECK(pop1(ctx, data, value));
2402 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2403 CHECK(pop1(ctx, data, value));
2404 lng2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2405 CHECK(pop1alloc1(ctx, data, value, data.res));
2406 lat2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2407 x = sin(fabs(lng2 - lng1) * 0.5);
2408 y = sin(fabs(lat2 - lat1) * 0.5);
2409 d =
2410 asin(sqrt((y * y) + cos(lat1) * cos(lat2) * x * x)) * 2 * GEO_RADIOUS;
2411 data.res->header.type = GRN_BULK;
2412 data.res->header.domain = GRN_DB_FLOAT;
2413 GRN_FLOAT_SET(ctx, data.res, d);
2414 }
2415 data.code++;
2416 break;
2417 case GRN_OP_GEO_DISTANCE3:
2418 {
2419 grn_obj *value = NULL;
2420 double lng1, lat1, lng2, lat2, p, q, m, n, x, y, d;
2421 CHECK(pop1(ctx, data, value));
2422 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2423 CHECK(pop1(ctx, data, value));
2424 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2425 CHECK(pop1(ctx, data, value));
2426 lng2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2427 CHECK(pop1alloc1(ctx, data, value, data.res));
2428 lat2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2429 p = (lat1 + lat2) * 0.5;
2430 q = (1 - GEO_BES_C3 * sin(p) * sin(p));
2431 m = GEO_BES_C1 / sqrt(q * q * q);
2432 n = GEO_BES_C2 / sqrt(q);
2433 x = n * cos(p) * fabs(lng1 - lng2);
2434 y = m * fabs(lat1 - lat2);
2435 d = sqrt((x * x) + (y * y));
2436 data.res->header.type = GRN_BULK;
2437 data.res->header.domain = GRN_DB_FLOAT;
2438 GRN_FLOAT_SET(ctx, data.res, d);
2439 }
2440 data.code++;
2441 break;
2442 case GRN_OP_GEO_DISTANCE4:
2443 {
2444 grn_obj *value = NULL;
2445 double lng1, lat1, lng2, lat2, p, q, m, n, x, y, d;
2446 CHECK(pop1(ctx, data, value));
2447 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2448 CHECK(pop1(ctx, data, value));
2449 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2450 CHECK(pop1(ctx, data, value));
2451 lng2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2452 CHECK(pop1alloc1(ctx, data, value, data.res));
2453 lat2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2454 p = (lat1 + lat2) * 0.5;
2455 q = (1 - GEO_GRS_C3 * sin(p) * sin(p));
2456 m = GEO_GRS_C1 / sqrt(q * q * q);
2457 n = GEO_GRS_C2 / sqrt(q);
2458 x = n * cos(p) * fabs(lng1 - lng2);
2459 y = m * fabs(lat1 - lat2);
2460 d = sqrt((x * x) + (y * y));
2461 data.res->header.type = GRN_BULK;
2462 data.res->header.domain = GRN_DB_FLOAT;
2463 GRN_FLOAT_SET(ctx, data.res, d);
2464 }
2465 data.code++;
2466 break;
2467 case GRN_OP_GEO_WITHINP5:
2468 {
2469 int r;
2470 grn_obj *value = NULL;
2471 double lng0, lat0, lng1, lat1, x, y, d;
2472 CHECK(pop1(ctx, data, value));
2473 lng0 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2474 CHECK(pop1(ctx, data, value));
2475 lat0 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2476 CHECK(pop1(ctx, data, value));
2477 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2478 CHECK(pop1(ctx, data, value));
2479 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2480 CHECK(pop1alloc1(ctx, data, value, data.res));
2481 x = (lng1 - lng0) * cos((lat0 + lat1) * 0.5);
2482 y = (lat1 - lat0);
2483 d = sqrt((x * x) + (y * y)) * GEO_RADIOUS;
2484 switch (value->header.domain) {
2485 case GRN_DB_INT32:
2486 r = d <= GRN_INT32_VALUE(value);
2487 break;
2488 case GRN_DB_FLOAT:
2489 r = d <= GRN_FLOAT_VALUE(value);
2490 break;
2491 default:
2492 r = 0;
2493 break;
2494 }
2495 GRN_INT32_SET(ctx, data.res, r);
2496 data.res->header.type = GRN_BULK;
2497 data.res->header.domain = GRN_DB_INT32;
2498 }
2499 data.code++;
2500 break;
2501 case GRN_OP_GEO_WITHINP6:
2502 {
2503 int r;
2504 grn_obj *value = NULL;
2505 double lng0, lat0, lng1, lat1, lng2, lat2, x, y, d;
2506 CHECK(pop1(ctx, data, value));
2507 lng0 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2508 CHECK(pop1(ctx, data, value));
2509 lat0 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2510 CHECK(pop1(ctx, data, value));
2511 lng1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2512 CHECK(pop1(ctx, data, value));
2513 lat1 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2514 CHECK(pop1(ctx, data, value));
2515 lng2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2516 CHECK(pop1alloc1(ctx, data, value, data.res));
2517 lat2 = GRN_GEO_MSEC2RADIAN(GRN_INT32_VALUE(value));
2518 x = (lng1 - lng0) * cos((lat0 + lat1) * 0.5);
2519 y = (lat1 - lat0);
2520 d = (x * x) + (y * y);
2521 x = (lng2 - lng1) * cos((lat1 + lat2) * 0.5);
2522 y = (lat2 - lat1);
2523 r = d <= (x * x) + (y * y);
2524 GRN_INT32_SET(ctx, data.res, r);
2525 data.res->header.type = GRN_BULK;
2526 data.res->header.domain = GRN_DB_INT32;
2527 }
2528 data.code++;
2529 break;
2530 case GRN_OP_GEO_WITHINP8:
2531 {
2532 int r;
2533 grn_obj *value = NULL;
2534 int64_t ln0, la0, ln1, la1, ln2, la2, ln3, la3;
2535 CHECK(pop1(ctx, data, value));
2536 ln0 = GRN_INT32_VALUE(value);
2537 CHECK(pop1(ctx, data, value));
2538 la0 = GRN_INT32_VALUE(value);
2539 CHECK(pop1(ctx, data, value));
2540 ln1 = GRN_INT32_VALUE(value);
2541 CHECK(pop1(ctx, data, value));
2542 la1 = GRN_INT32_VALUE(value);
2543 CHECK(pop1(ctx, data, value));
2544 ln2 = GRN_INT32_VALUE(value);
2545 CHECK(pop1(ctx, data, value));
2546 la2 = GRN_INT32_VALUE(value);
2547 CHECK(pop1(ctx, data, value));
2548 ln3 = GRN_INT32_VALUE(value);
2549 CHECK(pop1alloc1(ctx, data, value, data.res));
2550 la3 = GRN_INT32_VALUE(value);
2551 r = ((ln2 <= ln0) && (ln0 <= ln3) && (la2 <= la0) && (la0 <= la3));
2552 GRN_INT32_SET(ctx, data.res, r);
2553 data.res->header.type = GRN_BULK;
2554 data.res->header.domain = GRN_DB_INT32;
2555 }
2556 data.code++;
2557 break;
2558 case GRN_OP_PLUS:
2559 {
2560 grn_obj *x = NULL;
2561 grn_obj *y = NULL;
2562 CHECK(pop2alloc1(ctx, data, x, y, data.res));
2563 if (x->header.type == GRN_VECTOR && y->header.type == GRN_VECTOR) {
2564 grn_id domain_id = x->header.domain;
2565 if (domain_id == GRN_ID_NIL) {
2566 domain_id = y->header.domain;
2567 }
2568 if (domain_id == GRN_ID_NIL && grn_vector_size(ctx, x) > 0) {
2569 const char *content;
2570 grn_vector_get_element(ctx, x, 0, &content, NULL, &domain_id);
2571 }
2572 if (domain_id == GRN_ID_NIL && grn_vector_size(ctx, y) > 0) {
2573 const char *content;
2574 grn_vector_get_element(ctx, y, 0, &content, NULL, &domain_id);
2575 }
2576 if (domain_id == GRN_ID_NIL) {
2577 /* TODO: Consider better default for GRN_VECTOR. */
2578 domain_id = GRN_DB_SHORT_TEXT;
2579 }
2580 if (data.res == y) {
2581 grn_obj y_copied;
2582 GRN_OBJ_INIT(&y_copied, GRN_VECTOR, 0, y->header.domain);
2583 grn_vector_copy(ctx, y, &y_copied);
2584 grn_obj_reinit(ctx, data.res, domain_id, GRN_OBJ_VECTOR);
2585 grn_vector_copy(ctx, x, data.res);
2586 grn_vector_copy(ctx, &y_copied, data.res);
2587 GRN_OBJ_FIN(ctx, &y_copied);
2588 } else {
2589 if (data.res != x) {
2590 grn_obj_reinit(ctx, data.res, domain_id, GRN_OBJ_VECTOR);
2591 grn_vector_copy(ctx, x, data.res);
2592 }
2593 grn_vector_copy(ctx, y, data.res);
2594 }
2595 data.code++;
2596 } else if (x->header.type == GRN_VECTOR ||
2597 y->header.type == GRN_VECTOR) {
2598 grn_obj inspected_x;
2599 grn_obj inspected_y;
2600 GRN_TEXT_INIT(&inspected_x, 0);
2601 GRN_TEXT_INIT(&inspected_y, 0);
2602 grn_inspect(ctx, &inspected_x, x);
2603 grn_inspect(ctx, &inspected_y, y);
2604 ERR(GRN_INVALID_ARGUMENT,
2605 "<+> doesn't support %s + %s: <%.*s> + <%.*s>",
2606 x->header.type == GRN_VECTOR ? "vector" : "non-vector",
2607 y->header.type == GRN_VECTOR ? "vector" : "non-vector",
2608 (int)GRN_TEXT_LEN(&inspected_x),
2609 GRN_TEXT_VALUE(&inspected_x),
2610 (int)GRN_TEXT_LEN(&inspected_y),
2611 GRN_TEXT_VALUE(&inspected_y));
2612 GRN_OBJ_FIN(ctx, &inspected_x);
2613 GRN_OBJ_FIN(ctx, &inspected_y);
2614 return false;
2615 } else {
2616 CHECK(arithmetic_binary_operation_dispatch(ctx,
2617 data,
2618 GRN_OP_PLUS,
2619 x,
2620 y,
2621 data.res));
2622 }
2623 }
2624 break;
2625 case GRN_OP_MINUS:
2626 if (data.code->nargs == 1) {
2627 CHECK(arithmetic_unary_operation_dispatch(ctx, data, GRN_OP_MINUS));
2628 } else {
2629 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_MINUS));
2630 }
2631 break;
2632 case GRN_OP_STAR:
2633 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_STAR));
2634 break;
2635 case GRN_OP_SLASH:
2636 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_SLASH));
2637 break;
2638 case GRN_OP_MOD:
2639 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_MOD));
2640 break;
2641 case GRN_OP_BITWISE_NOT:
2642 CHECK(arithmetic_unary_operation_dispatch(ctx, data, GRN_OP_BITWISE_NOT));
2643 break;
2644 case GRN_OP_BITWISE_OR:
2645 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_BITWISE_OR));
2646 break;
2647 case GRN_OP_BITWISE_XOR:
2648 CHECK(
2649 arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_BITWISE_XOR));
2650 break;
2651 case GRN_OP_BITWISE_AND:
2652 CHECK(
2653 arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_BITWISE_AND));
2654 break;
2655 case GRN_OP_SHIFTL:
2656 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_SHIFTL));
2657 break;
2658 case GRN_OP_SHIFTR:
2659 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_SHIFTR));
2660 break;
2661 case GRN_OP_SHIFTRR:
2662 CHECK(arithmetic_binary_operation_dispatch(ctx, data, GRN_OP_SHIFTRR));
2663 break;
2664 case GRN_OP_INCR:
2665 case GRN_OP_DECR:
2666 case GRN_OP_INCR_POST:
2667 case GRN_OP_DECR_POST:
2668 CHECK(unary_operation_and_assign_dispatch(ctx, data, data.code->op));
2669 break;
2670 case GRN_OP_NOT:
2671 {
2672 grn_obj *value = NULL;
2673 bool value_boolean = false;
2674 CHECK(pop1alloc1(ctx, data, value, data.res));
2675 GRN_OBJ_IS_TRUE(ctx, value, value_boolean);
2676 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2677 GRN_BOOL_SET(ctx, data.res, !value_boolean);
2678 }
2679 data.code++;
2680 break;
2681 case GRN_OP_GET_MEMBER:
2682 {
2683 grn_obj *receiver = NULL;
2684 grn_obj *index_or_key = NULL;
2685 CHECK(pop2alloc1(ctx, data, receiver, index_or_key, data.res));
2686 if (receiver->header.type == GRN_PTR) {
2687 grn_obj *index = index_or_key;
2688 grn_expr_exec_get_member_vector(ctx, expr, receiver, index, data.res);
2689 } else {
2690 grn_obj *key = index_or_key;
2691 grn_expr_exec_get_member_table(ctx, expr, receiver, key, data.res);
2692 }
2693 data.code++;
2694 }
2695 break;
2696 case GRN_OP_REGEXP:
2697 {
2698 grn_obj *target = NULL;
2699 grn_obj *pattern = NULL;
2700 bool matched;
2701 CHECK(pop1(ctx, data, pattern));
2702 CHECK(pop1(ctx, data, target));
2703 with_spsave(ctx, data, [&]() {
2704 matched = grn_operator_exec_regexp(ctx, target, pattern);
2705 });
2706 CHECK(alloc1(ctx, data, data.res));
2707 grn_obj_reinit(ctx, data.res, GRN_DB_BOOL, 0);
2708 GRN_BOOL_SET(ctx, data.res, matched);
2709 }
2710 data.code++;
2711 break;
2712 default:
2713 ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "not implemented operator assigned");
2714 return false;
2715 }
2716 }
2717 ctx->impl->stack_curr = data.sp - data.s_;
2718 return true;
2719 }
2720
2721 static grn_obj *
2722 expr_exec(grn_ctx *ctx, grn_obj *expr)
2723 {
2724 uint32_t stack_curr = ctx->impl->stack_curr;
2725 grn_obj *value = nullptr;
2726 auto success = expr_exec_internal(ctx, expr);
2727 if (success) {
2728 if (ctx->impl->stack_curr > stack_curr) {
2729 value = grn_ctx_pop(ctx);
2730 }
2731 }
2732 if (ctx->impl->stack_curr > stack_curr) {
2733 /*
2734 GRN_LOG(ctx, GRN_LOG_WARNING, "stack balance=%d",
2735 stack_curr - ctx->impl->stack_curr);
2736 */
2737 ctx->impl->stack_curr = stack_curr;
2738 }
2739 return value;
2740 }
2741
2742 static grn_obj *
2743 grn_expr_executor_exec_general(grn_ctx *ctx,
2744 grn_expr_executor *executor,
2745 grn_id id)
2746 {
2747 if (!executor->variable && id != GRN_ID_NIL) {
2748 ERR(GRN_INVALID_ARGUMENT,
2749 "[expr-executor][exec][general] can't specify ID for no variable expr");
2750 return NULL;
2751 }
2752 if (executor->variable) {
2753 GRN_RECORD_SET(ctx, executor->variable, id);
2754 }
2755 return expr_exec(ctx, executor->expr);
2756 }
2757
2758 static void
2759 grn_expr_executor_fin_general(grn_ctx *ctx, grn_expr_executor *executor)
2760 {
2761 }
2762
2763 static void
2764 grn_expr_executor_init_constant(grn_ctx *ctx, grn_expr_executor *executor)
2765 {
2766 grn_expr *e = (grn_expr *)(executor->expr);
2767
2768 executor->data.constant.value = e->codes[0].value;
2769 }
2770
2771 static bool
2772 grn_expr_executor_is_constant(grn_ctx *ctx, grn_expr_executor *executor)
2773 {
2774 grn_expr *e = (grn_expr *)(executor->expr);
2775 grn_expr_code *target;
2776
2777 if (e->codes_curr != 1) {
2778 return false;
2779 }
2780
2781 target = &(e->codes[0]);
2782
2783 if (target->op != GRN_OP_PUSH) {
2784 return false;
2785 }
2786 if (!target->value) {
2787 return false;
2788 }
2789
2790 grn_expr_executor_init_constant(ctx, executor);
2791
2792 return true;
2793 }
2794
2795 static grn_obj *
2796 grn_expr_executor_exec_constant(grn_ctx *ctx,
2797 grn_expr_executor *executor,
2798 grn_id id)
2799 {
2800 return executor->data.constant.value;
2801 }
2802
2803 static void
2804 grn_expr_executor_fin_constant(grn_ctx *ctx, grn_expr_executor *executor)
2805 {
2806 }
2807
2808 static void
2809 grn_expr_executor_init_value(grn_ctx *ctx, grn_expr_executor *executor)
2810 {
2811 grn_expr *e = (grn_expr *)(executor->expr);
2812
2813 executor->data.value.column = e->codes[0].value;
2814 GRN_VOID_INIT(&(executor->data.value.value_buffer));
2815 }
2816
2817 static bool
2818 grn_expr_executor_is_value(grn_ctx *ctx, grn_expr_executor *executor)
2819 {
2820 grn_expr *e = (grn_expr *)(executor->expr);
2821 grn_expr_code *target;
2822
2823 if (e->codes_curr != 1) {
2824 return false;
2825 }
2826
2827 target = &(e->codes[0]);
2828
2829 if (target->op != GRN_OP_GET_VALUE) {
2830 return false;
2831 }
2832 if (!target->value) {
2833 return false;
2834 }
2835
2836 grn_expr_executor_init_value(ctx, executor);
2837
2838 return true;
2839 }
2840
2841 static grn_obj *
2842 grn_expr_executor_exec_value(grn_ctx *ctx,
2843 grn_expr_executor *executor,
2844 grn_id id)
2845 {
2846 grn_obj *value_buffer = &(executor->data.value.value_buffer);
2847
2848 GRN_BULK_REWIND(value_buffer);
2849 grn_obj_get_value(ctx, executor->data.value.column, id, value_buffer);
2850
2851 return value_buffer;
2852 }
2853
2854 static void
2855 grn_expr_executor_fin_value(grn_ctx *ctx, grn_expr_executor *executor)
2856 {
2857 GRN_OBJ_FIN(ctx, &(executor->data.value.value_buffer));
2858 }
2859
2860 #ifdef GRN_SUPPORT_REGEXP
2861 static void
2862 grn_expr_executor_init_simple_regexp(grn_ctx *ctx, grn_expr_executor *executor)
2863 {
2864 grn_expr *e = (grn_expr *)(executor->expr);
2865 grn_obj *result_buffer = &(executor->data.simple_regexp.result_buffer);
2866 grn_obj *pattern;
2867
2868 GRN_BOOL_INIT(result_buffer, 0);
2869 GRN_BOOL_SET(ctx, result_buffer, false);
2870
2871 pattern = e->codes[1].value;
2872 if (GRN_TEXT_LEN(pattern) == 0) {
2873 executor->data.simple_regexp.regex = NULL;
2874 return;
2875 }
2876
2877 executor->data.simple_regexp.regex =
2878 grn_onigmo_new(ctx,
2879 GRN_TEXT_VALUE(pattern),
2880 GRN_TEXT_LEN(pattern),
2881 GRN_ONIGMO_OPTION_DEFAULT,
2882 GRN_ONIGMO_SYNTAX_DEFAULT,
2883 "[expr-executor][regexp]");
2884 if (!executor->data.simple_regexp.regex) {
2885 return;
2886 }
2887
2888 GRN_VOID_INIT(&(executor->data.simple_regexp.value_buffer));
2889
2890 executor->data.simple_regexp.normalizer =
2891 grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1);
2892 }
2893
2894 static bool
2895 grn_expr_executor_is_simple_regexp(grn_ctx *ctx, grn_expr_executor *executor)
2896 {
2897 grn_expr *e = (grn_expr *)(executor->expr);
2898 grn_expr_code *target;
2899 grn_expr_code *pattern;
2900 grn_expr_code *operator_code;
2901
2902 if (e->codes_curr != 3) {
2903 return false;
2904 }
2905
2906 target = &(e->codes[0]);
2907 pattern = &(e->codes[1]);
2908 operator_code = &(e->codes[2]);
2909
2910 if (operator_code->op != GRN_OP_REGEXP) {
2911 return false;
2912 }
2913 if (operator_code->nargs != 2) {
2914 return false;
2915 }
2916
2917 if (target->op != GRN_OP_GET_VALUE) {
2918 return false;
2919 }
2920 if (target->nargs != 1) {
2921 return false;
2922 }
2923 if (!target->value) {
2924 return false;
2925 }
2926 if (target->value->header.type != GRN_COLUMN_VAR_SIZE) {
2927 return false;
2928 }
2929 if ((target->value->header.flags & GRN_OBJ_COLUMN_TYPE_MASK) !=
2930 GRN_OBJ_COLUMN_SCALAR) {
2931 return false;
2932 }
2933 switch (grn_obj_get_range(ctx, target->value)) {
2934 case GRN_DB_SHORT_TEXT:
2935 case GRN_DB_TEXT:
2936 case GRN_DB_LONG_TEXT:
2937 break;
2938 default:
2939 return false;
2940 }
2941
2942 if (pattern->op != GRN_OP_PUSH) {
2943 return false;
2944 }
2945 if (pattern->nargs != 1) {
2946 return false;
2947 }
2948 if (!pattern->value) {
2949 return false;
2950 }
2951 if (pattern->value->header.type != GRN_BULK) {
2952 return false;
2953 }
2954 switch (pattern->value->header.domain) {
2955 case GRN_DB_SHORT_TEXT:
2956 case GRN_DB_TEXT:
2957 case GRN_DB_LONG_TEXT:
2958 break;
2959 default:
2960 return false;
2961 }
2962
2963 if (!grn_onigmo_is_valid_encoding(ctx)) {
2964 return false;
2965 }
2966
2967 grn_expr_executor_init_simple_regexp(ctx, executor);
2968
2969 return true;
2970 }
2971
2972 static grn_obj *
2973 grn_expr_executor_exec_simple_regexp(grn_ctx *ctx,
2974 grn_expr_executor *executor,
2975 grn_id id)
2976 {
2977 grn_expr *e = (grn_expr *)(executor->expr);
2978 OnigRegex regex = static_cast<OnigRegex>(executor->data.simple_regexp.regex);
2979 grn_obj *value_buffer = &(executor->data.simple_regexp.value_buffer);
2980 grn_obj *result_buffer = &(executor->data.simple_regexp.result_buffer);
2981
2982 if (ctx->rc) {
2983 GRN_BOOL_SET(ctx, result_buffer, false);
2984 return result_buffer;
2985 }
2986
2987 if (!regex) {
2988 return result_buffer;
2989 }
2990
2991 grn_obj_reinit_for(ctx, value_buffer, e->codes[0].value);
2992 grn_obj_get_value(ctx, e->codes[0].value, id, value_buffer);
2993 {
2994 grn_obj *norm_target;
2995 const char *norm_target_raw;
2996 unsigned int norm_target_raw_length_in_bytes;
2997
2998 norm_target = grn_string_open(ctx,
2999 GRN_TEXT_VALUE(value_buffer),
3000 GRN_TEXT_LEN(value_buffer),
3001 executor->data.simple_regexp.normalizer,
3002 0);
3003 grn_string_get_normalized(ctx,
3004 norm_target,
3005 &norm_target_raw,
3006 &norm_target_raw_length_in_bytes,
3007 NULL);
3008
3009 {
3010 auto target_start = reinterpret_cast<const OnigUChar *>(norm_target_raw);
3011 auto target_end = target_start + norm_target_raw_length_in_bytes;
3012 OnigPosition position;
3013 position = onig_search(regex,
3014 target_start,
3015 target_end,
3016 target_start,
3017 target_end,
3018 NULL,
3019 ONIG_OPTION_NONE);
3020 grn_obj_close(ctx, norm_target);
3021 GRN_BOOL_SET(ctx, result_buffer, (position != ONIG_MISMATCH));
3022 return result_buffer;
3023 }
3024 }
3025 }
3026
3027 static void
3028 grn_expr_executor_fin_simple_regexp(grn_ctx *ctx, grn_expr_executor *executor)
3029 {
3030 GRN_OBJ_FIN(ctx, &(executor->data.simple_regexp.result_buffer));
3031
3032 if (!executor->data.simple_regexp.regex) {
3033 return;
3034 }
3035
3036 onig_free(static_cast<OnigRegex>(executor->data.simple_regexp.regex));
3037 GRN_OBJ_FIN(ctx, &(executor->data.simple_regexp.value_buffer));
3038 }
3039 #endif /* GRN_SUPPORT_REGEXP */
3040
3041 static void
3042 grn_expr_executor_init_simple_match(grn_ctx *ctx, grn_expr_executor *executor)
3043 {
3044 grn_expr *e = (grn_expr *)(executor->expr);
3045 grn_obj *result_buffer = &(executor->data.simple_match.result_buffer);
3046 grn_obj *sub_text;
3047
3048 GRN_BOOL_INIT(result_buffer, 0);
3049 GRN_BOOL_SET(ctx, result_buffer, false);
3050
3051 executor->data.simple_match.normalizer =
3052 grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1);
3053
3054 sub_text = e->codes[1].value;
3055 executor->data.simple_match.normalized_sub_text =
3056 grn_string_open(ctx,
3057 GRN_TEXT_VALUE(sub_text),
3058 GRN_TEXT_LEN(sub_text),
3059 executor->data.simple_match.normalizer,
3060 0);
3061 grn_string_get_normalized(
3062 ctx,
3063 executor->data.simple_match.normalized_sub_text,
3064 &(executor->data.simple_match.normalized_sub_text_raw),
3065 &(executor->data.simple_match.normalized_sub_text_raw_length_in_bytes),
3066 NULL);
3067 #ifdef GRN_SUPPORT_REGEXP
3068 executor->data.simple_match.regex = grn_onigmo_new(
3069 ctx,
3070 executor->data.simple_match.normalized_sub_text_raw,
3071 executor->data.simple_match.normalized_sub_text_raw_length_in_bytes,
3072 GRN_ONIGMO_OPTION_DEFAULT,
3073 ONIG_SYNTAX_ASIS,
3074 "[expr-executor][match]");
3075 #endif
3076
3077 grn_obj *target = e->codes[0].value;
3078 grn_obj *value_buffer = &(executor->data.simple_match.value_buffer);
3079 GRN_VOID_INIT(value_buffer);
3080 grn_obj_reinit_for(ctx, value_buffer, target);
3081 }
3082
3083 static bool
3084 grn_expr_executor_is_simple_match(grn_ctx *ctx, grn_expr_executor *executor)
3085 {
3086 grn_expr *e = (grn_expr *)(executor->expr);
3087 grn_expr_code *target;
3088 grn_expr_code *sub_text;
3089 grn_expr_code *operator_code;
3090
3091 if (e->codes_curr != 3) {
3092 return false;
3093 }
3094
3095 target = &(e->codes[0]);
3096 sub_text = &(e->codes[1]);
3097 operator_code = &(e->codes[2]);
3098
3099 if (operator_code->op != GRN_OP_MATCH) {
3100 return false;
3101 }
3102 if (operator_code->nargs != 2) {
3103 return false;
3104 }
3105
3106 if (target->op != GRN_OP_GET_VALUE) {
3107 return false;
3108 }
3109 if (target->nargs != 1) {
3110 return false;
3111 }
3112 if (!target->value) {
3113 return false;
3114 }
3115 if (!(grn_obj_is_key_accessor(ctx, target->value) ||
3116 grn_obj_is_text_family_scalar_column(ctx, target->value))) {
3117 return false;
3118 }
3119
3120 if (sub_text->op != GRN_OP_PUSH) {
3121 return false;
3122 }
3123 if (sub_text->nargs != 1) {
3124 return false;
3125 }
3126 if (!grn_obj_is_text_family_bulk(ctx, sub_text->value)) {
3127 return false;
3128 }
3129
3130 #ifdef GRN_SUPPORT_REGEXP
3131 if (!grn_onigmo_is_valid_encoding(ctx)) {
3132 return false;
3133 }
3134 #endif
3135
3136 grn_expr_executor_init_simple_match(ctx, executor);
3137
3138 return true;
3139 }
3140
3141 static bool
3142 grn_expr_executor_exec_simple_match_have_sub_text(grn_ctx *ctx,
3143 grn_expr_executor *executor,
3144 const char *text,
3145 unsigned int text_len)
3146 {
3147 unsigned int sub_text_len =
3148 executor->data.simple_match.normalized_sub_text_raw_length_in_bytes;
3149
3150 if (sub_text_len > text_len) {
3151 return false;
3152 }
3153
3154 #ifdef GRN_SUPPORT_REGEXP
3155 {
3156 auto regex = static_cast<OnigRegex>(executor->data.simple_match.regex);
3157 auto target_start = reinterpret_cast<const OnigUChar *>(text);
3158 auto target_end = target_start + text_len;
3159 OnigPosition position;
3160 position = onig_search(regex,
3161 target_start,
3162 target_end,
3163 target_start,
3164 target_end,
3165 NULL,
3166 ONIG_OPTION_NONE);
3167 return (position != ONIG_MISMATCH);
3168 }
3169 #else /* GRN_SUPPORT_REGEXP */
3170 {
3171 grn_raw_string string;
3172 string.value = text;
3173 string.length = text_len;
3174 grn_raw_string sub_string;
3175 sub_string.value = executor->data.simple_match.normalized_sub_text_raw;
3176 sub_string.length = sub_text_len;
3177 return grn_raw_string_have_sub_string(ctx, &string, &sub_string);
3178 }
3179 #endif /* GRN_SUPPORT_REGEXP */
3180 }
3181
3182 static grn_obj *
3183 grn_expr_executor_exec_simple_match(grn_ctx *ctx,
3184 grn_expr_executor *executor,
3185 grn_id id)
3186 {
3187 grn_expr *e = (grn_expr *)(executor->expr);
3188 grn_obj *value_buffer = &(executor->data.simple_match.value_buffer);
3189 grn_obj *result_buffer = &(executor->data.simple_match.result_buffer);
3190
3191 if (ctx->rc != GRN_SUCCESS) {
3192 GRN_BOOL_SET(ctx, result_buffer, false);
3193 return result_buffer;
3194 }
3195
3196 if (executor->data.simple_match.normalized_sub_text_raw_length_in_bytes ==
3197 0) {
3198 return result_buffer;
3199 }
3200
3201 GRN_BULK_REWIND(value_buffer);
3202 grn_obj_get_value(ctx, e->codes[0].value, id, value_buffer);
3203 {
3204 grn_obj *norm_target;
3205 norm_target = grn_string_open(ctx,
3206 GRN_TEXT_VALUE(value_buffer),
3207 GRN_TEXT_LEN(value_buffer),
3208 executor->data.simple_match.normalizer,
3209 0);
3210 const char *norm_target_raw;
3211 unsigned int norm_target_raw_length_in_bytes;
3212 grn_string_get_normalized(ctx,
3213 norm_target,
3214 &norm_target_raw,
3215 &norm_target_raw_length_in_bytes,
3216 NULL);
3217 bool have_sub_text = grn_expr_executor_exec_simple_match_have_sub_text(
3218 ctx,
3219 executor,
3220 norm_target_raw,
3221 norm_target_raw_length_in_bytes);
3222 GRN_BOOL_SET(ctx, result_buffer, have_sub_text);
3223 grn_obj_close(ctx, norm_target);
3224 }
3225
3226 return result_buffer;
3227 }
3228
3229 static void
3230 grn_expr_executor_fin_simple_match(grn_ctx *ctx, grn_expr_executor *executor)
3231 {
3232 GRN_OBJ_FIN(ctx, &(executor->data.simple_match.result_buffer));
3233
3234 #ifdef GRN_SUPPORT_REGEXP
3235 if (executor->data.simple_match.regex) {
3236 onig_free(static_cast<OnigRegex>(executor->data.simple_match.regex));
3237 }
3238 #endif /* GRN_SUPPORT_REGEXP */
3239
3240 grn_obj_close(ctx, executor->data.simple_match.normalized_sub_text);
3241 GRN_OBJ_FIN(ctx, &(executor->data.simple_match.value_buffer));
3242 }
3243
3244 static bool
3245 grn_expr_executor_data_simple_proc_init(
3246 grn_ctx *ctx,
3247 grn_expr_executor *executor,
3248 grn_expr_executor_data_simple_proc *data)
3249 {
3250 grn_proc_ctx *proc_ctx = &(data->proc_ctx);
3251 grn_expr *expr;
3252 grn_proc *proc;
3253
3254 data->args = NULL;
3255 data->n_args = 0;
3256 data->buffers = NULL;
3257 data->n_buffers = 0;
3258
3259 expr = (grn_expr *)(executor->expr);
3260 proc = (grn_proc *)(expr->codes[data->codes_start_offset].value);
3261 proc_ctx->proc = proc;
3262 proc_ctx->caller = executor->expr;
3263
3264 int n_args = 0;
3265 int n_buffers = 0;
3266 {
3267 uint32_t i;
3268 for (i = data->codes_start_offset + 1;
3269 i < expr->codes_curr - data->codes_end_offset - 1;
3270 i++) {
3271 switch (expr->codes[i].op) {
3272 case GRN_OP_GET_VALUE:
3273 n_args++;
3274 n_buffers++;
3275 break;
3276 case GRN_OP_PUSH:
3277 n_args++;
3278 break;
3279 default:
3280 return false;
3281 }
3282 }
3283 }
3284
3285 grn_obj **args = GRN_MALLOCN(grn_obj *, n_args);
3286 if (!args) {
3287 return false;
3288 }
3289 data->args = args;
3290 data->n_args = n_args;
3291
3292 if (n_buffers > 0) {
3293 grn_obj *buffers = GRN_MALLOCN(grn_obj, n_buffers);
3294 if (!buffers) {
3295 GRN_FREE(args);
3296 return false;
3297 }
3298 int i;
3299 for (i = 0; i < n_buffers; i++) {
3300 GRN_VOID_INIT(&(buffers[i]));
3301 }
3302 data->buffers = buffers;
3303 } else {
3304 data->buffers = NULL;
3305 }
3306 data->n_buffers = n_buffers;
3307
3308 return true;
3309 }
3310
3311 static void
3312 grn_expr_executor_data_simple_proc_fin(grn_ctx *ctx,
3313 grn_expr_executor *executor,
3314 grn_expr_executor_data_simple_proc *data)
3315 {
3316 GRN_FREE(data->args);
3317 int n_buffers = data->n_buffers;
3318 if (n_buffers > 0) {
3319 grn_obj *buffers = data->buffers;
3320 int i;
3321 for (i = 0; i < n_buffers; i++) {
3322 GRN_OBJ_FIN(ctx, &(buffers[i]));
3323 }
3324 GRN_FREE(buffers);
3325 }
3326 }
3327
3328 static grn_obj *
3329 grn_expr_executor_data_simple_proc_exec(
3330 grn_ctx *ctx,
3331 grn_expr_executor *executor,
3332 grn_expr_executor_data_simple_proc *data,
3333 grn_id id)
3334 {
3335 grn_proc_ctx *proc_ctx = &(data->proc_ctx);
3336 grn_obj **args = data->args;
3337 grn_obj *buffers = data->buffers;
3338 int n_args = data->n_args;
3339 grn_proc *proc;
3340 grn_expr *expr;
3341 grn_obj *result = NULL;
3342
3343 proc = proc_ctx->proc;
3344 if (executor->variable) {
3345 GRN_RECORD_SET(ctx, executor->variable, id);
3346 }
3347
3348 expr = (grn_expr *)(executor->expr);
3349 {
3350 uint32_t i_expr;
3351 uint32_t i_buffer;
3352 uint32_t i_arg;
3353 for (i_expr = data->codes_start_offset + 1, i_buffer = 0, i_arg = 0;
3354 i_expr < expr->codes_curr - data->codes_end_offset - 1;
3355 i_expr++, i_arg++) {
3356 switch (expr->codes[i_expr].op) {
3357 case GRN_OP_GET_VALUE:
3358 {
3359 grn_obj *target = expr->codes[i_expr].value;
3360 bool found = false;
3361 auto n = GRN_RECORD_VECTOR_SIZE(&(executor->replace_ids));
3362 for (size_t i = 0; i < n; ++i) {
3363 auto target_id = GRN_RECORD_VALUE_AT(&(executor->replace_ids), i);
3364 if (target_id == DB_OBJ(target)->id) {
3365 args[i_arg] = GRN_PTR_VALUE_AT(&(executor->replace_values), i);
3366 found = true;
3367 break;
3368 }
3369 }
3370 if (!found) {
3371 grn_obj *buffer = &(buffers[i_buffer++]);
3372 GRN_BULK_REWIND(buffer);
3373 grn_obj_get_value(ctx, target, id, buffer);
3374 args[i_arg] = buffer;
3375 }
3376 break;
3377 }
3378 case GRN_OP_PUSH:
3379 args[i_arg] = expr->codes[i_expr].value;
3380 break;
3381 default:
3382 break;
3383 }
3384 }
3385 }
3386
3387 uint32_t values_curr = expr->values_curr;
3388 if (proc->funcs[PROC_INIT]) {
3389 proc_ctx->phase = PROC_INIT;
3390 grn_obj *sub_result =
3391 proc->funcs[PROC_INIT](ctx, n_args, args, &(proc_ctx->user_data));
3392 if (sub_result) {
3393 result = sub_result;
3394 }
3395 }
3396 if (proc->funcs[PROC_NEXT]) {
3397 proc_ctx->phase = PROC_NEXT;
3398 grn_obj *sub_result =
3399 proc->funcs[PROC_NEXT](ctx, n_args, args, &(proc_ctx->user_data));
3400 if (sub_result) {
3401 result = sub_result;
3402 }
3403 }
3404 if (proc->funcs[PROC_FIN]) {
3405 proc_ctx->phase = PROC_FIN;
3406 grn_obj *sub_result =
3407 proc->funcs[PROC_FIN](ctx, n_args, args, &(proc_ctx->user_data));
3408 if (sub_result) {
3409 result = sub_result;
3410 }
3411 }
3412 expr->values_curr = values_curr;
3413
3414 return result;
3415 }
3416
3417 static bool
3418 grn_expr_executor_init_simple_proc(grn_ctx *ctx, grn_expr_executor *executor)
3419 {
3420 grn_expr_executor_data_simple_proc *data = &(executor->data.simple_proc.data);
3421 data->codes_start_offset = 0;
3422 data->codes_end_offset = 0;
3423 return grn_expr_executor_data_simple_proc_init(ctx, executor, data);
3424 }
3425
3426 static bool
3427 grn_expr_executor_is_simple_proc(grn_ctx *ctx, grn_expr_executor *executor)
3428 {
3429 grn_expr *e = (grn_expr *)(executor->expr);
3430
3431 /*
3432 * 0: GRN_OP_PUSH(func)
3433 * n-1: GRN_OP_CALL(func)
3434 */
3435 if (e->codes_curr < 2) {
3436 return false;
3437 }
3438
3439 grn_expr_code *func = &(e->codes[0]);
3440 if (func->op != GRN_OP_PUSH) {
3441 return false;
3442 }
3443 if (!grn_obj_is_function_proc(ctx, func->value)) {
3444 return false;
3445 }
3446
3447 grn_proc *proc = (grn_proc *)(func->value);
3448 if (!proc->funcs[PROC_INIT] && !proc->funcs[PROC_NEXT] &&
3449 !proc->funcs[PROC_FIN]) {
3450 return false;
3451 }
3452
3453 if (func->modify != (int32_t)(e->codes_curr - 1)) {
3454 return false;
3455 }
3456 if (e->codes[func->modify].op != GRN_OP_CALL) {
3457 return false;
3458 }
3459
3460 return grn_expr_executor_init_simple_proc(ctx, executor);
3461 }
3462
3463 static grn_obj *
3464 grn_expr_executor_exec_simple_proc(grn_ctx *ctx,
3465 grn_expr_executor *executor,
3466 grn_id id)
3467 {
3468 grn_expr_executor_data_simple_proc *data = &(executor->data.simple_proc.data);
3469 return grn_expr_executor_data_simple_proc_exec(ctx, executor, data, id);
3470 }
3471
3472 static void
3473 grn_expr_executor_fin_simple_proc(grn_ctx *ctx, grn_expr_executor *executor)
3474 {
3475 grn_expr_executor_data_simple_proc *data = &(executor->data.simple_proc.data);
3476 grn_expr_executor_data_simple_proc_fin(ctx, executor, data);
3477 }
3478
3479 static bool
3480 grn_expr_executor_is_simple_condition_constant(grn_ctx *ctx,
3481 grn_expr_executor *executor)
3482 {
3483 grn_expr *e = (grn_expr *)(executor->expr);
3484 grn_expr_code *target;
3485 grn_expr_code *constant;
3486 grn_expr_code *operator_code;
3487 grn_id target_range;
3488 grn_id constant_range;
3489 bool constant_value_is_int = true;
3490 int64_t constant_value_int = 0;
3491 uint64_t constant_value_uint = 0;
3492 bool constant_always_over = false;
3493 bool constant_always_under = false;
3494
3495 if (e->codes_curr != 3) {
3496 return false;
3497 }
3498
3499 target = &(e->codes[0]);
3500 constant = &(e->codes[1]);
3501 operator_code = &(e->codes[2]);
3502
3503 switch (operator_code->op) {
3504 case GRN_OP_EQUAL:
3505 case GRN_OP_NOT_EQUAL:
3506 case GRN_OP_LESS:
3507 case GRN_OP_GREATER:
3508 case GRN_OP_LESS_EQUAL:
3509 case GRN_OP_GREATER_EQUAL:
3510 break;
3511 default:
3512 return false;
3513 }
3514 if (operator_code->nargs != 2) {
3515 return false;
3516 }
3517
3518 if (target->op != GRN_OP_GET_VALUE) {
3519 return false;
3520 }
3521 if (target->nargs != 1) {
3522 return false;
3523 }
3524 if (target->value->header.type != GRN_COLUMN_FIX_SIZE) {
3525 return false;
3526 }
3527
3528 target_range = grn_obj_get_range(ctx, target->value);
3529 if (!(GRN_DB_INT8 <= target_range && target_range <= GRN_DB_UINT64)) {
3530 return false;
3531 }
3532
3533 if (constant->op != GRN_OP_PUSH) {
3534 return false;
3535 }
3536 if (constant->nargs != 1) {
3537 return false;
3538 }
3539 if (!constant->value) {
3540 return false;
3541 }
3542 if (constant->value->header.type != GRN_BULK) {
3543 return false;
3544 }
3545 constant_range = constant->value->header.domain;
3546 if (!(GRN_DB_INT8 <= constant_range && constant_range <= GRN_DB_UINT64)) {
3547 return false;
3548 }
3549
3550 #define CASE_INT(N) \
3551 GRN_DB_INT##N : constant_value_is_int = true; \
3552 constant_value_int = GRN_INT##N##_VALUE(constant->value); \
3553 break
3554 #define CASE_UINT(N) \
3555 GRN_DB_UINT##N : constant_value_is_int = false; \
3556 constant_value_uint = GRN_UINT##N##_VALUE(constant->value); \
3557 break
3558
3559 switch (constant_range) {
3560 case CASE_INT(8); case CASE_UINT(8); case CASE_INT(16); case CASE_UINT(16);
3561 case CASE_INT(32); case CASE_UINT(32); case CASE_INT(64);
3562 case CASE_UINT(64); default:
3563 return false;
3564 }
3565
3566 #undef CASE_INT
3567 #undef CASE_UINT
3568
3569 #define CASE_INT(N) \
3570 GRN_DB_INT##N : if (constant_value_is_int) \
3571 { \
3572 if (constant_value_int > INT##N##_MAX) { \
3573 constant_always_over = true; \
3574 } else if (constant_value_int < INT##N##_MIN) { \
3575 constant_always_under = true; \
3576 } \
3577 } \
3578 else \
3579 { \
3580 if (constant_value_uint > INT##N##_MAX) { \
3581 constant_always_over = true; \
3582 } \
3583 } \
3584 break
3585 #define CASE_UINT(N) \
3586 GRN_DB_UINT##N : if (constant_value_is_int) \
3587 { \
3588 if (constant_value_int > UINT##N##_MAX) { \
3589 constant_always_over = true; \
3590 } else if (constant_value_int < 0) { \
3591 constant_always_under = true; \
3592 } \
3593 } \
3594 else \
3595 { \
3596 if (constant_value_uint > UINT##N##_MAX) { \
3597 constant_always_over = true; \
3598 } \
3599 } \
3600 break
3601
3602 switch (target_range) {
3603 case CASE_INT(8); case CASE_UINT(8); case CASE_INT(16); case CASE_UINT(16);
3604 case CASE_INT(32); case CASE_UINT(32); case CASE_INT(64);
3605 case GRN_DB_UINT64:
3606 if (constant_value_is_int && constant_value_int < 0) {
3607 constant_always_under = true;
3608 }
3609 break;
3610 default:
3611 return false;
3612 }
3613
3614 if (!constant_always_over && !constant_always_under) {
3615 return false;
3616 }
3617
3618 {
3619 bool result;
3620 grn_obj *result_buffer =
3621 &(executor->data.simple_condition_constant.result_buffer);
3622
3623 switch (operator_code->op) {
3624 case GRN_OP_EQUAL:
3625 result = false;
3626 break;
3627 case GRN_OP_NOT_EQUAL:
3628 result = true;
3629 break;
3630 case GRN_OP_LESS:
3631 case GRN_OP_LESS_EQUAL:
3632 result = constant_always_over;
3633 break;
3634 case GRN_OP_GREATER:
3635 case GRN_OP_GREATER_EQUAL:
3636 result = constant_always_under;
3637 break;
3638 default:
3639 return false;
3640 }
3641
3642 GRN_BOOL_INIT(result_buffer, 0);
3643 GRN_BOOL_SET(ctx, result_buffer, result);
3644
3645 return true;
3646 }
3647 }
3648
3649 static grn_obj *
3650 grn_expr_executor_exec_simple_condition_constant(grn_ctx *ctx,
3651 grn_expr_executor *executor,
3652 grn_id id)
3653 {
3654 grn_obj *result_buffer =
3655 &(executor->data.simple_condition_constant.result_buffer);
3656 return result_buffer;
3657 }
3658
3659 static void
3660 grn_expr_executor_fin_simple_condition_constant(grn_ctx *ctx,
3661 grn_expr_executor *executor)
3662 {
3663 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition_constant.result_buffer));
3664 }
3665
3666 static void
3667 grn_expr_executor_init_simple_condition_ra(grn_ctx *ctx,
3668 grn_expr_executor *executor)
3669 {
3670 grn_expr *e = (grn_expr *)(executor->expr);
3671 grn_obj *target;
3672 grn_obj *constant;
3673 grn_operator op;
3674 grn_obj *result_buffer;
3675 grn_obj *value_buffer;
3676 grn_obj *constant_buffer;
3677
3678 target = e->codes[0].value;
3679 constant = e->codes[1].value;
3680 op = e->codes[2].op;
3681
3682 result_buffer = &(executor->data.simple_condition_ra.result_buffer);
3683 GRN_BOOL_INIT(result_buffer, 0);
3684 GRN_BOOL_SET(ctx, result_buffer, false);
3685
3686 value_buffer = &(executor->data.simple_condition_ra.value_buffer);
3687 GRN_VOID_INIT(value_buffer);
3688 grn_obj_reinit_for(ctx, value_buffer, target);
3689
3690 executor->data.simple_condition_ra.ra = (grn_ra *)target;
3691 GRN_RA_CACHE_INIT(executor->data.simple_condition_ra.ra,
3692 &(executor->data.simple_condition_ra.ra_cache));
3693 grn_ra_info(ctx,
3694 executor->data.simple_condition_ra.ra,
3695 &(executor->data.simple_condition_ra.ra_element_size));
3696
3697 executor->data.simple_condition_ra.exec = grn_operator_to_exec_func(op);
3698
3699 constant_buffer = &(executor->data.simple_condition_ra.constant_buffer);
3700 if (grn_obj_is_reference_column(ctx, target)) {
3701 GRN_OBJ_INIT(constant_buffer, GRN_BULK, 0, constant->header.domain);
3702 grn_obj_cast(ctx, constant, constant_buffer, false);
3703 } else {
3704 GRN_VOID_INIT(constant_buffer);
3705 grn_obj_reinit_for(ctx, constant_buffer, target);
3706 grn_obj_cast(ctx, constant, constant_buffer, false);
3707 }
3708 }
3709
3710 static bool
3711 grn_expr_executor_is_simple_condition_ra(grn_ctx *ctx,
3712 grn_expr_executor *executor)
3713 {
3714 grn_expr *e = (grn_expr *)(executor->expr);
3715 grn_expr_code *target;
3716 grn_expr_code *constant;
3717 grn_expr_code *operator_code;
3718
3719 if (e->codes_curr != 3) {
3720 return false;
3721 }
3722
3723 target = &(e->codes[0]);
3724 constant = &(e->codes[1]);
3725 operator_code = &(e->codes[2]);
3726
3727 switch (operator_code->op) {
3728 case GRN_OP_EQUAL:
3729 case GRN_OP_NOT_EQUAL:
3730 case GRN_OP_LESS:
3731 case GRN_OP_GREATER:
3732 case GRN_OP_LESS_EQUAL:
3733 case GRN_OP_GREATER_EQUAL:
3734 break;
3735 default:
3736 return false;
3737 }
3738 if (operator_code->nargs != 2) {
3739 return false;
3740 }
3741
3742 if (target->op != GRN_OP_GET_VALUE) {
3743 return false;
3744 }
3745 if (target->nargs != 1) {
3746 return false;
3747 }
3748 if (target->value->header.type != GRN_COLUMN_FIX_SIZE) {
3749 return false;
3750 }
3751
3752 if (constant->op != GRN_OP_PUSH) {
3753 return false;
3754 }
3755 if (constant->nargs != 1) {
3756 return false;
3757 }
3758 if (!constant->value) {
3759 return false;
3760 }
3761 if (constant->value->header.type != GRN_BULK) {
3762 return false;
3763 }
3764
3765 if (!grn_obj_is_reference_column(ctx, target->value)) {
3766 grn_obj constant_buffer;
3767 grn_rc rc;
3768 GRN_VOID_INIT(&constant_buffer);
3769 grn_obj_reinit_for(ctx, &constant_buffer, target->value);
3770 rc = grn_obj_cast(ctx, constant->value, &constant_buffer, false);
3771 GRN_OBJ_FIN(ctx, &constant_buffer);
3772 if (rc != GRN_SUCCESS) {
3773 return false;
3774 }
3775 }
3776
3777 grn_expr_executor_init_simple_condition_ra(ctx, executor);
3778
3779 return true;
3780 }
3781
3782 static grn_obj *
3783 grn_expr_executor_exec_simple_condition_ra(grn_ctx *ctx,
3784 grn_expr_executor *executor,
3785 grn_id id)
3786 {
3787 grn_obj *result_buffer = &(executor->data.simple_condition_ra.result_buffer);
3788 grn_obj *value_buffer = &(executor->data.simple_condition_ra.value_buffer);
3789 grn_obj *constant_buffer =
3790 &(executor->data.simple_condition_ra.constant_buffer);
3791
3792 if (ctx->rc) {
3793 GRN_BOOL_SET(ctx, result_buffer, false);
3794 return result_buffer;
3795 }
3796
3797 {
3798 grn_ra *ra = executor->data.simple_condition_ra.ra;
3799 grn_ra_cache *ra_cache = &(executor->data.simple_condition_ra.ra_cache);
3800 unsigned int ra_element_size =
3801 executor->data.simple_condition_ra.ra_element_size;
3802 void *raw_value;
3803 raw_value = grn_ra_ref_cache(ctx, ra, id, ra_cache);
3804 GRN_BULK_REWIND(value_buffer);
3805 grn_bulk_write(ctx,
3806 value_buffer,
3807 static_cast<char *>(raw_value),
3808 ra_element_size);
3809 }
3810
3811 if (executor->data.simple_condition_ra.exec(ctx,
3812 value_buffer,
3813 constant_buffer)) {
3814 GRN_BOOL_SET(ctx, result_buffer, true);
3815 } else {
3816 GRN_BOOL_SET(ctx, result_buffer, false);
3817 }
3818 return result_buffer;
3819 }
3820
3821 static void
3822 grn_expr_executor_fin_simple_condition_ra(grn_ctx *ctx,
3823 grn_expr_executor *executor)
3824 {
3825 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition_ra.result_buffer));
3826 GRN_RA_CACHE_FIN(ctx,
3827 executor->data.simple_condition_ra.ra,
3828 &(executor->data.simple_condition_ra.ra_cache));
3829 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition_ra.value_buffer));
3830 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition_ra.constant_buffer));
3831 }
3832
3833 static void
3834 grn_expr_executor_init_simple_condition(grn_ctx *ctx,
3835 grn_expr_executor *executor)
3836 {
3837 grn_expr *e = (grn_expr *)(executor->expr);
3838 grn_obj *target;
3839 grn_obj *constant;
3840 grn_operator op;
3841 grn_obj *result_buffer;
3842 grn_obj *value_buffer;
3843 grn_obj *constant_buffer;
3844 grn_rc rc;
3845
3846 target = e->codes[0].value;
3847 constant = e->codes[1].value;
3848 op = e->codes[2].op;
3849
3850 executor->data.simple_condition.need_exec = true;
3851
3852 result_buffer = &(executor->data.simple_condition.result_buffer);
3853 GRN_BOOL_INIT(result_buffer, 0);
3854 GRN_BOOL_SET(ctx, result_buffer, false);
3855
3856 value_buffer = &(executor->data.simple_condition.value_buffer);
3857 GRN_VOID_INIT(value_buffer);
3858 grn_obj_reinit_for(ctx, value_buffer, target);
3859
3860 executor->data.simple_condition.exec = grn_operator_to_exec_func(op);
3861
3862 constant_buffer = &(executor->data.simple_condition.constant_buffer);
3863 GRN_VOID_INIT(constant_buffer);
3864 grn_obj_reinit_for(ctx, constant_buffer, target);
3865 rc = grn_obj_cast(ctx, constant, constant_buffer, false);
3866 if (rc != GRN_SUCCESS) {
3867 grn_obj *type;
3868
3869 type = grn_ctx_at(ctx, constant_buffer->header.domain);
3870 if (grn_obj_is_table(ctx, type)) {
3871 GRN_BOOL_SET(ctx, result_buffer, (op == GRN_OP_NOT_EQUAL));
3872 executor->data.simple_condition.need_exec = false;
3873 } else {
3874 int type_name_size;
3875 char type_name[GRN_TABLE_MAX_KEY_SIZE];
3876 grn_obj inspected;
3877
3878 type_name_size =
3879 grn_obj_name(ctx, type, type_name, GRN_TABLE_MAX_KEY_SIZE);
3880 GRN_TEXT_INIT(&inspected, 0);
3881 grn_inspect(ctx, &inspected, constant);
3882 ERR(rc,
3883 "[expr-executor][condition] "
3884 "failed to cast to <%.*s>: <%.*s>",
3885 type_name_size,
3886 type_name,
3887 (int)GRN_TEXT_LEN(&inspected),
3888 GRN_TEXT_VALUE(&inspected));
3889 }
3890 }
3891 }
3892
3893 static bool
3894 grn_expr_executor_is_simple_condition(grn_ctx *ctx, grn_expr_executor *executor)
3895 {
3896 grn_expr *e = (grn_expr *)(executor->expr);
3897 grn_expr_code *target;
3898 grn_expr_code *constant;
3899 grn_expr_code *operator_code;
3900
3901 if (e->codes_curr != 3) {
3902 return false;
3903 }
3904
3905 target = &(e->codes[0]);
3906 constant = &(e->codes[1]);
3907 operator_code = &(e->codes[2]);
3908
3909 switch (operator_code->op) {
3910 case GRN_OP_EQUAL:
3911 case GRN_OP_NOT_EQUAL:
3912 case GRN_OP_LESS:
3913 case GRN_OP_GREATER:
3914 case GRN_OP_LESS_EQUAL:
3915 case GRN_OP_GREATER_EQUAL:
3916 break;
3917 default:
3918 return false;
3919 }
3920 if (operator_code->nargs != 2) {
3921 return false;
3922 }
3923
3924 if (target->op != GRN_OP_GET_VALUE) {
3925 return false;
3926 }
3927 if (target->nargs != 1) {
3928 return false;
3929 }
3930 if (!grn_obj_is_scalar_column(ctx, target->value)) {
3931 return false;
3932 }
3933
3934 if (constant->op != GRN_OP_PUSH) {
3935 return false;
3936 }
3937 if (constant->nargs != 1) {
3938 return false;
3939 }
3940 if (!constant->value) {
3941 return false;
3942 }
3943 if (constant->value->header.type != GRN_BULK) {
3944 return false;
3945 }
3946
3947 grn_expr_executor_init_simple_condition(ctx, executor);
3948
3949 return true;
3950 }
3951
3952 static grn_obj *
3953 grn_expr_executor_exec_simple_condition(grn_ctx *ctx,
3954 grn_expr_executor *executor,
3955 grn_id id)
3956 {
3957 grn_expr *e = (grn_expr *)(executor->expr);
3958 grn_obj *target;
3959 grn_obj *result_buffer = &(executor->data.simple_condition.result_buffer);
3960 grn_obj *value_buffer = &(executor->data.simple_condition.value_buffer);
3961 grn_obj *constant_buffer = &(executor->data.simple_condition.constant_buffer);
3962
3963 if (ctx->rc) {
3964 GRN_BOOL_SET(ctx, result_buffer, false);
3965 return result_buffer;
3966 }
3967
3968 if (!executor->data.simple_condition.need_exec) {
3969 return result_buffer;
3970 }
3971
3972 target = e->codes[0].value;
3973 GRN_BULK_REWIND(value_buffer);
3974 grn_obj_get_value(ctx, target, id, value_buffer);
3975
3976 if (executor->data.simple_condition.exec(ctx,
3977 value_buffer,
3978 constant_buffer)) {
3979 GRN_BOOL_SET(ctx, result_buffer, true);
3980 } else {
3981 GRN_BOOL_SET(ctx, result_buffer, false);
3982 }
3983 return result_buffer;
3984 }
3985
3986 static void
3987 grn_expr_executor_fin_simple_condition(grn_ctx *ctx,
3988 grn_expr_executor *executor)
3989 {
3990 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition.result_buffer));
3991 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition.value_buffer));
3992 GRN_OBJ_FIN(ctx, &(executor->data.simple_condition.constant_buffer));
3993 }
3994
3995 static bool
3996 grn_expr_executor_init_simple_proc_scorer(grn_ctx *ctx,
3997 grn_expr_executor *executor)
3998 {
3999 grn_expr_executor_data_simple_proc *data =
4000 &(executor->data.simple_proc_scorer.data);
4001 /* The first GRN_OP_GET_REF for _score */
4002 data->codes_start_offset = 1;
4003 /* The last GRN_OP_ASSIGN */
4004 data->codes_end_offset = 1;
4005 return grn_expr_executor_data_simple_proc_init(ctx, executor, data);
4006 }
4007
4008 static void
4009 grn_expr_executor_fin_simple_proc_scorer(grn_ctx *ctx,
4010 grn_expr_executor *executor)
4011 {
4012 grn_expr_executor_data_simple_proc *data =
4013 &(executor->data.simple_proc_scorer.data);
4014 grn_expr_executor_data_simple_proc_fin(ctx, executor, data);
4015 }
4016
4017 static bool
4018 grn_expr_executor_is_simple_proc_scorer(grn_ctx *ctx,
4019 grn_expr_executor *executor)
4020 {
4021 grn_expr *e = (grn_expr *)(executor->expr);
4022
4023 /*
4024 * 0: GRN_OP_GET_REF(_score)
4025 * 1: GRN_OP_PUSH(func)
4026 * n-2: GRN_OP_CALL(func)
4027 * n-1: GRN_OP_ASSIGN(_score)
4028 */
4029 if (e->codes_curr < 4) {
4030 return false;
4031 }
4032
4033 grn_expr_code *score = &(e->codes[0]);
4034 if (score->op != GRN_OP_GET_REF) {
4035 return false;
4036 }
4037 if (!grn_obj_is_score_accessor(ctx, score->value)) {
4038 return false;
4039 }
4040 executor->data.simple_proc_scorer.score_column = score->value;
4041
4042 if (score->modify != (int32_t)(e->codes_curr - 1)) {
4043 return false;
4044 }
4045 grn_expr_code *operation = &(e->codes[score->modify]);
4046 if (operation->op != GRN_OP_ASSIGN) {
4047 return false;
4048 }
4049
4050 const size_t func_index = 1;
4051 grn_expr_code *func = &(e->codes[func_index]);
4052 if (func->op != GRN_OP_PUSH) {
4053 return false;
4054 }
4055 if (!grn_obj_is_function_proc(ctx, func->value)) {
4056 return false;
4057 }
4058 grn_proc *proc = (grn_proc *)(func->value);
4059 if (!proc->funcs[PROC_INIT] && !proc->funcs[PROC_NEXT] &&
4060 !proc->funcs[PROC_FIN]) {
4061 return false;
4062 }
4063
4064 if ((func->modify + func_index) != e->codes_curr - 2) {
4065 return false;
4066 }
4067 if (e->codes[func->modify + func_index].op != GRN_OP_CALL) {
4068 return false;
4069 }
4070
4071 if (!grn_expr_executor_init_simple_proc_scorer(ctx, executor)) {
4072 grn_expr_executor_fin_simple_proc_scorer(ctx, executor);
4073 return false;
4074 }
4075
4076 return true;
4077 }
4078
4079 static grn_obj *
4080 grn_expr_executor_exec_simple_proc_scorer(grn_ctx *ctx,
4081 grn_expr_executor *executor,
4082 grn_id id)
4083 {
4084 grn_expr_executor_data_simple_proc *data =
4085 &(executor->data.simple_proc_scorer.data);
4086 grn_obj *score =
4087 grn_expr_executor_data_simple_proc_exec(ctx, executor, data, id);
4088 if (score) {
4089 grn_obj *score_column = executor->data.simple_proc_scorer.score_column;
4090 grn_obj_set_value(ctx, score_column, id, score, GRN_OBJ_SET);
4091 }
4092 return score;
4093 }
4094
4095 static double
4096 grn_expr_executor_scorer_get_value(grn_ctx *ctx,
4097 grn_expr_executor *executor,
4098 grn_id id,
4099 grn_obj *args,
4100 grn_expr_executor_scorer_data *data)
4101 {
4102 grn_obj *source_buffer = &(data->source_buffer);
4103 grn_obj *score_buffer = &(executor->data.scorer.score_buffer);
4104 if (data->cache) {
4105 size_t size;
4106 void *value = grn_column_cache_ref(ctx, data->cache, id, &size);
4107 if (size == 0) {
4108 return 0.0;
4109 }
4110 if (source_buffer->header.domain == GRN_DB_FLOAT) {
4111 return *((double *)value);
4112 }
4113 GRN_TEXT_SET(ctx, source_buffer, value, size);
4114 } else {
4115 GRN_BULK_REWIND(source_buffer);
4116 grn_obj_get_value(ctx, data->source, id, source_buffer);
4117 if (GRN_BULK_VSIZE(source_buffer) == 0) {
4118 return 0.0;
4119 }
4120 if (source_buffer->header.domain == GRN_DB_FLOAT) {
4121 return GRN_FLOAT_VALUE(source_buffer);
4122 }
4123 }
4124 GRN_BULK_REWIND(score_buffer);
4125 grn_rc rc = grn_obj_cast(ctx, source_buffer, score_buffer, true);
4126 if (rc == GRN_SUCCESS) {
4127 return GRN_FLOAT_VALUE(score_buffer);
4128 } else {
4129 return 0.0;
4130 }
4131 }
4132
4133 static double
4134 grn_expr_executor_scorer_push(grn_ctx *ctx,
4135 grn_expr_executor *executor,
4136 grn_id id,
4137 grn_obj *args,
4138 grn_expr_executor_scorer_data *data)
4139 {
4140 grn_obj *source = data->source;
4141 grn_obj *score_buffer = &(executor->data.scorer.score_buffer);
4142 if (GRN_BULK_VSIZE(source) == 0) {
4143 return 0.0;
4144 }
4145 if (source->header.domain == GRN_DB_FLOAT) {
4146 return GRN_FLOAT_VALUE(source);
4147 }
4148 GRN_BULK_REWIND(score_buffer);
4149 grn_rc rc = grn_obj_cast(ctx, source, score_buffer, true);
4150 if (rc == GRN_SUCCESS) {
4151 return GRN_FLOAT_VALUE(score_buffer);
4152 } else {
4153 return 0.0;
4154 }
4155 }
4156
4157 static double
4158 grn_expr_executor_scorer_star(grn_ctx *ctx,
4159 grn_expr_executor *executor,
4160 grn_id id,
4161 grn_obj *args,
4162 grn_expr_executor_scorer_data *data)
4163 {
4164 double right;
4165 GRN_FLOAT_POP(args, right);
4166 double left;
4167 GRN_FLOAT_POP(args, left);
4168 return left * right;
4169 }
4170
4171 static double
4172 grn_expr_executor_scorer_plus(grn_ctx *ctx,
4173 grn_expr_executor *executor,
4174 grn_id id,
4175 grn_obj *args,
4176 grn_expr_executor_scorer_data *data)
4177 {
4178 double right;
4179 GRN_FLOAT_POP(args, right);
4180 double left;
4181 GRN_FLOAT_POP(args, left);
4182 return left + right;
4183 }
4184
4185 static double
4186 grn_expr_executor_scorer_minus(grn_ctx *ctx,
4187 grn_expr_executor *executor,
4188 grn_id id,
4189 grn_obj *args,
4190 grn_expr_executor_scorer_data *data)
4191 {
4192 double right;
4193 GRN_FLOAT_POP(args, right);
4194 double left;
4195 GRN_FLOAT_POP(args, left);
4196 return left - right;
4197 }
4198
4199 static bool
4200 grn_expr_executor_init_scorer(grn_ctx *ctx, grn_expr_executor *executor)
4201 {
4202 grn_expr *e = (grn_expr *)(executor->expr);
4203
4204 GRN_FLOAT_INIT(&(executor->data.scorer.args), GRN_OBJ_VECTOR);
4205 GRN_FLOAT_INIT(&(executor->data.scorer.score_buffer), 0);
4206
4207 executor->data.scorer.n_funcs = 0;
4208 /* The first GRN_OP_GET_REF and the last GRN_OP_ASSIGN are needless. */
4209 size_t n_allocated_funcs = e->codes_curr - 2;
4210 executor->data.scorer.funcs =
4211 GRN_MALLOCN(grn_expr_executor_scorer_func, n_allocated_funcs);
4212 if (!executor->data.scorer.funcs) {
4213 return false;
4214 }
4215 executor->data.scorer.datas =
4216 GRN_MALLOCN(grn_expr_executor_scorer_data, n_allocated_funcs);
4217 if (!executor->data.scorer.datas) {
4218 GRN_FREE(executor->data.scorer.funcs);
4219 return false;
4220 }
4221 executor->data.scorer.n_allocated_funcs = n_allocated_funcs;
4222 {
4223 size_t i;
4224 for (i = 0; i < executor->data.scorer.n_allocated_funcs; i++) {
4225 executor->data.scorer.funcs[i] = NULL;
4226 GRN_VOID_INIT(&(executor->data.scorer.datas[i].source_buffer));
4227 executor->data.scorer.datas[i].source = NULL;
4228 executor->data.scorer.datas[i].cache = NULL;
4229 executor->data.scorer.datas[i].n_required_args = 0;
4230 }
4231 }
4232
4233 {
4234 uint32_t i;
4235 for (i = 1; i < (e->codes_curr - 1); i++) {
4236 grn_expr_code *code = &(e->codes[i]);
4237 size_t nth_func = executor->data.scorer.n_funcs;
4238 grn_expr_executor_scorer_data *data =
4239 &(executor->data.scorer.datas[nth_func]);
4240 switch (code->op) {
4241 case GRN_OP_GET_VALUE:
4242 {
4243 data->source = code->value;
4244 data->cache = grn_column_cache_open(ctx, data->source);
4245 grn_id range = grn_obj_get_range(ctx, data->source);
4246 grn_obj_reinit(ctx, &(data->source_buffer), range, 0);
4247 if (data->cache) {
4248 data->source_buffer.header.impl_flags = GRN_OBJ_DO_SHALLOW_COPY;
4249 }
4250 executor->data.scorer.funcs[nth_func] =
4251 grn_expr_executor_scorer_get_value;
4252 break;
4253 }
4254 case GRN_OP_PUSH:
4255 data->source = code->value;
4256 executor->data.scorer.funcs[nth_func] = grn_expr_executor_scorer_push;
4257 break;
4258 case GRN_OP_STAR:
4259 data->n_required_args = 2;
4260 executor->data.scorer.funcs[nth_func] = grn_expr_executor_scorer_star;
4261 break;
4262 case GRN_OP_PLUS:
4263 data->n_required_args = 2;
4264 executor->data.scorer.funcs[nth_func] = grn_expr_executor_scorer_plus;
4265 break;
4266 case GRN_OP_MINUS:
4267 data->n_required_args = 2;
4268 executor->data.scorer.funcs[nth_func] = grn_expr_executor_scorer_minus;
4269 break;
4270 default:
4271 return false;
4272 }
4273 executor->data.scorer.n_funcs++;
4274 }
4275 }
4276
4277 return true;
4278 }
4279
4280 static void
4281 grn_expr_executor_fin_scorer(grn_ctx *ctx, grn_expr_executor *executor)
4282 {
4283 GRN_OBJ_FIN(ctx, &(executor->data.scorer.args));
4284 GRN_OBJ_FIN(ctx, &(executor->data.scorer.score_buffer));
4285
4286 size_t n_allocated_funcs = executor->data.scorer.n_allocated_funcs;
4287 if (n_allocated_funcs == 0) {
4288 return;
4289 }
4290
4291 size_t i;
4292 for (i = 0; i < n_allocated_funcs; i++) {
4293 grn_expr_executor_scorer_data *data = &(executor->data.scorer.datas[i]);
4294 GRN_OBJ_FIN(ctx, &(data->source_buffer));
4295 if (data->cache) {
4296 grn_column_cache_close(ctx, data->cache);
4297 }
4298 }
4299 GRN_FREE(executor->data.scorer.funcs);
4300 GRN_FREE(executor->data.scorer.datas);
4301 }
4302
4303 static bool
4304 grn_expr_executor_is_scorer(grn_ctx *ctx, grn_expr_executor *executor)
4305 {
4306 grn_expr *e = (grn_expr *)(executor->expr);
4307
4308 if (e->codes_curr < 2) {
4309 return false;
4310 }
4311
4312 grn_expr_code *score = &(e->codes[0]);
4313 if (score->op != GRN_OP_GET_REF) {
4314 return false;
4315 }
4316 if (!grn_obj_is_score_accessor(ctx, score->value)) {
4317 return false;
4318 }
4319 executor->data.scorer.score_column = score->value;
4320
4321 grn_expr_code *operation = &(e->codes[score->modify]);
4322 if (operation->op != GRN_OP_ASSIGN) {
4323 return false;
4324 }
4325
4326 if (!grn_expr_executor_init_scorer(ctx, executor)) {
4327 grn_expr_executor_fin_scorer(ctx, executor);
4328 return false;
4329 }
4330
4331 return true;
4332 }
4333
4334 static grn_obj *
4335 grn_expr_executor_exec_scorer(grn_ctx *ctx,
4336 grn_expr_executor *executor,
4337 grn_id id)
4338 {
4339 double score = 0.0;
4340 grn_obj *args = &(executor->data.scorer.args);
4341 GRN_BULK_REWIND(args);
4342 size_t n_funcs = executor->data.scorer.n_funcs;
4343 size_t i;
4344 for (i = 0; i < n_funcs; i++) {
4345 grn_expr_executor_scorer_func func = executor->data.scorer.funcs[i];
4346 grn_expr_executor_scorer_data *data = &(executor->data.scorer.datas[i]);
4347 double value = func(ctx, executor, id, args, data);
4348 GRN_FLOAT_PUT(ctx, args, value);
4349 score = value;
4350 }
4351 grn_obj *score_column = executor->data.scorer.score_column;
4352 grn_obj *score_buffer = &(executor->data.scorer.score_buffer);
4353 GRN_FLOAT_SET(ctx, score_buffer, score);
4354 grn_obj_set_value(ctx, score_column, id, score_buffer, GRN_OBJ_SET);
4355
4356 return score_buffer;
4357 }
4358
4359 extern "C" {
4360 grn_rc
4361 grn_expr_executor_init(grn_ctx *ctx, grn_expr_executor *executor, grn_obj *expr)
4362 {
4363 GRN_API_ENTER;
4364
4365 if (!grn_obj_is_expr(ctx, expr)) {
4366 grn_rc rc = ctx->rc;
4367 grn_obj inspected;
4368 GRN_TEXT_INIT(&inspected, 0);
4369 grn_inspect(ctx, &inspected, expr);
4370 if (rc == GRN_SUCCESS) {
4371 rc = GRN_INVALID_ARGUMENT;
4372 }
4373 ERR(rc,
4374 "[expr-executor][init] invalid expression: %.*s",
4375 (int)GRN_TEXT_LEN(&inspected),
4376 GRN_TEXT_VALUE(&inspected));
4377 GRN_OBJ_FIN(ctx, &inspected);
4378 GRN_API_RETURN(ctx->rc);
4379 }
4380
4381 executor->expr = expr;
4382 executor->variable = grn_expr_get_var_by_offset(ctx, expr, 0);
4383 GRN_RECORD_INIT(&(executor->replace_ids), GRN_OBJ_VECTOR, GRN_ID_NIL);
4384 GRN_PTR_INIT(&(executor->replace_values), GRN_OBJ_VECTOR, GRN_ID_NIL);
4385 if (grn_expr_executor_is_constant(ctx, executor)) {
4386 executor->exec = grn_expr_executor_exec_constant;
4387 executor->fin = grn_expr_executor_fin_constant;
4388 } else if (grn_expr_executor_is_value(ctx, executor)) {
4389 executor->exec = grn_expr_executor_exec_value;
4390 executor->fin = grn_expr_executor_fin_value;
4391 #ifdef GRN_SUPPORT_REGEXP
4392 } else if (grn_expr_executor_is_simple_regexp(ctx, executor)) {
4393 executor->exec = grn_expr_executor_exec_simple_regexp;
4394 executor->fin = grn_expr_executor_fin_simple_regexp;
4395 #endif /* GRN_SUPPORT_REGEXP */
4396 } else if (grn_expr_executor_is_simple_match(ctx, executor)) {
4397 executor->exec = grn_expr_executor_exec_simple_match;
4398 executor->fin = grn_expr_executor_fin_simple_match;
4399 } else if (grn_expr_executor_is_simple_proc(ctx, executor)) {
4400 executor->exec = grn_expr_executor_exec_simple_proc;
4401 executor->fin = grn_expr_executor_fin_simple_proc;
4402 } else if (grn_expr_executor_is_simple_condition_constant(ctx, executor)) {
4403 executor->exec = grn_expr_executor_exec_simple_condition_constant;
4404 executor->fin = grn_expr_executor_fin_simple_condition_constant;
4405 } else if (grn_expr_executor_is_simple_condition_ra(ctx, executor)) {
4406 executor->exec = grn_expr_executor_exec_simple_condition_ra;
4407 executor->fin = grn_expr_executor_fin_simple_condition_ra;
4408 } else if (grn_expr_executor_is_simple_condition(ctx, executor)) {
4409 executor->exec = grn_expr_executor_exec_simple_condition;
4410 executor->fin = grn_expr_executor_fin_simple_condition;
4411 } else if (grn_expr_executor_is_simple_proc_scorer(ctx, executor)) {
4412 executor->exec = grn_expr_executor_exec_simple_proc_scorer;
4413 executor->fin = grn_expr_executor_fin_simple_proc_scorer;
4414 } else if (grn_expr_executor_is_scorer(ctx, executor)) {
4415 executor->exec = grn_expr_executor_exec_scorer;
4416 executor->fin = grn_expr_executor_fin_scorer;
4417 } else {
4418 grn_expr_executor_init_general(ctx, executor);
4419 executor->exec = grn_expr_executor_exec_general;
4420 executor->fin = grn_expr_executor_fin_general;
4421 }
4422
4423 GRN_API_RETURN(ctx->rc);
4424 }
4425
4426 grn_rc
4427 grn_expr_executor_fin(grn_ctx *ctx, grn_expr_executor *executor)
4428 {
4429 GRN_API_ENTER;
4430
4431 if (!executor) {
4432 GRN_API_RETURN(GRN_SUCCESS);
4433 }
4434
4435 executor->fin(ctx, executor);
4436
4437 GRN_API_RETURN(GRN_SUCCESS);
4438 }
4439
4440 grn_expr_executor *
4441 grn_expr_executor_open(grn_ctx *ctx, grn_obj *expr)
4442 {
4443 grn_expr_executor *executor;
4444
4445 GRN_API_ENTER;
4446
4447 executor =
4448 static_cast<grn_expr_executor *>(GRN_CALLOC(sizeof(grn_expr_executor)));
4449 if (!executor) {
4450 char errbuf[GRN_CTX_MSGSIZE];
4451 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
4452 ERR(ctx->rc, "[expr-executor][open] failed to allocate: %s", errbuf);
4453 GRN_API_RETURN(NULL);
4454 }
4455
4456 grn_expr_executor_init(ctx, executor, expr);
4457
4458 if (ctx->rc != GRN_SUCCESS) {
4459 GRN_FREE(executor);
4460 executor = NULL;
4461 }
4462
4463 GRN_API_RETURN(executor);
4464 }
4465
4466 grn_rc
4467 grn_expr_executor_close(grn_ctx *ctx, grn_expr_executor *executor)
4468 {
4469 GRN_API_ENTER;
4470
4471 if (!executor) {
4472 GRN_API_RETURN(GRN_SUCCESS);
4473 }
4474
4475 grn_expr_executor_fin(ctx, executor);
4476 GRN_FREE(executor);
4477
4478 GRN_API_RETURN(GRN_SUCCESS);
4479 }
4480
4481 grn_rc
4482 grn_expr_executor_add_replace_value(grn_ctx *ctx,
4483 grn_expr_executor *executor,
4484 grn_id id,
4485 grn_obj *value)
4486 {
4487 GRN_API_ENTER;
4488
4489 if (!executor) {
4490 GRN_API_RETURN(GRN_SUCCESS);
4491 }
4492
4493 GRN_RECORD_PUT(ctx, &(executor->replace_ids), id);
4494 GRN_PTR_PUT(ctx, &(executor->replace_values), value);
4495
4496 GRN_API_RETURN(GRN_SUCCESS);
4497 }
4498
4499 grn_obj *
4500 grn_expr_executor_exec(grn_ctx *ctx, grn_expr_executor *executor, grn_id id)
4501 {
4502 grn_obj *value;
4503
4504 GRN_API_ENTER;
4505
4506 if (!executor) {
4507 GRN_API_RETURN(NULL);
4508 }
4509
4510 value = executor->exec(ctx, executor, id);
4511 if (ctx->rc != GRN_SUCCESS) {
4512 value = NULL;
4513 }
4514
4515 GRN_API_RETURN(value);
4516 }
4517 }