Fixed newline characters throughout the code
[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 void FIRST_lfta_LFTA_AGGR_INIT_(gs_uint32_t* scratch) {
103         *scratch = UINT_MAX;            // we will encode uninitialized value of UINT_MAX
104         return;
105 }
106
107 void FIRST_lfta_LFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
108         if (*scratch == UINT_MAX)
109                 *scratch = val;
110         return;
111 }
112
113 gs_retval_t FIRST_lfta_LFTA_AGGR_FLUSHME_( gs_uint32_t* scratch) { return 0; }
114
115 void FIRST_lfta_LFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
116         *res = *scratch;
117 }
118
119 void FIRST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
120
121 void FIRST_ULL_lfta_LFTA_AGGR_INIT_(gs_uint64_t* scratch) {
122         *scratch = ULLONG_MAX;          // we will encode uninitialized value of ULLONG_MAX
123         return;
124 }
125
126 void FIRST_ULL_lfta_LFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
127         if (*scratch == ULLONG_MAX)
128                 *scratch = val;
129         return;
130 }
131
132 gs_retval_t FIRST_ULL_lfta_LFTA_AGGR_FLUSHME_( gs_uint64_t* scratch) { return 0; }
133
134 void FIRST_ULL_lfta_LFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
135         *res = *scratch;
136 }
137
138 void FIRST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
139
140
141
142 void FIRST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
143         scratch->data = NULL;
144         return;
145 }
146
147 void FIRST_STR_lfta_LFTA_AGGR_UPDATE_(struct gs_string* scratch, struct gs_string* val) {
148         if (!scratch->data) {
149                 str_assign_with_copy(NULL, scratch, val);
150         }
151         return;
152 }
153
154 gs_retval_t FIRST_STR_lfta_LFTA_AGGR_FLUSHME_(struct gs_string* scratch) { return 0; }
155
156 void FIRST_STR_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, struct gs_string* scratch) {
157         *res = *scratch;
158 }
159
160 void FIRST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) { 
161         if (scratch->data) 
162                 fta_free(NULL, scratch->data);
163 }
164
165 void LAST_lfta_LFTA_AGGR_INIT_(gs_uint32_t* scratch) { }
166
167 void LAST_lfta_LFTA_AGGR_UPDATE_(gs_uint32_t* scratch, gs_uint32_t val) {
168         *scratch = val;
169         return;
170 }
171
172 gs_retval_t LAST_lfta_LFTA_AGGR_FLUSHME_( gs_uint32_t* scratch) { return 0; }
173
174 void LAST_lfta_LFTA_AGGR_OUTPUT_(gs_uint32_t* res, gs_uint32_t* scratch) {
175         *res = *scratch;
176 }
177
178 void LAST_lfta_LFTA_AGGR_DESTROY_(gs_uint32_t* scratch) { return; }
179
180 void LAST_ULL_lfta_LFTA_AGGR_INIT_(gs_uint64_t* scratch) { }
181
182 void LAST_ULL_lfta_LFTA_AGGR_UPDATE_(gs_uint64_t* scratch, gs_uint64_t val) {
183         *scratch = val;
184         return;
185 }
186
187 gs_retval_t LAST_ULL_lfta_LFTA_AGGR_FLUSHME_( gs_uint64_t* scratch) { return 0; }
188
189 void LAST_ULL_lfta_LFTA_AGGR_OUTPUT_(gs_uint64_t* res, gs_uint64_t* scratch) {
190         *res = *scratch;
191 }
192
193 void LAST_ULL_lfta_LFTA_AGGR_DESTROY_(gs_uint64_t* scratch) { return; }
194
195
196
197 void LAST_STR_lfta_LFTA_AGGR_INIT_(struct gs_string* scratch) {
198         scratch->data = NULL;
199         return;
200 }
201
202 void LAST_STR_lfta_LFTA_AGGR_UPDATE_(struct gs_string* scratch, struct gs_string* val) {
203         if (!scratch->data) {
204                 str_assign_with_copy(NULL, scratch, val);
205         } else {
206                 if (!str_compare(scratch, val)) {
207                         fta_free(NULL, scratch->data);
208                         str_assign_with_copy(NULL, scratch, val);
209                 }
210         }
211         return;
212 }
213
214 gs_retval_t LAST_STR_lfta_LFTA_AGGR_FLUSHME_(struct gs_string* scratch) { return 0; }
215
216 void LAST_STR_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, struct gs_string* scratch) {
217         *res = *scratch;
218 }
219
220 void LAST_STR_lfta_LFTA_AGGR_DESTROY_(struct gs_string* scratch) { 
221         if (scratch->data) 
222                 fta_free(NULL, scratch->data);
223 }
224
225
226 /////////////////////////////////////////////////////////
227 //              running_array_aggr aggregate
228
229 struct running_array_aggr_str{
230         gs_uint32_t num_list[4];
231         gs_uint8_t n_num;
232 };
233
234 void running_array_aggr_lfta_LFTA_AGGR_INIT_(char* scratch) {
235         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
236         aggr->n_num = 0;
237 }
238
239 void running_array_aggr_lfta_LFTA_AGGR_UPDATE_(char* scratch, gs_uint32_t val) {
240         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
241         aggr->num_list[aggr->n_num++] = val;
242 }
243
244 gs_retval_t running_array_aggr_lfta_LFTA_AGGR_FLUSHME_(char* scratch) {
245         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;
246         return (aggr->n_num == 4);
247 }
248 void running_array_aggr_lfta_LFTA_AGGR_OUTPUT_(struct gs_string* res, char* scratch) {
249         struct running_array_aggr_str* aggr = (struct running_array_aggr_str*)scratch;  
250         res->data = scratch;
251         res->length = aggr->n_num * sizeof(gs_uint32_t);
252         res->owner = NULL;
253 }
254
255 void running_array_aggr_lfta_LFTA_AGGR_DESTROY_(char* scratch) { }
256