460cd3fee998a4fe4b94389379a280a8cf470691
[com/gs-lite.git] / src / lib / gscplftaaux / rts_udaf.c
1 /* ------------------------------------------------
2 Copyright 2014 AT&T Intellectual Property
3    Licensed under the Apache License, Version 2.0 (the "License");
4    you may not use this file except in compliance with the License.
5    You may obtain a copy of the License at
6
7      http://www.apache.org/licenses/LICENSE-2.0
8
9    Unless required by applicable law or agreed to in writing, software
10    distributed under the License is distributed on an "AS IS" BASIS,
11    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12    See the License for the specific language governing permissions and
13    limitations under the License.
14  ------------------------------------------- */
15
16 #include "rts_udaf.h"
17 #include "gsconfig.h"
18 #include "gstypes.h"
19 #include <stdio.h>
20 #include <limits.h>
21 #include <math.h>
22
23
24 #define max(a,b) ((a) > (b) ? (a) : (b))
25 #define MAX_BUFSIZE     128
26
27
28 /****************************************************************/
29 /* LFTA functions                                               */
30 /****************************************************************/
31
32
33 ////////////////////////////////////////////////////////////////////////
34 ////            avg_udaf
35
36 typedef struct avg_udaf_lfta_struct_t{
37         gs_int64_t sum;
38         gs_uint32_t cnt;
39 } avg_udaf_lfta_struct_t;
40
41 void avg_udaf_lfta_LFTA_AGGR_INIT_(gs_sp_t b){
42         avg_udaf_lfta_struct_t *s = (avg_udaf_lfta_struct_t *)b;
43         s->sum = 0;
44         s->cnt = 0;
45 }
46
47 void avg_udaf_lfta_LFTA_AGGR_UPDATE_(gs_sp_t  b,gs_uint32_t v){
48         avg_udaf_lfta_struct_t *s = (avg_udaf_lfta_struct_t *)b;
49         s->sum += v;
50         s->cnt++;
51 }
52
53 gs_retval_t avg_udaf_lfta_LFTA_AGGR_FLUSHME_(gs_sp_t b){
54         return 0;
55 }
56
57 void avg_udaf_lfta_LFTA_AGGR_OUTPUT_(struct gs_string *r,gs_sp_t b){
58         r->length = 12;
59         r->data = b;
60 }
61
62 void avg_udaf_lfta_LFTA_AGGR_DESTROY_(gs_sp_t b){
63         return;
64 }
65
66 /////////////////////////////////////////////////////////
67 //              Moving sum
68
69 typedef struct moving_sum_lfta_struct{
70         gs_uint32_t sum;
71         gs_uint32_t N;
72 } moving_sum_lfta_struct;
73
74 void moving_sum_lfta_LFTA_AGGR_INIT_(gs_sp_t b){
75         moving_sum_lfta_struct *s = (moving_sum_lfta_struct *)b;
76         s->sum = 0;
77         s->N = 0;
78 }
79
80 void moving_sum_lfta_LFTA_AGGR_UPDATE_(gs_sp_t b, gs_uint32_t v, gs_uint32_t N){
81         moving_sum_lfta_struct *s = (moving_sum_lfta_struct *)b;
82         s->sum += v;
83         s->N = N;
84 }
85         
86 gs_retval_t moving_sum_lfta_LFTA_AGGR_FLUSHME_(gs_sp_t b){
87         return 0;
88 }
89
90 void moving_sum_lfta_LFTA_AGGR_OUTPUT_(gs_uint64_t *r, gs_sp_t b){
91         moving_sum_lfta_struct *s = (moving_sum_lfta_struct *)b;
92         *r = ((gs_uint64_t)(s->N) << 32) | (gs_uint64_t)(s->sum);
93 }
94
95 gs_retval_t moving_sum_lfta_LFTA_AGGR_DESTROY_(gs_sp_t b){
96         return 0;
97 }
98
99 /////////////////////////////////////////////////////////
100 //              FIRST and LAST aggregates
101
102 ///////////////// FIRST
103
104 // uint
105 void FIRST_lfta_LFTA_AGGR_INIT_(gs_uint32_t* scratch) {
106         *scratch = UINT_MAX;    // we will encode uninitialized value of UINT_MAX
107         return;
108 }
109
110 void FIRST_lfta_LFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
111         if (*scratch == UINT_MAX)
112                 *scratch = val;
113         return;
114 }
115 gs_retval_t FIRST_lfta_LFTA_AGGR_FLUSHME_( gs_uint32_t* scratch) { return 0; }
116 void FIRST_lfta_LFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
117         *res = *scratch;
118 }
119 void FIRST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
120
121 // int
122 void FIRST_INT_lfta_LFTA_AGGR_INIT_(gs_int32_t* scratch) {
123         *scratch = INT_MAX;     // we will encode uninitialized value of ULLONG_MAX
124         return;
125 }
126 void FIRST_INT_lfta_LFTA_AGGR_UPDATE_(gs_int32_t* scratch, gs_int32_t val) {
127         if (*scratch == INT_MAX)
128                 *scratch = val;
129         return;
130 }
131 gs_retval_t FIRST_INT_lfta_LFTA_AGGR_FLUSHME_( gs_int32_t* scratch) { return 0; }
132 void FIRST_INT_lfta_LFTA_AGGR_OUTPUT_(gs_int32_t* res, gs_int32_t* scratch) {
133         *res = *scratch;
134 }
135 void FIRST_INT_lfta_LFTA_AGGR_DESTROY_(gs_int32_t* scratch) { return; }
136
137 // ullong
138 void FIRST_ULL_lfta_LFTA_AGGR_INIT_(gs_uint64_t* scratch) {
139         *scratch = ULLONG_MAX;          // we will encode uninitialized value of ULLONG_MAX
140         return;
141 }
142 void FIRST_ULL_lfta_LFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
143         if (*scratch == ULLONG_MAX)
144                 *scratch = val;
145         return;
146 }
147 gs_retval_t FIRST_ULL_lfta_LFTA_AGGR_FLUSHME_( gs_uint64_t* scratch) { return 0; }
148 void FIRST_ULL_lfta_LFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
149         *res = *scratch;
150 }
151 void FIRST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
152
153 // llong
154 void FIRST_LL_lfta_LFTA_AGGR_INIT_(gs_int64_t* scratch) {
155         *scratch = LLONG_MAX;           // we will encode uninitialized value of ULLONG_MAX
156         return;
157 }
158 void FIRST_LL_lfta_LFTA_AGGR_UPDATE_(gs_int64_t* scratch, gs_int64_t val) {
159         if (*scratch == LLONG_MAX)
160                 *scratch = val;
161         return;
162 }
163 gs_retval_t FIRST_LL_lfta_LFTA_AGGR_FLUSHME_( gs_int64_t* scratch) { return 0; }
164 void FIRST_LL_lfta_LFTA_AGGR_OUTPUT_(gs_int64_t* res, gs_int64_t* scratch) {
165         *res = *scratch;
166 }
167 void FIRST_LL_lfta_LFTA_AGGR_DESTROY_(gs_int64_t* scratch) { return; }
168
169 // string
170 void FIRST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
171         scratch->data = NULL;
172         return;
173 }
174 void FIRST_STR_lfta_LFTA_AGGR_UPDATE_(struct gs_string* scratch, struct gs_string* val) {
175         if (!scratch->data) {
176                 str_assign_with_copy(NULL, scratch, val);
177         }
178         return;
179 }
180 gs_retval_t FIRST_STR_lfta_LFTA_AGGR_FLUSHME_(struct gs_string* scratch) { return 0; }
181 void FIRST_STR_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, struct gs_string* scratch) {
182         *res = *scratch;
183 }
184 void FIRST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) { 
185         if (scratch->data) 
186                 fta_free(NULL, scratch->data);
187 }
188
189 ///////////////// LAST
190
191 // uint
192 void LAST_lfta_LFTA_AGGR_INIT_(gs_uint32_t* scratch) { }
193 void LAST_lfta_LFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
194         *scratch = val;
195         return;
196 }
197 gs_retval_t LAST_lfta_LFTA_AGGR_FLUSHME_( gs_uint32_t* scratch) { return 0; }
198
199 void LAST_lfta_LFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
200         *res = *scratch;
201 }
202 void LAST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
203
204 // int
205 void LAST_INT_lfta_LFTA_AGGR_INIT_(gs_int32_t* scratch) { }
206 void LAST_INT_lfta_LFTA_AGGR_UPDATE_(gs_int32_t* scratch, gs_int32_t val) {
207         *scratch = val;
208         return;
209 }
210 gs_retval_t LAST_INT_lfta_LFTA_AGGR_FLUSHME_( gs_int32_t* scratch) { return 0; }
211 void LAST_INT_lfta_LFTA_AGGR_OUTPUT_(gs_int32_t* res, gs_int32_t* scratch) {
212         *res = *scratch;
213 }
214 void LAST_INT_lfta_LFTA_AGGR_DESTROY_(gs_int32_t* scratch) { return; }
215
216 // ullong
217 void LAST_ULL_lfta_LFTA_AGGR_INIT_(gs_uint64_t* scratch) { }
218 void LAST_ULL_lfta_LFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
219         *scratch = val;
220         return;
221 }
222 gs_retval_t LAST_ULL_lfta_LFTA_AGGR_FLUSHME_( gs_uint64_t* scratch) { return 0; }
223 void LAST_ULL_lfta_LFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
224         *res = *scratch;
225 }
226 void LAST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
227
228 // llong
229 void LAST_LL_lfta_LFTA_AGGR_INIT_(gs_int64_t* scratch) { }
230 void LAST_LL_lfta_LFTA_AGGR_UPDATE_(gs_int64_t* scratch, gs_int64_t val) {
231         *scratch = val;
232         return;
233 }
234 gs_retval_t LAST_LL_lfta_LFTA_AGGR_FLUSHME_( gs_int64_t* scratch) { return 0; }
235 void LAST_LL_lfta_LFTA_AGGR_OUTPUT_(gs_int64_t* res, gs_int64_t* scratch) {
236         *res = *scratch;
237 }
238 void LAST_LL_lfta_LFTA_AGGR_DESTROY_(gs_int64_t* scratch) { return; }
239
240 // string
241 void LAST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
242         scratch->data = NULL;
243         return;
244 }
245 void LAST_STR_lfta_LFTA_AGGR_UPDATE_(struct gs_string* scratch, struct gs_string* val) {
246         if (!scratch->data) {
247                 str_assign_with_copy(NULL, scratch, val);
248         } else {
249                 if (!str_compare(scratch, val)) {
250                         fta_free(NULL, scratch->data);
251                         str_assign_with_copy(NULL, scratch, val);
252                 }
253         }
254         return;
255 }
256 gs_retval_t LAST_STR_lfta_LFTA_AGGR_FLUSHME_(struct gs_string* scratch) { return 0; }
257
258 void LAST_STR_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, struct gs_string* scratch) {
259         *res = *scratch;
260 }
261
262 void LAST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) { 
263         if (scratch->data) 
264                 fta_free(NULL, scratch->data);
265 }
266
267 ////////////////////////////////////////////////////////
268 //      count_diff aggregate
269 struct lfta_count_diff_scratch{
270         gs_uint32_t count;
271         union{
272                 gs_uint32_t ui;
273                 gs_int32_t i;
274                 gs_uint64_t ul;
275                 gs_int64_t l;
276         } first;
277         union{
278                 gs_uint32_t ui;
279                 gs_int32_t i;
280                 gs_uint64_t ul;
281                 gs_int64_t l;
282         } last;
283 };
284
285 //      unsigned int
286 void count_diff_lfta_ui_LFTA_AGGR_INIT_(gs_sp_t s) {
287         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
288         scratch->count = 0;
289         scratch->first.l = 0;
290         scratch->last.l = 0;
291 }
292 void count_diff_lfta_ui_LFTA_AGGR_UPDATE_(gs_sp_t s, gs_uint32_t val) {
293         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
294         if(scratch->count==0){
295                 scratch->count = 1;
296                 scratch->first.ui = val;
297         }else{
298                 if(scratch->last.ui != val)
299                         scratch->count++;
300         }
301         scratch->last.ui = val;
302 }
303 void count_diff_lfta_ui_LFTA_AGGR_OUTPUT_(struct gs_string * res, gs_sp_t scratch) {
304         res->data = (gs_sp_t)scratch;
305         res->length = sizeof(struct lfta_count_diff_scratch);
306         res->owner = NULL;
307 }
308 void count_diff_lfta_ui_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
309
310 gs_retval_t count_diff_lfta_ui_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
311         return 0;
312 }
313
314 //      int
315 void count_diff_lfta_i_LFTA_AGGR_INIT_(gs_sp_t s) {
316         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
317         scratch->count = 0;
318         scratch->first.l = 0;
319         scratch->last.l = 0;
320 }
321 void count_diff_lfta_i_LFTA_AGGR_UPDATE_(gs_sp_t s, gs_int32_t val) {
322         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
323         if(scratch->count==0){
324                 scratch->count = 1;
325                 scratch->first.i = val;
326         }else{
327                 if(scratch->last.i != val)
328                         scratch->count++;
329         }
330         scratch->last.i = val;
331 }
332 void count_diff_lfta_i_LFTA_AGGR_OUTPUT_(struct gs_string * res, gs_sp_t scratch) {
333         res->data = (gs_sp_t)scratch;
334         res->length = sizeof(struct lfta_count_diff_scratch);
335         res->owner = NULL;
336 }
337 void count_diff_lfta_i_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
338 gs_retval_t count_diff_lfta_i_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
339         return 0;
340 }
341
342 //      unsigned long long int
343 void count_diff_lfta_ul_LFTA_AGGR_INIT_(gs_sp_t s) {
344         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
345         scratch->count = 0;
346         scratch->first.l = 0;
347         scratch->last.l = 0;
348 }
349 void count_diff_lfta_ul_LFTA_AGGR_UPDATE_(gs_sp_t s, gs_uint64_t val) {
350         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
351         if(scratch->count==0){
352                 scratch->count = 1;
353                 scratch->first.ul = val;
354         }else{
355                 if(scratch->last.ul != val)
356                         scratch->count++;
357         }
358         scratch->last.ul = val;
359 }
360 void count_diff_lfta_ul_LFTA_AGGR_OUTPUT_(struct gs_string * res, gs_sp_t scratch) {
361         res->data = (gs_sp_t)scratch;
362         res->length = sizeof(struct lfta_count_diff_scratch);
363         res->owner = NULL;
364 }
365 void count_diff_lfta_ul_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
366 gs_retval_t count_diff_lfta_ul_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
367         return 0;
368 }
369
370 //      long long int
371 void count_diff_lfta_l_LFTA_AGGR_INIT_(gs_sp_t s) {
372         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
373         scratch->count = 0;
374         scratch->first.l = 0;
375         scratch->last.l = 0;
376 }
377 void count_diff_lfta_l_LFTA_AGGR_UPDATE_(gs_sp_t s, gs_int64_t val) {
378         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
379         if(scratch->count==0){
380                 scratch->count = 1;
381                 scratch->first.l = val;
382         }else{
383                 if(scratch->last.l != val)
384                         scratch->count++;
385         }
386         scratch->last.l = val;
387 }
388 void count_diff_lfta_l_LFTA_AGGR_OUTPUT_(struct gs_string* res, gs_sp_t scratch) {
389         res->data = (gs_sp_t)scratch;
390         res->length = sizeof(struct lfta_count_diff_scratch);
391         res->owner = NULL;
392 }
393 void count_diff_lfta_l_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
394 gs_retval_t count_diff_lfta_l_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
395         return 0;
396 }
397
398 //      string
399
400 static gs_uint64_t local_hash_string(struct gs_string *x){
401         gs_uint32_t i;
402         gs_uint64_t ret=0,tmp_sum = 0;
403         for(i=0;i<x->length;++i){
404                 tmp_sum |= (x->data[i]) << (8*(i%4));
405                 if((i%4) == 3){
406                         ret = tmp_sum + 12916008961267169387ull * ret;
407                         tmp_sum = 0;
408                 }
409         }
410         if((i%4)!=0) ret = tmp_sum + 12916008961267169387ull * ret;
411         return(ret);
412 }
413
414
415 void count_diff_lfta_s_LFTA_AGGR_INIT_(gs_sp_t s) {
416         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
417         scratch->count = 0;
418         scratch->first.ul = 0;
419         scratch->last.ul = 0;
420 }
421 void count_diff_lfta_s_LFTA_AGGR_UPDATE_(gs_sp_t s, struct gs_string* val) {
422         struct lfta_count_diff_scratch* scratch = (struct lfta_count_diff_scratch*)s;
423         gs_uint64_t hashval;
424         hashval = local_hash_string(val);
425         if(scratch->count==0){
426                 scratch->count = 1;
427                 scratch->first.ul = hashval;
428         }else{
429                 if(scratch->last.ul != hashval)
430                         scratch->count++;
431         }
432         scratch->last.ul = hashval;
433 }
434 void count_diff_lfta_s_LFTA_AGGR_OUTPUT_(struct gs_string* res, gs_sp_t scratch) {
435         res->data = (gs_sp_t)scratch;
436         res->length = sizeof(struct lfta_count_diff_scratch);
437         res->owner = NULL;
438 }
439 void count_diff_lfta_s_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
440 gs_retval_t count_diff_lfta_s_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
441         return 0;
442 }
443
444
445
446
447
448
449         
450
451
452
453
454 /////////////////////////////////////////////////////////
455 //              running_array_aggr aggregate
456
457 struct running_array_aggr_str{
458         gs_uint32_t num_list[4];
459         gs_uint8_t n_num;
460 };
461
462 void running_array_aggr_lfta_LFTA_AGGR_INIT_(char* scratch) {
463         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
464         aggr->n_num = 0;
465 }
466
467 void running_array_aggr_lfta_LFTA_AGGR_UPDATE_(char* scratch, gs_uint32_t val) {
468         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
469         aggr->num_list[aggr->n_num++] = val;
470 }
471
472 gs_retval_t running_array_aggr_lfta_LFTA_AGGR_FLUSHME_(char* scratch) {
473         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
474         return (aggr->n_num == 4);
475 }
476 void running_array_aggr_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, char* scratch) {
477         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;  
478         res->data = scratch;
479         res->length = aggr->n_num * sizeof(gs_uint32_t);
480         res->owner = NULL;
481 }
482
483 void running_array_aggr_lfta_LFTA_AGGR_DESTROY_(char* scratch) { }
484