Debian Codegraph @

lib/window_function_executor.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2019-2025 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_ctx.h"
20 #include "grn_window_function_executor.h"
21
22 #ifdef GRN_WITH_APACHE_ARROW
23 # include "grn_arrow.hpp"
24 # include <groonga/arrow.hpp>
25 # include <arrow/compute/api.h>
26 #endif
27
28 extern "C" {
29 static bool grn_window_function_executor_all_tables_at_once_enable = true;
30
31 void
32 grn_window_function_executor_init_from_env(void)
33 {
34 {
35 char grn_window_function_executor_all_tables_at_once_enable_env[GRN_ENV_BUFFER_SIZE];
36 grn_getenv("GRN_WINDOW_FUNCTION_EXECUTOR_ALL_TABLES_AT_ONCE_ENABLE",
37 grn_window_function_executor_all_tables_at_once_enable_env,
38 GRN_ENV_BUFFER_SIZE);
39 if (strcmp(grn_window_function_executor_all_tables_at_once_enable_env, "no") == 0) {
40 grn_window_function_executor_all_tables_at_once_enable = false;
41 } else {
42 grn_window_function_executor_all_tables_at_once_enable = true;
43 }
44 }
45 }
46
47 grn_rc
48 grn_window_function_executor_init(grn_ctx *ctx,
49 grn_window_function_executor *executor)
50 {
51 GRN_API_ENTER;
52
53 GRN_TEXT_INIT(&(executor->tag), 0);
54 GRN_PTR_INIT(&(executor->tables), GRN_OBJ_VECTOR, GRN_ID_NIL);
55 GRN_BOOL_INIT(&(executor->is_context_tables), 0);
56 GRN_TEXT_INIT(&(executor->source), 0);
57 GRN_TEXT_INIT(&(executor->sort_keys), 0);
58 GRN_TEXT_INIT(&(executor->group_keys), 0);
59 GRN_TEXT_INIT(&(executor->output_column_name), 0);
60 executor->context.sort_keys = NULL;
61 executor->context.n_sort_keys = 0;
62 executor->context.group_keys = NULL;
63 executor->context.n_group_keys = 0;
64 executor->context.window_sort_keys = NULL;
65 executor->context.n_window_sort_keys = 0;
66 GRN_PTR_INIT(&(executor->context.key_columns), GRN_OBJ_VECTOR, GRN_ID_NIL);
67 executor->context.sorted = NULL;
68 executor->values.n = 0;
69 executor->values.previous = NULL;
70 executor->values.current = NULL;
71 GRN_PTR_INIT(&(executor->window_function_calls), GRN_OBJ_VECTOR, GRN_ID_NIL);
72 GRN_PTR_INIT(&(executor->output_columns), GRN_OBJ_VECTOR, GRN_ID_NIL);
73 grn_window_init(ctx, &(executor->window));
74
75 GRN_API_RETURN(ctx->rc);
76 }
77
78 static void
79 grn_window_function_executor_rewind(grn_ctx *ctx,
80 grn_window_function_executor *executor)
81 {
82 GRN_BULK_REWIND(&(executor->context.key_columns));
83
84 grn_obj *window_function_calls = &(executor->window_function_calls);
85 const size_t n_calls = GRN_PTR_VECTOR_SIZE(window_function_calls);
86 for (size_t i = 0; i < n_calls; i++) {
87 grn_obj *window_function_call = GRN_PTR_VALUE_AT(window_function_calls, i);
88 if (window_function_call) {
89 grn_obj_close(ctx, window_function_call);
90 }
91 }
92 GRN_BULK_REWIND(window_function_calls);
93
94 grn_obj *output_columns = &(executor->output_columns);
95 const size_t n_output_columns = GRN_PTR_VECTOR_SIZE(output_columns);
96 for (size_t i = 0; i < n_output_columns; i++) {
97 grn_obj *output_column = GRN_PTR_VALUE_AT(output_columns, i);
98 if (grn_obj_is_accessor(ctx, output_column)) {
99 grn_obj_close(ctx, output_column);
100 }
101 }
102 GRN_BULK_REWIND(output_columns);
103 }
104
105 grn_rc
106 grn_window_function_executor_fin(grn_ctx *ctx,
107 grn_window_function_executor *executor)
108 {
109 GRN_API_ENTER;
110
111 if (!executor) {
112 GRN_API_RETURN(GRN_SUCCESS);
113 }
114
115 grn_window_fin(ctx, &(executor->window));
116
117 grn_window_function_executor_rewind(ctx, executor);
118 GRN_OBJ_FIN(ctx, &(executor->context.key_columns));
119 GRN_OBJ_FIN(ctx, &(executor->output_columns));
120 GRN_OBJ_FIN(ctx, &(executor->window_function_calls));
121
122 if (executor->values.n > 0) {
123 for (size_t i = 0; i < executor->values.n; i++) {
124 GRN_OBJ_FIN(ctx, &(executor->values.previous[i]));
125 GRN_OBJ_FIN(ctx, &(executor->values.current[i]));
126 }
127 GRN_FREE(executor->values.previous);
128 GRN_FREE(executor->values.current);
129 }
130
131 if (executor->context.sorted) {
132 grn_obj_close(ctx, executor->context.sorted);
133 }
134 if (executor->context.window_sort_keys) {
135 GRN_FREE(executor->context.window_sort_keys);
136 }
137 if (executor->context.group_keys) {
138 grn_table_sort_key_close(
139 ctx,
140 executor->context.group_keys,
141 static_cast<uint32_t>(executor->context.n_group_keys));
142 }
143 if (executor->context.sort_keys) {
144 grn_table_sort_key_close(
145 ctx,
146 executor->context.sort_keys,
147 static_cast<uint32_t>(executor->context.n_sort_keys));
148 }
149
150 GRN_OBJ_FIN(ctx, &(executor->output_column_name));
151 GRN_OBJ_FIN(ctx, &(executor->group_keys));
152 GRN_OBJ_FIN(ctx, &(executor->sort_keys));
153 GRN_OBJ_FIN(ctx, &(executor->source));
154 GRN_OBJ_FIN(ctx, &(executor->is_context_tables));
155 GRN_OBJ_FIN(ctx, &(executor->tables));
156 GRN_OBJ_FIN(ctx, &(executor->tag));
157
158 GRN_API_RETURN(GRN_SUCCESS);
159 }
160
161 grn_window_function_executor *
162 grn_window_function_executor_open(grn_ctx *ctx)
163 {
164 GRN_API_ENTER;
165
166 grn_window_function_executor *executor;
167 executor = static_cast<grn_window_function_executor *>(
168 GRN_CALLOC(sizeof(grn_window_function_executor)));
169 if (!executor) {
170 char errbuf[GRN_CTX_MSGSIZE];
171 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
172 ERR(ctx->rc,
173 "[window-function-executor][open] failed to allocate: %s",
174 errbuf);
175 GRN_API_RETURN(NULL);
176 }
177
178 grn_window_function_executor_init(ctx, executor);
179
180 if (ctx->rc != GRN_SUCCESS) {
181 GRN_FREE(executor);
182 executor = NULL;
183 }
184
185 GRN_API_RETURN(executor);
186 }
187
188 grn_rc
189 grn_window_function_executor_close(grn_ctx *ctx,
190 grn_window_function_executor *executor)
191 {
192 GRN_API_ENTER;
193
194 if (!executor) {
195 GRN_API_RETURN(GRN_SUCCESS);
196 }
197
198 grn_window_function_executor_fin(ctx, executor);
199 GRN_FREE(executor);
200
201 GRN_API_RETURN(GRN_SUCCESS);
202 }
203
204 grn_rc
205 grn_window_function_executor_set_tag(grn_ctx *ctx,
206 grn_window_function_executor *executor,
207 const char *tag,
208 size_t tag_size)
209 {
210 GRN_API_ENTER;
211
212 if (!executor) {
213 ERR(GRN_INVALID_ARGUMENT,
214 "[window-function-executor][tag][set] executor is NULL");
215 GRN_API_RETURN(ctx->rc);
216 }
217
218 GRN_TEXT_SET(ctx, &(executor->tag), tag, tag_size);
219
220 GRN_API_RETURN(ctx->rc);
221 }
222
223 grn_rc
224 grn_window_function_executor_add_table(grn_ctx *ctx,
225 grn_window_function_executor *executor,
226 grn_obj *table)
227 {
228 GRN_API_ENTER;
229
230 if (!executor) {
231 ERR(GRN_INVALID_ARGUMENT,
232 "%.*s[window-function-executor][table][add] executor is NULL",
233 (int)GRN_TEXT_LEN(&(executor->tag)),
234 GRN_TEXT_VALUE(&(executor->tag)));
235 GRN_API_RETURN(ctx->rc);
236 }
237
238 grn_window_function_executor_rewind(ctx, executor);
239
240 GRN_PTR_PUT(ctx, &(executor->tables), table);
241 GRN_BOOL_PUT(ctx, &(executor->is_context_tables), false);
242
243 GRN_API_RETURN(ctx->rc);
244 }
245
246 grn_rc
247 grn_window_function_executor_add_context_table(grn_ctx *ctx,
248 grn_window_function_executor *executor,
249 grn_obj *table)
250 {
251 GRN_API_ENTER;
252
253 if (!executor) {
254 ERR(GRN_INVALID_ARGUMENT,
255 "%.*s[window-function-executor][context-table][add] executor is NULL",
256 (int)GRN_TEXT_LEN(&(executor->tag)),
257 GRN_TEXT_VALUE(&(executor->tag)));
258 GRN_API_RETURN(ctx->rc);
259 }
260
261 grn_window_function_executor_rewind(ctx, executor);
262
263 GRN_PTR_PUT(ctx, &(executor->tables), table);
264 GRN_BOOL_PUT(ctx, &(executor->is_context_tables), true);
265
266 GRN_API_RETURN(ctx->rc);
267 }
268
269 grn_rc
270 grn_window_function_executor_set_source(grn_ctx *ctx,
271 grn_window_function_executor *executor,
272 const char *source,
273 size_t source_size)
274 {
275 GRN_API_ENTER;
276
277 if (!executor) {
278 ERR(GRN_INVALID_ARGUMENT,
279 "%.*s[window-function-executor][source][set] executor is NULL",
280 (int)GRN_TEXT_LEN(&(executor->tag)),
281 GRN_TEXT_VALUE(&(executor->tag)));
282 GRN_API_RETURN(ctx->rc);
283 }
284
285 GRN_TEXT_SET(ctx, &(executor->source), source, source_size);
286
287 GRN_API_RETURN(ctx->rc);
288 }
289
290 grn_rc
291 grn_window_function_executor_set_sort_keys(grn_ctx *ctx,
292 grn_window_function_executor *executor,
293 const char *sort_keys,
294 size_t sort_keys_size)
295 {
296 GRN_API_ENTER;
297
298 if (!executor) {
299 ERR(GRN_INVALID_ARGUMENT,
300 "%.*s[window-function-executor][sort-keys][set] executor is NULL",
301 (int)GRN_TEXT_LEN(&(executor->tag)),
302 GRN_TEXT_VALUE(&(executor->tag)));
303 GRN_API_RETURN(ctx->rc);
304 }
305
306 GRN_TEXT_SET(ctx, &(executor->sort_keys), sort_keys, sort_keys_size);
307
308 GRN_API_RETURN(ctx->rc);
309 }
310
311 grn_rc
312 grn_window_function_executor_set_group_keys(grn_ctx *ctx,
313 grn_window_function_executor *executor,
314 const char *group_keys,
315 size_t group_keys_size)
316 {
317 GRN_API_ENTER;
318
319 if (!executor) {
320 ERR(GRN_INVALID_ARGUMENT,
321 "%.*s[window-function-executor][group-keys][set] executor is NULL",
322 (int)GRN_TEXT_LEN(&(executor->tag)),
323 GRN_TEXT_VALUE(&(executor->tag)));
324 GRN_API_RETURN(ctx->rc);
325 }
326
327 GRN_TEXT_SET(ctx, &(executor->group_keys), group_keys, group_keys_size);
328
329 GRN_API_RETURN(ctx->rc);
330 }
331
332 grn_rc
333 grn_window_function_executor_set_output_column_name(grn_ctx *ctx,
334 grn_window_function_executor *executor,
335 const char *name,
336 size_t name_size)
337 {
338 GRN_API_ENTER;
339
340 if (!executor) {
341 ERR(GRN_INVALID_ARGUMENT,
342 "%.*s[window-function-executor][output-column-name][set] "
343 "executor is NULL",
344 (int)GRN_TEXT_LEN(&(executor->tag)),
345 GRN_TEXT_VALUE(&(executor->tag)));
346 GRN_API_RETURN(ctx->rc);
347 }
348
349 GRN_TEXT_SET(ctx, &(executor->output_column_name), name, name_size);
350
351 GRN_API_RETURN(ctx->rc);
352 }
353
354 static bool
355 grn_window_function_executor_is_ascending(grn_ctx *ctx,
356 grn_window_function_executor *executor)
357 {
358 const size_t sort_keys_len = GRN_TEXT_LEN(&(executor->sort_keys));
359 if (sort_keys_len == 0) {
360 return true;
361 }
362
363 const char *sort_keys = GRN_TEXT_VALUE(&(executor->sort_keys));
364 for (size_t i = 0; i < sort_keys_len; i++) {
365 switch (sort_keys[i]) {
366 case ' ' :
367 break;
368 case '-' :
369 return false;
370 default :
371 return true;
372 }
373 }
374
375 return true;
376 }
377
378 #ifdef GRN_WITH_APACHE_ARROW
379 namespace {
380 struct GrnSortKeys {
381 grn_table_sort_key *keys_;
382 size_t n_keys_;
383 GrnSortKeys(grn_table_sort_key *keys,
384 size_t n_keys) : keys_(keys),
385 n_keys_(n_keys) {
386 }
387 };
388
389 constexpr uint64_t pack_source_id(uint32_t table_index, grn_id record_id) {
390 return (static_cast<uint64_t>(table_index) << 32) | record_id;
391 }
392
393 constexpr uint32_t unpack_source_id_table_index(uint64_t source_id) {
394 return source_id >> 32;
395 }
396
397 constexpr uint32_t unpack_source_id_record_id(uint64_t source_id) {
398 return source_id & ((1ULL << 32) - 1);
399 }
400
401 class AllTablesExecutor {
402 public:
403 AllTablesExecutor(grn_ctx *ctx,
404 grn_window_function_executor *executor,
405 const char *tag)
406 : ctx_(ctx),
407 executor_(executor),
408 executor_tag_(GRN_TEXT_VALUE(&(executor->tag)),
409 GRN_TEXT_LEN(&(executor->tag))),
410 tag_(tag),
411 sort_keys_builders_(),
412 group_keys_builders_(),
413 arrow_sort_keys_() {
414 }
415
416 void execute() {
417 auto ctx = ctx_;
418
419 auto record_batch = build_record_batch();
420 if (!record_batch) {
421 return;
422 }
423 ::arrow::Datum datum(record_batch);
424 ::arrow::compute::SortOptions options(arrow_sort_keys_);
425 auto sorted_indices = ::arrow::compute::SortIndices(datum, options);
426 if (!grnarrow::check(ctx_,
427 sorted_indices,
428 executor_tag_,
429 tag_,
430 " failed to sort")) {
431 return;
432 }
433
434 grn_window_set_is_sorted(ctx_,
435 &(executor_->window),
436 !sort_keys_builders_.empty());
437
438 auto source_ids =
439 std::static_pointer_cast<::arrow::UInt64Array>(record_batch->column(0));
440 auto raw_source_ids = source_ids->raw_values();
441 auto indices =
442 std::static_pointer_cast<::arrow::UInt64Array>(*sorted_indices);
443 auto n_rows = indices->length();
444 auto raw_indices = indices->raw_values();
445
446 auto n_group_keys = group_keys_builders_.size();
447 if (n_group_keys == 0) {
448 for (int64_t i = 0; i < n_rows; ++i) {
449 const auto index = raw_indices[i];
450 const auto source_id = raw_source_ids[index];
451 const auto table_index = unpack_source_id_table_index(source_id);
452 const auto record_id = unpack_source_id_record_id(source_id);
453 auto table = GRN_PTR_VALUE_AT(&(executor_->tables), table_index);
454 const auto is_context_table =
455 GRN_BOOL_VALUE_AT(&(executor_->is_context_tables), table_index);
456 auto window_function_call =
457 GRN_PTR_VALUE_AT(&(executor_->window_function_calls), table_index);
458 auto output_column =
459 GRN_PTR_VALUE_AT(&(executor_->output_columns), table_index);
460 grn_window_add_record(ctx_,
461 &(executor_->window),
462 table,
463 is_context_table,
464 record_id,
465 window_function_call,
466 output_column,
467 &(executor_->context.key_columns));
468 if (ctx_->rc != GRN_SUCCESS) {
469 break;
470 }
471 }
472 if (ctx_->rc == GRN_SUCCESS &&
473 !grn_window_is_empty(ctx, &(executor_->window))) {
474 grn_window_execute(ctx, &(executor_->window));
475 grn_window_reset(ctx, &(executor_->window));
476 }
477 return;
478 }
479
480 if (executor_->values.n == 0) {
481 executor_->values.n = n_group_keys;
482 executor_->values.previous = GRN_MALLOCN(grn_obj, n_group_keys);
483 executor_->values.current = GRN_MALLOCN(grn_obj, n_group_keys);
484 for (size_t i = 0; i < n_group_keys; ++i) {
485 GRN_VOID_INIT(&(executor_->values.previous[i]));
486 GRN_VOID_INIT(&(executor_->values.current[i]));
487 }
488 }
489 if (n_group_keys != executor_->values.n) {
490 ERR(GRN_INVALID_ARGUMENT,
491 "%.*s%s the number of group keys in tables is erratic: "
492 "<%" GRN_FMT_SIZE ">: <%" GRN_FMT_SIZE ">",
493 static_cast<int>(GRN_TEXT_LEN(&(executor_->tag))),
494 GRN_TEXT_VALUE(&(executor_->tag)),
495 tag_,
496 n_group_keys,
497 executor_->values.n);
498 return;
499 }
500
501 for (int64_t i = 0; i < n_rows; ++i) {
502 const auto index = raw_indices[i];
503 const auto source_id = raw_source_ids[index];
504 const auto table_index = unpack_source_id_table_index(source_id);
505 const auto record_id = unpack_source_id_record_id(source_id);
506
507 auto table = GRN_PTR_VALUE_AT(&(executor_->tables), table_index);
508 const auto is_context_table =
509 GRN_BOOL_VALUE_AT(&(executor_->is_context_tables), table_index);
510 auto window_function_call =
511 GRN_PTR_VALUE_AT(&(executor_->window_function_calls), table_index);
512 auto output_column =
513 GRN_PTR_VALUE_AT(&(executor_->output_columns), table_index);
514
515 auto is_group_key_changed = false;
516 for (size_t j = 0; j < n_group_keys; j++) {
517 auto reverse_j = n_group_keys - j - 1;
518 auto previous_value = &(executor_->values.previous[reverse_j]);
519 auto current_value = &(executor_->values.current[reverse_j]);
520 // +1 is for the first "_source_id" column
521 auto group_key = record_batch->column(reverse_j + 1).get();
522
523 if (is_group_key_changed) {
524 GRN_BULK_REWIND(previous_value);
525 grn::arrow::get_value(ctx_, group_key, index, previous_value);
526 } else {
527 GRN_BULK_REWIND(current_value);
528 grn::arrow::get_value(ctx_, group_key, index, current_value);
529 if ((GRN_BULK_VSIZE(current_value) !=
530 GRN_BULK_VSIZE(previous_value)) ||
531 (memcmp(GRN_BULK_HEAD(current_value),
532 GRN_BULK_HEAD(previous_value),
533 GRN_BULK_VSIZE(current_value)) != 0)) {
534 is_group_key_changed = true;
535 grn_bulk_write_from(ctx,
536 previous_value,
537 GRN_BULK_HEAD(current_value),
538 0,
539 GRN_BULK_VSIZE(current_value));
540 }
541 }
542 }
543
544 if (is_group_key_changed &&
545 !grn_window_is_empty(ctx, &(executor_->window))) {
546 grn_window_execute(ctx, &(executor_->window));
547 if (ctx_->rc != GRN_SUCCESS) {
548 break;
549 }
550 grn_window_reset(ctx, &(executor_->window));
551 }
552 grn_window_add_record(ctx,
553 &(executor_->window),
554 table,
555 is_context_table,
556 record_id,
557 window_function_call,
558 output_column,
559 &(executor_->context.key_columns));
560 if (ctx_->rc != GRN_SUCCESS) {
561 break;
562 }
563 }
564 if (ctx_->rc == GRN_SUCCESS &&
565 !grn_window_is_empty(ctx, &(executor_->window))) {
566 grn_window_execute(ctx, &(executor_->window));
567 grn_window_reset(ctx, &(executor_->window));
568 }
569 }
570
571 private:
572 std::shared_ptr<::arrow::RecordBatch> build_record_batch() {
573 std::vector<std::shared_ptr<::arrow::Field>> fields;
574 fields.push_back(::arrow::field("_source_id",
575 ::arrow::uint64(),
576 false));
577 std::unique_ptr<::arrow::ArrayBuilder> id_builder;
578 auto status = ::arrow::MakeBuilder(::arrow::default_memory_pool(),
579 ::arrow::uint64(),
580 &id_builder);
581 if (!grnarrow::check(ctx_,
582 status,
583 executor_tag_,
584 tag_,
585 " failed to make builder for source ID")) {
586 return nullptr;
587 }
588
589 auto id_builder_raw =
590 static_cast<::arrow::UInt64Builder *>(id_builder.get());
591 std::vector<std::shared_ptr<::arrow::Array>> arrays;
592 const auto n_tables = GRN_PTR_VECTOR_SIZE(&(executor_->tables));
593 for (size_t i = 0; i < n_tables; ++i) {
594 grn_obj *table = GRN_PTR_VALUE_AT(&(executor_->tables), i);
595 GRN_TABLE_EACH_BEGIN(ctx_, table, cursor, id) {
596 uint64_t source_id = pack_source_id(static_cast<uint32_t>(i), id);
597 status = id_builder_raw->Append(source_id);
598 if (!grnarrow::check(ctx_,
599 status,
600 executor_tag_,
601 tag_,
602 " failed to append a source ID")) {
603 break;
604 }
605 } GRN_TABLE_EACH_END(ctx_, cursor);
606 if (ctx_->rc != GRN_SUCCESS) {
607 return nullptr;
608 }
609 if (!parse_keys(table,
610 &(executor_->group_keys),
611 "group",
612 group_keys_builders_)) {
613 return nullptr;
614 }
615 if (!parse_keys(table,
616 &(executor_->sort_keys),
617 "sort",
618 sort_keys_builders_)) {
619 return nullptr;
620 }
621 }
622 auto id_array_result = id_builder_raw->Finish();
623 if (!grnarrow::check(ctx_,
624 id_array_result,
625 executor_tag_,
626 tag_,
627 " failed to build source id array")) {
628 return nullptr;
629 }
630 arrays.push_back(*id_array_result);
631 if (!build_keys(group_keys_builders_,
632 0,
633 "group",
634 arrays,
635 fields)) {
636 return nullptr;
637 }
638 if (!build_keys(sort_keys_builders_,
639 group_keys_builders_.size(),
640 "sort",
641 arrays,
642 fields)) {
643 return nullptr;
644 }
645 return ::arrow::RecordBatch::Make(::arrow::schema(fields),
646 arrays[0]->length(),
647 arrays);
648 }
649
650 bool parse_keys(grn_obj *table,
651 grn_obj *text,
652 const char *type_name,
653 std::vector<std::unique_ptr<grn::arrow::ArrayBuilder>>& builders) {
654 auto ctx = ctx_;
655 if (GRN_TEXT_LEN(text) == 0) {
656 return true;
657 }
658 unsigned int n_sort_keys = 0;
659 auto sort_keys = grn_table_sort_key_from_str(ctx_,
660 GRN_TEXT_VALUE(text),
661 GRN_TEXT_LEN(text),
662 table,
663 &n_sort_keys);
664 if (!sort_keys) {
665 ERR(ctx->rc,
666 "%.*s%s failed to parse %s keys: <%.*s>",
667 static_cast<int>(GRN_TEXT_LEN(&(executor_->tag))),
668 GRN_TEXT_VALUE(&(executor_->tag)),
669 tag_,
670 type_name,
671 static_cast<int>(GRN_TEXT_LEN(text)),
672 GRN_TEXT_VALUE(text));
673 return false;
674 }
675 for (unsigned int i = 0; i < n_sort_keys; ++i) {
676 const auto& sort_key = sort_keys[i];
677 GRN_PTR_PUT(ctx_, &(executor_->context.key_columns), sort_key.key);
678 if (builders.size() < n_sort_keys) {
679 builders.push_back(std::make_unique<grn::arrow::ArrayBuilder>(ctx));
680 grn::TextBulk name(ctx);
681 grn_obj_to_script_syntax(ctx, sort_key.key, *name);
682 arrow_sort_keys_.emplace_back(
683 std::string("_") + type_name + "_" + name.value(),
684 sort_key.flags & GRN_TABLE_SORT_DESC ?
685 ::arrow::compute::SortOrder::Descending :
686 ::arrow::compute::SortOrder::Ascending);
687 }
688 auto& builder = builders[i];
689 auto cursor = grn_table_cursor_open(ctx, table,
690 NULL, 0,
691 NULL, 0,
692 0, -1,
693 GRN_CURSOR_ASCENDING);
694 auto status = builder->add_column(sort_key.key, cursor);
695 if (!grnarrow::check(ctx,
696 status,
697 executor_tag_,
698 tag_,
699 " failed to add ",
700 type_name,
701 " key values: <",
702 arrow_sort_keys_[i].target.ToString(),
703 ">")) {
704 return false;
705 }
706 grn_table_cursor_close(ctx, cursor);
707 }
708 grn_table_sort_key_close(ctx, sort_keys, n_sort_keys);
709 return true;
710 }
711
712 bool build_keys(std::vector<std::unique_ptr<grn::arrow::ArrayBuilder>> &builders,
713 size_t arrow_sort_keys_offset,
714 const char *type_name,
715 std::vector<std::shared_ptr<::arrow::Array>> &arrays,
716 std::vector<std::shared_ptr<::arrow::Field>> &fields) {
717 for (size_t i = 0; i < builders.size(); ++i) {
718 auto &builder = builders[i];
719 const auto &arrow_sort_key =
720 arrow_sort_keys_[arrow_sort_keys_offset + i];
721 auto array_result = builder->finish();
722 if (!grnarrow::check(ctx_,
723 array_result,
724 executor_tag_,
725 tag_,
726 " failed to build ",
727 type_name,
728 " key array: <",
729 arrow_sort_key.target.ToString(),
730 ">")) {
731 return false;
732 }
733 auto array = *array_result;
734 arrays.push_back(array);
735 auto field_name = *(arrow_sort_key.target.name());
736 fields.push_back(::arrow::field(field_name,
737 array->type(),
738 false));
739 }
740 return true;
741 }
742
743 grn_ctx *ctx_;
744 grn_window_function_executor *executor_;
745 std::string_view executor_tag_;
746 const char *tag_;
747 std::vector<std::unique_ptr<grn::arrow::ArrayBuilder>> sort_keys_builders_;
748 std::vector<std::unique_ptr<grn::arrow::ArrayBuilder>> group_keys_builders_;
749 std::vector<GrnSortKeys> grn_sort_keys_vector_;
750 std::vector<GrnSortKeys> grn_group_keys_vector_;
751 std::vector<::arrow::compute::SortKey> arrow_sort_keys_;
752 };
753 }
754 #endif
755
756 static void
757 grn_window_function_executor_execute_all_tables(
758 grn_ctx *ctx,
759 grn_window_function_executor *executor,
760 const char *tag)
761 {
762 #ifdef GRN_WITH_APACHE_ARROW
763 AllTablesExecutor all_tables_executor(ctx, executor, tag);
764 all_tables_executor.execute();
765 #endif
766 }
767
768 static void
769 grn_window_function_executor_execute_per_table(
770 grn_ctx *ctx,
771 grn_window_function_executor *executor,
772 const char *tag)
773 {
774 const bool is_ascending =
775 grn_window_function_executor_is_ascending(ctx, executor);
776 const size_t n_tables = GRN_PTR_VECTOR_SIZE(&(executor->tables));
777 bool in_before_context = true;
778 bool in_after_context = false;
779 for (size_t i = 0; i < n_tables; i++) {
780 size_t nth_table = (is_ascending ? i : (n_tables - i - 1));
781 grn_obj *table = GRN_PTR_VALUE_AT(&(executor->tables), nth_table);
782 const bool is_context_table =
783 GRN_BOOL_VALUE_AT(&(executor->is_context_tables), nth_table);
784 if (in_before_context) {
785 if (!is_context_table) {
786 in_before_context = false;
787 }
788 } else if (!in_after_context) {
789 if (is_context_table) {
790 in_after_context = true;
791 }
792 }
793
794 grn_obj *output_column =
795 GRN_PTR_VALUE_AT(&(executor->output_columns), nth_table);
796 grn_obj *window_function_call =
797 GRN_PTR_VALUE_AT(&(executor->window_function_calls), nth_table);
798
799 unsigned int n_sort_keys = 0;
800 grn_table_sort_key *sort_keys = NULL;
801 if (GRN_TEXT_LEN(&(executor->sort_keys)) > 0) {
802 sort_keys =
803 grn_table_sort_key_from_str(
804 ctx,
805 GRN_TEXT_VALUE(&(executor->sort_keys)),
806 static_cast<unsigned int>(GRN_TEXT_LEN(&(executor->sort_keys))),
807 table,
808 &n_sort_keys);
809 if (!sort_keys) {
810 ERR(ctx->rc,
811 "%.*s%s failed to parse sort keys: <%.*s>",
812 (int)GRN_TEXT_LEN(&(executor->tag)),
813 GRN_TEXT_VALUE(&(executor->tag)),
814 tag,
815 (int)GRN_TEXT_LEN(&(executor->sort_keys)),
816 GRN_TEXT_VALUE(&(executor->sort_keys)));
817 return;
818 }
819 if (executor->context.sort_keys) {
820 grn_table_sort_key_close(
821 ctx,
822 executor->context.sort_keys,
823 static_cast<uint32_t>(executor->context.n_sort_keys));
824 }
825 executor->context.sort_keys = sort_keys;
826 executor->context.n_sort_keys = n_sort_keys;
827 }
828
829 unsigned int n_group_keys = 0;
830 grn_table_sort_key *group_keys = NULL;
831 if (GRN_TEXT_LEN(&(executor->group_keys)) > 0) {
832 group_keys =
833 grn_table_sort_key_from_str(
834 ctx,
835 GRN_TEXT_VALUE(&(executor->group_keys)),
836 static_cast<unsigned int>(GRN_TEXT_LEN(&(executor->group_keys))),
837 table,
838 &n_group_keys);
839 if (!group_keys) {
840 ERR(ctx->rc,
841 "%.*s%s failed to parse group keys: <%.*s>",
842 (int)GRN_TEXT_LEN(&(executor->tag)),
843 GRN_TEXT_VALUE(&(executor->tag)),
844 tag,
845 (int)GRN_TEXT_LEN(&(executor->group_keys)),
846 GRN_TEXT_VALUE(&(executor->group_keys)));
847 return;
848 }
849 if (executor->context.group_keys) {
850 grn_table_sort_key_close(
851 ctx,
852 executor->context.group_keys,
853 static_cast<uint32_t>(executor->context.n_group_keys));
854 }
855 executor->context.group_keys = group_keys;
856 executor->context.n_group_keys = n_group_keys;
857 }
858
859 const size_t n_window_sort_keys = n_sort_keys + n_group_keys;
860 if (executor->context.n_window_sort_keys < n_window_sort_keys) {
861 if (executor->context.window_sort_keys) {
862 GRN_FREE(executor->context.window_sort_keys);
863 }
864 executor->context.window_sort_keys =
865 GRN_MALLOCN(grn_table_sort_key, n_window_sort_keys);
866 if (!executor->context.window_sort_keys) {
867 grn_rc rc = ctx->rc;
868 char message[GRN_CTX_MSGSIZE];
869 if (rc == GRN_SUCCESS) {
870 rc = GRN_NO_MEMORY_AVAILABLE;
871 }
872 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
873 ERR(rc,
874 "%.*s%s failed to allocate internal sort keys: %s",
875 (int)GRN_TEXT_LEN(&(executor->tag)),
876 GRN_TEXT_VALUE(&(executor->tag)),
877 tag,
878 message);
879 return;
880 }
881 executor->context.n_window_sort_keys = n_window_sort_keys;
882 }
883 grn_table_sort_key *window_sort_keys = executor->context.window_sort_keys;
884 grn_obj *key_columns = &(executor->context.key_columns);
885 for (size_t j = 0; j < n_group_keys; j++) {
886 window_sort_keys[j] = group_keys[j];
887 GRN_PTR_PUT(ctx, key_columns, window_sort_keys[j].key);
888 }
889 for (size_t j = 0; j < n_sort_keys; j++) {
890 window_sort_keys[j + n_group_keys] = sort_keys[j];
891 GRN_PTR_PUT(ctx, key_columns, window_sort_keys[j + n_group_keys].key);
892 }
893
894 grn_obj *sorted = grn_table_create(ctx,
895 NULL, 0, NULL,
896 GRN_OBJ_TABLE_NO_KEY,
897 NULL,
898 table);
899 if (!sorted) {
900 grn_rc rc = ctx->rc;
901 char errbuf[GRN_CTX_MSGSIZE];
902 if (rc == GRN_SUCCESS) {
903 rc = GRN_NO_MEMORY_AVAILABLE;
904 }
905 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
906 ERR(rc,
907 "%.*s%s failed to allocate table to store sorted result: %s",
908 (int)GRN_TEXT_LEN(&(executor->tag)),
909 GRN_TEXT_VALUE(&(executor->tag)),
910 tag,
911 errbuf);
912 return;
913 }
914 if (executor->context.sorted) {
915 grn_obj_close(ctx, executor->context.sorted);
916 }
917 executor->context.sorted = sorted;
918 grn_table_sort(ctx,
919 table,
920 0, -1,
921 sorted,
922 window_sort_keys,
923 static_cast<int>(n_window_sort_keys));
924
925 grn_window_set_is_sorted(ctx, &(executor->window), n_sort_keys > 0);
926 if (n_group_keys > 0) {
927 if (executor->values.n == 0) {
928 executor->values.n = n_group_keys;
929 executor->values.previous = GRN_MALLOCN(grn_obj, n_group_keys);
930 executor->values.current = GRN_MALLOCN(grn_obj, n_group_keys);
931 for (size_t j = 0; j < n_group_keys; j++) {
932 GRN_VOID_INIT(&(executor->values.previous[j]));
933 GRN_VOID_INIT(&(executor->values.current[j]));
934 }
935 }
936 if (n_group_keys != executor->values.n) {
937 ERR(GRN_INVALID_ARGUMENT,
938 "%.*s%s the number of group keys in tables is erratic: "
939 "<%u>: <%" GRN_FMT_SIZE ">",
940 (int)GRN_TEXT_LEN(&(executor->tag)),
941 GRN_TEXT_VALUE(&(executor->tag)),
942 tag,
943 n_group_keys,
944 executor->values.n);
945 return;
946 }
947
948 GRN_TABLE_EACH_BEGIN(ctx, sorted, cursor, id) {
949 void *value;
950 grn_table_cursor_get_value(ctx, cursor, &value);
951 grn_id record_id = *((grn_id *)value);
952
953 bool is_group_key_changed = false;
954 for (size_t j = 0; j < n_group_keys; j++) {
955 size_t reverse_j = n_group_keys - j - 1;
956 grn_obj *previous_value = &(executor->values.previous[reverse_j]);
957 grn_obj *current_value = &(executor->values.current[reverse_j]);
958 grn_obj *group_key = group_keys[reverse_j].key;
959
960 if (is_group_key_changed) {
961 GRN_BULK_REWIND(previous_value);
962 grn_obj_get_value(ctx, group_key, record_id, previous_value);
963 } else {
964 GRN_BULK_REWIND(current_value);
965 grn_obj_get_value(ctx, group_key, record_id, current_value);
966 if ((GRN_BULK_VSIZE(current_value) !=
967 GRN_BULK_VSIZE(previous_value)) ||
968 (memcmp(GRN_BULK_HEAD(current_value),
969 GRN_BULK_HEAD(previous_value),
970 GRN_BULK_VSIZE(current_value)) != 0)) {
971 is_group_key_changed = true;
972 grn_bulk_write_from(ctx,
973 previous_value,
974 GRN_BULK_HEAD(current_value),
975 0,
976 GRN_BULK_VSIZE(current_value));
977 }
978 }
979 }
980
981 if (is_group_key_changed &&
982 !grn_window_is_empty(ctx, &(executor->window))) {
983 if (!in_before_context) {
984 grn_window_execute(ctx, &(executor->window));
985 if (ctx->rc != GRN_SUCCESS) {
986 break;
987 }
988 }
989 grn_window_reset(ctx, &(executor->window));
990 if (in_after_context) {
991 break;
992 }
993 }
994 grn_window_add_record(ctx,
995 &(executor->window),
996 table,
997 is_context_table,
998 record_id,
999 window_function_call,
1000 output_column,
1001 key_columns);
1002 if (ctx->rc != GRN_SUCCESS) {
1003 break;
1004 }
1005 } GRN_TABLE_EACH_END(ctx, cursor);
1006 } else {
1007 GRN_TABLE_EACH_BEGIN(ctx, sorted, cursor, id) {
1008 void *value;
1009 grn_id record_id;
1010
1011 grn_table_cursor_get_value(ctx, cursor, &value);
1012 record_id = *((grn_id *)value);
1013 grn_window_add_record(ctx,
1014 &(executor->window),
1015 table,
1016 is_context_table,
1017 record_id,
1018 window_function_call,
1019 output_column,
1020 key_columns);
1021 if (ctx->rc != GRN_SUCCESS) {
1022 break;
1023 }
1024 } GRN_TABLE_EACH_END(ctx, cursor);
1025 }
1026 }
1027
1028 if (ctx->rc == GRN_SUCCESS &&
1029 !grn_window_is_empty(ctx, &(executor->window))) {
1030 grn_window_execute(ctx, &(executor->window));
1031 grn_window_reset(ctx, &(executor->window));
1032 }
1033 }
1034
1035 grn_rc
1036 grn_window_function_executor_execute(grn_ctx *ctx,
1037 grn_window_function_executor *executor)
1038 {
1039 const char *tag = "[window-function-executor][execute]";
1040
1041 GRN_API_ENTER;
1042
1043 if (!executor) {
1044 ERR(GRN_INVALID_ARGUMENT,
1045 "%.*s%s executor is NULL",
1046 (int)GRN_TEXT_LEN(&(executor->tag)),
1047 GRN_TEXT_VALUE(&(executor->tag)),
1048 tag);
1049 GRN_API_RETURN(ctx->rc);
1050 }
1051
1052 grn_obj *source = &(executor->source);
1053 if (GRN_TEXT_LEN(source) == 0) {
1054 ERR(GRN_INVALID_ARGUMENT,
1055 "%.*s%s no source",
1056 (int)GRN_TEXT_LEN(&(executor->tag)),
1057 GRN_TEXT_VALUE(&(executor->tag)),
1058 tag);
1059 GRN_API_RETURN(ctx->rc);
1060 }
1061
1062 grn_obj *output_column_name = &(executor->output_column_name);
1063 if (GRN_TEXT_LEN(output_column_name) == 0) {
1064 ERR(GRN_INVALID_ARGUMENT,
1065 "%.*s%s no output column",
1066 (int)GRN_TEXT_LEN(&(executor->tag)),
1067 GRN_TEXT_VALUE(&(executor->tag)),
1068 tag);
1069 GRN_API_RETURN(ctx->rc);
1070 }
1071
1072 const size_t n_tables = GRN_PTR_VECTOR_SIZE(&(executor->tables));
1073 if (n_tables == 0) {
1074 GRN_API_RETURN(ctx->rc);
1075 }
1076
1077 grn_window_function_executor_rewind(ctx, executor);
1078
1079 for (size_t i = 0; i < n_tables; i++) {
1080 grn_obj *table = GRN_PTR_VALUE_AT(&(executor->tables), i);
1081 grn_obj *output_column =
1082 grn_obj_column(ctx,
1083 table,
1084 GRN_TEXT_VALUE(output_column_name),
1085 static_cast<uint32_t>(GRN_TEXT_LEN(output_column_name)));
1086 if (!output_column) {
1087 char table_name[GRN_TABLE_MAX_KEY_SIZE];
1088 int table_name_size;
1089 table_name_size = grn_obj_name(ctx,
1090 table,
1091 table_name,
1092 GRN_TABLE_MAX_KEY_SIZE);
1093 if (table_name_size == 0) {
1094 grn_strcpy(table_name, GRN_TABLE_MAX_KEY_SIZE, "(anonymous)");
1095 table_name_size = static_cast<int>(strlen(table_name));
1096 }
1097 ERR(GRN_INVALID_ARGUMENT,
1098 "%.*s%s output column doesn't exist: <%.*s>: <%.*s>",
1099 (int)GRN_TEXT_LEN(&(executor->tag)),
1100 GRN_TEXT_VALUE(&(executor->tag)),
1101 tag,
1102 table_name_size,
1103 table_name,
1104 (int)GRN_TEXT_LEN(output_column_name),
1105 GRN_TEXT_VALUE(output_column_name));
1106 GRN_API_RETURN(ctx->rc);
1107 }
1108 GRN_PTR_PUT(ctx, &(executor->output_columns), output_column);
1109
1110 grn_obj *window_function_call;
1111 grn_obj *record;
1112 GRN_EXPR_CREATE_FOR_QUERY(ctx, table, window_function_call, record);
1113 if (!window_function_call) {
1114 char message[GRN_CTX_MSGSIZE];
1115 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1116 ERR(GRN_INVALID_ARGUMENT,
1117 "%.*s%s failed to create expression to compile window function call: "
1118 "%s",
1119 (int)GRN_TEXT_LEN(&(executor->tag)),
1120 GRN_TEXT_VALUE(&(executor->tag)),
1121 tag,
1122 message);
1123 GRN_API_RETURN(ctx->rc);
1124 }
1125 GRN_PTR_PUT(ctx, &(executor->window_function_calls), window_function_call);
1126 grn_expr_parse(ctx,
1127 window_function_call,
1128 GRN_TEXT_VALUE(source),
1129 static_cast<unsigned int>(GRN_TEXT_LEN(source)),
1130 NULL,
1131 GRN_OP_MATCH,
1132 GRN_OP_AND,
1133 GRN_EXPR_SYNTAX_SCRIPT);
1134 if (ctx->rc != GRN_SUCCESS) {
1135 char message[GRN_CTX_MSGSIZE];
1136 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
1137 ERR(ctx->rc,
1138 "%.*s%s failed to parse window function call: <%.*s>: %s",
1139 (int)GRN_TEXT_LEN(&(executor->tag)),
1140 GRN_TEXT_VALUE(&(executor->tag)),
1141 tag,
1142 (int)GRN_TEXT_LEN(source),
1143 GRN_TEXT_VALUE(source),
1144 message);
1145 GRN_API_RETURN(ctx->rc);
1146 }
1147 }
1148
1149 bool process_all_tables_at_once = false;
1150 #ifdef GRN_WITH_APACHE_ARROW
1151 process_all_tables_at_once =
1152 grn_window_function_executor_all_tables_at_once_enable &&
1153 (n_tables > 1);
1154 #endif
1155 if (process_all_tables_at_once) {
1156 grn_window_function_executor_execute_all_tables(ctx, executor, tag);
1157 } else {
1158 grn_window_function_executor_execute_per_table(ctx, executor, tag);
1159 }
1160
1161 GRN_API_RETURN(ctx->rc);
1162 }
1163 }