Optimize Hex and add related Doc (#6697)
I tested hex in a 1000w times for loop with random numbers, old hex avg time cost is 4.92 s,optimize hex avg time cost is 0.46 s which faster nearly 10x.
This commit is contained in:
@ -323,10 +323,25 @@ StringVal MathFunctions::hex_int(FunctionContext* ctx, const BigIntVal& v) {
|
||||
if (v.is_null) {
|
||||
return StringVal::null();
|
||||
}
|
||||
// TODO: this is probably unreasonably slow
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::uppercase << v.val;
|
||||
return AnyValUtil::from_string_temp(ctx, ss.str());
|
||||
|
||||
uint64_t num = v.val;
|
||||
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
// uint64_t max value 0xFFFFFFFFFFFFFFFF , 16 'F'
|
||||
// need 1 more space for '\0'
|
||||
char ans[17];
|
||||
int i = 0;
|
||||
while (num) {
|
||||
ans[i++] = hex[num & 15];
|
||||
num = num >> 4;
|
||||
}
|
||||
ans[i] = '\0';
|
||||
// reverse
|
||||
for (int k = 0, j = i - 1; k <= j; k++, j--) {
|
||||
char tmp = ans[j];
|
||||
ans[j] = ans[k];
|
||||
ans[k] = tmp;
|
||||
}
|
||||
return AnyValUtil::from_string_temp(ctx, ans);
|
||||
}
|
||||
|
||||
StringVal MathFunctions::hex_string(FunctionContext* ctx, const StringVal& s) {
|
||||
@ -347,8 +362,8 @@ StringVal MathFunctions::unhex(FunctionContext* ctx, const StringVal& s) {
|
||||
if (s.is_null) {
|
||||
return StringVal::null();
|
||||
}
|
||||
// For uneven number of chars return empty string like Hive does.
|
||||
if (s.len % 2 != 0) {
|
||||
// For odd number of chars return empty string like Hive does.
|
||||
if (s.len & 1) {
|
||||
return StringVal();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user