Debian Codegraph @

lib/window_function.c

raw symbols SCIP
1 /*
2 Copyright(C) 2016-2017 Brazil
3 Copyright(C) 2019-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_ctx.h"
21 #include "grn_db.h"
22 #include "grn_expr.h"
23 #include "grn_window_function.h"
24
25 #include <string.h>
26
27 static void
28 grn_window_shard_init(grn_ctx *ctx,
29 grn_window_shard *shard,
30 grn_obj *table,
31 bool is_context_table,
32 grn_obj *window_function_call,
33 grn_obj *output_column,
34 grn_obj *key_columns)
35 {
36 shard->table = table;
37 shard->is_context_table = is_context_table;
38 shard->window_function_call = window_function_call;
39 grn_expr *expr = (grn_expr *)(window_function_call);
40 shard->window_function = (grn_proc *)(expr->codes[0].value);
41 GRN_PTR_INIT(&(shard->arguments), GRN_OBJ_VECTOR, GRN_ID_NIL);
42 uint32_t n_arguments = expr->codes_curr - 1;
43 for (uint32_t i = 1; i < n_arguments; i++) {
44 /* TODO: Check op. */
45 GRN_PTR_PUT(ctx, &(shard->arguments), expr->codes[i].value);
46 }
47 shard->output_column = output_column;
48 GRN_PTR_INIT(&(shard->key_columns), GRN_OBJ_VECTOR, GRN_ID_NIL);
49 size_t n_key_columns = GRN_PTR_VECTOR_SIZE(key_columns);
50 for (size_t i = 0; i < n_key_columns; i++) {
51 grn_obj *key_column = GRN_PTR_VALUE_AT(key_columns, i);
52 GRN_PTR_PUT(ctx, &(shard->key_columns), key_column);
53 }
54 GRN_RECORD_INIT(&(shard->ids), GRN_OBJ_VECTOR, grn_obj_id(ctx, table));
55 shard->current_index = -1;
56 }
57
58 static void
59 grn_window_shard_fin(grn_ctx *ctx,
60 grn_window_shard *shard)
61 {
62 GRN_OBJ_FIN(ctx, &(shard->arguments));
63 GRN_OBJ_FIN(ctx, &(shard->key_columns));
64 GRN_OBJ_FIN(ctx, &(shard->ids));
65 }
66
67 grn_rc
68 grn_window_init(grn_ctx *ctx,
69 grn_window *window)
70 {
71 GRN_API_ENTER;
72
73 window->shards = NULL;
74 window->n_shards = 0;
75 window->current_shard = -1;
76 window->current_id = GRN_ID_NIL;
77 window->direction = GRN_WINDOW_DIRECTION_ASCENDING;
78 window->values.n = 0;
79 window->values.buffer1 = NULL;
80 window->values.buffer2 = NULL;
81 window->values.previous = NULL;
82 window->values.current = NULL;
83 window->is_value_changed_computed = false;
84 window->is_value_changed = false;
85
86 GRN_API_RETURN(GRN_SUCCESS);
87 }
88
89 grn_rc
90 grn_window_fin(grn_ctx *ctx, grn_window *window)
91 {
92 GRN_API_ENTER;
93
94 grn_window_reset(ctx, window);
95
96 if (window->values.buffer1) {
97 for (size_t i = 0; i < window->values.n; i++) {
98 GRN_OBJ_FIN(ctx, &(window->values.buffer1[i]));
99 }
100 GRN_FREE(window->values.buffer1);
101 }
102 if (window->values.buffer2) {
103 for (size_t i = 0; i < window->values.n; i++) {
104 GRN_OBJ_FIN(ctx, &(window->values.buffer2[i]));
105 }
106 GRN_FREE(window->values.buffer2);
107 }
108
109 GRN_API_RETURN(ctx->rc);
110 }
111
112 grn_id
113 grn_window_next(grn_ctx *ctx, grn_window *window)
114 {
115 GRN_API_ENTER;
116
117 if (!window) {
118 GRN_API_RETURN(GRN_ID_NIL);
119 }
120
121 window->is_value_changed_computed = false;
122
123 if (window->current_shard < 0) {
124 GRN_API_RETURN(GRN_ID_NIL);
125 }
126
127 grn_window_shard *shard = &(window->shards[window->current_shard]);
128 if (window->direction == GRN_WINDOW_DIRECTION_ASCENDING) {
129 if ((size_t)(shard->current_index) >=
130 GRN_RECORD_VECTOR_SIZE(&(shard->ids))) {
131 if ((size_t)(window->current_shard + 1) < window->n_shards) {
132 window->current_shard++;
133 shard = &(window->shards[window->current_shard]);
134 } else {
135 GRN_API_RETURN(GRN_ID_NIL);
136 }
137 }
138 } else {
139 if (shard->current_index < 0) {
140 if (window->current_shard > 0) {
141 window->current_shard--;
142 shard = &(window->shards[window->current_shard]);
143 } else {
144 GRN_API_RETURN(GRN_ID_NIL);
145 }
146 }
147 }
148
149 grn_id next_id = GRN_RECORD_VALUE_AT(&(shard->ids), shard->current_index);
150 if (window->direction == GRN_WINDOW_DIRECTION_ASCENDING) {
151 shard->current_index++;
152 } else {
153 shard->current_index--;
154 }
155
156 window->current_id = next_id;
157 GRN_API_RETURN(window->current_id);
158 }
159
160 grn_rc
161 grn_window_rewind(grn_ctx *ctx, grn_window *window)
162 {
163 GRN_API_ENTER;
164
165 if (!window) {
166 ERR(GRN_INVALID_ARGUMENT, "[window][rewind] window is NULL");
167 GRN_API_RETURN(ctx->rc);
168 }
169
170 if (window->direction == GRN_WINDOW_DIRECTION_ASCENDING) {
171 window->current_shard = 0;
172 for (size_t i = 0; i < window->n_shards; i++) {
173 grn_window_shard *shard = &(window->shards[i]);
174 shard->current_index = 0;
175 }
176 } else {
177 window->current_shard = (ssize_t)(window->n_shards - 1);
178 for (size_t i = 0; i < window->n_shards; i++) {
179 grn_window_shard *shard = &(window->shards[i]);
180 shard->current_index =
181 (ssize_t)(GRN_RECORD_VECTOR_SIZE(&(shard->ids)) - 1);
182 }
183 }
184
185 if (window->values.buffer1) {
186 for (size_t i = 0; i < window->values.n; i++) {
187 GRN_BULK_REWIND(&(window->values.buffer1[i]));
188 }
189 }
190 if (window->values.buffer2) {
191 for (size_t i = 0; i < window->values.n; i++) {
192 GRN_BULK_REWIND(&(window->values.buffer2[i]));
193 }
194 }
195
196 window->is_value_changed_computed = false;
197
198 GRN_API_RETURN(GRN_SUCCESS);
199 }
200
201 grn_obj *
202 grn_window_get_table(grn_ctx *ctx, grn_window *window)
203 {
204 GRN_API_ENTER;
205
206 if (!window) {
207 ERR(GRN_INVALID_ARGUMENT, "[window][table][get] window is NULL");
208 GRN_API_RETURN(NULL);
209 }
210
211 if (window->current_shard < 0) {
212 GRN_API_RETURN(NULL);
213 }
214
215 grn_window_shard *shard = &(window->shards[window->current_shard]);
216 GRN_API_RETURN(shard->table);
217 }
218
219 bool
220 grn_window_is_context_table(grn_ctx *ctx, grn_window *window)
221 {
222 GRN_API_ENTER;
223
224 if (!window) {
225 ERR(GRN_INVALID_ARGUMENT, "[window][is-context-table] window is NULL");
226 GRN_API_RETURN(false);
227 }
228
229 if (window->current_shard < 0) {
230 GRN_API_RETURN(false);
231 }
232
233 grn_window_shard *shard = &(window->shards[window->current_shard]);
234 GRN_API_RETURN(shard->is_context_table);
235 }
236
237 grn_obj *
238 grn_window_get_output_column(grn_ctx *ctx, grn_window *window)
239 {
240 GRN_API_ENTER;
241
242 if (!window) {
243 ERR(GRN_INVALID_ARGUMENT, "[window][output-column][get] window is NULL");
244 GRN_API_RETURN(NULL);
245 }
246
247 if (window->current_shard < 0) {
248 GRN_API_RETURN(NULL);
249 }
250
251 grn_window_shard *shard = &(window->shards[window->current_shard]);
252 if (shard->is_context_table) {
253 GRN_API_RETURN(NULL);
254 } else {
255 GRN_API_RETURN(shard->output_column);
256 }
257 }
258
259 size_t
260 grn_window_get_n_arguments(grn_ctx *ctx, grn_window *window)
261 {
262 GRN_API_ENTER;
263
264 if (!window) {
265 ERR(GRN_INVALID_ARGUMENT, "[window][n-arguments][get] window is NULL");
266 GRN_API_RETURN(0);
267 }
268
269 if (window->current_shard < 0) {
270 GRN_API_RETURN(0);
271 }
272
273 grn_window_shard *shard = &(window->shards[window->current_shard]);
274 GRN_API_RETURN(GRN_PTR_VECTOR_SIZE(&(shard->arguments)));
275 }
276
277 grn_obj *
278 grn_window_get_argument(grn_ctx *ctx, grn_window *window, size_t i)
279 {
280 GRN_API_ENTER;
281
282 if (!window) {
283 ERR(GRN_INVALID_ARGUMENT, "[window][argument][get] window is NULL");
284 GRN_API_RETURN(NULL);
285 }
286
287 if (window->current_shard < 0) {
288 GRN_API_RETURN(NULL);
289 }
290
291 grn_window_shard *shard = &(window->shards[window->current_shard]);
292 if (i < GRN_PTR_VECTOR_SIZE(&(shard->arguments))) {
293 GRN_API_RETURN(GRN_PTR_VALUE_AT(&(shard->arguments), i));
294 } else {
295 GRN_API_RETURN(NULL);
296 }
297 }
298
299 grn_obj *
300 grn_window_get_key_columns(grn_ctx *ctx, grn_window *window)
301 {
302 GRN_API_ENTER;
303
304 if (!window) {
305 ERR(GRN_INVALID_ARGUMENT, "[window][n-key-columns][get] window is NULL");
306 GRN_API_RETURN(NULL);
307 }
308
309 if (window->current_shard < 0) {
310 GRN_API_RETURN(NULL);
311 }
312
313 grn_window_shard *shard = &(window->shards[window->current_shard]);
314 GRN_API_RETURN(&(shard->key_columns));
315 }
316
317 grn_rc
318 grn_window_set_direction(grn_ctx *ctx,
319 grn_window *window,
320 grn_window_direction direction)
321 {
322 GRN_API_ENTER;
323
324 const char *tag = "[window][direction][set]";
325 if (!window) {
326 ERR(GRN_INVALID_ARGUMENT, "%s window is NULL", tag);
327 GRN_API_RETURN(ctx->rc);
328 }
329
330 switch (direction) {
331 case GRN_WINDOW_DIRECTION_ASCENDING :
332 window->direction = direction;
333 break;
334 case GRN_WINDOW_DIRECTION_DESCENDING :
335 window->direction = direction;
336 break;
337 default :
338 ERR(GRN_INVALID_ARGUMENT,
339 "%s direction must be "
340 "GRN_WINDOW_DIRECTION_ASCENDING(%d) or "
341 "GRN_WINDOW_DIRECTION_DESCENDING(%d): %d",
342 tag,
343 GRN_WINDOW_DIRECTION_ASCENDING,
344 GRN_WINDOW_DIRECTION_DESCENDING,
345 direction);
346 GRN_API_RETURN(ctx->rc);
347 break;
348 }
349 grn_window_rewind(ctx, window);
350
351 GRN_API_RETURN(GRN_SUCCESS);
352 }
353
354 grn_rc
355 grn_window_reset(grn_ctx *ctx,
356 grn_window *window)
357 {
358 GRN_API_ENTER;
359
360 for (size_t i = 0; i < window->n_shards; i++) {
361 grn_window_shard *shard = &(window->shards[i]);
362 grn_window_shard_fin(ctx, shard);
363 }
364 if (window->shards) {
365 GRN_FREE(window->shards);
366 window->shards = NULL;
367 window->n_shards = 0;
368 window->current_shard = -1;
369 }
370
371 window->current_id = GRN_ID_NIL;
372
373 if (window->values.buffer1) {
374 for (size_t i = 0; i < window->values.n; i++) {
375 GRN_BULK_REWIND(&(window->values.buffer1[i]));
376 }
377 }
378 if (window->values.buffer2) {
379 for (size_t i = 0; i < window->values.n; i++) {
380 GRN_BULK_REWIND(&(window->values.buffer2[i]));
381 }
382 }
383 window->is_value_changed_computed = false;
384 window->is_value_changed = false;
385
386 GRN_API_RETURN(ctx->rc);
387 }
388
389 static bool
390 grn_expr_is_window_function_call(grn_ctx *ctx,
391 grn_obj *window_function_call)
392 {
393 grn_expr *expr = (grn_expr *)window_function_call;
394 grn_expr_code *func;
395 grn_expr_code *call;
396
397 func = &(expr->codes[0]);
398 call = &(expr->codes[expr->codes_curr - 1]);
399
400 if (func->op != GRN_OP_PUSH) {
401 return false;
402 }
403 if (!grn_obj_is_window_function_proc(ctx, func->value)) {
404 return false;
405 }
406
407 if (call->op != GRN_OP_CALL) {
408 return false;
409 }
410 if (call->nargs != (int32_t)(expr->codes_curr - 1)) {
411 return false;
412 }
413
414 return true;
415 }
416
417 static bool
418 grn_window_add_record_validate(grn_ctx *ctx,
419 grn_window *window,
420 grn_obj *table,
421 grn_obj *window_function_call,
422 grn_obj *output_column,
423 grn_obj *key_columns,
424 const char *tag)
425 {
426 if (!table) {
427 ERR(GRN_INVALID_ARGUMENT, "%s table is NULL", tag);
428 return false;
429 }
430
431 if (!grn_expr_is_window_function_call(ctx, window_function_call)) {
432 grn_obj inspected;
433 GRN_TEXT_INIT(&inspected, 0);
434 grn_inspect(ctx, &inspected, window_function_call);
435 ERR(GRN_INVALID_ARGUMENT,
436 "%s must be window function call: %.*s",
437 tag,
438 (int)GRN_TEXT_LEN(&inspected),
439 GRN_TEXT_VALUE(&inspected));
440 GRN_OBJ_FIN(ctx, &inspected);
441 return false;
442 }
443
444 if (!output_column) {
445 ERR(GRN_INVALID_ARGUMENT, "%s output column is NULL", tag);
446 return false;
447 }
448
449 return true;
450 }
451
452 grn_rc
453 grn_window_add_record(grn_ctx *ctx,
454 grn_window *window,
455 grn_obj *table,
456 bool is_context_table,
457 grn_id record_id,
458 grn_obj *window_function_call,
459 grn_obj *output_column,
460 grn_obj *key_columns)
461 {
462 GRN_API_ENTER;
463 const char *tag = "[window][record][add]";
464 if (window->n_shards == 0) {
465 if (!grn_window_add_record_validate(ctx,
466 window,
467 table,
468 window_function_call,
469 output_column,
470 key_columns,
471 tag)) {
472 GRN_API_RETURN(ctx->rc);
473 }
474 window->shards = GRN_MALLOCN(grn_window_shard, 1);
475 grn_window_shard_init(ctx,
476 &(window->shards[0]),
477 table,
478 is_context_table,
479 window_function_call,
480 output_column,
481 key_columns);
482 window->current_shard = 0;
483 window->n_shards = 1;
484 } else if (window->shards[window->n_shards - 1].table != table) {
485 if (!grn_window_add_record_validate(ctx,
486 window,
487 table,
488 window_function_call,
489 output_column,
490 key_columns,
491 tag)) {
492 GRN_API_RETURN(ctx->rc);
493 }
494 const size_t new_n_shards = window->n_shards + 1;
495 grn_window_shard *shards =
496 GRN_REALLOC(window->shards, sizeof(grn_window_shard) * new_n_shards);
497 if (!shards) {
498 grn_rc rc = ctx->rc;
499 if (rc == GRN_SUCCESS) {
500 rc = GRN_NO_MEMORY_AVAILABLE;
501 }
502 char message[GRN_CTX_MSGSIZE];
503 grn_strcpy(message, GRN_CTX_MSGSIZE, ctx->errbuf);
504 ERR(rc,
505 "%s failed to expand shards: %s",
506 tag,
507 message);
508 GRN_API_RETURN(ctx->rc);
509 }
510 window->shards = shards;
511 window->n_shards = new_n_shards;
512 grn_window_shard_init(ctx,
513 &(window->shards[window->n_shards - 1]),
514 table,
515 is_context_table,
516 window_function_call,
517 output_column,
518 key_columns);
519 }
520 GRN_RECORD_PUT(ctx,
521 &(window->shards[window->n_shards - 1].ids),
522 record_id);
523 GRN_API_RETURN(ctx->rc);
524 }
525
526 bool
527 grn_window_is_empty(grn_ctx *ctx,
528 grn_window *window)
529 {
530 GRN_API_ENTER;
531 bool is_empty = true;
532 for (size_t i = 0; i < window->n_shards; i++) {
533 grn_window_shard *shard = &(window->shards[i]);
534 if (GRN_RECORD_VECTOR_SIZE(&(shard->ids)) > 0) {
535 is_empty = false;
536 break;
537 }
538 }
539 GRN_API_RETURN(is_empty);
540 }
541
542 bool
543 grn_window_is_sorted(grn_ctx *ctx, grn_window *window)
544 {
545 GRN_API_ENTER;
546
547 if (!window) {
548 ERR(GRN_INVALID_ARGUMENT, "[window][is-sorted] window is NULL");
549 GRN_API_RETURN(false);
550 }
551
552 GRN_API_RETURN(window->is_sorted);
553 }
554
555 grn_rc
556 grn_window_set_is_sorted(grn_ctx *ctx, grn_window *window, bool is_sorted)
557 {
558 GRN_API_ENTER;
559
560 if (!window) {
561 ERR(GRN_INVALID_ARGUMENT, "[window][is-sorted][set] window is NULL");
562 GRN_API_RETURN(ctx->rc);
563 }
564
565 window->is_sorted = is_sorted;
566
567 GRN_API_RETURN(ctx->rc);
568 }
569
570 bool
571 grn_window_is_value_changed(grn_ctx *ctx, grn_window *window)
572 {
573 GRN_API_ENTER;
574
575 if (!window) {
576 ERR(GRN_INVALID_ARGUMENT, "[window][is-value-changed] window is NULL");
577 GRN_API_RETURN(false);
578 }
579
580 if (window->is_value_changed_computed) {
581 GRN_API_RETURN(window->is_value_changed);
582 }
583
584 grn_obj *key_columns = grn_window_get_key_columns(ctx, window);
585 if (!key_columns) {
586 GRN_API_RETURN(false);
587 }
588
589 if (GRN_PTR_VECTOR_SIZE(key_columns) == 0) {
590 GRN_API_RETURN(false);
591 }
592
593 window->is_value_changed = false;
594 if (window->values.n == 0) {
595 window->values.n = GRN_PTR_VECTOR_SIZE(key_columns);
596 window->values.buffer1 = GRN_MALLOCN(grn_obj, window->values.n);
597 window->values.buffer2 = GRN_MALLOCN(grn_obj, window->values.n);
598 for (size_t i = 0; i < window->values.n; i++) {
599 GRN_VOID_INIT(&(window->values.buffer1[i]));
600 GRN_VOID_INIT(&(window->values.buffer2[i]));
601 }
602 window->values.previous = window->values.buffer1;
603 window->values.current = window->values.buffer2;
604 }
605
606 for (size_t i = 0; i < window->values.n; i++) {
607 grn_obj *key_column = GRN_PTR_VALUE_AT(key_columns, i);
608 grn_obj *value = &(window->values.current[i]);
609 GRN_BULK_REWIND(value);
610 grn_obj_get_value(ctx, key_column, window->current_id, value);
611 }
612 for (size_t i = 0; i < window->values.n; i++) {
613 grn_obj *previous = &(window->values.previous[i]);
614 grn_obj *current = &(window->values.current[i]);
615 if ((GRN_BULK_VSIZE(previous) != GRN_BULK_VSIZE(current)) ||
616 (memcmp(GRN_BULK_HEAD(previous),
617 GRN_BULK_HEAD(current),
618 GRN_BULK_VSIZE(previous)) != 0)) {
619 window->is_value_changed = true;
620 break;
621 }
622 }
623 window->is_value_changed_computed = true;
624 {
625 grn_obj *tmp = window->values.current;
626 window->values.current = window->values.previous;
627 window->values.previous = tmp;
628 }
629
630 GRN_API_RETURN(window->is_value_changed);
631 }
632
633 size_t
634 grn_window_get_size(grn_ctx *ctx,
635 grn_window *window)
636 {
637 GRN_API_ENTER;
638 size_t n_ids = 0;
639 for (size_t i = 0; i < window->n_shards; i++) {
640 grn_window_shard *shard = &(window->shards[i]);
641 n_ids += GRN_RECORD_VECTOR_SIZE(&(shard->ids));
642 }
643 GRN_API_RETURN(n_ids);
644 }
645
646 grn_obj *
647 grn_window_function_create(grn_ctx *ctx,
648 const char *name,
649 int name_size,
650 grn_window_function_func *func)
651 {
652 grn_obj *window_function = NULL;
653
654 GRN_API_ENTER;
655
656 if (name_size == -1) {
657 name_size = (int)strlen(name);
658 }
659
660 window_function = grn_proc_create(ctx,
661 name,
662 name_size,
663 GRN_PROC_WINDOW_FUNCTION,
664 NULL, NULL, NULL, 0, NULL);
665 if (!window_function) {
666 char errbuf[GRN_CTX_MSGSIZE];
667 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
668 ERR(GRN_WINDOW_FUNCTION_ERROR,
669 "[window-function][%.*s] failed to create proc: %s",
670 name_size, name,
671 errbuf);
672 GRN_API_RETURN(NULL);
673 }
674
675 {
676 grn_proc *proc = (grn_proc *)window_function;
677 proc->callbacks.window_function = func;
678 }
679
680 GRN_API_RETURN(window_function);
681 }
682
683 grn_rc
684 grn_window_execute(grn_ctx *ctx, grn_window *window)
685 {
686 GRN_API_ENTER;
687
688 if (window->n_shards == 0) {
689 GRN_API_RETURN(ctx->rc);
690 }
691 bool have_non_context_table = false;
692 size_t i;
693 for (i = 0; i < window->n_shards; i++) {
694 if (!window->shards[i].is_context_table) {
695 have_non_context_table = true;
696 break;
697 }
698 }
699 if (!have_non_context_table) {
700 GRN_API_RETURN(ctx->rc);
701 }
702
703 grn_window_rewind(ctx, window);
704 grn_window_shard *shard = &(window->shards[window->current_shard]);
705 grn_window_function_func *window_function_func =
706 shard->window_function->callbacks.window_function;
707 grn_rc rc = window_function_func(ctx,
708 shard->output_column,
709 window,
710 (grn_obj **)GRN_BULK_HEAD(&(shard->arguments)),
711 (int)GRN_PTR_VECTOR_SIZE(&(shard->arguments)));
712
713 GRN_API_RETURN(rc);
714 }
715
716 /* Deprecated since 9.0.2. */
717 grn_rc
718 grn_table_apply_window_function(grn_ctx *ctx,
719 grn_obj *table,
720 grn_obj *output_column,
721 grn_window_definition *definition,
722 grn_obj *window_function_call)
723 {
724 GRN_API_ENTER;
725
726 if (!table) {
727 ERR(GRN_INVALID_ARGUMENT,
728 "[table][apply][window-function] table is NULL");
729 GRN_API_RETURN(ctx->rc);
730 }
731
732 const size_t n_sort_keys = definition->n_group_keys + definition->n_sort_keys;
733 grn_table_sort_key *sort_keys = GRN_MALLOCN(grn_table_sort_key, n_sort_keys);
734 if (!sort_keys) {
735 grn_rc rc = ctx->rc;
736 char errbuf[GRN_CTX_MSGSIZE];
737 if (rc == GRN_SUCCESS) {
738 rc = GRN_NO_MEMORY_AVAILABLE;
739 }
740 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
741 ERR(rc,
742 "[table][apply][window-function] "
743 "failed to allocate internal sort keys: %s",
744 errbuf);
745 GRN_API_RETURN(ctx->rc);
746 }
747
748 grn_obj key_columns;
749 GRN_PTR_INIT(&key_columns, GRN_OBJ_VECTOR, GRN_ID_NIL);
750 for (size_t i = 0; i < definition->n_group_keys; i++) {
751 sort_keys[i] = definition->group_keys[i];
752 GRN_PTR_PUT(ctx, &key_columns, sort_keys[i].key);
753 }
754 for (size_t i = 0; i < definition->n_sort_keys; i++) {
755 sort_keys[i + definition->n_group_keys] = definition->sort_keys[i];
756 GRN_PTR_PUT(ctx, &key_columns, sort_keys[i + definition->n_group_keys].key);
757 }
758
759 grn_obj *sorted = grn_table_create(ctx,
760 NULL, 0, NULL,
761 GRN_OBJ_TABLE_NO_KEY,
762 NULL,
763 table);
764 if (!sorted) {
765 grn_rc rc = ctx->rc;
766 char errbuf[GRN_CTX_MSGSIZE];
767 if (rc == GRN_SUCCESS) {
768 rc = GRN_NO_MEMORY_AVAILABLE;
769 }
770 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
771 GRN_OBJ_FIN(ctx, &key_columns);
772 GRN_FREE(sort_keys);
773 ERR(rc,
774 "[table][apply][window-function] "
775 "failed to allocate table to store sorted result: %s",
776 errbuf);
777 GRN_API_RETURN(ctx->rc);
778 }
779 grn_table_sort(ctx,
780 table,
781 0, -1,
782 sorted,
783 sort_keys, (int)n_sort_keys);
784
785 grn_window window;
786 grn_window_init(ctx, &window);
787 grn_window_set_is_sorted(ctx, &window, definition->n_sort_keys > 0);
788 if (definition->n_group_keys > 0) {
789 grn_obj *previous_values = GRN_MALLOCN(grn_obj, definition->n_group_keys);
790 grn_obj *current_values = GRN_MALLOCN(grn_obj, definition->n_group_keys);
791
792 const size_t n = definition->n_group_keys;
793 for (size_t i = 0; i < n; i++) {
794 GRN_VOID_INIT(&(previous_values[i]));
795 GRN_VOID_INIT(&(current_values[i]));
796 }
797
798 GRN_TABLE_EACH_BEGIN(ctx, sorted, cursor, id) {
799 void *value;
800 grn_table_cursor_get_value(ctx, cursor, &value);
801 const grn_id record_id = *((grn_id *)value);
802
803 bool is_group_key_changed = false;
804 for (size_t i = 0; i < n; i++) {
805 const size_t reverse_i = n - i - 1;
806 grn_obj *previous_value = &(previous_values[reverse_i]);
807 grn_obj *current_value = &(current_values[reverse_i]);
808 grn_obj *group_key = definition->group_keys[reverse_i].key;
809
810 if (is_group_key_changed) {
811 GRN_BULK_REWIND(previous_value);
812 grn_obj_get_value(ctx, group_key, record_id, previous_value);
813 } else {
814 GRN_BULK_REWIND(current_value);
815 grn_obj_get_value(ctx, group_key, record_id, current_value);
816 if ((GRN_BULK_VSIZE(current_value) !=
817 GRN_BULK_VSIZE(previous_value)) ||
818 (memcmp(GRN_BULK_HEAD(current_value),
819 GRN_BULK_HEAD(previous_value),
820 GRN_BULK_VSIZE(current_value)) != 0)) {
821 is_group_key_changed = true;
822 grn_bulk_write_from(ctx,
823 previous_value,
824 GRN_BULK_HEAD(current_value),
825 0,
826 GRN_BULK_VSIZE(current_value));
827 }
828 }
829 }
830
831 if (is_group_key_changed && !grn_window_is_empty(ctx, &window)) {
832 grn_window_execute(ctx, &window);
833 if (ctx->rc != GRN_SUCCESS) {
834 break;
835 }
836 grn_window_reset(ctx, &window);
837 }
838 grn_window_add_record(ctx,
839 &window,
840 table,
841 false,
842 record_id,
843 window_function_call,
844 output_column,
845 &key_columns);
846 if (ctx->rc != GRN_SUCCESS) {
847 break;
848 }
849 } GRN_TABLE_EACH_END(ctx, cursor);
850 for (size_t i = 0; i < definition->n_group_keys; i++) {
851 GRN_OBJ_FIN(ctx, &(previous_values[i]));
852 GRN_OBJ_FIN(ctx, &(current_values[i]));
853 }
854 GRN_FREE(previous_values);
855 GRN_FREE(current_values);
856 } else {
857 GRN_TABLE_EACH_BEGIN(ctx, sorted, cursor, id) {
858 void *value;
859 grn_table_cursor_get_value(ctx, cursor, &value);
860 const grn_id record_id = *((grn_id *)value);
861 grn_window_add_record(ctx,
862 &window,
863 table,
864 false,
865 record_id,
866 window_function_call,
867 output_column,
868 &key_columns);
869 if (ctx->rc != GRN_SUCCESS) {
870 break;
871 }
872 } GRN_TABLE_EACH_END(ctx, cursor);
873 }
874 if (ctx->rc == GRN_SUCCESS && !grn_window_is_empty(ctx, &window)) {
875 grn_window_execute(ctx, &window);
876 }
877
878 grn_window_fin(ctx, &window);
879
880 grn_obj_close(ctx, sorted);
881
882 GRN_OBJ_FIN(ctx, &key_columns);
883
884 GRN_FREE(sort_keys);
885
886 GRN_API_RETURN(ctx->rc);
887 }