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 ------------------------------------------- */
24 #define max(a,b) ((a) > (b) ? (a) : (b))
25 #define MAX_BUFSIZE 128
28 /****************************************************************/
30 /****************************************************************/
33 ////////////////////////////////////////////////////////////////////////
36 typedef struct avg_udaf_lfta_struct_t{
39 } avg_udaf_lfta_struct_t;
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;
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;
53 gs_retval_t avg_udaf_lfta_LFTA_AGGR_FLUSHME_(gs_sp_t b){
57 void avg_udaf_lfta_LFTA_AGGR_OUTPUT_(struct gs_string *r,gs_sp_t b){
62 void avg_udaf_lfta_LFTA_AGGR_DESTROY_(gs_sp_t b){
66 /////////////////////////////////////////////////////////
69 typedef struct moving_sum_lfta_struct{
72 } moving_sum_lfta_struct;
74 void moving_sum_lfta_LFTA_AGGR_INIT_(gs_sp_t b){
75 moving_sum_lfta_struct *s = (moving_sum_lfta_struct *)b;
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;
86 gs_retval_t moving_sum_lfta_LFTA_AGGR_FLUSHME_(gs_sp_t b){
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);
95 gs_retval_t moving_sum_lfta_LFTA_AGGR_DESTROY_(gs_sp_t b){
99 /////////////////////////////////////////////////////////
100 // FIRST and LAST aggregates
102 ///////////////// FIRST
105 void FIRST_lfta_LFTA_AGGR_INIT_(gs_uint32_t* scratch) {
106 *scratch = UINT_MAX; // we will encode uninitialized value of UINT_MAX
110 void FIRST_lfta_LFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
111 if (*scratch == UINT_MAX)
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) {
119 void FIRST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
122 void FIRST_INT_lfta_LFTA_AGGR_INIT_(gs_int32_t* scratch) {
123 *scratch = INT_MAX; // we will encode uninitialized value of ULLONG_MAX
126 void FIRST_INT_lfta_LFTA_AGGR_UPDATE_(gs_int32_t* scratch, gs_int32_t val) {
127 if (*scratch == INT_MAX)
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) {
135 void FIRST_INT_lfta_LFTA_AGGR_DESTROY_(gs_int32_t* scratch) { return; }
138 void FIRST_ULL_lfta_LFTA_AGGR_INIT_(gs_uint64_t* scratch) {
139 *scratch = ULLONG_MAX; // we will encode uninitialized value of ULLONG_MAX
142 void FIRST_ULL_lfta_LFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
143 if (*scratch == ULLONG_MAX)
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) {
151 void FIRST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
154 void FIRST_LL_lfta_LFTA_AGGR_INIT_(gs_int64_t* scratch) {
155 *scratch = LLONG_MAX; // we will encode uninitialized value of ULLONG_MAX
158 void FIRST_LL_lfta_LFTA_AGGR_UPDATE_(gs_int64_t* scratch, gs_int64_t val) {
159 if (*scratch == LLONG_MAX)
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) {
167 void FIRST_LL_lfta_LFTA_AGGR_DESTROY_(gs_int64_t* scratch) { return; }
170 void FIRST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
171 scratch->data = NULL;
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);
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) {
184 void FIRST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) {
186 fta_free(NULL, scratch->data);
189 ///////////////// LAST
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) {
197 gs_retval_t LAST_lfta_LFTA_AGGR_FLUSHME_( gs_uint32_t* scratch) { return 0; }
199 void LAST_lfta_LFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
202 void LAST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
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) {
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) {
214 void LAST_INT_lfta_LFTA_AGGR_DESTROY_(gs_int32_t* scratch) { return; }
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) {
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) {
226 void LAST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
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) {
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) {
238 void LAST_LL_lfta_LFTA_AGGR_DESTROY_(gs_int64_t* scratch) { return; }
241 void LAST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
242 scratch->data = NULL;
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);
249 if (!str_compare(scratch, val)) {
250 fta_free(NULL, scratch->data);
251 str_assign_with_copy(NULL, scratch, val);
256 gs_retval_t LAST_STR_lfta_LFTA_AGGR_FLUSHME_(struct gs_string* scratch) { return 0; }
258 void LAST_STR_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, struct gs_string* scratch) {
262 void LAST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) {
264 fta_free(NULL, scratch->data);
267 ////////////////////////////////////////////////////////
268 // count_diff aggregate
269 struct lfta_count_diff_scratch{
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;
289 scratch->first.l = 0;
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){
296 scratch->first.ui = val;
298 if(scratch->last.ui != val)
301 scratch->last.ui = val;
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);
308 void count_diff_lfta_ui_LFTA_AGGR_DESTROY_(gs_sp_t s) { }
310 gs_retval_t count_diff_lfta_ui_LFTA_AGGR_FLUSHME_(gs_sp_t s) {
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;
318 scratch->first.l = 0;
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){
325 scratch->first.i = val;
327 if(scratch->last.i != val)
330 scratch->last.i = val;
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);
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) {
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;
346 scratch->first.l = 0;
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){
353 scratch->first.ul = val;
355 if(scratch->last.ul != val)
358 scratch->last.ul = val;
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);
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) {
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;
374 scratch->first.l = 0;
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){
381 scratch->first.l = val;
383 if(scratch->last.l != val)
386 scratch->last.l = val;
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);
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) {
400 static gs_uint64_t local_hash_string(struct gs_string *x){
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));
406 ret = tmp_sum + 12916008961267169387ull * ret;
410 if((i%4)!=0) ret = tmp_sum + 12916008961267169387ull * ret;
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;
418 scratch->first.ul = 0;
419 scratch->last.ul = 0;
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;
424 hashval = local_hash_string(val);
425 if(scratch->count==0){
427 scratch->first.ul = hashval;
429 if(scratch->last.ul != hashval)
432 scratch->last.ul = hashval;
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);
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) {
454 /////////////////////////////////////////////////////////
455 // running_array_aggr aggregate
457 struct running_array_aggr_str{
458 gs_uint32_t num_list[4];
462 void running_array_aggr_lfta_LFTA_AGGR_INIT_(char* scratch) {
463 struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
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;
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);
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;
479 res->length = aggr->n_num * sizeof(gs_uint32_t);
483 void running_array_aggr_lfta_LFTA_AGGR_DESTROY_(char* scratch) { }