Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
uda-sample-test.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 <iostream>
16 
17 #include <udf/uda-test-harness.h>
18 #include "uda-sample.h"
19 
20 #include "common/names.h"
21 
22 using namespace impala;
23 using namespace impala_udf;
24 
25 bool TestCount() {
26  // Use the UDA test harness to validate the COUNT UDA.
29 
30  // Run the UDA over 10000 non-null values
31  vector<IntVal> no_nulls;
32  no_nulls.resize(10000);
33  if (!test.Execute(no_nulls, BigIntVal(no_nulls.size()))) {
34  cerr << test.GetErrorMsg() << endl;
35  return false;
36  }
37 
38  // Run the UDA with some nulls
39  vector<IntVal> some_nulls;
40  some_nulls.resize(10000);
41  int expected = some_nulls.size();
42  for (int i = 0; i < some_nulls.size(); i += 100) {
43  some_nulls[i] = IntVal::null();
44  --expected;
45  }
46  if (!test.Execute(some_nulls, BigIntVal(expected))) {
47  cerr << test.GetErrorMsg() << endl;
48  return false;
49  }
50 
51  return true;
52 }
53 
54 bool TestAvg() {
57  test.SetIntermediateSize(16);
58 
59  vector<DoubleVal> vals;
60  for (int i = 0; i < 1001; ++i) {
61  vals.push_back(DoubleVal(i));
62  }
63  if (!test.Execute<DoubleVal>(vals, DoubleVal(500))) {
64  cerr << test.GetErrorMsg() << endl;
65  return false;
66  }
67  return true;
68 }
69 
71  // Use the UDA test harness to validate the COUNT UDA.
75 
76  vector<StringVal> values;
77  values.push_back("Hello");
78  values.push_back("World");
79 
80  vector<StringVal> separators;
81  for(int i = 0; i < values.size(); ++i) {
82  separators.push_back(",");
83  }
84  if (!test.Execute(values, separators, StringVal("Hello,World"))) {
85  cerr << test.GetErrorMsg() << endl;
86  return false;
87  }
88 
89  return true;
90 }
91 
92 int main(int argc, char** argv) {
93  bool passed = true;
94  passed &= TestCount();
95  passed &= TestAvg();
96  passed &= TestStringConcat();
97  cerr << (passed ? "Tests passed." : "Tests failed.") << endl;
98  return 0;
99 }
bool TestCount()
void SetIntermediateSize(int byte_size)
This must be called if the INTERMEDIATE is TYPE_FIXED_BUFFER.
bool Execute(const std::vector< INPUT1 > &values1, const std::vector< INPUT2 > &values2, const RESULT &expected, UdaExecutionMode mode=ALL)
Runs the UDA in all the modes, validating the result is 'expected' each time.
BigIntVal CountFinalize(FunctionContext *context, const BigIntVal &val)
Definition: uda-test.cc:49
void CountUpdate(FunctionContext *context, const IntVal &input, BigIntVal *val)
Definition: uda-test.cc:39
void StringConcatUpdate(FunctionContext *context, const StringVal &arg1, const StringVal &arg2, StringVal *val)
Definition: uda-sample.cc:83
bool TestAvg()
bool TestStringConcat()
void StringConcatMerge(FunctionContext *context, const StringVal &src, StringVal *dst)
Definition: uda-sample.cc:99
void AvgInit(FunctionContext *context, BufferVal *val)
Definition: uda-sample.cc:49
void CountInit(FunctionContext *context, BigIntVal *val)
This is an example of the COUNT aggregate function.
Definition: uda-test.cc:34
StringVal StringConcatFinalize(FunctionContext *context, const StringVal &val)
Definition: uda-sample.cc:104
bool Execute(const std::vector< INPUT > &values, const RESULT &expected, UdaExecutionMode mode=ALL)
Runs the UDA in all the modes, validating the result is 'expected' each time.
void CountMerge(FunctionContext *context, const BigIntVal &src, BigIntVal *dst)
Definition: uda-test.cc:45
int main(int argc, char **argv)
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
void AvgUpdate(FunctionContext *context, const DoubleVal &input, BufferVal *val)
Definition: uda-sample.cc:54
const std::string & GetErrorMsg() const
Returns the failure string if any.
DoubleVal AvgFinalize(FunctionContext *context, const BufferVal &val)
Definition: uda-sample.cc:69