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
7 http://www.apache.org/licenses/LICENSE-2.0
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 ------------------------------------------- */
19 #include "hfta_udaf.h"
29 #include "hfta_runtime_library.h"
32 #define max(a,b) ((a) > (b) ? (a) : (b))
33 #define min(x,y) ((x) < (y) ? (x) : (y))
34 #define lg(x) (log(x) / log(2))
39 // -------------------------------------------------------------------
40 // moving sum over N intervals
42 struct moving_sum_udaf_str{
48 void moving_sum_udaf_HFTA_AGGR_INIT_(gs_sp_t buf){
49 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) buf;
50 u->N=0; u->pos=0; u->sums=NULL;
53 void moving_sum_udaf_HFTA_AGGR_UPDATE_(gs_sp_t buf, gs_uint32_t s, gs_uint32_t N) {
54 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) buf;
56 u->sums = (gs_uint32_t *)malloc(N*sizeof(gs_uint32_t));
57 for(gs_int32_t i=0;i<N;i++)
64 void super_moving_sum_udaf_HFTA_AGGR_UPDATE_(gs_sp_t buf, gs_uint64_t sub_sum) {
65 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) buf;
66 gs_uint32_t s = (gs_uint32_t)(sub_sum & 0xffffffff);
68 gs_uint32_t N = (gs_uint32_t)((sub_sum & 0xffffffff00000000ull) >> 32);
69 u->sums = (gs_uint32_t *)malloc(N*sizeof(gs_uint32_t));
70 for(gs_int32_t i=0;i<N;i++)
77 void moving_sum_udaf_HFTA_AGGR_OUTPUT_(gs_p_t *result, gs_sp_t buf){
78 *result = (gs_p_t)(buf);
81 void moving_sum_udaf_HFTA_AGGR_DESTROY_(gs_sp_t buf){
82 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) buf;
87 void moving_sum_udaf_HFTA_AGGR_REINIT_( gs_sp_t buf){
88 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) buf;
95 gs_uint32_t moving_sum_extract(gs_p_t result){
96 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) result;
104 gs_float_t moving_sum_extract_exp(gs_p_t result, gs_float_t alpha){
105 struct moving_sum_udaf_str * u = (struct moving_sum_udaf_str *) result;
106 gs_uint32_t p=0, i=0;
107 gs_float_t s=0.0, m=1.0;
109 for(i=0; i<u->N;i++){
121 // -------------------------------------------------------------------
122 // sum over 3 intervals : test rUDAF
124 struct sum3_udaf_str{
130 void sum3_HFTA_AGGR_INIT_(gs_sp_t buf) {
131 struct sum3_udaf_str * u = (struct sum3_udaf_str *) buf;
132 u->s_0 = 0; u->s_1 = 0; u->s_2 = 0;
136 void sum3_HFTA_AGGR_UPDATE_(gs_sp_t buf, gs_uint32_t s) {
137 struct sum3_udaf_str * u = (struct sum3_udaf_str *) buf;
142 void sum3_HFTA_AGGR_OUTPUT_(gs_uint32_t *result, gs_sp_t buf) {
143 struct sum3_udaf_str * u = (struct sum3_udaf_str *) buf;
144 *result = u->s_0 + u->s_1 + u->s_2;
148 void sum3_HFTA_AGGR_DESTROY_(gs_sp_t buf) {
152 void sum3_HFTA_AGGR_REINIT_( gs_sp_t buf) {
153 struct sum3_udaf_str * u = (struct sum3_udaf_str *) buf;
161 #define HISTORY_LENGTH 1024
163 /////////////////////////////////////////////////////////////////////////
164 ///// Calculate the average of all positive gs_float_t numbers
166 struct posavg_struct{
171 void POSAVG_HFTA_AGGR_INIT_(gs_sp_t buf) {
172 struct posavg_struct * a = (struct posavg_struct *) buf;
178 void POSAVG_HFTA_AGGR_UPDATE_(gs_sp_t buf, gs_float_t v) {
179 struct posavg_struct * a = (struct posavg_struct *) buf;
187 void POSAVG_HFTA_AGGR_OUTPUT_(gs_float_t * v, gs_sp_t buf) {
188 struct posavg_struct * a = (struct posavg_struct *) buf;
197 void POSAVG_HFTA_AGGR_DESTROY_(gs_sp_t buf) {
201 /////////////////////////////////////////////////////////////////////////
202 ///// avg_udaf (simple example)
204 // struct received from subaggregate
205 struct avg_udaf_lfta_struct_t{
211 struct avg_udaf_hfta_struct_t{
216 // avg_udaf functions
217 void avg_udaf_HFTA_AGGR_INIT_(gs_sp_t b){
218 avg_udaf_hfta_struct_t *s = (avg_udaf_hfta_struct_t *) b;
223 void avg_udaf_HFTA_AGGR_UPDATE_(gs_sp_t b, gs_uint32_t v){
224 avg_udaf_hfta_struct_t *s = (avg_udaf_hfta_struct_t *) b;
229 void avg_udaf_HFTA_AGGR_OUTPUT_(vstring *r,gs_sp_t b){
231 r->offset = (gs_p_t)(b);
232 r->reserved = SHALLOW_COPY;
235 void avg_udaf_HFTA_AGGR_DESTROY_(gs_sp_t b){
240 // avg_udaf superaggregate functions
241 void avg_udaf_hfta_HFTA_AGGR_INIT_(gs_sp_t b){
242 avg_udaf_hfta_struct_t *s = (avg_udaf_hfta_struct_t *) b;
247 void avg_udaf_hfta_HFTA_AGGR_UPDATE_(gs_sp_t b, vstring *v){
248 if(v->length != 12) return;
249 avg_udaf_hfta_struct_t *s = (avg_udaf_hfta_struct_t *) b;
250 avg_udaf_lfta_struct_t *vs = (avg_udaf_lfta_struct_t *)(v->offset);
255 void avg_udaf_hfta_HFTA_AGGR_OUTPUT_(vstring *r,gs_sp_t b){
257 r->offset = (gs_p_t)(b);
258 r->reserved = SHALLOW_COPY;
261 void avg_udaf_hfta_HFTA_AGGR_DESTROY_(gs_sp_t b){
265 // Extraction function
266 gs_float_t extr_avg_fcn(vstring *v){
267 if(v->length != 12) return 0;
268 avg_udaf_hfta_struct_t *vs = (avg_udaf_hfta_struct_t *)(v->offset);
269 gs_float_t r = (gs_float_t)(vs->sum) / vs->cnt;
273 /////////////////////////////////////////////////////////
277 void FIRST_HFTA_AGGR_INIT_(gs_uint32_t* scratch) {
278 *scratch = UINT_MAX; // we will encode uninitialized value of UINT_MAX
281 void FIRST_HFTA_AGGR_REINIT_(gs_uint32_t* scratch) { }
283 void FIRST_HFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
284 if (*scratch == UINT_MAX)
288 void FIRST_HFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
292 void FIRST_HFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { }
294 void FIRST_ULL_HFTA_AGGR_INIT_(gs_uint64_t* scratch) {
295 *scratch = UINT_MAX; // we will encode uninitialized value of UINT_MAX
298 void FIRST_ULL_HFTA_AGGR_REINIT_(gs_uint64_t* scratch) { }
300 void FIRST_ULL_HFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
301 if (*scratch == UINT_MAX)
305 void FIRST_ULL_HFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
309 void FIRST_ULL_HFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { }
312 void FIRST_STR_HFTA_AGGR_INIT_(vstring* scratch) {
316 void FIRST_STR_HFTA_AGGR_REINIT_(vstring* scratch) { }
318 void FIRST_STR_HFTA_AGGR_UPDATE_(vstring* scratch, vstring* val) {
319 if (!scratch->offset) {
320 scratch->length = val->length;
321 scratch->offset = val->offset;
322 scratch->reserved = SHALLOW_COPY;
326 void FIRST_STR_HFTA_AGGR_OUTPUT_(vstring* res, vstring* scratch) {
330 void FIRST_STR_HFTA_AGGR_DESTROY_(vstring* scratch) { }
334 void FIRST_hfta_HFTA_AGGR_INIT_(gs_uint32_t* scratch) {
335 *scratch = UINT_MAX; // we will encode uninitialized value of UINT_MAX
338 void FIRST_hfta_HFTA_AGGR_REINIT_(gs_uint32_t* scratch) { }
340 void FIRST_hfta_HFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
341 if (*scratch == UINT_MAX)
345 void FIRST_hfta_HFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
349 void FIRST_hfta_HFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { }
351 void FIRST_ULL_hfta_HFTA_AGGR_INIT_(gs_uint64_t* scratch) {
352 *scratch = UINT_MAX; // we will encode uninitialized value of UINT_MAX
355 void FIRST_ULL_hfta_HFTA_AGGR_REINIT_(gs_uint64_t* scratch) { }
357 void FIRST_ULL_hfta_HFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
358 if (*scratch == UINT_MAX)
362 void FIRST_ULL_hfta_HFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
366 void FIRST_ULL_hfta_HFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { }
369 void FIRST_STR_hfta_HFTA_AGGR_INIT_(vstring* scratch) {
373 void FIRST_STR_hfta_HFTA_AGGR_REINIT_(vstring* scratch) { }
375 void FIRST_STR_hfta_HFTA_AGGR_UPDATE_(vstring* scratch, vstring* val) {
376 if (!scratch->offset) {
377 scratch->length = val->length;
378 scratch->offset = val->offset;
379 scratch->reserved = SHALLOW_COPY;
383 void FIRST_STR_hfta_HFTA_AGGR_OUTPUT_(vstring* res, vstring* scratch) {
387 void FIRST_STR_hfta_HFTA_AGGR_DESTROY_(vstring* scratch) { }
390 /////////////////////////////////////////////////////////
395 void LAST_HFTA_AGGR_INIT_(gs_uint32_t* scratch) { }
397 void LAST_HFTA_AGGR_REINIT_(gs_uint32_t* scratch) { }
399 void LAST_HFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
403 void LAST_HFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
407 void LAST_HFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { }
409 void LAST_ULLHFTA_AGGR_INIT_(gs_uint64_t* scratch) { }
411 void LAST_ULL_HFTA_AGGR_REINIT_(gs_uint64_t* scratch) { }
413 void LAST_ULL_HFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
417 void LAST_ULL_HFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
421 void LAST_ULL_HFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { }
424 void LAST_STR_HFTA_AGGR_INIT_(vstring* scratch) {
428 void LAST_STR_HFTA_AGGR_REINIT_(vstring* scratch) { }
430 void LAST_STR_HFTA_AGGR_UPDATE_(vstring* scratch, vstring* val) {
431 scratch->length = val->length;
432 scratch->offset = val->offset;
433 scratch->reserved = SHALLOW_COPY;
436 void LAST_STR_HFTA_AGGR_OUTPUT_(vstring* res, vstring* scratch) {
440 void LAST_STR_HFTA_AGGR_DESTROY_(vstring* scratch) { }
444 void LAST_hfta_HFTA_AGGR_INIT_(gs_uint32_t* scratch) { }
446 void LAST_hfta_HFTA_AGGR_REINIT_(gs_uint32_t* scratch) { }
448 void LAST_hfta_HFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
452 void LAST_hfta_HFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
456 void LAST_hfta_HFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { }
458 void LAST_ULL_hfta_HFTA_AGGR_INIT_(gs_uint64_t* scratch) { }
460 void LAST_ULL_hfta_HFTA_AGGR_REINIT_(gs_uint64_t* scratch) { }
462 void LAST_ULL_hfta_HFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
466 void LAST_ULL_hfta_HFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
470 void LAST_ULL_hfta_HFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { }
473 void LAST_STR_hfta_HFTA_AGGR_INIT_(vstring* scratch) {
477 void LAST_STR_hfta_HFTA_AGGR_REINIT_(vstring* scratch) { }
479 void LAST_STR_hfta_HFTA_AGGR_UPDATE_(vstring* scratch, vstring* val) {
480 scratch->length = val->length;
481 scratch->offset = val->offset;
482 scratch->reserved = SHALLOW_COPY;
485 void LAST_STR_hfta_HFTA_AGGR_OUTPUT_(vstring* res, vstring* scratch) {
489 void LAST_STR_hfta_HFTA_AGGR_DESTROY_(vstring* scratch) { }
492 /////////////////////////////////////////////////////////
493 // running_array_aggr aggregate
495 void running_array_aggr_hfta_HFTA_AGGR_INIT_(vstring* scratch) {
496 scratch->offset = NULL;
500 void running_array_aggr_hfta_HFTA_AGGR_REINIT_(vstring* scratch) { }
502 void running_array_aggr_hfta_HFTA_AGGR_UPDATE_(vstring* scratch, vstring* val) {
505 gs_uint32_t* ints = (gs_uint32_t*)val->offset;
506 switch (val->length / sizeof (gs_uint32_t)) {
508 sprintf(buffer, "%u,%u,%u,%u", ints[0], ints[1], ints[2], ints[3]);
511 sprintf(buffer, "%u,%u,%u", ints[0], ints[1], ints[2]);
514 sprintf(buffer, "%u,%u", ints[0], ints[1]);
517 sprintf(buffer, "%u", ints[0]);
522 int buf_len = strlen(buffer);
524 // append the content of buffer to scratch
525 if (!scratch->offset) {
526 Vstring_Constructor(scratch, buffer);
528 scratch->offset = (gs_p_t)realloc((void*)scratch->offset, scratch->length + buf_len + 1);
529 *((char*)scratch->offset + scratch->length) = ',';
530 memcpy((void*)(scratch->offset + scratch->length + 1), (void*)buffer, buf_len);
531 scratch->length += buf_len + 1;
532 scratch->reserved = INTERNAL;
536 void running_array_aggr_hfta_HFTA_AGGR_OUTPUT_(vstring* res, vstring* scratch) {
538 res->reserved = SHALLOW_COPY;
541 void running_array_aggr_hfta_HFTA_AGGR_DESTROY_(vstring* scratch) {
542 hfta_vstr_destroy(scratch);