From 7fcaca872a0bfe6b2807c9d7495a157464ba1990 Mon Sep 17 00:00:00 2001 From: Prudhvi Porandla Date: Tue, 16 Jul 2019 11:43:19 +0530 Subject: [PATCH 1/5] div for integer types --- cpp/src/gandiva/function_registry_arithmetic.cc | 4 ++++ cpp/src/gandiva/precompiled/arithmetic_ops.cc | 14 ++++++++++++++ cpp/src/gandiva/precompiled/arithmetic_ops_test.cc | 12 ++++++++++++ cpp/src/gandiva/precompiled/types.h | 2 ++ 4 files changed, 32 insertions(+) diff --git a/cpp/src/gandiva/function_registry_arithmetic.cc b/cpp/src/gandiva/function_registry_arithmetic.cc index 6a74a19e20c..74e528acecb 100644 --- a/cpp/src/gandiva/function_registry_arithmetic.cc +++ b/cpp/src/gandiva/function_registry_arithmetic.cc @@ -68,6 +68,10 @@ std::vector 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), diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops.cc b/cpp/src/gandiva/precompiled/arithmetic_ops.cc index 76fa32b5696..77343fc4b13 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops.cc @@ -180,4 +180,18 @@ 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(in1 / in2); \ + } + +DIV(int32) +DIV(int64) + } // extern "C" diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc index a7b6269b2fd..a6c6e6746dc 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc @@ -69,4 +69,16 @@ TEST(TestArithmeticOps, TestDivide) { EXPECT_EQ(context.has_error(), false); } +TEST(TestArithmeticOps, TestDiv) { + gandiva::ExecutionContext context; + EXPECT_EQ(div_int64_int64(reinterpret_cast(&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(&context), 101, 111), 0); + EXPECT_EQ(context.has_error(), false); + context.Reset(); +} + } // namespace gandiva diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h index 9dbf67e160c..33bc7fa2a02 100644 --- a/cpp/src/gandiva/precompiled/types.h +++ b/cpp/src/gandiva/precompiled/types.h @@ -125,6 +125,8 @@ 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); + float64 cbrt_int32(int32); float64 cbrt_int64(int64); float64 cbrt_float32(float32); From 014658da60fa17aedaec2d9d55e1d5fc373b1a23 Mon Sep 17 00:00:00 2001 From: Prudhvi Porandla Date: Tue, 16 Jul 2019 14:14:10 +0530 Subject: [PATCH 2/5] div for float, double types --- cpp/src/gandiva/precompiled/arithmetic_ops.cc | 14 ++++++++++++++ cpp/src/gandiva/precompiled/arithmetic_ops_test.cc | 10 ++++++++++ cpp/src/gandiva/precompiled/types.h | 1 + 3 files changed, 25 insertions(+) diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops.cc b/cpp/src/gandiva/precompiled/arithmetic_ops.cc index 77343fc4b13..21d3bf26671 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops.cc @@ -194,4 +194,18 @@ NUMERIC_FUNCTION(DIVIDE) 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 trunc(in1 / in2); \ + } + +DIV_FLOAT(float32) +DIV_FLOAT(float64) + } // extern "C" diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc index a6c6e6746dc..2ff0d6e4d0e 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc @@ -79,6 +79,16 @@ TEST(TestArithmeticOps, TestDiv) { EXPECT_EQ(div_int64_int64(reinterpret_cast(&context), 101, 111), 0); EXPECT_EQ(context.has_error(), false); context.Reset(); + + EXPECT_EQ(div_float64_float64(reinterpret_cast(&context), 1010.1010, 2.1), 481.0); + EXPECT_EQ(context.has_error(), false); + context.Reset(); + + EXPECT_EQ(div_float64_float64(reinterpret_cast(&context), 1010.1010, 0.00000), 0.0); + EXPECT_EQ(context.has_error(), true); + EXPECT_EQ(context.get_error(), "divide by zero error"); + context.Reset(); + } } // namespace gandiva diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h index 33bc7fa2a02..dce747a33aa 100644 --- a/cpp/src/gandiva/precompiled/types.h +++ b/cpp/src/gandiva/precompiled/types.h @@ -126,6 +126,7 @@ 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); + float64 div_float64_float64(int64 context, float64 in1, float64 in2); float64 cbrt_int32(int32); float64 cbrt_int64(int64); From d6a3d608a438da14bc868aef45c590b3f6086d32 Mon Sep 17 00:00:00 2001 From: Prudhvi Porandla Date: Tue, 16 Jul 2019 16:23:27 +0530 Subject: [PATCH 3/5] lint --- cpp/src/gandiva/precompiled/types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h index dce747a33aa..9db56c33e91 100644 --- a/cpp/src/gandiva/precompiled/types.h +++ b/cpp/src/gandiva/precompiled/types.h @@ -126,7 +126,7 @@ 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); - float64 div_float64_float64(int64 context, float64 in1, float64 in2); +float64 div_float64_float64(int64 context, float64 in1, float64 in2); float64 cbrt_int32(int32); float64 cbrt_int64(int64); From 7ac9090eaa269c355e68146cfaa29f98b9d2d6f2 Mon Sep 17 00:00:00 2001 From: Prudhvi Porandla Date: Tue, 16 Jul 2019 16:39:55 +0530 Subject: [PATCH 4/5] explicit cast after trunc --- cpp/src/gandiva/precompiled/arithmetic_ops.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops.cc b/cpp/src/gandiva/precompiled/arithmetic_ops.cc index 21d3bf26671..88e93729b19 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops.cc @@ -202,7 +202,7 @@ DIV(int64) gdv_fn_context_set_error_msg(context, err_msg); \ return 0; \ } \ - return trunc(in1 / in2); \ + return static_cast(trunc(in1 / in2)); \ } DIV_FLOAT(float32) From aa02da53ff509e72ac42c31e483a798d19221a04 Mon Sep 17 00:00:00 2001 From: Prudhvi Porandla Date: Wed, 17 Jul 2019 16:09:22 +0530 Subject: [PATCH 5/5] add test for float32; specify namespace of trunc --- cpp/src/gandiva/precompiled/arithmetic_ops.cc | 2 +- .../gandiva/precompiled/arithmetic_ops_test.cc | 16 +++++++++++----- cpp/src/gandiva/precompiled/types.h | 1 + 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops.cc b/cpp/src/gandiva/precompiled/arithmetic_ops.cc index 88e93729b19..9334e08ccf6 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops.cc @@ -202,7 +202,7 @@ DIV(int64) gdv_fn_context_set_error_msg(context, err_msg); \ return 0; \ } \ - return static_cast(trunc(in1 / in2)); \ + return static_cast(::trunc(in1 / in2)); \ } DIV_FLOAT(float32) diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc index 2ff0d6e4d0e..0375783fffb 100644 --- a/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc +++ b/cpp/src/gandiva/precompiled/arithmetic_ops_test.cc @@ -79,16 +79,22 @@ TEST(TestArithmeticOps, TestDiv) { EXPECT_EQ(div_int64_int64(reinterpret_cast(&context), 101, 111), 0); EXPECT_EQ(context.has_error(), false); context.Reset(); - - EXPECT_EQ(div_float64_float64(reinterpret_cast(&context), 1010.1010, 2.1), 481.0); + + EXPECT_EQ(div_float64_float64(reinterpret_cast(&context), 1010.1010, 2.1), + 481.0); EXPECT_EQ(context.has_error(), false); context.Reset(); - - EXPECT_EQ(div_float64_float64(reinterpret_cast(&context), 1010.1010, 0.00000), 0.0); + + EXPECT_EQ(div_float64_float64(reinterpret_cast(&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(&context), 1010.1010f, 2.1f), + 481.0f); + EXPECT_EQ(context.has_error(), false); + context.Reset(); } } // namespace gandiva diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h index 9db56c33e91..397cf99ddf5 100644 --- a/cpp/src/gandiva/precompiled/types.h +++ b/cpp/src/gandiva/precompiled/types.h @@ -126,6 +126,7 @@ 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);