Debian Codegraph @

lib/window_functions.c

raw symbols SCIP
1 /*
2 Copyright(C) 2016-2018 Brazil
3 Copyright(C) 2019-2021 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_db.h"
21 #include "grn_window_functions.h"
22
23 static grn_rc
24 window_record_number(grn_ctx *ctx,
25 grn_obj *first_output_column,
26 grn_window *window,
27 grn_obj **first_args,
28 int first_n_args)
29 {
30 grn_id id;
31 uint32_t nth_record = 1;
32 grn_obj value;
33
34 GRN_UINT32_INIT(&value, 0);
35 while ((id = grn_window_next(ctx, window))) {
36 GRN_UINT32_SET(ctx, &value, nth_record);
37 grn_obj *output_column = grn_window_get_output_column(ctx, window);
38 if (output_column) {
39 grn_obj_set_value(ctx, output_column, id, &value, GRN_OBJ_SET);
40 }
41 nth_record++;
42 }
43 GRN_OBJ_FIN(ctx, &value);
44
45 return GRN_SUCCESS;
46 }
47
48 static grn_rc
49 window_sum(grn_ctx *ctx,
50 grn_obj *first_output_column,
51 grn_window *window,
52 grn_obj **first_args,
53 int first_n_args)
54 {
55 if (first_n_args != 1) {
56 GRN_PLUGIN_ERROR(ctx,
57 GRN_INVALID_ARGUMENT,
58 "window_sum(): wrong number of arguments (%d for 1)",
59 first_n_args);
60 return ctx->rc;
61 }
62
63 grn_obj *first_target = first_args[0];
64 if (!(grn_obj_is_scalar_column(ctx, first_target) ||
65 grn_obj_is_accessor(ctx, first_target))) {
66 grn_obj inspected;
67 GRN_TEXT_INIT(&inspected, 0);
68 grn_inspect(ctx, &inspected, first_target);
69 GRN_PLUGIN_ERROR(ctx,
70 GRN_INVALID_ARGUMENT,
71 "window_sum(): "
72 "the target column must be "
73 "scalar column or accessor: <%.*s>",
74 (int)GRN_TEXT_LEN(&inspected),
75 GRN_TEXT_VALUE(&inspected));
76 GRN_OBJ_FIN(ctx, &inspected);
77 return ctx->rc;
78 }
79
80 const grn_id target_range_id =
81 grn_obj_get_range(ctx, first_target);
82 switch (target_range_id) {
83 case GRN_DB_INT8 :
84 case GRN_DB_INT16 :
85 case GRN_DB_INT32 :
86 case GRN_DB_INT64 :
87 case GRN_DB_UINT8 :
88 case GRN_DB_UINT16 :
89 case GRN_DB_UINT32 :
90 case GRN_DB_UINT64 :
91 case GRN_DB_FLOAT32 :
92 case GRN_DB_FLOAT :
93 break;
94 default :
95 {
96 grn_obj inspected;
97 GRN_TEXT_INIT(&inspected, 0);
98 grn_inspect(ctx, &inspected, first_target);
99 GRN_PLUGIN_ERROR(ctx,
100 GRN_INVALID_ARGUMENT,
101 "window_sum(): "
102 "the target column must be number column: <%.*s>",
103 (int)GRN_TEXT_LEN(&inspected),
104 GRN_TEXT_VALUE(&inspected));
105 GRN_OBJ_FIN(ctx, &inspected);
106 return ctx->rc;
107 }
108 break;
109 }
110
111 grn_obj sum;
112 const grn_id output_column_range_id =
113 grn_obj_get_range(ctx, first_output_column);
114 switch (output_column_range_id) {
115 case GRN_DB_INT8 :
116 case GRN_DB_INT16 :
117 case GRN_DB_INT32 :
118 case GRN_DB_INT64 :
119 GRN_INT64_INIT(&sum, 0);
120 break;
121 case GRN_DB_UINT8 :
122 case GRN_DB_UINT16 :
123 case GRN_DB_UINT32 :
124 case GRN_DB_UINT64 :
125 GRN_UINT64_INIT(&sum, 0);
126 break;
127 case GRN_DB_FLOAT32 :
128 GRN_FLOAT32_INIT(&sum, 0);
129 break;
130 case GRN_DB_FLOAT :
131 GRN_FLOAT_INIT(&sum, 0);
132 break;
133 default :
134 {
135 grn_obj inspected;
136 GRN_TEXT_INIT(&inspected, 0);
137 grn_inspect(ctx, &inspected, first_output_column);
138 GRN_PLUGIN_ERROR(ctx,
139 GRN_INVALID_ARGUMENT,
140 "window_sum(): "
141 "the output column must be number column: <%.*s>",
142 (int)GRN_TEXT_LEN(&inspected),
143 GRN_TEXT_VALUE(&inspected));
144 GRN_OBJ_FIN(ctx, &inspected);
145 return ctx->rc;
146 }
147 break;
148 }
149
150 grn_obj value;
151 GRN_VOID_INIT(&value);
152
153 if (grn_window_is_sorted(ctx, window)) {
154 grn_id id;
155 while ((id = grn_window_next(ctx, window))) {
156 GRN_BULK_REWIND(&value);
157 grn_obj *target = grn_window_get_argument(ctx, window, 0);
158 grn_obj_get_value(ctx, target, id, &value);
159 switch (target_range_id) {
160 case GRN_DB_INT8 :
161 GRN_INT64_SET(ctx,
162 &sum,
163 GRN_INT64_VALUE(&sum) + GRN_INT8_VALUE(&value));
164 break;
165 case GRN_DB_INT16 :
166 GRN_INT64_SET(ctx,
167 &sum,
168 GRN_INT64_VALUE(&sum) + GRN_INT16_VALUE(&value));
169 break;
170 case GRN_DB_INT32 :
171 GRN_INT64_SET(ctx,
172 &sum,
173 GRN_INT64_VALUE(&sum) + GRN_INT32_VALUE(&value));
174 break;
175 case GRN_DB_INT64 :
176 GRN_INT64_SET(ctx,
177 &sum,
178 GRN_INT64_VALUE(&sum) + GRN_INT64_VALUE(&value));
179 break;
180 case GRN_DB_UINT8 :
181 GRN_UINT64_SET(ctx,
182 &sum,
183 GRN_UINT64_VALUE(&sum) + GRN_UINT8_VALUE(&value));
184 break;
185 case GRN_DB_UINT16 :
186 GRN_UINT64_SET(ctx,
187 &sum,
188 GRN_UINT64_VALUE(&sum) + GRN_UINT16_VALUE(&value));
189 break;
190 case GRN_DB_UINT32 :
191 GRN_UINT64_SET(ctx,
192 &sum,
193 GRN_UINT64_VALUE(&sum) + GRN_UINT32_VALUE(&value));
194 break;
195 case GRN_DB_UINT64 :
196 GRN_UINT64_SET(ctx,
197 &sum,
198 GRN_UINT64_VALUE(&sum) + GRN_UINT64_VALUE(&value));
199 break;
200 case GRN_DB_FLOAT32 :
201 GRN_FLOAT32_SET(ctx,
202 &sum,
203 GRN_FLOAT32_VALUE(&sum) + GRN_FLOAT32_VALUE(&value));
204 break;
205 case GRN_DB_FLOAT :
206 GRN_FLOAT_SET(ctx,
207 &sum,
208 GRN_FLOAT_VALUE(&sum) + GRN_FLOAT_VALUE(&value));
209 break;
210 default :
211 break;
212 }
213 grn_obj *output_column = grn_window_get_output_column(ctx, window);
214 if (output_column) {
215 grn_obj_set_value(ctx, output_column, id, &sum, GRN_OBJ_SET);
216 }
217 }
218 } else {
219 int64_t sum_raw_int64 = 0;
220 uint64_t sum_raw_uint64 = 0;
221 float sum_raw_float = 0.0;
222 double sum_raw_double = 0.0;
223
224 grn_id id;
225 while ((id = grn_window_next(ctx, window))) {
226 GRN_BULK_REWIND(&value);
227 grn_obj *target = grn_window_get_argument(ctx, window, 0);
228 grn_obj_get_value(ctx, target, id, &value);
229 switch (target_range_id) {
230 case GRN_DB_INT8 :
231 sum_raw_int64 += GRN_INT8_VALUE(&value);
232 break;
233 case GRN_DB_INT16 :
234 sum_raw_int64 += GRN_INT16_VALUE(&value);
235 break;
236 case GRN_DB_INT32 :
237 sum_raw_int64 += GRN_INT32_VALUE(&value);
238 break;
239 case GRN_DB_INT64 :
240 sum_raw_int64 += GRN_INT64_VALUE(&value);
241 break;
242 case GRN_DB_UINT8 :
243 sum_raw_uint64 += GRN_UINT8_VALUE(&value);
244 break;
245 case GRN_DB_UINT16 :
246 sum_raw_uint64 += GRN_UINT16_VALUE(&value);
247 break;
248 case GRN_DB_UINT32 :
249 sum_raw_uint64 += GRN_UINT32_VALUE(&value);
250 break;
251 case GRN_DB_UINT64 :
252 sum_raw_uint64 += GRN_UINT64_VALUE(&value);
253 break;
254 case GRN_DB_FLOAT32 :
255 sum_raw_float += GRN_FLOAT32_VALUE(&value);
256 break;
257 case GRN_DB_FLOAT :
258 sum_raw_double += GRN_FLOAT_VALUE(&value);
259 break;
260 default :
261 break;
262 }
263 }
264
265 switch (output_column_range_id) {
266 case GRN_DB_INT8 :
267 case GRN_DB_INT16 :
268 case GRN_DB_INT32 :
269 case GRN_DB_INT64 :
270 GRN_INT64_SET(ctx, &sum, sum_raw_int64);
271 break;
272 case GRN_DB_UINT8 :
273 case GRN_DB_UINT16 :
274 case GRN_DB_UINT32 :
275 case GRN_DB_UINT64 :
276 GRN_UINT64_SET(ctx, &sum, sum_raw_uint64);
277 break;
278 case GRN_DB_FLOAT32 :
279 GRN_FLOAT32_SET(ctx, &sum, sum_raw_float);
280 break;
281 case GRN_DB_FLOAT :
282 GRN_FLOAT_SET(ctx, &sum, sum_raw_double);
283 break;
284 }
285
286 grn_window_rewind(ctx, window);
287 while ((id = grn_window_next(ctx, window))) {
288 grn_obj *output_column = grn_window_get_output_column(ctx, window);
289 if (output_column) {
290 grn_obj_set_value(ctx, output_column, id, &sum, GRN_OBJ_SET);
291 }
292 }
293 }
294
295 GRN_OBJ_FIN(ctx, &value);
296 GRN_OBJ_FIN(ctx, &sum);
297
298 return GRN_SUCCESS;
299 }
300
301 static grn_rc
302 window_count(grn_ctx *ctx,
303 grn_obj *first_output_column,
304 grn_window *window,
305 grn_obj **first_args,
306 int first_n_args)
307 {
308 if (first_n_args != 0) {
309 GRN_PLUGIN_ERROR(ctx,
310 GRN_INVALID_ARGUMENT,
311 "window_count(): wrong number of arguments (%d for 0)",
312 first_n_args);
313 return ctx->rc;
314 }
315
316 grn_obj n_records;
317 grn_id output_column_range_id;
318 output_column_range_id = grn_obj_get_range(ctx, first_output_column);
319 switch (output_column_range_id) {
320 case GRN_DB_INT8 :
321 case GRN_DB_INT16 :
322 case GRN_DB_INT32 :
323 case GRN_DB_INT64 :
324 GRN_INT64_INIT(&n_records, 0);
325 break;
326 case GRN_DB_UINT8 :
327 case GRN_DB_UINT16 :
328 case GRN_DB_UINT32 :
329 case GRN_DB_UINT64 :
330 GRN_UINT64_INIT(&n_records, 0);
331 break;
332 case GRN_DB_FLOAT32 :
333 GRN_FLOAT32_INIT(&n_records, 0);
334 break;
335 case GRN_DB_FLOAT :
336 GRN_FLOAT_INIT(&n_records, 0);
337 break;
338 default :
339 {
340 grn_obj inspected;
341 GRN_TEXT_INIT(&inspected, 0);
342 grn_inspect(ctx, &inspected, first_output_column);
343 GRN_PLUGIN_ERROR(ctx,
344 GRN_INVALID_ARGUMENT,
345 "window_count(): "
346 "the output column must be number column: <%.*s>",
347 (int)GRN_TEXT_LEN(&inspected),
348 GRN_TEXT_VALUE(&inspected));
349 GRN_OBJ_FIN(ctx, &inspected);
350 return ctx->rc;
351 }
352 break;
353 }
354
355 if (grn_window_is_sorted(ctx, window)) {
356 uint32_t n_records_raw = 0;
357 grn_id id;
358 while ((id = grn_window_next(ctx, window))) {
359 n_records_raw++;
360 switch (output_column_range_id) {
361 case GRN_DB_INT8 :
362 case GRN_DB_INT16 :
363 case GRN_DB_INT32 :
364 case GRN_DB_INT64 :
365 GRN_INT64_SET(ctx, &n_records, n_records_raw);
366 break;
367 case GRN_DB_UINT8 :
368 case GRN_DB_UINT16 :
369 case GRN_DB_UINT32 :
370 case GRN_DB_UINT64 :
371 GRN_UINT64_SET(ctx, &n_records, n_records_raw);
372 break;
373 case GRN_DB_FLOAT32 :
374 GRN_FLOAT32_SET(ctx, &n_records, n_records_raw);
375 break;
376 case GRN_DB_FLOAT :
377 GRN_FLOAT_SET(ctx, &n_records, n_records_raw);
378 break;
379 default :
380 break;
381 }
382 grn_obj *output_column = grn_window_get_output_column(ctx, window);
383 if (output_column) {
384 grn_obj_set_value(ctx, output_column, id, &n_records, GRN_OBJ_SET);
385 }
386 }
387 } else {
388 uint32_t n_records_raw = 0;
389 grn_id id;
390 while ((id = grn_window_next(ctx, window))) {
391 n_records_raw++;
392 }
393
394 switch (output_column_range_id) {
395 case GRN_DB_INT8 :
396 case GRN_DB_INT16 :
397 case GRN_DB_INT32 :
398 case GRN_DB_INT64 :
399 GRN_INT64_SET(ctx, &n_records, n_records_raw);
400 break;
401 case GRN_DB_UINT8 :
402 case GRN_DB_UINT16 :
403 case GRN_DB_UINT32 :
404 case GRN_DB_UINT64 :
405 GRN_UINT64_SET(ctx, &n_records, n_records_raw);
406 break;
407 case GRN_DB_FLOAT32 :
408 GRN_FLOAT32_SET(ctx, &n_records, n_records_raw);
409 break;
410 case GRN_DB_FLOAT :
411 GRN_FLOAT_SET(ctx, &n_records, n_records_raw);
412 break;
413 }
414
415 grn_window_rewind(ctx, window);
416 while ((id = grn_window_next(ctx, window))) {
417 grn_obj *output_column = grn_window_get_output_column(ctx, window);
418 if (output_column) {
419 grn_obj_set_value(ctx, output_column, id, &n_records, GRN_OBJ_SET);
420 }
421 }
422 }
423
424 GRN_OBJ_FIN(ctx, &n_records);
425
426 return GRN_SUCCESS;
427 }
428
429 static grn_rc
430 window_rank(grn_ctx *ctx,
431 grn_obj *first_output_column,
432 grn_window *window,
433 grn_obj **first_args,
434 int first_n_args)
435 {
436 grn_id id;
437 uint32_t rank = 0;
438 uint32_t gap = 0;
439 grn_obj value;
440
441 GRN_UINT32_INIT(&value, 0);
442 while ((id = grn_window_next(ctx, window))) {
443 if (grn_window_is_value_changed(ctx, window)) {
444 rank += gap + 1;
445 gap = 0;
446 } else {
447 gap++;
448 }
449 GRN_UINT32_SET(ctx, &value, rank);
450 grn_obj *output_column = grn_window_get_output_column(ctx, window);
451 if (output_column) {
452 grn_obj_set_value(ctx, output_column, id, &value, GRN_OBJ_SET);
453 }
454 }
455 GRN_OBJ_FIN(ctx, &value);
456
457 return GRN_SUCCESS;
458 }
459
460 grn_rc
461 grn_db_init_builtin_window_functions(grn_ctx *ctx)
462 {
463 /* For backward compatibility. */
464 grn_window_function_create(ctx,
465 "record_number", -1,
466 window_record_number);
467 grn_window_function_create(ctx,
468 "window_record_number", -1,
469 window_record_number);
470
471 grn_window_function_create(ctx,
472 "window_sum", -1,
473 window_sum);
474
475 grn_window_function_create(ctx,
476 "window_count", -1,
477 window_count);
478
479 grn_window_function_create(ctx,
480 "window_rank", -1,
481 window_rank);
482
483 return GRN_SUCCESS;
484 }