Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uda-sample.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
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 #include "uda-sample.h"
16 #include <assert.h>
17 
18 using namespace impala_udf;
19 
20 // ---------------------------------------------------------------------------
21 // This is a sample of implementing a COUNT aggregate function.
22 // ---------------------------------------------------------------------------
23 void CountInit(FunctionContext* context, BigIntVal* val) {
24  val->is_null = false;
25  val->val = 0;
26 }
27 
28 void CountUpdate(FunctionContext* context, const IntVal& input, BigIntVal* val) {
29  if (input.is_null) return;
30  ++val->val;
31 }
32 
33 void CountMerge(FunctionContext* context, const BigIntVal& src, BigIntVal* dst) {
34  dst->val += src.val;
35 }
36 
38  return val;
39 }
40 
41 // ---------------------------------------------------------------------------
42 // This is a sample of implementing a AVG aggregate function.
43 // ---------------------------------------------------------------------------
44 struct AvgStruct {
45  double sum;
46  int64_t count;
47 };
48 
49 void AvgInit(FunctionContext* context, BufferVal* val) {
50  assert(sizeof(AvgStruct) == 16);
51  memset(*val, 0, sizeof(AvgStruct));
52 }
53 
54 void AvgUpdate(FunctionContext* context, const DoubleVal& input, BufferVal* val) {
55  if (input.is_null) return;
56  AvgStruct* avg = reinterpret_cast<AvgStruct*>(*val);
57  avg->sum += input.val;
58  ++avg->count;
59 }
60 
61 void AvgMerge(FunctionContext* context, const BufferVal& src, BufferVal* dst) {
62  if (src == NULL) return;
63  const AvgStruct* src_struct = reinterpret_cast<const AvgStruct*>(src);
64  AvgStruct* dst_struct = reinterpret_cast<AvgStruct*>(*dst);
65  dst_struct->sum += src_struct->sum;
66  dst_struct->count += src_struct->count;
67 }
68 
70  if (val == NULL) return DoubleVal::null();
71  AvgStruct* val_struct = reinterpret_cast<AvgStruct*>(val);
72  return DoubleVal(val_struct->sum / val_struct->count);
73 }
74 
75 // ---------------------------------------------------------------------------
76 // This is a sample of implementing the STRING_CONCAT aggregate function.
77 // Example: select string_concat(string_col, ",") from table
78 // ---------------------------------------------------------------------------
80  val->is_null = true;
81 }
82 
83 void StringConcatUpdate(FunctionContext* context, const StringVal& arg1,
84  const StringVal& arg2, StringVal* val) {
85  if (val->is_null) {
86  val->is_null = false;
87  *val = StringVal(context, arg1.len);
88  memcpy(val->ptr, arg1.ptr, arg1.len);
89  } else {
90  int new_len = val->len + arg1.len + arg2.len;
91  StringVal new_val(context, new_len);
92  memcpy(new_val.ptr, val->ptr, val->len);
93  memcpy(new_val.ptr + val->len, arg2.ptr, arg2.len);
94  memcpy(new_val.ptr + val->len + arg2.len, arg1.ptr, arg1.len);
95  *val = new_val;
96  }
97 }
98 
99 void StringConcatMerge(FunctionContext* context, const StringVal& src, StringVal* dst) {
100  if (src.is_null) return;
101  StringConcatUpdate(context, src, ",", dst);
102 }
103 
105  return val;
106 }
107 
108 // ---------------------------------------------------------------------------
109 // This is a sample of implementing the SUM aggregate function for decimals.
110 // Example: select sum_small_decimal(dec_col) from table
111 // It is different than the builtin sum since it can easily overflow but can
112 // be faster for small tables.
113 // ---------------------------------------------------------------------------
115  val->is_null = true;
116  val->val4 = 0;
117 }
118 
120  const DecimalVal& src, DecimalVal* dst) {
121  assert(ctx->GetArgType(0)->scale == 2);
122  assert(ctx->GetArgType(0)->precision == 9);
123  if (src.is_null) return;
124  dst->is_null = false;
125  dst->val4 += src.val4;
126 }
127 
129  if (src.is_null) return;
130  dst->is_null = false;
131  dst->val4 += src.val4;
132 }
int precision
Only valid if type == TYPE_DECIMAL.
Definition: udf.h:75
void SumSmallDecimalMerge(FunctionContext *, const DecimalVal &src, DecimalVal *dst)
Definition: uda-sample.cc:128
void SumSmallDecimalUpdate(FunctionContext *ctx, const DecimalVal &src, DecimalVal *dst)
Definition: uda-sample.cc:119
void SumSmallDecimalInit(FunctionContext *, DecimalVal *val)
Definition: uda-sample.cc:114
void StringConcatUpdate(FunctionContext *context, const StringVal &arg1, const StringVal &arg2, StringVal *val)
Definition: uda-sample.cc:83
uint8_t * BufferVal
Definition: udf.h:600
uint8_t * ptr
Definition: udf.h:523
bool is_null
Definition: udf.h:359
void CountInit(FunctionContext *context, BigIntVal *val)
This is an example of the COUNT aggregate function.
Definition: uda-sample.cc:23
double sum
Definition: uda-sample.cc:45
const TypeDesc * GetArgType(int arg_idx) const
Definition: udf.cc:425
void StringConcatMerge(FunctionContext *context, const StringVal &src, StringVal *dst)
Definition: uda-sample.cc:99
BigIntVal CountFinalize(FunctionContext *context, const BigIntVal &val)
Definition: uda-sample.cc:37
void AvgInit(FunctionContext *context, BufferVal *val)
Definition: uda-sample.cc:49
int64_t count
Definition: uda-sample.cc:46
void CountUpdate(FunctionContext *context, const IntVal &input, BigIntVal *val)
Definition: uda-sample.cc:28
StringVal StringConcatFinalize(FunctionContext *context, const StringVal &val)
Definition: uda-sample.cc:104
void CountMerge(FunctionContext *context, const BigIntVal &src, BigIntVal *dst)
Definition: uda-sample.cc:33
void StringConcatInit(FunctionContext *context, StringVal *val)
Definition: uda-sample.cc:79
void AvgMerge(FunctionContext *context, const BufferVal &src, BufferVal *dst)
Definition: uda-sample.cc:61
static DoubleVal null()
Definition: udf.h:480
void AvgUpdate(FunctionContext *context, const DoubleVal &input, BufferVal *val)
Definition: uda-sample.cc:54
DoubleVal AvgFinalize(FunctionContext *context, const BufferVal &val)
Definition: uda-sample.cc:69