Debian Codegraph @

lib/output_columns.c

raw symbols SCIP
1 /*
2 Copyright(C) 2009-2018 Brazil
3 Copyright(C) 2018-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_expr.h"
21 #include "grn_output_columns.h"
22
23 grn_obj *
24 grn_output_columns_parse(grn_ctx *ctx,
25 grn_obj *table,
26 const char *raw_output_columns,
27 size_t raw_output_columns_size)
28 {
29 grn_obj *output_columns = NULL;
30 grn_obj *variable;
31
32 GRN_API_ENTER;
33
34 GRN_EXPR_CREATE_FOR_QUERY(ctx, table, output_columns, variable);
35 if (ctx->rc == GRN_SUCCESS) {
36 grn_expr_parse(ctx,
37 output_columns,
38 raw_output_columns,
39 (unsigned int)raw_output_columns_size,
40 NULL,
41 GRN_OP_MATCH,
42 GRN_OP_AND,
43 GRN_EXPR_SYNTAX_OUTPUT_COLUMNS);
44 if (ctx->rc != GRN_SUCCESS) {
45 grn_obj_close(ctx, output_columns);
46 output_columns = NULL;
47 }
48 }
49
50 GRN_API_RETURN(output_columns);
51 }
52
53 grn_rc
54 grn_output_columns_get_offsets(grn_ctx *ctx,
55 grn_obj *output_columns,
56 grn_obj *offsets)
57 {
58 ptrdiff_t previous_comma_offset = -1;
59 bool is_first_comma = true;
60 bool have_comma = false;
61 grn_expr *expr = (grn_expr *)output_columns;
62 grn_expr_code *code;
63 grn_expr_code *code_end = expr->codes + expr->codes_curr;
64
65 GRN_API_ENTER;
66
67 for (code = expr->codes; code < code_end; code++) {
68 ptrdiff_t code_start_offset;
69
70 if (code->op != GRN_OP_COMMA) {
71 continue;
72 }
73
74 have_comma = true;
75 if (is_first_comma) {
76 unsigned int n_used_codes;
77 ptrdiff_t code_end_offset;
78
79 n_used_codes = grn_expr_code_n_used_codes(ctx, expr->codes, code - 1);
80 code_end_offset = code - expr->codes - n_used_codes;
81 GRN_UINT32_PUT(ctx, offsets, 0);
82 GRN_UINT32_PUT(ctx, offsets, code_end_offset);
83 code_start_offset = code_end_offset;
84 is_first_comma = false;
85 } else {
86 code_start_offset = previous_comma_offset + 1;
87 }
88
89 GRN_UINT32_PUT(ctx, offsets, code_start_offset);
90 GRN_UINT32_PUT(ctx, offsets, code - expr->codes);
91
92 previous_comma_offset = code - expr->codes;
93 }
94
95 if (!have_comma && expr->codes_curr > 0) {
96 GRN_UINT32_PUT(ctx, offsets, 0);
97 GRN_UINT32_PUT(ctx, offsets, expr->codes_curr);
98 }
99
100 GRN_API_RETURN(ctx->rc);
101 }
102
103 static void
104 grn_output_columns_apply_inspect_columns(grn_ctx *ctx,
105 grn_obj *buffer,
106 grn_obj *columns)
107 {
108 size_t i, n;
109
110 GRN_TEXT_PUTS(ctx, buffer, "[");
111 n = GRN_BULK_VSIZE(columns) / sizeof(grn_obj *);
112 for (i = 0; i < n; i++) {
113 grn_obj *column = GRN_PTR_VALUE_AT(columns, i);
114
115 if (i > 0) {
116 GRN_TEXT_PUTS(ctx, buffer, ", ");
117 }
118 if (grn_obj_is_accessor(ctx, column)) {
119 grn_inspect(ctx, buffer, column);
120 } else {
121 char name[GRN_TABLE_MAX_KEY_SIZE];
122 int name_size;
123
124 name_size = grn_obj_name(ctx, column, name, sizeof(name));
125 GRN_TEXT_PUTS(ctx, buffer, "<");
126 GRN_TEXT_PUT(ctx, buffer, name, name_size);
127 GRN_TEXT_PUTS(ctx, buffer, ">");
128 }
129 }
130 GRN_TEXT_PUTS(ctx, buffer, "]");
131 }
132
133 static grn_obj *
134 grn_output_columns_apply_detect_target_table(grn_ctx *ctx,
135 grn_obj *columns)
136 {
137 grn_obj *target_table = NULL;
138 size_t i, n;
139
140 n = GRN_BULK_VSIZE(columns) / sizeof(grn_obj *);
141 for (i = 0; i < n; i++) {
142 grn_obj *column = GRN_PTR_VALUE_AT(columns, i);
143 grn_obj *table;
144
145 if (grn_obj_is_accessor(ctx, column)) {
146 table = ((grn_accessor *)column)->obj;
147 grn_obj_refer(ctx, table);
148 } else {
149 grn_id table_id;
150 table_id = column->header.domain;
151 table = grn_ctx_at(ctx, table_id);
152 }
153 if (target_table) {
154 const bool valid_table = (table == target_table);
155 grn_obj_unref(ctx, table);
156 if (!valid_table) {
157 char column_name[GRN_TABLE_MAX_KEY_SIZE];
158 int column_name_size;
159 char target_table_name[GRN_TABLE_MAX_KEY_SIZE];
160 int target_table_name_size;
161
162 column_name_size = grn_obj_name(ctx,
163 column,
164 column_name,
165 sizeof(column_name));
166 target_table_name_size = grn_obj_name(ctx,
167 target_table,
168 target_table_name,
169 sizeof(target_table_name));
170 ERR(GRN_INVALID_ARGUMENT,
171 "[output-columns][apply] "
172 "all columns must belong to the same table: <%.*s>: <%.*s>",
173 column_name_size, column_name,
174 target_table_name_size, target_table_name);
175 grn_obj_unref(ctx, table);
176 return NULL;
177 }
178 } else {
179 target_table = table;
180 }
181 }
182
183 return target_table;
184 }
185
186 static void
187 grn_output_columns_apply_add_records_no_key(grn_ctx *ctx,
188 grn_obj *source_table,
189 grn_obj *target_table,
190 grn_obj *ids)
191 {
192 GRN_TABLE_EACH_BEGIN(ctx, source_table, cursor, source_id) {
193 grn_id target_id;
194
195 target_id = grn_table_add(ctx,
196 target_table,
197 NULL,
198 0,
199 NULL);
200 if (target_id == GRN_ID_NIL) {
201 break;
202 }
203
204 GRN_RECORD_PUT(ctx, ids, source_id);
205 GRN_RECORD_PUT(ctx, ids, target_id);
206 } GRN_TABLE_EACH_END(ctx, cursor);
207 }
208
209 static void
210 grn_output_columns_apply_add_records_key(grn_ctx *ctx,
211 grn_obj *output_columns,
212 grn_obj *columns,
213 grn_obj *offsets,
214 grn_obj *source_table,
215 grn_obj *target_table,
216 grn_obj *ids)
217 {
218 grn_obj target_key_buffer;
219 size_t n_columns;
220 size_t key_offset;
221
222 if (target_table->header.domain == GRN_DB_SHORT_TEXT) {
223 GRN_SHORT_TEXT_INIT(&target_key_buffer, 0);
224 } else {
225 GRN_VALUE_FIX_SIZE_INIT(&target_key_buffer,
226 0,
227 target_table->header.domain);
228 }
229
230 n_columns = GRN_BULK_VSIZE(columns) / sizeof(grn_obj *);
231 {
232 bool have_key = false;
233 size_t i;
234 for (i = 0; i < n_columns; i++) {
235 grn_obj *column = GRN_PTR_VALUE_AT(columns, i);
236 if (grn_obj_is_key_accessor(ctx, column)) {
237 if (have_key) {
238 grn_obj inspected_columns;
239 GRN_TEXT_INIT(&inspected_columns, 0);
240 grn_output_columns_apply_inspect_columns(ctx,
241 &inspected_columns,
242 columns);
243 ERR(GRN_INVALID_ARGUMENT,
244 "[output-columns][apply] "
245 "_key is specified multiple times: %.*s",
246 (int)(GRN_TEXT_LEN(&inspected_columns)),
247 GRN_TEXT_VALUE(&inspected_columns));
248 GRN_OBJ_FIN(ctx, &inspected_columns);
249 goto exit;
250 }
251 key_offset = i;
252 have_key = true;
253 }
254 }
255
256 if (!have_key) {
257 grn_obj inspected_columns;
258 GRN_TEXT_INIT(&inspected_columns, 0);
259 grn_output_columns_apply_inspect_columns(ctx, &inspected_columns, columns);
260 ERR(GRN_INVALID_ARGUMENT,
261 "[output-columns][apply] "
262 "_key must be specified: %.*s",
263 (int)(GRN_TEXT_LEN(&inspected_columns)),
264 GRN_TEXT_VALUE(&inspected_columns));
265 GRN_OBJ_FIN(ctx, &inspected_columns);
266 goto exit;
267 }
268 }
269
270 {
271 grn_expr *expr = (grn_expr *)output_columns;
272 grn_expr_code *codes = expr->codes;
273 uint32_t codes_curr = expr->codes_curr;
274 uint32_t code_start_offset;
275 uint32_t code_end_offset;
276 grn_expr_executor executor;
277
278 code_start_offset = GRN_UINT32_VALUE_AT(offsets, key_offset * 2);
279 code_end_offset = GRN_UINT32_VALUE_AT(offsets, key_offset * 2 + 1);
280 expr->codes += code_start_offset;
281 expr->codes_curr = code_end_offset - code_start_offset;
282 grn_expr_executor_init(ctx, &executor, output_columns);
283 GRN_TABLE_EACH_BEGIN(ctx, source_table, cursor, source_id) {
284 grn_rc cast_rc;
285 grn_id target_id;
286 grn_obj *value;
287
288 value = grn_expr_executor_exec(ctx, &executor, source_id);
289 if (!value) {
290 continue;
291 }
292
293 GRN_BULK_REWIND(&target_key_buffer);
294 cast_rc = grn_obj_cast(ctx, value, &target_key_buffer, false);
295 if (cast_rc != GRN_SUCCESS) {
296 continue;
297 }
298
299 target_id = grn_table_add(ctx,
300 target_table,
301 GRN_BULK_HEAD(&target_key_buffer),
302 (unsigned int)GRN_BULK_VSIZE(&target_key_buffer),
303 NULL);
304 if (target_id == GRN_ID_NIL) {
305 continue;
306 }
307
308 GRN_RECORD_PUT(ctx, ids, source_id);
309 GRN_RECORD_PUT(ctx, ids, target_id);
310 } GRN_TABLE_EACH_END(ctx, cursor);
311 expr->codes = codes;
312 expr->codes_curr = codes_curr;
313 }
314
315 exit :
316 GRN_OBJ_FIN(ctx, &target_key_buffer);
317 }
318
319 static void
320 grn_output_columns_apply_add_records(grn_ctx *ctx,
321 grn_obj *output_columns,
322 grn_obj *columns,
323 grn_obj *offsets,
324 grn_obj *ids)
325 {
326 grn_obj *target_table;
327 grn_obj *variable;
328 grn_obj *source_table;
329
330 target_table = grn_output_columns_apply_detect_target_table(ctx, columns);
331 if (!target_table) {
332 return;
333 }
334
335 variable = grn_expr_get_var_by_offset(ctx, output_columns, 0);
336 source_table = grn_ctx_at(ctx, variable->header.domain);
337
338 if (target_table->header.type == GRN_TABLE_NO_KEY) {
339 grn_output_columns_apply_add_records_no_key(ctx,
340 source_table,
341 target_table,
342 ids);
343 } else {
344 grn_output_columns_apply_add_records_key(ctx,
345 output_columns,
346 columns,
347 offsets,
348 source_table,
349 target_table,
350 ids);
351 }
352 grn_obj_unref(ctx, target_table);
353 }
354
355 static void
356 grn_output_columns_apply_one(grn_ctx *ctx,
357 grn_obj *output_columns,
358 grn_obj *ids,
359 uint32_t code_start_offset,
360 uint32_t code_end_offset,
361 grn_obj *column)
362 {
363 grn_expr *expr = (grn_expr *)output_columns;
364 grn_expr_code *codes = expr->codes;
365 uint32_t codes_curr = expr->codes_curr;
366 grn_expr_executor executor;
367 size_t i;
368 size_t n_ids;
369
370 expr->codes += code_start_offset;
371 expr->codes_curr = code_end_offset - code_start_offset;
372 grn_expr_executor_init(ctx, &executor, output_columns);
373 n_ids = GRN_BULK_VSIZE(ids) / sizeof(grn_id);
374 for (i = 0; i < n_ids; i += 2) {
375 grn_id source_id = GRN_RECORD_VALUE_AT(ids, i);
376 grn_id target_id = GRN_RECORD_VALUE_AT(ids, i + 1);
377 grn_obj *value;
378
379 value = grn_expr_executor_exec(ctx, &executor, source_id);
380 if (value) {
381 grn_obj_set_value(ctx, column, target_id, value, GRN_OBJ_SET);
382 }
383 }
384 grn_expr_executor_fin(ctx, &executor);
385
386 expr->codes = codes;
387 expr->codes_curr = codes_curr;
388 }
389
390 grn_rc
391 grn_output_columns_apply(grn_ctx *ctx,
392 grn_obj *output_columns,
393 grn_obj *columns)
394 {
395 size_t n_columns;
396 grn_obj offsets;
397 grn_obj ids;
398 size_t n_offsets;
399
400 GRN_API_ENTER;
401
402 GRN_UINT32_INIT(&offsets, GRN_OBJ_VECTOR);
403 GRN_RECORD_INIT(&ids, GRN_OBJ_VECTOR, GRN_ID_NIL);
404
405 n_columns = GRN_BULK_VSIZE(columns) / sizeof(grn_obj *);
406 if (n_columns == 0) {
407 goto exit;
408 }
409
410 grn_output_columns_get_offsets(ctx, output_columns, &offsets);
411 if (ctx->rc != GRN_SUCCESS) {
412 goto exit;
413 }
414 n_offsets = GRN_BULK_VSIZE(&offsets) / sizeof(uint32_t) / 2;
415 if (n_columns != n_offsets) {
416 grn_obj inspected_columns;
417 GRN_TEXT_INIT(&inspected_columns, 0);
418 grn_output_columns_apply_inspect_columns(ctx,
419 &inspected_columns,
420 columns);
421 ERR(GRN_INVALID_ARGUMENT,
422 "[output-columns][apply] "
423 "the number of columns (%" GRN_FMT_SIZE ") must be %" GRN_FMT_SIZE ": "
424 "%.*s",
425 n_columns,
426 n_offsets,
427 (int)GRN_TEXT_LEN(&inspected_columns),
428 GRN_TEXT_VALUE(&inspected_columns));
429 GRN_OBJ_FIN(ctx, &inspected_columns);
430 goto exit;
431 }
432
433 grn_output_columns_apply_add_records(ctx,
434 output_columns,
435 columns,
436 &offsets,
437 &ids);
438 if (ctx->rc != GRN_SUCCESS) {
439 goto exit;
440 }
441
442 {
443 size_t i;
444 for (i = 0; i < n_offsets; i++) {
445 grn_obj *column = GRN_PTR_VALUE_AT(columns, i);
446 uint32_t code_start_offset;
447 uint32_t code_end_offset;
448
449 if (grn_obj_is_key_accessor(ctx, column)) {
450 continue;
451 }
452
453 code_start_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2);
454 code_end_offset = GRN_UINT32_VALUE_AT(&offsets, i * 2 + 1);
455 grn_output_columns_apply_one(ctx,
456 output_columns,
457 &ids,
458 code_start_offset,
459 code_end_offset,
460 column);
461 }
462 }
463
464 exit :
465 GRN_OBJ_FIN(ctx, &offsets);
466 GRN_OBJ_FIN(ctx, &ids);
467
468 GRN_API_RETURN(ctx->rc);
469 }