Skip to content

Commit

Permalink
Merge pull request apache#91 from rui-mo/round
Browse files Browse the repository at this point in the history
[oap-native-sql] adding double round support
  • Loading branch information
zhouyuan authored Sep 3, 2020
2 parents 583cf4d + ba6fbb4 commit e863f88
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cpp/src/gandiva/function_registry_math_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ std::vector<NativeFunction> GetMathOpsFunctionRegistry() {
UNARY_SAFE_NULL_IF_NULL(abs, {}, float32, float32),
UNARY_SAFE_NULL_IF_NULL(abs, {}, float64, float64),

BINARY_GENERIC_SAFE_NULL_IF_NULL(round, {}, float64, int32, float64),
BINARY_GENERIC_SAFE_NULL_IF_NULL(round, {}, float64, int64, float64),

// decimal functions
UNARY_SAFE_NULL_IF_NULL(abs, {}, decimal128, decimal128),
UNARY_SAFE_NULL_IF_NULL(ceil, {}, decimal128, decimal128),
Expand Down
18 changes: 18 additions & 0 deletions cpp/src/gandiva/precompiled/extended_math_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C" {

#include <math.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "./types.h"
Expand Down Expand Up @@ -90,6 +91,23 @@ ABS_TYPES_UNARY(int64, uint64)
ABS_FTYPES_UNARY(float32, float32)
ABS_FTYPES_UNARY(float64, float64)

// round
#define ROUND_TYPES_UNARY(IN_TYPE1, IN_TYPE2, OUT_TYPE) \
FORCE_INLINE \
gdv_##OUT_TYPE round_##IN_TYPE1##_##IN_TYPE2(gdv_##IN_TYPE1 val, gdv_##IN_TYPE2 dp) { \
int charsNeeded = 1 + snprintf(NULL, 0, "%.*f", (int) dp, val); \
char* buffer = reinterpret_cast<char*>(malloc(charsNeeded)); \
snprintf(buffer, charsNeeded, "%.*f", (int) dp, nextafter(val, val*2)); \
double result = atof(buffer); \
free(buffer); \
return static_cast<gdv_##OUT_TYPE>(result); \
}

ROUND_TYPES_UNARY(float64, int32, float64)
ROUND_TYPES_UNARY(float64, int64, float64)

#undef ROUND_TYPES_UNARY

FORCE_INLINE
void set_error_for_logbase(int64_t execution_context, double base) {
char const* prefix = "divide by zero error with log of base";
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/gandiva/precompiled/extended_math_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ TEST(TestExtendedMathOps, TestTruncate) {
EXPECT_EQ(truncate_int64_int32(8124674407369523212, -2), 8124674407369523200);
}

TEST(TestExtendedMathOps, TestRound) {
EXPECT_EQ(round_float64_int32(1234.56789, 4), 1234.5679);
EXPECT_EQ(round_float64_int64(1234.56789, 4), 1234.5679);
EXPECT_EQ(round_float64_int32(-1234.56789, 4), -1234.5679);
EXPECT_EQ(round_float64_int64(-1234.56789, 4), -1234.5679);
}

} // namespace gandiva
3 changes: 3 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ gdv_uint64 abs_int64(gdv_int64);
gdv_float32 abs_float32(gdv_float32);
gdv_float64 abs_float64(gdv_float64);

gdv_float64 round_float64_int32(gdv_float64 in1, gdv_int32 in2);
gdv_float64 round_float64_int64(gdv_float64 in1, gdv_int64 in2);

gdv_int32 bitwise_and_int32_int32(gdv_int32 in1, gdv_int32 in2);
gdv_int64 bitwise_and_int64_int64(gdv_int64 in1, gdv_int64 in2);
gdv_int32 bitwise_or_int32_int32(gdv_int32 in1, gdv_int32 in2);
Expand Down

0 comments on commit e863f88

Please sign in to comment.