28 using boost::algorithm::is_any_of;
29 using boost::algorithm::join;
30 using boost::algorithm::split;
31 using boost::algorithm::to_lower;
47 #define ONE_ARG_MATH_FN(NAME, RET_TYPE, INPUT_TYPE, FN) \
48 RET_TYPE MathFunctions::NAME(FunctionContext* ctx, const INPUT_TYPE& v) { \
49 if (v.is_null) return RET_TYPE::null(); \
50 return RET_TYPE(FN(v.val)); \
73 if (v.
is_null)
return FloatVal::null();
74 return FloatVal((v.
val > 0) ? 1.0f : ((v.
val < 0) ? -1.0f : 0.0f));
88 if (v.
is_null)
return BigIntVal::null();
89 return BigIntVal(static_cast<int64_t>(v.
val + ((v.
val < 0) ? -0.5 : 0.5)));
99 if (v.
is_null)
return DoubleVal::null();
117 if (scope == FunctionContext::THREAD_LOCAL) {
118 uint32_t* seed =
reinterpret_cast<uint32_t*
>(ctx->
Allocate(
sizeof(uint32_t)));
124 ctx->
SetError(
"Seed argument to rand() must be constant");
129 if (seed_arg->is_null) {
132 *seed = seed_arg->
val;
144 reinterpret_cast<uint32_t*
>(ctx->
GetFunctionState(FunctionContext::THREAD_LOCAL));
145 DCHECK(seed != NULL);
146 *seed = rand_r(seed);
148 return DoubleVal(static_cast<double>(*seed) / RAND_MAX);
152 if (seed.
is_null)
return DoubleVal::null();
157 if (v.
is_null)
return StringVal::null();
162 const size_t max_bits =
sizeof(
uint64_t) * 8;
163 char result[max_bits];
164 uint32_t index = max_bits;
166 result[--index] =
'0' + (n & 1);
172 if (v.
is_null)
return StringVal::null();
175 ss << hex << uppercase << v.
val;
180 if (s.
is_null)
return StringVal::null();
182 ss << hex << uppercase << setfill(
'0');
183 for (
int i = 0; i < s.
len; ++i) {
186 ss << setw(2) << (static_cast<int32_t>(s.
ptr[i]) & 0xFF);
192 if (s.
is_null)
return StringVal::null();
196 int result_len = s.
len / 2;
197 char result[result_len];
200 while (s_index < s.
len) {
202 for (
int j = 0; j < 2; ++j, ++s_index) {
203 switch(s.
ptr[s_index]) {
214 c += (s.
ptr[s_index] -
'0') * ((j == 0) ? 16 : 1);
223 c += (s.
ptr[s_index] -
'A' + 10) * ((j == 0) ? 16 : 1);
232 c += (s.
ptr[s_index] -
'a' + 10) * ((j == 0) ? 16 : 1);
239 result[res_index] = c;
254 return StringVal::null();
258 if (src_base.
val < 0 && num.
val >= 0)
return StringVal::null();
259 int64_t decimal_num = num.
val;
260 if (src_base.
val != 10) {
280 return StringVal::null();
284 int64_t decimal_num = StringParser::StringToInt<int64_t>(
285 reinterpret_cast<char*
>(num_str.
ptr), num_str.
len, src_base.
val, &parse_res);
286 if (src_base.
val < 0 && decimal_num >= 0) {
288 return StringVal::null();
292 return StringVal(reinterpret_cast<uint8_t*>(const_cast<char*>(
"0")), 1);
300 const size_t max_digits =
sizeof(
uint64_t) * 8 + 1;
301 char buf[max_digits];
302 int32_t result_len = 0;
303 int32_t buf_index = max_digits - 1;
307 temp_num =
abs(src_num);
311 temp_num =
static_cast<uint64_t>(src_num);
313 int abs_base =
abs(dest_base);
316 temp_num /= abs_base;
319 }
while (temp_num > 0);
321 if (src_num < 0 && dest_base < 0) {
322 buf[buf_index] =
'-';
334 int32_t digit = temp_num % 10;
336 if (digit >= src_base) {
340 *result += digit * place;
348 }
while (temp_num > 0);
349 *result = (src_num < 0) ? -(*result) : *result;
361 *num = numeric_limits<uint64_t>::max();
399 if (val.is_null)
return val;
416 return DecimalVal::null();
423 return BigIntVal::null();
425 return BigIntVal(static_cast<int64_t>(x.
val) / static_cast<int64_t>(y.
val));
435 DCHECK_GT(num_args, 0);
436 if (args[0].is_null)
return VAL_TYPE::null();
438 for (
int i = 1; i < num_args; ++i) {
439 if (args[i].is_null)
return VAL_TYPE::null();
441 if (args[i].val < args[result_idx].val) result_idx = i;
443 if (args[i].val > args[result_idx].val) result_idx = i;
446 return VAL_TYPE(args[result_idx].val);
451 DCHECK_GT(num_args, 0);
452 if (args[0].is_null)
return StringVal::null();
454 for (
int i = 1; i < num_args; ++i) {
455 if (args[i].is_null)
return StringVal::null();
458 if (val < result_val) result_val = val;
460 if (val > result_val) result_val = val;
470 DCHECK_GT(num_args, 0);
471 if (args[0].is_null)
return TimestampVal::null();
473 for (
int i = 1; i < num_args; ++i) {
474 if (args[i].is_null)
return TimestampVal::null();
477 if (val < result_val) result_val = val;
479 if (val > result_val) result_val = val;
489 DCHECK_GT(num_args, 0);
490 if (args[0].is_null)
return DecimalVal::null();
493 for (
int i = 1; i < num_args; ++i) {
494 if (args[i].is_null)
return DecimalVal::null();
498 if (args[i].val4 < result_val.
val4) result_val = args[i];
500 if (args[i].val4 > result_val.
val4) result_val = args[i];
505 if (args[i].val8 < result_val.
val8) result_val = args[i];
507 if (args[i].val8 > result_val.
val8) result_val = args[i];
512 if (args[i].val16 < result_val.
val16) result_val = args[i];
514 if (args[i].val16 > result_val.
val16) result_val = args[i];
524 template TinyIntVal MathFunctions::Positive<TinyIntVal>(
526 template SmallIntVal MathFunctions::Positive<SmallIntVal>(
528 template IntVal MathFunctions::Positive<IntVal>(
530 template BigIntVal MathFunctions::Positive<BigIntVal>(
532 template FloatVal MathFunctions::Positive<FloatVal>(
534 template DoubleVal MathFunctions::Positive<DoubleVal>(
536 template DecimalVal MathFunctions::Positive<DecimalVal>(
539 template TinyIntVal MathFunctions::Negative<TinyIntVal>(
541 template SmallIntVal MathFunctions::Negative<SmallIntVal>(
543 template IntVal MathFunctions::Negative<IntVal>(
545 template BigIntVal MathFunctions::Negative<BigIntVal>(
547 template FloatVal MathFunctions::Negative<FloatVal>(
549 template DoubleVal MathFunctions::Negative<DoubleVal>(
552 template TinyIntVal MathFunctions::LeastGreatest<TinyIntVal, true>(
554 template SmallIntVal MathFunctions::LeastGreatest<SmallIntVal, true>(
556 template IntVal MathFunctions::LeastGreatest<IntVal, true>(
558 template BigIntVal MathFunctions::LeastGreatest<BigIntVal, true>(
560 template FloatVal MathFunctions::LeastGreatest<FloatVal, true>(
562 template DoubleVal MathFunctions::LeastGreatest<DoubleVal, true>(
565 template TinyIntVal MathFunctions::LeastGreatest<TinyIntVal, false>(
567 template SmallIntVal MathFunctions::LeastGreatest<SmallIntVal, false>(
569 template IntVal MathFunctions::LeastGreatest<IntVal, false>(
571 template BigIntVal MathFunctions::LeastGreatest<BigIntVal, false>(
573 template FloatVal MathFunctions::LeastGreatest<FloatVal, false>(
575 template DoubleVal MathFunctions::LeastGreatest<DoubleVal, false>(
578 template StringVal MathFunctions::LeastGreatest<true>(
580 template StringVal MathFunctions::LeastGreatest<false>(
583 template TimestampVal MathFunctions::LeastGreatest<true>(
585 template TimestampVal MathFunctions::LeastGreatest<false>(
588 template DecimalVal MathFunctions::LeastGreatest<true>(
590 template DecimalVal MathFunctions::LeastGreatest<false>(
static BigIntVal Int_divide_BigIntVal_BigIntVal(FunctionContext *, const BigIntVal &, const BigIntVal &)
static DoubleVal RandSeed(FunctionContext *, const BigIntVal &seed)
static const int32_t MIN_BASE
static void RandPrepare(FunctionContext *, FunctionContext::FunctionStateScope)
Used for both Rand() and RandSeed()
const TypeDesc & GetReturnType() const
static StringVal FromBuffer(FunctionContext *ctx, const char *ptr, int len)
int128_t abs(const int128_t &x)
static ColumnType TypeDescToColumnType(const FunctionContext::TypeDesc &type)
static StringVal HexInt(FunctionContext *, const BigIntVal &)
static DoubleVal FmodDouble(FunctionContext *, const DoubleVal &, const DoubleVal &)
static StringVal ConvInt(FunctionContext *, const BigIntVal &n, const TinyIntVal &src_base, const TinyIntVal &dst_base)
static FloatVal FmodFloat(FunctionContext *, const FloatVal &, const FloatVal &)
static DoubleVal Log2(FunctionContext *, const DoubleVal &)
static const int32_t MAX_BASE
This object has a compatible storage format with boost::ptime.
static bool HandleParseResult(int8_t dest_base, int64_t *num, StringParser::ParseResult parse_res)
static StringVal HexString(FunctionContext *, const StringVal &)
void ToTimestampVal(impala_udf::TimestampVal *tv) const
static DoubleVal Pi(FunctionContext *)
static FloatVal Sign(FunctionContext *, const DoubleVal &)
static DoubleVal Degrees(FunctionContext *, const DoubleVal &)
static DoubleVal RoundUpTo(FunctionContext *, const DoubleVal &, const IntVal &)
const TypeDesc * GetArgType(int arg_idx) const
static BigIntVal QuotientBigInt(FunctionContext *, const BigIntVal &, const BigIntVal &)
ONE_ARG_MATH_FN(Abs, BigIntVal, BigIntVal, llabs)
static StringVal ConvString(FunctionContext *, const StringVal &s, const TinyIntVal &src_base, const TinyIntVal &dst_base)
void * GetFunctionState(FunctionStateScope scope) const
int GetByteSize() const
Returns the byte size of this type. Returns 0 for variable length types.
static StringVal FromString(FunctionContext *ctx, const std::string &s)
static BigIntVal Round(FunctionContext *, const DoubleVal &)
bool IsArgConstant(int arg_idx) const
void SetFunctionState(FunctionStateScope scope, void *ptr)
static VAL_TYPE LeastGreatest(FunctionContext *, int num_args, const VAL_TYPE *args)
static DoubleVal Rand(FunctionContext *)
static BigIntVal QuotientDouble(FunctionContext *, const DoubleVal &, const DoubleVal &)
static const char * ALPHANUMERIC_CHARS
static TimestampValue FromTimestampVal(const impala_udf::TimestampVal &udf_value)
static StringVal Bin(FunctionContext *, const BigIntVal &)
uint8_t * Allocate(int byte_size)
static DoubleVal Pow(FunctionContext *, const DoubleVal &base, const DoubleVal &val)
static T Negative(FunctionContext *, const T &)
static StringValue FromStringVal(const impala_udf::StringVal &sv)
static DoubleVal E(FunctionContext *)
static bool DecimalInBaseToDecimal(int64_t src_num, int8_t src_base, int64_t *result)
static DoubleVal PmodDouble(FunctionContext *, const DoubleVal &, const DoubleVal &)
void SetError(const char *error_msg)
void ToStringVal(impala_udf::StringVal *sv) const
static DoubleVal Log(FunctionContext *, const DoubleVal &base, const DoubleVal &val)
AnyVal * GetConstantArg(int arg_idx) const
static StringVal DecimalToBase(FunctionContext *, int64_t src_num, int8_t dest_base)
Converts src_num in decimal to dest_base.
static BigIntVal PmodBigInt(FunctionContext *, const BigIntVal &, const BigIntVal &)
static StringVal Unhex(FunctionContext *, const StringVal &)
static DoubleVal Radians(FunctionContext *, const DoubleVal &)
static T Positive(FunctionContext *, const T &)