| 1 | /* |
| 2 | Copyright(C) 2020-2023 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_db.h" |
| 20 | #include "grn_aggregators.h" |
| 21 | #include "grn_bulk.h" |
| 22 | |
| 23 | #include <math.h> |
| 24 | |
| 25 | static bool |
| 26 | numeric_aggregator_validate_target(grn_ctx *ctx, |
| 27 | grn_obj *target, |
| 28 | const char *tag) |
| 29 | { |
| 30 | if (grn_obj_is_number_family_scalar_column(ctx, target)) { |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | if (grn_obj_is_number_family_scalar_accessor(ctx, target)) { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | grn_obj inspected; |
| 39 | GRN_TEXT_INIT(&inspected, 0); |
| 40 | grn_inspect_limited(ctx, &inspected, target); |
| 41 | GRN_PLUGIN_ERROR(ctx, |
| 42 | GRN_INVALID_ARGUMENT, |
| 43 | "%s target must be numeric column: %.*s", |
| 44 | tag, |
| 45 | (int)GRN_TEXT_LEN(&inspected), |
| 46 | GRN_TEXT_VALUE(&inspected)); |
| 47 | GRN_OBJ_FIN(ctx, &inspected); |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | static void * |
| 52 | aggregator_sum_init(grn_ctx *ctx, grn_aggregator_data *data) |
| 53 | { |
| 54 | const char *tag = "[aggregator][sum]"; |
| 55 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 56 | size_t n_args = GRN_PTR_VECTOR_SIZE(args); |
| 57 | if (n_args != 1) { |
| 58 | GRN_PLUGIN_ERROR(ctx, |
| 59 | GRN_INVALID_ARGUMENT, |
| 60 | "%s wrong number of arguments " |
| 61 | "(%" GRN_FMT_SIZE " for 1)", |
| 62 | tag, |
| 63 | n_args); |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 68 | if (!numeric_aggregator_validate_target(ctx, target, tag)) { |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | return NULL; |
| 73 | } |
| 74 | |
| 75 | static grn_rc |
| 76 | aggregator_sum_update(grn_ctx *ctx, grn_aggregator_data *data) |
| 77 | { |
| 78 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 79 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 80 | grn_obj current_value; |
| 81 | GRN_VOID_INIT(¤t_value); |
| 82 | grn_id source_id = grn_aggregator_data_get_source_id(ctx, data); |
| 83 | grn_obj_get_value(ctx, target, source_id, ¤t_value); |
| 84 | |
| 85 | grn_obj *output_column = grn_aggregator_data_get_output_column(ctx, data); |
| 86 | grn_id group_id = grn_aggregator_data_get_group_id(ctx, data); |
| 87 | grn_obj_set_value(ctx, output_column, group_id, ¤t_value, GRN_OBJ_INCR); |
| 88 | GRN_OBJ_FIN(ctx, ¤t_value); |
| 89 | |
| 90 | return ctx->rc; |
| 91 | } |
| 92 | |
| 93 | typedef struct { |
| 94 | uint32_t n_values; |
| 95 | double mean; |
| 96 | } aggregator_mean_value; |
| 97 | |
| 98 | typedef struct { |
| 99 | grn_hash *values; |
| 100 | } aggregator_mean_data; |
| 101 | |
| 102 | static void * |
| 103 | aggregator_mean_init(grn_ctx *ctx, grn_aggregator_data *data) |
| 104 | { |
| 105 | const char *tag = "[aggregator][mean]"; |
| 106 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 107 | size_t n_args = GRN_PTR_VECTOR_SIZE(args); |
| 108 | if (n_args != 1) { |
| 109 | GRN_PLUGIN_ERROR(ctx, |
| 110 | GRN_INVALID_ARGUMENT, |
| 111 | "%s wrong number of arguments " |
| 112 | "(%" GRN_FMT_SIZE " for 1)", |
| 113 | tag, |
| 114 | n_args); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 119 | if (!numeric_aggregator_validate_target(ctx, target, tag)) { |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | aggregator_mean_data *mean_data = GRN_CALLOC(sizeof(aggregator_mean_data)); |
| 124 | if (!mean_data) { |
| 125 | GRN_PLUGIN_ERROR(ctx, |
| 126 | GRN_NO_MEMORY_AVAILABLE, |
| 127 | "%s failed to allocate: %s", |
| 128 | tag, |
| 129 | ctx->errbuf); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | mean_data->values = grn_hash_create(ctx, |
| 134 | NULL, |
| 135 | sizeof(grn_id), |
| 136 | sizeof(aggregator_mean_value), |
| 137 | GRN_OBJ_TABLE_HASH_KEY); |
| 138 | if (!mean_data->values) { |
| 139 | GRN_PLUGIN_ERROR(ctx, |
| 140 | GRN_NO_MEMORY_AVAILABLE, |
| 141 | "%s failed to create hash table: %s", |
| 142 | tag, |
| 143 | ctx->errbuf); |
| 144 | GRN_FREE(mean_data); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | return mean_data; |
| 149 | } |
| 150 | |
| 151 | static grn_rc |
| 152 | aggregator_mean_update(grn_ctx *ctx, grn_aggregator_data *data) |
| 153 | { |
| 154 | const char *tag = "[aggregator][mean]"; |
| 155 | aggregator_mean_data *mean_data = |
| 156 | grn_aggregator_data_get_user_data(ctx, data); |
| 157 | grn_id group_id = grn_aggregator_data_get_group_id(ctx, data); |
| 158 | void *value; |
| 159 | int added; |
| 160 | grn_id value_id = grn_hash_add(ctx, |
| 161 | mean_data->values, |
| 162 | &group_id, |
| 163 | sizeof(grn_id), |
| 164 | &value, |
| 165 | &added); |
| 166 | if (value_id == GRN_ID_NIL) { |
| 167 | GRN_PLUGIN_ERROR(ctx, |
| 168 | GRN_INVALID_ARGUMENT, |
| 169 | "%s failed to add ID: <%u>: %s", |
| 170 | tag, |
| 171 | group_id, |
| 172 | ctx->errbuf); |
| 173 | return ctx->rc; |
| 174 | } |
| 175 | |
| 176 | aggregator_mean_value *mean_value = value; |
| 177 | if (added) { |
| 178 | mean_value->n_values = 0; |
| 179 | mean_value->mean = 0.0; |
| 180 | } |
| 181 | |
| 182 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 183 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 184 | grn_obj current_value; |
| 185 | GRN_VOID_INIT(¤t_value); |
| 186 | grn_id source_id = grn_aggregator_data_get_source_id(ctx, data); |
| 187 | grn_obj_get_value(ctx, target, source_id, ¤t_value); |
| 188 | double current_value_float = grn_bulk_get_float(ctx, ¤t_value); |
| 189 | GRN_OBJ_FIN(ctx, ¤t_value); |
| 190 | |
| 191 | mean_value->n_values++; |
| 192 | mean_value->mean += |
| 193 | (current_value_float - mean_value->mean) / mean_value->n_values; |
| 194 | |
| 195 | return ctx->rc; |
| 196 | } |
| 197 | |
| 198 | static grn_rc |
| 199 | aggregator_mean_fin(grn_ctx *ctx, grn_aggregator_data *data) |
| 200 | { |
| 201 | aggregator_mean_data *mean_data = |
| 202 | grn_aggregator_data_get_user_data(ctx, data); |
| 203 | |
| 204 | grn_obj *output_column = grn_aggregator_data_get_output_column(ctx, data); |
| 205 | grn_obj mean_float; |
| 206 | GRN_FLOAT_INIT(&mean_float, 0); |
| 207 | GRN_HASH_EACH_BEGIN(ctx, mean_data->values, cursor, id) |
| 208 | { |
| 209 | void *key; |
| 210 | unsigned int key_size; |
| 211 | void *value; |
| 212 | grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value); |
| 213 | grn_id group_id = *((grn_id *)key); |
| 214 | aggregator_mean_value *mean_value = value; |
| 215 | GRN_FLOAT_SET(ctx, &mean_float, mean_value->mean); |
| 216 | grn_obj_set_value(ctx, output_column, group_id, &mean_float, GRN_OBJ_SET); |
| 217 | } |
| 218 | GRN_HASH_EACH_END(ctx, cursor); |
| 219 | GRN_OBJ_FIN(ctx, &mean_float); |
| 220 | |
| 221 | grn_hash_close(ctx, mean_data->values); |
| 222 | GRN_FREE(mean_data); |
| 223 | |
| 224 | return ctx->rc; |
| 225 | } |
| 226 | |
| 227 | typedef struct { |
| 228 | uint32_t n_values; |
| 229 | double mean; |
| 230 | double m2; |
| 231 | } aggregator_sd_value; |
| 232 | |
| 233 | typedef struct { |
| 234 | grn_hash *values; |
| 235 | bool unbiased; |
| 236 | } aggregator_sd_data; |
| 237 | |
| 238 | static void * |
| 239 | aggregator_sd_init(grn_ctx *ctx, grn_aggregator_data *data) |
| 240 | { |
| 241 | const char *tag = "[aggregator][sd]"; |
| 242 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 243 | size_t n_args = GRN_PTR_VECTOR_SIZE(args); |
| 244 | if (!(1 <= n_args && n_args <= 2)) { |
| 245 | GRN_PLUGIN_ERROR(ctx, |
| 246 | GRN_INVALID_ARGUMENT, |
| 247 | "%s wrong number of arguments " |
| 248 | "(%" GRN_FMT_SIZE " for 1..2)", |
| 249 | tag, |
| 250 | n_args); |
| 251 | return NULL; |
| 252 | } |
| 253 | |
| 254 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 255 | if (!numeric_aggregator_validate_target(ctx, target, tag)) { |
| 256 | return NULL; |
| 257 | } |
| 258 | |
| 259 | bool unbiased = false; |
| 260 | if (n_args >= 2) { |
| 261 | grn_obj *options = GRN_PTR_VALUE_AT(args, 1); |
| 262 | grn_rc rc = grn_proc_options_parse(ctx, |
| 263 | options, |
| 264 | tag, |
| 265 | "unbiased", |
| 266 | GRN_PROC_OPTION_VALUE_BOOL, |
| 267 | &unbiased, |
| 268 | false); |
| 269 | if (rc != GRN_SUCCESS) { |
| 270 | return NULL; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | aggregator_sd_data *sd_data = GRN_CALLOC(sizeof(aggregator_sd_data)); |
| 275 | if (!sd_data) { |
| 276 | GRN_PLUGIN_ERROR(ctx, |
| 277 | GRN_NO_MEMORY_AVAILABLE, |
| 278 | "%s failed to allocate: %s", |
| 279 | tag, |
| 280 | ctx->errbuf); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | sd_data->values = grn_hash_create(ctx, |
| 285 | NULL, |
| 286 | sizeof(grn_id), |
| 287 | sizeof(aggregator_sd_value), |
| 288 | GRN_OBJ_TABLE_HASH_KEY); |
| 289 | if (!sd_data->values) { |
| 290 | GRN_PLUGIN_ERROR(ctx, |
| 291 | GRN_NO_MEMORY_AVAILABLE, |
| 292 | "%s failed to create hash table: %s", |
| 293 | tag, |
| 294 | ctx->errbuf); |
| 295 | GRN_FREE(sd_data); |
| 296 | return NULL; |
| 297 | } |
| 298 | sd_data->unbiased = unbiased; |
| 299 | |
| 300 | return sd_data; |
| 301 | } |
| 302 | |
| 303 | static grn_rc |
| 304 | aggregator_sd_update(grn_ctx *ctx, grn_aggregator_data *data) |
| 305 | { |
| 306 | const char *tag = "[aggregator][sd]"; |
| 307 | aggregator_sd_data *sd_data = grn_aggregator_data_get_user_data(ctx, data); |
| 308 | grn_id group_id = grn_aggregator_data_get_group_id(ctx, data); |
| 309 | void *value; |
| 310 | int added; |
| 311 | grn_id value_id = grn_hash_add(ctx, |
| 312 | sd_data->values, |
| 313 | &group_id, |
| 314 | sizeof(grn_id), |
| 315 | &value, |
| 316 | &added); |
| 317 | if (value_id == GRN_ID_NIL) { |
| 318 | GRN_PLUGIN_ERROR(ctx, |
| 319 | GRN_INVALID_ARGUMENT, |
| 320 | "%s failed to add ID: <%u>: %s", |
| 321 | tag, |
| 322 | group_id, |
| 323 | ctx->errbuf); |
| 324 | return ctx->rc; |
| 325 | } |
| 326 | |
| 327 | aggregator_sd_value *sd_value = value; |
| 328 | if (added) { |
| 329 | sd_value->n_values = 0; |
| 330 | sd_value->mean = 0.0; |
| 331 | sd_value->m2 = 0.0; |
| 332 | } |
| 333 | |
| 334 | grn_obj *args = grn_aggregator_data_get_args(ctx, data); |
| 335 | grn_obj *target = GRN_PTR_VALUE_AT(args, 0); |
| 336 | grn_obj current_value; |
| 337 | GRN_VOID_INIT(¤t_value); |
| 338 | grn_id source_id = grn_aggregator_data_get_source_id(ctx, data); |
| 339 | grn_obj_get_value(ctx, target, source_id, ¤t_value); |
| 340 | double current_value_float = grn_bulk_get_float(ctx, ¤t_value); |
| 341 | GRN_OBJ_FIN(ctx, ¤t_value); |
| 342 | |
| 343 | const double mean_previous = sd_value->mean; |
| 344 | const double m2_previous = sd_value->m2; |
| 345 | sd_value->n_values++; |
| 346 | sd_value->mean += (current_value_float - mean_previous) / sd_value->n_values; |
| 347 | /* Welfold's online algorithm */ |
| 348 | sd_value->m2 = m2_previous + ((current_value_float - mean_previous) * |
| 349 | (current_value_float - sd_value->mean)); |
| 350 | |
| 351 | return ctx->rc; |
| 352 | } |
| 353 | |
| 354 | static grn_rc |
| 355 | aggregator_sd_fin(grn_ctx *ctx, grn_aggregator_data *data) |
| 356 | { |
| 357 | aggregator_sd_data *sd_data = grn_aggregator_data_get_user_data(ctx, data); |
| 358 | |
| 359 | grn_obj *output_column = grn_aggregator_data_get_output_column(ctx, data); |
| 360 | grn_obj sd_float; |
| 361 | GRN_FLOAT_INIT(&sd_float, 0); |
| 362 | GRN_HASH_EACH_BEGIN(ctx, sd_data->values, cursor, id) |
| 363 | { |
| 364 | void *key; |
| 365 | unsigned int key_size; |
| 366 | void *value; |
| 367 | grn_hash_cursor_get_key_value(ctx, cursor, &key, &key_size, &value); |
| 368 | grn_id group_id = *((grn_id *)key); |
| 369 | aggregator_sd_value *sd_value = value; |
| 370 | double sd; |
| 371 | if (sd_data->unbiased) { |
| 372 | if (sd_value->n_values > 1) { |
| 373 | sd = sqrt(sd_value->m2 / (sd_value->n_values - 1)); |
| 374 | } else { |
| 375 | sd = NAN; |
| 376 | } |
| 377 | } else { |
| 378 | sd = sqrt(sd_value->m2 / sd_value->n_values); |
| 379 | } |
| 380 | GRN_FLOAT_SET(ctx, &sd_float, sd); |
| 381 | grn_obj_set_value(ctx, output_column, group_id, &sd_float, GRN_OBJ_SET); |
| 382 | } |
| 383 | GRN_HASH_EACH_END(ctx, cursor); |
| 384 | GRN_OBJ_FIN(ctx, &sd_float); |
| 385 | |
| 386 | grn_hash_close(ctx, sd_data->values); |
| 387 | GRN_FREE(sd_data); |
| 388 | |
| 389 | return ctx->rc; |
| 390 | } |
| 391 | |
| 392 | grn_rc |
| 393 | grn_db_init_builtin_aggregators(grn_ctx *ctx) |
| 394 | { |
| 395 | grn_aggregator_create(ctx, |
| 396 | "aggregator_sum", |
| 397 | -1, |
| 398 | aggregator_sum_init, |
| 399 | aggregator_sum_update, |
| 400 | NULL); |
| 401 | |
| 402 | grn_aggregator_create(ctx, |
| 403 | "aggregator_mean", |
| 404 | -1, |
| 405 | aggregator_mean_init, |
| 406 | aggregator_mean_update, |
| 407 | aggregator_mean_fin); |
| 408 | |
| 409 | grn_aggregator_create(ctx, |
| 410 | "aggregator_sd", |
| 411 | -1, |
| 412 | aggregator_sd_init, |
| 413 | aggregator_sd_update, |
| 414 | aggregator_sd_fin); |
| 415 | |
| 416 | return GRN_SUCCESS; |
| 417 | } |