Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cpp/src/gandiva/function_registry_arithmetic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ std::vector<NativeFunction> GetArithmeticFunctionRegistry() {
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(divide, {}, decimal128),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(mod, {"modulo"}, decimal128),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(mod, {"modulo"}, float64),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(div, {}, int32),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(div, {}, int64),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(div, {}, float32),
BINARY_SYMMETRIC_UNSAFE_NULL_IF_NULL(div, {}, float64),

// compare functions
BINARY_RELATIONAL_SAFE_NULL_IF_NULL(equal, {}, decimal128),
Expand Down
28 changes: 28 additions & 0 deletions cpp/src/gandiva/precompiled/arithmetic_ops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,32 @@ NUMERIC_BOOL_DATE_FUNCTION(IS_NOT_DISTINCT_FROM)

NUMERIC_FUNCTION(DIVIDE)

#define DIV(TYPE) \
FORCE_INLINE \
TYPE div_##TYPE##_##TYPE(int64 context, TYPE in1, TYPE in2) { \
if (in2 == 0) { \
char const* err_msg = "divide by zero error"; \
gdv_fn_context_set_error_msg(context, err_msg); \
return 0; \
} \
return static_cast<TYPE>(in1 / in2); \
}

DIV(int32)
DIV(int64)

#define DIV_FLOAT(TYPE) \
FORCE_INLINE \
TYPE div_##TYPE##_##TYPE(int64 context, TYPE in1, TYPE in2) { \
if (in2 == 0) { \
char const* err_msg = "divide by zero error"; \
gdv_fn_context_set_error_msg(context, err_msg); \
return 0; \
} \
return static_cast<TYPE>(::trunc(in1 / in2)); \
}

DIV_FLOAT(float32)
DIV_FLOAT(float64)

} // extern "C"
28 changes: 28 additions & 0 deletions cpp/src/gandiva/precompiled/arithmetic_ops_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,32 @@ TEST(TestArithmeticOps, TestDivide) {
EXPECT_EQ(context.has_error(), false);
}

TEST(TestArithmeticOps, TestDiv) {
gandiva::ExecutionContext context;
EXPECT_EQ(div_int64_int64(reinterpret_cast<int64>(&context), 101, 0), 0);
EXPECT_EQ(context.has_error(), true);
EXPECT_EQ(context.get_error(), "divide by zero error");
context.Reset();

EXPECT_EQ(div_int64_int64(reinterpret_cast<int64>(&context), 101, 111), 0);
EXPECT_EQ(context.has_error(), false);
context.Reset();

EXPECT_EQ(div_float64_float64(reinterpret_cast<int64>(&context), 1010.1010, 2.1),
481.0);
EXPECT_EQ(context.has_error(), false);
context.Reset();

EXPECT_EQ(div_float64_float64(reinterpret_cast<int64>(&context), 1010.1010, 0.00000),
0.0);
EXPECT_EQ(context.has_error(), true);
EXPECT_EQ(context.get_error(), "divide by zero error");
context.Reset();

EXPECT_EQ(div_float32_float32(reinterpret_cast<int64>(&context), 1010.1010f, 2.1f),
481.0f);
EXPECT_EQ(context.has_error(), false);
context.Reset();
}

} // namespace gandiva
4 changes: 4 additions & 0 deletions cpp/src/gandiva/precompiled/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ float64 mod_float64_float64(int64 context, float64 left, float64 right);

int64 divide_int64_int64(int64 context, int64 in1, int64 in2);

int64 div_int64_int64(int64 context, int64 in1, int64 in2);
float32 div_float32_float32(int64 context, float32 in1, float32 in2);
float64 div_float64_float64(int64 context, float64 in1, float64 in2);

float64 cbrt_int32(int32);
float64 cbrt_int64(int64);
float64 cbrt_float32(float32);
Expand Down