15 #include <gtest/gtest.h>
17 #include <boost/date_time/posix_time/posix_time.hpp>
26 using boost::gregorian::date;
27 using boost::posix_time::nanoseconds;
28 using boost::posix_time::ptime;
29 using boost::posix_time::to_iso_extended_string;
30 using boost::posix_time::to_simple_string;
31 using namespace impala;
32 using namespace impala_udf;
39 cerr << (arg1.
is_null ?
"NULL" : string((
char*)arg1.
ptr, arg1.
len)) << endl;
44 if (input.
is_null)
return StringVal::null();
47 for (
int i = 0; i < input.
len; ++i) {
48 result.
ptr[i] = toupper(input.
ptr[i]);
62 v = std::min(v, f1.
val);
70 v = std::min(v, f2.
val);
78 v = std::min(v, f3.
val);
81 return is_null ? FloatVal::null() :
FloatVal(v);
87 for (
int i = 0; i < n; ++i) {
88 if (args[i].is_null)
continue;
92 if (all_null)
return StringVal::null();
96 for (
int i = 0; i < n; ++i) {
97 if (args[i].is_null)
continue;
98 memcpy(result.
ptr + offset, args[i].
ptr, args[i].
len);
99 offset += args[i].
len;
109 EXPECT_EQ(context->
version(), FunctionContext::v1_3);
111 EXPECT_TRUE(context->
error_msg() == NULL);
112 return IntVal::null();
117 EXPECT_TRUE(context->
error_msg() == NULL);
120 EXPECT_TRUE(strcmp(context->
error_msg(),
"Fail") == 0);
121 return IntVal::null();
125 EXPECT_TRUE(context->
Allocate(0) == NULL);
126 uint8_t* buffer = context->
Allocate(10);
127 EXPECT_TRUE(buffer != NULL);
128 memset(buffer, 0, 10);
129 context->
Free(buffer);
130 return IntVal::null();
134 ptime t(*(date*)&time.
date);
137 ss << to_iso_extended_string(t.date()) <<
" " << to_simple_string(t.time_of_day());
140 memcpy(result.ptr, s.data(), result.len);
146 if (scope == FunctionContext::THREAD_LOCAL) {
152 ASSERT_TRUE(bytes != NULL);
160 EXPECT_TRUE(state != NULL);
161 memset(state, 0, bytes.
val);
162 return SmallIntVal::null();
167 if (scope == FunctionContext::THREAD_LOCAL) {
169 context->
Free(reinterpret_cast<uint8_t*>(state));
174 TEST(UdfTest, TestFunctionContext) {
175 EXPECT_TRUE(UdfTestHarness::ValidateUdf<IntVal>(
ValidateUdf, IntVal::null()));
176 EXPECT_FALSE(UdfTestHarness::ValidateUdf<IntVal>(
ValidateFail, IntVal::null()));
177 EXPECT_TRUE(UdfTestHarness::ValidateUdf<IntVal>(
ValidateMem, IntVal::null()));
180 vector<AnyVal*> constant_args;
181 constant_args.push_back(arg.get());
182 EXPECT_TRUE((UdfTestHarness::ValidateUdf<SmallIntVal, SmallIntVal>(
189 EXPECT_FALSE(UdfTestHarness::ValidateUdf<DoubleVal>(
ZeroUdf,
DoubleVal(10)));
191 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, StringVal>(
194 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, StringVal>(
197 EXPECT_TRUE((UdfTestHarness::ValidateUdf<FloatVal, FloatVal, FloatVal, FloatVal>(
199 EXPECT_TRUE((UdfTestHarness::ValidateUdf<FloatVal, FloatVal, FloatVal, FloatVal>(
201 EXPECT_TRUE((UdfTestHarness::ValidateUdf<FloatVal, FloatVal, FloatVal, FloatVal>(
202 Min3, FloatVal::null(), FloatVal::null(), FloatVal::null(), FloatVal::null())));
205 TEST(UdfTest, TestTimestampVal) {
208 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, TimestampVal>(
212 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, TimestampVal>(
216 TEST(UdfTest, TestDecimalVal) {
248 vector<StringVal> input;
252 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, StringVal>(
256 EXPECT_TRUE((UdfTestHarness::ValidateUdf<StringVal, StringVal>(
261 EXPECT_TRUE((UdfTestHarness::ValidateUdf<IntVal, BigIntVal, IntVal>(
268 EXPECT_TRUE((UdfTestHarness::ValidateUdf<BigIntVal, BigIntVal>(
271 EXPECT_FALSE((UdfTestHarness::ValidateUdf<BigIntVal, BigIntVal>(
274 EXPECT_FALSE((UdfTestHarness::ValidateUdf<BigIntVal, BigIntVal>(
279 int main(
int argc,
char** argv) {
281 ::testing::InitGoogleTest(&argc, argv);
282 return RUN_ALL_TESTS();
int main(int argc, char **argv)
BigIntVal MemTest(FunctionContext *context, const BigIntVal &bytes)
int64_t time_of_day
Nanoseconds in current day.
IntVal ValidateFail(FunctionContext *context)
void MemTestPrepare(FunctionContext *context, FunctionContext::FunctionStateScope scope)
bool has_error() const
Returns true if there's been an error set.
StringVal TimeToString(FunctionContext *context, const TimestampVal &time)
FloatVal Min3(FunctionContext *context, const FloatVal &f1, const FloatVal &f2, const FloatVal &f3)
int32_t date
Gregorian date. This has the same binary format as boost::gregorian::date.
This object has a compatible storage format with boost::ptime.
ImpalaVersion version() const
Returns the version of Impala that's currently running.
StringVal LogUdf(FunctionContext *context, const StringVal &arg1)
void InitGoogleLoggingSafe(const char *arg)
void * GetFunctionState(FunctionStateScope scope) const
void Free(uint8_t *buffer)
Frees a buffer returned from Allocate() or Reallocate()
void SetFunctionState(FunctionStateScope scope, void *ptr)
DoubleVal ZeroUdf(FunctionContext *context)
IntVal ValidateMem(FunctionContext *context)
const char * error_msg() const
Returns the current error message. Returns NULL if there is no error.
SmallIntVal ValidateSharedState(FunctionContext *context, SmallIntVal bytes)
uint8_t * Allocate(int byte_size)
uint8_t offset[7 *64-sizeof(uint64_t)]
void MemTestClose(FunctionContext *context, FunctionContext::FunctionStateScope scope)
IntVal ValidateUdf(FunctionContext *context)
void SetError(const char *error_msg)
void ValidateSharedStateClose(FunctionContext *context, FunctionContext::FunctionStateScope scope)
void ValidateSharedStatePrepare(FunctionContext *context, FunctionContext::FunctionStateScope scope)
BigIntVal DoubleFreeTest(FunctionContext *context, BigIntVal bytes)
AnyVal * GetConstantArg(int arg_idx) const
StringVal Concat(FunctionContext *context, int n, const StringVal *args)
IntVal NumVarArgs(FunctionContext *, const BigIntVal &dummy, int n, const IntVal *args)
StringVal UpperUdf(FunctionContext *context, const StringVal &input)