Debian Codegraph @

lib/proc/proc_in_records.c

raw symbols SCIP
1 /*
2 Copyright (C) 2017-2018 Brazil
3 Copyright (C) 2022 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_proc.h"
21 #include "../grn_db.h"
22 #include "../grn_expr.h"
23 #include "../grn_store.h"
24
25 #include <groonga/plugin.h>
26
27 typedef struct {
28 size_t n_conditions;
29 grn_obj *condition_table;
30 grn_obj condition_columns;
31 grn_operator *condition_modes;
32 grn_obj *search_result;
33 } grn_in_records_data;
34
35 static void
36 grn_in_records_data_free(grn_ctx *ctx, grn_in_records_data *data)
37 {
38 size_t i;
39 size_t n_condition_columns;
40
41 if (!data) {
42 return;
43 }
44
45 GRN_PLUGIN_FREE(ctx, data->condition_modes);
46
47 n_condition_columns =
48 GRN_BULK_VSIZE(&(data->condition_columns)) / sizeof(grn_obj *);
49 for (i = 0; i < n_condition_columns; i++) {
50 grn_obj *condition_column;
51 condition_column = GRN_PTR_VALUE_AT(&(data->condition_columns), i);
52 if (condition_column && condition_column->header.type == GRN_ACCESSOR) {
53 grn_obj_unlink(ctx, condition_column);
54 }
55 }
56 GRN_OBJ_FIN(ctx, &(data->condition_columns));
57
58 if (data->search_result) {
59 grn_obj_close(ctx, data->search_result);
60 }
61
62 GRN_PLUGIN_FREE(ctx, data);
63 }
64
65 static grn_obj *
66 func_in_records_init(grn_ctx *ctx,
67 int n_args,
68 grn_obj **args,
69 grn_user_data *user_data)
70 {
71 grn_in_records_data *data;
72 grn_obj *condition_table;
73 grn_expr_code *codes;
74 size_t n_arg_codes;
75 size_t n_logical_args;
76 size_t n_conditions;
77 size_t i;
78 size_t nth;
79
80 {
81 grn_obj *caller;
82 grn_expr *expr;
83 grn_expr_code *call_code;
84
85 caller = grn_plugin_proc_get_caller(ctx, user_data);
86 expr = (grn_expr *)caller;
87 call_code = expr->codes + expr->codes_curr - 1;
88 n_logical_args = (size_t)(call_code->nargs - 1);
89 codes = expr->codes + 1;
90 n_arg_codes = expr->codes_curr - 2;
91 }
92
93 if (n_logical_args < 4) {
94 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
95 "in_records(): wrong number of arguments "
96 "(%" GRN_FMT_SIZE " for 4..)",
97 n_logical_args);
98 return NULL;
99 }
100
101 if ((n_logical_args % 3) != 1) {
102 GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
103 "in_records(): the number of arguments "
104 "must be 1 + 3n (%" GRN_FMT_SIZE ")",
105 n_logical_args);
106 return NULL;
107 }
108
109 n_conditions = (size_t)((n_logical_args - 1) / 3);
110
111 condition_table = codes[0].value;
112 if (!grn_obj_is_table(ctx, condition_table)) {
113 grn_obj inspected;
114 GRN_TEXT_INIT(&inspected, 0);
115 grn_inspect(ctx, &inspected, condition_table);
116 GRN_PLUGIN_ERROR(ctx,
117 GRN_INVALID_ARGUMENT,
118 "in_records(): the first argument must be a table: <%.*s>",
119 (int)GRN_TEXT_LEN(&inspected),
120 GRN_TEXT_VALUE(&inspected));
121 GRN_OBJ_FIN(ctx, &inspected);
122 return NULL;
123 }
124
125 data = GRN_PLUGIN_CALLOC(ctx, sizeof(grn_in_records_data));
126 if (!data) {
127 GRN_PLUGIN_ERROR(ctx,
128 GRN_INVALID_ARGUMENT,
129 "in_records(): failed to allocate internal data");
130 return NULL;
131 }
132 user_data->ptr = data;
133
134 data->n_conditions = n_conditions;
135 data->condition_table = condition_table;
136 GRN_PTR_INIT(&(data->condition_columns), GRN_OBJ_VECTOR, GRN_ID_NIL);
137 data->condition_modes = GRN_PLUGIN_MALLOCN(ctx, grn_operator, n_conditions);
138 if (!data->condition_modes) {
139 GRN_PLUGIN_ERROR(ctx,
140 GRN_INVALID_ARGUMENT,
141 "in_records(): "
142 "failed to allocate internal data for condition modes");
143 goto exit;
144 }
145
146 for (i = 1, nth = 0; i < n_arg_codes; nth++) {
147 int value_i = (int)i;
148 int mode_name_i;
149 grn_obj *mode_name;
150 int column_name_i;
151 grn_obj *column_name;
152 grn_obj *condition_column;
153
154 value_i += codes[value_i].modify;
155
156 mode_name_i = value_i + 1;
157 mode_name = codes[mode_name_i].value;
158 data->condition_modes[nth] = grn_proc_get_value_mode(ctx,
159 mode_name,
160 GRN_OP_EQUAL,
161 "in_records()");
162 if (ctx->rc != GRN_SUCCESS) {
163 goto exit;
164 }
165
166 column_name_i = mode_name_i + 1;
167 column_name = codes[column_name_i].value;
168 if (!grn_obj_is_text_family_bulk(ctx, column_name)) {
169 grn_obj inspected;
170 GRN_TEXT_INIT(&inspected, 0);
171 grn_inspect(ctx, &inspected, condition_table);
172 GRN_PLUGIN_ERROR(ctx,
173 GRN_INVALID_ARGUMENT,
174 "in_records(): "
175 "the %dth argument must be column name as string: "
176 "<%.*s>",
177 column_name_i,
178 (int)GRN_TEXT_LEN(&inspected),
179 GRN_TEXT_VALUE(&inspected));
180 GRN_OBJ_FIN(ctx, &inspected);
181 goto exit;
182 }
183
184 condition_column = grn_obj_column(ctx, condition_table,
185 GRN_TEXT_VALUE(column_name),
186 (uint32_t)GRN_TEXT_LEN(column_name));
187 if (!condition_column) {
188 grn_obj inspected;
189 GRN_TEXT_INIT(&inspected, 0);
190 grn_inspect(ctx, &inspected, condition_table);
191 GRN_PLUGIN_ERROR(ctx,
192 GRN_INVALID_ARGUMENT,
193 "in_records(): "
194 "the %dth argument must be existing column name: "
195 "<%.*s>: <%.*s>",
196 column_name_i,
197 (int)GRN_TEXT_LEN(column_name),
198 GRN_TEXT_VALUE(column_name),
199 (int)GRN_TEXT_LEN(&inspected),
200 GRN_TEXT_VALUE(&inspected));
201 GRN_OBJ_FIN(ctx, &inspected);
202 goto exit;
203 }
204 GRN_PTR_PUT(ctx, &(data->condition_columns), condition_column);
205
206 i = (size_t)(column_name_i + 1);
207 }
208
209 return NULL;
210
211 exit :
212 grn_in_records_data_free(ctx, data);
213
214 return NULL;
215 }
216
217 static grn_obj *
218 func_in_records_next(grn_ctx *ctx,
219 int n_args,
220 grn_obj **args,
221 grn_user_data *user_data)
222 {
223 grn_in_records_data *data = user_data->ptr;
224 grn_obj *found;
225 grn_obj *condition;
226 grn_obj *variable;
227 int i;
228
229 found = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_BOOL, 0);
230 if (!found) {
231 return NULL;
232 }
233 GRN_BOOL_SET(ctx, found, false);
234
235 if (!data) {
236 return found;
237 }
238
239 GRN_EXPR_CREATE_FOR_QUERY(ctx,
240 data->condition_table,
241 condition,
242 variable);
243 if (!condition) {
244 grn_rc rc = ctx->rc;
245 if (rc == GRN_SUCCESS) {
246 rc = GRN_NO_MEMORY_AVAILABLE;
247 }
248 GRN_PLUGIN_ERROR(ctx,
249 rc,
250 "in_records(): "
251 "failed to create internal expression: %s",
252 ctx->errbuf);
253 return found;
254 }
255
256 for (i = 1; i < n_args; i += 3) {
257 int nth = (i - 1) / 3;
258 grn_obj *value = args[i];
259 grn_obj *condition_column;
260 grn_operator condition_mode;
261
262 condition_column = GRN_PTR_VALUE_AT(&(data->condition_columns), nth);
263 condition_mode = data->condition_modes[nth];
264
265 switch (condition_mode) {
266 case GRN_OP_EQUAL :
267 case GRN_OP_NOT_EQUAL :
268 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
269 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
270 grn_expr_append_op(ctx, condition, condition_mode, 2);
271 break;
272 case GRN_OP_LESS :
273 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
274 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
275 grn_expr_append_op(ctx, condition, GRN_OP_GREATER_EQUAL, 2);
276 break;
277 case GRN_OP_GREATER :
278 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
279 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
280 grn_expr_append_op(ctx, condition, GRN_OP_LESS_EQUAL, 2);
281 break;
282 case GRN_OP_LESS_EQUAL :
283 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
284 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
285 grn_expr_append_op(ctx, condition, GRN_OP_GREATER, 2);
286 break;
287 case GRN_OP_GREATER_EQUAL :
288 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
289 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
290 grn_expr_append_op(ctx, condition, GRN_OP_LESS, 2);
291 break;
292 default :
293 grn_expr_append_obj(ctx, condition, value, GRN_OP_PUSH, 1);
294 grn_expr_append_obj(ctx, condition, condition_column, GRN_OP_GET_VALUE, 1);
295 grn_expr_append_op(ctx, condition, condition_mode, 2);
296 break;
297 }
298
299 if (nth > 0) {
300 grn_expr_append_op(ctx, condition, GRN_OP_AND, 2);
301 }
302 }
303
304 data->search_result = grn_table_select(ctx,
305 data->condition_table,
306 condition,
307 data->search_result,
308 GRN_OP_OR);
309 if (grn_table_size(ctx, data->search_result) > 0) {
310 GRN_BOOL_SET(ctx, found, true);
311
312 GRN_TABLE_EACH_BEGIN(ctx, data->search_result, cursor, id) {
313 grn_table_cursor_delete(ctx, cursor);
314 } GRN_TABLE_EACH_END(ctx, cursor);
315 }
316
317 grn_obj_close(ctx, condition);
318
319 return found;
320 }
321
322 static grn_obj *
323 func_in_records_fin(grn_ctx *ctx,
324 int n_args,
325 grn_obj **args,
326 grn_user_data *user_data)
327 {
328 grn_in_records_data *data = user_data->ptr;
329
330 grn_in_records_data_free(ctx, data);
331
332 return NULL;
333 }
334
335 static grn_rc
336 selector_in_records(grn_ctx *ctx,
337 grn_obj *table,
338 grn_obj *index,
339 int n_args,
340 grn_obj **args,
341 grn_obj *res,
342 grn_operator op)
343 {
344 grn_obj *condition_table;
345 grn_operator *condition_modes = NULL;
346 grn_obj condition_columns;
347 int i, nth;
348
349 /* TODO: Enable me when function call is supported. */
350 return GRN_FUNCTION_NOT_IMPLEMENTED;
351
352 if (n_args < 5) {
353 GRN_PLUGIN_ERROR(ctx,
354 GRN_INVALID_ARGUMENT,
355 "in_records(): wrong number of arguments (%d for 4..)",
356 n_args - 1);
357 return ctx->rc;
358 }
359
360 condition_table = args[1];
361 if (!grn_obj_is_table(ctx, condition_table)) {
362 grn_obj inspected;
363 GRN_TEXT_INIT(&inspected, 0);
364 grn_inspect(ctx, &inspected, condition_table);
365 GRN_PLUGIN_ERROR(ctx,
366 GRN_INVALID_ARGUMENT,
367 "in_records(): the first argument must be a table: <%.*s>",
368 (int)GRN_TEXT_LEN(&inspected),
369 GRN_TEXT_VALUE(&inspected));
370 GRN_OBJ_FIN(ctx, &inspected);
371 return ctx->rc;
372 }
373
374 condition_modes = GRN_PLUGIN_MALLOCN(ctx,
375 grn_operator,
376 (size_t)((n_args - 2) / 3));
377 GRN_PTR_INIT(&condition_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
378 for (i = 2, nth = 0; i < n_args; i += 3, nth++) {
379 int mode_name_i = i + 1;
380 int column_name_i = i + 2;
381 grn_obj *mode_name;
382 grn_operator mode;
383 grn_obj *column_name;
384 grn_obj *condition_column;
385
386 mode_name = args[mode_name_i];
387 mode = grn_proc_get_value_mode(ctx,
388 mode_name,
389 GRN_OP_EQUAL,
390 "in_records()");
391 if (ctx->rc != GRN_SUCCESS) {
392 goto exit;
393 }
394
395 condition_modes[nth] = mode;
396
397 column_name = args[column_name_i];
398 if (!grn_obj_is_text_family_bulk(ctx, column_name)) {
399 grn_obj inspected;
400 GRN_TEXT_INIT(&inspected, 0);
401 grn_inspect(ctx, &inspected, condition_table);
402 GRN_PLUGIN_ERROR(ctx,
403 GRN_INVALID_ARGUMENT,
404 "in_records(): "
405 "the %dth argument must be column name as string: "
406 "<%.*s>",
407 column_name_i,
408 (int)GRN_TEXT_LEN(&inspected),
409 GRN_TEXT_VALUE(&inspected));
410 GRN_OBJ_FIN(ctx, &inspected);
411 goto exit;
412 }
413
414 condition_column = grn_obj_column(ctx, condition_table,
415 GRN_TEXT_VALUE(column_name),
416 (uint32_t)GRN_TEXT_LEN(column_name));
417 if (!condition_column) {
418 grn_obj inspected;
419 GRN_TEXT_INIT(&inspected, 0);
420 grn_inspect(ctx, &inspected, condition_table);
421 GRN_PLUGIN_ERROR(ctx,
422 GRN_INVALID_ARGUMENT,
423 "in_records(): "
424 "the %dth argument must be existing column name: "
425 "<%.*s>: <%.*s>",
426 column_name_i,
427 (int)GRN_TEXT_LEN(column_name),
428 GRN_TEXT_VALUE(column_name),
429 (int)GRN_TEXT_LEN(&inspected),
430 GRN_TEXT_VALUE(&inspected));
431 GRN_OBJ_FIN(ctx, &inspected);
432 goto exit;
433 }
434 GRN_PTR_PUT(ctx, &condition_columns, condition_column);
435 }
436
437 {
438 grn_obj condition_column_value;
439
440 GRN_VOID_INIT(&condition_column_value);
441 GRN_TABLE_EACH_BEGIN(ctx, condition_table, cursor, id) {
442 grn_obj *sub_res = NULL;
443
444 for (i = 2; i < n_args; i += 3) {
445 int nth = (i - 2) / 3;
446 grn_operator sub_op;
447 grn_obj *condition_column;
448 grn_operator condition_mode;
449 grn_obj *column = args[i];
450 grn_obj *expr;
451 grn_obj *variable;
452
453 if (nth == 0) {
454 sub_op = GRN_OP_OR;
455 } else {
456 sub_op = GRN_OP_AND;
457 }
458
459 condition_column = GRN_PTR_VALUE_AT(&condition_columns, nth);
460 condition_mode = condition_modes[nth];
461
462 GRN_BULK_REWIND(&condition_column_value);
463 grn_obj_get_value(ctx,
464 condition_column,
465 id,
466 &condition_column_value);
467
468 GRN_EXPR_CREATE_FOR_QUERY(ctx, table, expr, variable);
469 if (!expr) {
470 GRN_PLUGIN_ERROR(ctx,
471 GRN_INVALID_ARGUMENT,
472 "in_records(): failed to create expression");
473 GRN_OBJ_FIN(ctx, &condition_column_value);
474 if (sub_res) {
475 grn_obj_close(ctx, sub_res);
476 }
477 goto exit;
478 }
479 grn_expr_append_obj(ctx, expr, column, GRN_OP_GET_VALUE, 1);
480 grn_expr_append_obj(ctx, expr, &condition_column_value, GRN_OP_PUSH, 1);
481 grn_expr_append_op(ctx, expr, condition_mode, 2);
482 sub_res = grn_table_select(ctx, table, expr, sub_res, sub_op);
483 grn_obj_close(ctx, expr);
484 }
485
486 if (sub_res) {
487 grn_table_setoperation(ctx, res, sub_res, res, op);
488 grn_obj_close(ctx, sub_res);
489 }
490 } GRN_TABLE_EACH_END(ctx, cursor);
491 GRN_OBJ_FIN(ctx, &condition_column_value);
492 }
493
494 exit :
495 GRN_PLUGIN_FREE(ctx, condition_modes);
496
497 for (i = 2; i < n_args; i += 3) {
498 int nth = (i - 2) / 3;
499 grn_obj *condition_column;
500 condition_column = GRN_PTR_VALUE_AT(&condition_columns, nth);
501 if (condition_column && condition_column->header.type == GRN_ACCESSOR) {
502 grn_obj_unlink(ctx, condition_column);
503 }
504 }
505 GRN_OBJ_FIN(ctx, &condition_columns);
506
507 return ctx->rc;
508 }
509
510 void
511 grn_proc_init_in_records(grn_ctx *ctx)
512 {
513 grn_obj *selector_proc;
514
515 selector_proc = grn_proc_create(ctx, "in_records", -1, GRN_PROC_FUNCTION,
516 func_in_records_init,
517 func_in_records_next,
518 func_in_records_fin,
519 0,
520 NULL);
521 grn_proc_set_selector(ctx, selector_proc, selector_in_records);
522 grn_proc_set_selector_operator(ctx, selector_proc, GRN_OP_NOP);
523 }