forked from swilly22/redis-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagg_funcs.c
425 lines (344 loc) · 11.6 KB
/
agg_funcs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include <float.h>
#include "agg_funcs.h"
#include "aggregate.h"
#include "repository.h"
#include "../value.h"
#include <math.h>
#include "../util/qsort.h"
#define ISLT(a,b) ((*a) < (*b))
typedef struct {
size_t num;
double total;
} __agg_sumCtx;
int __agg_sumStep(AggCtx *ctx, SIValue *argv, int argc) {
// convert the value of the input sequence to a double if possible
__agg_sumCtx *ac = Agg_FuncCtx(ctx);
double n;
for(int i = 0; i < argc; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"SUM Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
ac->num++;
ac->total += n;
}
return AGG_OK;
}
int __agg_sumReduceNext(AggCtx *ctx) {
__agg_sumCtx *ac = Agg_FuncCtx(ctx);
Agg_SetResult(ctx, SI_DoubleVal(ac->total));
return AGG_OK;
}
AggCtx* Agg_SumFunc() {
__agg_sumCtx *ac = malloc(sizeof(__agg_sumCtx));
ac->num = 0;
ac->total = 0;
return Agg_Reduce(ac, __agg_sumStep, __agg_sumReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
size_t count;
double total;
} __agg_avgCtx;
int __agg_avgStep(AggCtx *ctx, SIValue *argv, int argc) {
// convert the value of the input sequence to a double if possible
__agg_avgCtx *ac = Agg_FuncCtx(ctx);
double n;
for(int i = 0; i < argc; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"AVG Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
ac->count++;
ac->total += n;
}
return AGG_OK;
}
int __agg_avgReduceNext(AggCtx *ctx) {
__agg_avgCtx *ac = Agg_FuncCtx(ctx);
if(ac->count > 0) {
Agg_SetResult(ctx, SI_DoubleVal(ac->total / ac->count));
} else {
Agg_SetResult(ctx, SI_DoubleVal(0));
}
return AGG_OK;
}
AggCtx* Agg_AvgFunc() {
__agg_avgCtx *ac = malloc(sizeof(__agg_avgCtx));
ac->count = 0;
ac->total = 0;
return Agg_Reduce(ac, __agg_avgStep, __agg_avgReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
double max;
} __agg_maxCtx;
int __agg_maxStep(AggCtx *ctx, SIValue *argv, int argc) {
// convert the value of the input sequence to a double if possible
__agg_maxCtx *ac = Agg_FuncCtx(ctx);
double n;
for(int i = 0; i < argc; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"MAX Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
if(n > ac->max) {
ac->max = n;
}
}
return AGG_OK;
}
int __agg_maxReduceNext(AggCtx *ctx) {
__agg_maxCtx *ac = Agg_FuncCtx(ctx);
Agg_SetResult(ctx, SI_DoubleVal(ac->max));
return AGG_OK;
}
AggCtx* Agg_MaxFunc() {
__agg_maxCtx *ac = malloc(sizeof(__agg_maxCtx));
ac->max = -DBL_MAX;
return Agg_Reduce(ac, __agg_maxStep, __agg_maxReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
double min;
} __agg_minCtx;
int __agg_minStep(AggCtx *ctx, SIValue *argv, int argc) {
// convert the value of the input sequence to a double if possible
__agg_minCtx *ac = Agg_FuncCtx(ctx);
double n;
for(int i = 0; i < argc; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"MIN Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
if(n < ac->min) {
ac->min = n;
}
}
return AGG_OK;
}
int __agg_minReduceNext(AggCtx *ctx) {
__agg_minCtx *ac = Agg_FuncCtx(ctx);
Agg_SetResult(ctx, SI_DoubleVal(ac->min));
return AGG_OK;
}
AggCtx* Agg_MinFunc() {
__agg_minCtx *ac = malloc(sizeof(__agg_minCtx));
ac->min = DBL_MAX;
return Agg_Reduce(ac, __agg_minStep, __agg_minReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
size_t count;
} __agg_countCtx;
int __agg_countStep(AggCtx *ctx, SIValue *argv, int argc) {
__agg_countCtx *ac = Agg_FuncCtx(ctx);
ac->count += argc;
return AGG_OK;
}
int __agg_countReduceNext(AggCtx *ctx) {
__agg_countCtx *ac = Agg_FuncCtx(ctx);
Agg_SetResult(ctx, SI_DoubleVal(ac->count));
return AGG_OK;
}
AggCtx* Agg_CountFunc() {
__agg_countCtx *ac = malloc(sizeof(__agg_countCtx));
ac->count = 0;
return Agg_Reduce(ac, __agg_countStep, __agg_countReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
double percentile;
double *values;
size_t count;
size_t values_allocated;
} __agg_percCtx;
// This function is agnostic as to percentile method
int __agg_percStep(AggCtx *ctx, SIValue *argv, int argc) {
__agg_percCtx *ac = Agg_FuncCtx(ctx);
// The last argument is the requested percentile, which we only
// need to apply on the first function invocation (at which time
// _agg_percCtx->percentile will be -1)
if (ac->percentile < 0) {
if (!SIValue_ToDouble(&argv[argc - 1], &ac->percentile)) {
return Agg_SetError(ctx,
"PERC_DISC Could not convert percentile argument to double");
}
if (ac->percentile < 0 || ac->percentile > 1) {
return Agg_SetError(ctx,
"PERC_DISC Invalid input for percentile is not a valid argument, must be a number in the range 0.0 to 1.0");
}
}
if (ac->count + argc - 1 > ac->values_allocated) {
ac->values_allocated *= 2;
ac->values = realloc(ac->values, sizeof(double) * ac->values_allocated);
}
double n;
for (int i = 0; i < argc - 1; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"PERC_DISC Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
ac->values[ac->count] = n;
ac->count++;
}
return AGG_OK;
}
int __agg_percDiscReduceNext(AggCtx *ctx) {
__agg_percCtx *ac = Agg_FuncCtx(ctx);
QSORT(double, ac->values, ac->count, ISLT);
// If ac->percentile == 0, employing this formula would give an index of -1
int idx = ac->percentile > 0 ? ceil(ac->percentile * ac->count) - 1 : 0;
double n = ac->values[idx];
Agg_SetResult(ctx, SI_DoubleVal(n));
free(ac->values);
return AGG_OK;
}
int __agg_percContReduceNext(AggCtx *ctx) {
__agg_percCtx *ac = Agg_FuncCtx(ctx);
QSORT(double, ac->values, ac->count, ISLT);
if (ac->percentile == 1.0 || ac->count == 1) {
Agg_SetResult(ctx, SI_DoubleVal(ac->values[ac->count - 1]));
free(ac->values);
return AGG_OK;
}
double int_val, fraction_val;
double float_idx = ac->percentile * (ac->count - 1);
// Split the temp value into its integer and fractional values
fraction_val = modf(float_idx, &int_val);
int index = int_val; // Casting the integral part of the value to an int for convenience
if (!fraction_val) {
// A valid index was requested, so we can directly return a value
Agg_SetResult(ctx, SI_DoubleVal(ac->values[index]));
free(ac->values);
return AGG_OK;
}
double lhs, rhs;
lhs = ac->values[index] * (1 - fraction_val);
rhs = ac->values[index + 1] * fraction_val;
Agg_SetResult(ctx, SI_DoubleVal(lhs + rhs));
free(ac->values);
return AGG_OK;
}
// The percentile initializers are identical save for the ReduceNext function they specify
AggCtx* Agg_PercDiscFunc() {
__agg_percCtx *ac = malloc(sizeof(__agg_percCtx));
ac->count = 0;
ac->values = malloc(1024 * sizeof(double));
ac->values_allocated = 1024;
// Percentile will be updated by the first call to Step
ac->percentile = -1;
return Agg_Reduce(ac, __agg_percStep, __agg_percDiscReduceNext);
}
AggCtx* Agg_PercContFunc() {
__agg_percCtx *ac = malloc(sizeof(__agg_percCtx));
ac->count = 0;
ac->values = malloc(1024 * sizeof(double));
ac->values_allocated = 1024;
// Percentile will be updated by the first call to Step
ac->percentile = -1;
return Agg_Reduce(ac, __agg_percStep, __agg_percContReduceNext);
}
//------------------------------------------------------------------------
typedef struct {
double *values;
double total;
size_t count;
size_t values_allocated;
int is_sampled;
} __agg_stdevCtx;
int __agg_StdevStep(AggCtx *ctx, SIValue *argv, int argc) {
__agg_stdevCtx *ac = Agg_FuncCtx(ctx);
if (ac->count + argc > ac->values_allocated) {
ac->values_allocated *= 2;
ac->values = realloc(ac->values, sizeof(double) * ac->values_allocated);
}
double n;
for (int i = 0; i < argc; i ++) {
if (!SIValue_ToDouble(&argv[i], &n)) {
if (!SIValue_IsNullPtr(&argv[i])) {
// not convertible to double!
return Agg_SetError(ctx,
"STDEV Could not convert upstream value to double");
} else {
return AGG_OK;
}
}
ac->values[ac->count] = n;
ac->total += n;
ac->count++;
}
return AGG_OK;
}
int __agg_StdevReduceNext(AggCtx *ctx) {
__agg_stdevCtx *ac = Agg_FuncCtx(ctx);
if (ac->count < 2) {
Agg_SetResult(ctx, SI_DoubleVal(0));
free(ac->values);
return AGG_OK;
}
double mean = ac->total / ac->count;
long double sum = 0;
for (int i = 0; i < ac->count; i ++) {
sum += (ac->values[i] - mean) * (ac->values[i] + mean);
}
// is_sampled will be equal to 1 in the Stdev case and 0 in the StdevP case
double variance = sum / (ac->count - ac->is_sampled);
double stdev = sqrt(variance);
Agg_SetResult(ctx, SI_DoubleVal(stdev));
free(ac->values);
return AGG_OK;
}
AggCtx* Agg_StdevFunc() {
__agg_stdevCtx *ac = malloc(sizeof(__agg_stdevCtx));
ac->is_sampled = 1;
ac->count = 0;
ac->total = 0;
ac->values = malloc(1024 * sizeof(double));
ac->values_allocated = 1024;
return Agg_Reduce(ac, __agg_StdevStep, __agg_StdevReduceNext);
}
// StdevP is identical to Stdev save for an altered value we can check for with a bool
AggCtx* Agg_StdevPFunc() {
AggCtx *func = Agg_StdevFunc();
__agg_stdevCtx *ac = Agg_FuncCtx(func);
ac->is_sampled = 0;
return func;
}
//------------------------------------------------------------------------
void Agg_RegisterFuncs() {
Agg_RegisterFunc("sum", Agg_SumFunc);
Agg_RegisterFunc("avg", Agg_AvgFunc);
Agg_RegisterFunc("max", Agg_MaxFunc);
Agg_RegisterFunc("min", Agg_MinFunc);
Agg_RegisterFunc("count", Agg_CountFunc);
Agg_RegisterFunc("percentileDisc", Agg_PercDiscFunc);
Agg_RegisterFunc("percentileCont", Agg_PercContFunc);
Agg_RegisterFunc("stDev", Agg_StdevFunc);
Agg_RegisterFunc("stDevP", Agg_StdevPFunc);
}