You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ra...@apache.org on 2019/07/31 10:25:32 UTC

[arrow] branch master updated: ARROW-6023: [C++][Gandiva] Add functions in Gandiva

This is an automated email from the ASF dual-hosted git repository.

ravindra pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new cac4957  ARROW-6023: [C++][Gandiva] Add functions in Gandiva
cac4957 is described below

commit cac4957759f238bd8e3b1b905e06477ea94444dd
Author: Prudhvi Porandla <pr...@icloud.com>
AuthorDate: Wed Jul 31 15:55:11 2019 +0530

    ARROW-6023: [C++][Gandiva] Add functions in Gandiva
    
    int32 castINT(int64) : cast int64 to int32
    float4 castFLOAT4(float8) : cast float8 to float4
    int64 truncate(int64, int32 scale) : if scale is negative, make last -scale digits zero
    timestamp add(date, int32 days) : add days to date(in milliseconds) and return   timestamp
    
    Closes #4932 from pprudhvi/castlongtoint and squashes the following commits:
    
    a26624826 <Prudhvi Porandla> lint
    ac4e07eef <Prudhvi Porandla> add truncate_int64_int32
    c4d0136ae <Prudhvi Porandla> add add_date64_int64 method
    2d97c8aac <Prudhvi Porandla> add functions to cast int64 to int32 and float64 to flaot32
    
    Authored-by: Prudhvi Porandla <pr...@icloud.com>
    Signed-off-by: Pindikura Ravindra <ra...@dremio.com>
---
 cpp/src/gandiva/function_registry_arithmetic.cc           |  2 ++
 cpp/src/gandiva/function_registry_math_ops.cc             |  3 +++
 cpp/src/gandiva/function_registry_timestamp_arithmetic.cc |  3 +++
 cpp/src/gandiva/precompiled/arithmetic_ops.cc             |  4 +++-
 cpp/src/gandiva/precompiled/extended_math_ops.cc          | 15 +++++++++++++++
 cpp/src/gandiva/precompiled/extended_math_ops_test.cc     |  9 +++++++++
 cpp/src/gandiva/precompiled/time_test.cc                  |  3 +++
 cpp/src/gandiva/precompiled/types.h                       |  3 +++
 8 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/cpp/src/gandiva/function_registry_arithmetic.cc b/cpp/src/gandiva/function_registry_arithmetic.cc
index 859d53a..7f11d9e 100644
--- a/cpp/src/gandiva/function_registry_arithmetic.cc
+++ b/cpp/src/gandiva/function_registry_arithmetic.cc
@@ -37,10 +37,12 @@ std::vector<NativeFunction> GetArithmeticFunctionRegistry() {
   static std::vector<NativeFunction> arithmetic_fn_registry_ = {
       UNARY_SAFE_NULL_IF_NULL(not, {}, boolean, boolean),
       UNARY_SAFE_NULL_IF_NULL(castBIGINT, {}, int32, int64),
+      UNARY_SAFE_NULL_IF_NULL(castINT, {}, int64, int32),
       UNARY_SAFE_NULL_IF_NULL(castBIGINT, {}, decimal128, int64),
 
       // cast to float32
       UNARY_CAST_TO_FLOAT32(int32), UNARY_CAST_TO_FLOAT32(int64),
+      UNARY_CAST_TO_FLOAT32(float64),
 
       // cast to float64
       UNARY_CAST_TO_FLOAT64(int32), UNARY_CAST_TO_FLOAT64(int64),
diff --git a/cpp/src/gandiva/function_registry_math_ops.cc b/cpp/src/gandiva/function_registry_math_ops.cc
index 046556b..5f77688 100644
--- a/cpp/src/gandiva/function_registry_math_ops.cc
+++ b/cpp/src/gandiva/function_registry_math_ops.cc
@@ -70,6 +70,9 @@ std::vector<NativeFunction> GetMathOpsFunctionRegistry() {
       BINARY_GENERIC_SAFE_NULL_IF_NULL(round, {}, decimal128, int32, decimal128),
       BINARY_GENERIC_SAFE_NULL_IF_NULL(truncate, {"trunc"}, decimal128, int32,
                                        decimal128),
+
+      NativeFunction("truncate", {"trunc"}, DataTypeVector{int64(), int32()}, int64(),
+                     kResultNullIfNull, "truncate_int64_int32"),
   };
 
   return math_fn_registry_;
diff --git a/cpp/src/gandiva/function_registry_timestamp_arithmetic.cc b/cpp/src/gandiva/function_registry_timestamp_arithmetic.cc
index b9d41ea..d77fd20 100644
--- a/cpp/src/gandiva/function_registry_timestamp_arithmetic.cc
+++ b/cpp/src/gandiva/function_registry_timestamp_arithmetic.cc
@@ -71,6 +71,9 @@ std::vector<NativeFunction> GetDateTimeArithmeticFunctionRegistry() {
       DATE_ADD_FNS(date_add, {}),
       DATE_ADD_FNS(add, {}),
 
+      NativeFunction("add", {}, DataTypeVector{date64(), int64()}, timestamp(),
+                     kResultNullIfNull, "add_date64_int64"),
+
       DATE_DIFF_FNS(date_sub, {}),
       DATE_DIFF_FNS(subtract, {}),
       DATE_DIFF_FNS(date_diff, {})};
diff --git a/cpp/src/gandiva/precompiled/arithmetic_ops.cc b/cpp/src/gandiva/precompiled/arithmetic_ops.cc
index 9334e08..7d1b48c 100644
--- a/cpp/src/gandiva/precompiled/arithmetic_ops.cc
+++ b/cpp/src/gandiva/precompiled/arithmetic_ops.cc
@@ -93,14 +93,16 @@ NUMERIC_DATE_TYPES(BINARY_RELATIONAL, greater_than_or_equal_to, >=)
 // cast fns : takes one param type, returns another type.
 #define CAST_UNARY(NAME, IN_TYPE, OUT_TYPE) \
   FORCE_INLINE                              \
-  OUT_TYPE NAME##_##IN_TYPE(IN_TYPE in) { return (OUT_TYPE)in; }
+  OUT_TYPE NAME##_##IN_TYPE(IN_TYPE in) { return static_cast<OUT_TYPE>(in); }
 
 CAST_UNARY(castBIGINT, int32, int64)
+CAST_UNARY(castINT, int64, int32)
 CAST_UNARY(castFLOAT4, int32, float32)
 CAST_UNARY(castFLOAT4, int64, float32)
 CAST_UNARY(castFLOAT8, int32, float64)
 CAST_UNARY(castFLOAT8, int64, float64)
 CAST_UNARY(castFLOAT8, float32, float64)
+CAST_UNARY(castFLOAT4, float64, float32)
 
 // simple nullable functions, result value = fn(input validity)
 #define VALIDITY_OP(NAME, TYPE, OP) \
diff --git a/cpp/src/gandiva/precompiled/extended_math_ops.cc b/cpp/src/gandiva/precompiled/extended_math_ops.cc
index b17ccd8..8ae50c7 100644
--- a/cpp/src/gandiva/precompiled/extended_math_ops.cc
+++ b/cpp/src/gandiva/precompiled/extended_math_ops.cc
@@ -15,6 +15,8 @@
 // specific language governing permissions and limitations
 // under the License.
 
+#include "gandiva/precompiled/decimal_ops.h"
+
 extern "C" {
 
 #include <math.h>
@@ -108,4 +110,17 @@ LOG_WITH_BASE(float64, float64, float64)
 
 POWER(float64, float64, float64)
 
+FORCE_INLINE
+int64 truncate_int64_int32(int64 in, int32 out_scale) {
+  bool overflow = false;
+  arrow::BasicDecimal128 decimal = gandiva::decimalops::FromInt64(in, 38, 0, &overflow);
+  arrow::BasicDecimal128 decimal_with_outscale = gandiva::decimalops::Truncate(
+      gandiva::BasicDecimalScalar128(decimal, 38, 0), out_scale, &overflow);
+  if (out_scale < 0) {
+    out_scale = 0;
+  }
+  return gandiva::decimalops::ToInt64(
+      gandiva::BasicDecimalScalar128(decimal_with_outscale, 38, out_scale), &overflow);
+}
+
 }  // extern "C"
diff --git a/cpp/src/gandiva/precompiled/extended_math_ops_test.cc b/cpp/src/gandiva/precompiled/extended_math_ops_test.cc
index 4f91260..b5208da 100644
--- a/cpp/src/gandiva/precompiled/extended_math_ops_test.cc
+++ b/cpp/src/gandiva/precompiled/extended_math_ops_test.cc
@@ -87,4 +87,13 @@ TEST(TestExtendedMathOps, TestLogWithBase) {
   EXPECT_EQ(context1.has_error(), false);
 }
 
+TEST(TestExtendedMathOps, TestTruncate) {
+  EXPECT_EQ(truncate_int64_int32(1234, 4), 1234);
+  EXPECT_EQ(truncate_int64_int32(-1234, 4), -1234);
+  EXPECT_EQ(truncate_int64_int32(1234, -4), 0);
+  EXPECT_EQ(truncate_int64_int32(-1234, -2), -1200);
+  EXPECT_EQ(truncate_int64_int32(8124674407369523212, 0), 8124674407369523212);
+  EXPECT_EQ(truncate_int64_int32(8124674407369523212, -2), 8124674407369523200);
+}
+
 }  // namespace gandiva
diff --git a/cpp/src/gandiva/precompiled/time_test.cc b/cpp/src/gandiva/precompiled/time_test.cc
index c9f0f68..f30198e 100644
--- a/cpp/src/gandiva/precompiled/time_test.cc
+++ b/cpp/src/gandiva/precompiled/time_test.cc
@@ -274,6 +274,9 @@ TEST(TestTime, TimeStampAdd) {
   EXPECT_EQ(date_add_int64_timestamp(4, StringToTimestamp("2000-02-27 00:00:00")),
             StringToTimestamp("2000-03-02 00:00:00"));
 
+  EXPECT_EQ(add_date64_int64(StringToTimestamp("2000-02-27 00:00:00"), 4),
+            StringToTimestamp("2000-03-02 00:00:00"));
+
   // date_sub
   EXPECT_EQ(date_sub_int32_timestamp(7, StringToTimestamp("2000-05-01 00:00:00")),
             StringToTimestamp("2000-04-24 00:00:00"));
diff --git a/cpp/src/gandiva/precompiled/types.h b/cpp/src/gandiva/precompiled/types.h
index 7d6a9c4..6931efa 100644
--- a/cpp/src/gandiva/precompiled/types.h
+++ b/cpp/src/gandiva/precompiled/types.h
@@ -96,6 +96,7 @@ int64 date_add_int32_timestamp(int32, timestamp);
 int64 add_int64_timestamp(int64, timestamp);
 int64 add_int32_timestamp(int32, timestamp);
 int64 date_add_int64_timestamp(int64, timestamp);
+timestamp add_date64_int64(date64, int64);
 
 int64 date_sub_int32_timestamp(int32, timestamp);
 int64 subtract_int32_timestamp(int32, timestamp);
@@ -165,6 +166,8 @@ date64 castDATE_utf8(int64_t execution_context, const char* input, int32 length)
 timestamp castTIMESTAMP_utf8(int64_t execution_context, const char* input, int32 length);
 timestamp castTIMESTAMP_date64(date64);
 
+int64 truncate_int64_int32(int64 in, int32 out_scale);
+
 }  // extern "C"
 
 #endif  // PRECOMPILED_TYPES_H