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 e3ae1b0  ARROW-5824: [Gandiva][C++] Fix decimal null literals.
e3ae1b0 is described below

commit e3ae1b0883909dbc17fb7d17d9addf2cf4f03b35
Author: Praveen <prav...@dremio.com>
AuthorDate: Thu Jul 4 07:21:16 2019 +0530

    ARROW-5824: [Gandiva][C++] Fix decimal null literals.
    
    - Type was hard coded as 0,0
    - Use the input type for literals instead.
    
    Author: Praveen <prav...@dremio.com>
    
    Closes #4780 from praveenbingo/ARROW-5824 and squashes the following 
commits:
    
    2fa0eebcc <Praveen> Address review comments.
    d56918c29 <Praveen> Fix lint issues.
    41b1b0f69 <Praveen> ARROW-5824:  Fix decimal null literals.
---
 cpp/src/gandiva/tests/decimal_test.cc | 56 +++++++++++++++++++++++++++++++++++
 cpp/src/gandiva/tree_expr_builder.cc  |  4 ++-
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/cpp/src/gandiva/tests/decimal_test.cc 
b/cpp/src/gandiva/tests/decimal_test.cc
index 9941fea..9bb08e1 100644
--- a/cpp/src/gandiva/tests/decimal_test.cc
+++ b/cpp/src/gandiva/tests/decimal_test.cc
@@ -787,4 +787,60 @@ TEST_F(TestDecimal, TestHash64WithSeed) {
   // hash with, without seed are not equal
   EXPECT_NE(int64_arr_WS->Value(4), int64_arr->Value(4));
 }
+
+TEST_F(TestDecimal, TestNullDecimalConstant) {
+  // schema for input fields
+  constexpr int32_t precision = 36;
+  constexpr int32_t scale = 18;
+  auto decimal_type = std::make_shared<arrow::Decimal128Type>(precision, 
scale);
+  auto field_b = field("b", decimal_type);
+  auto field_c = field("c", arrow::boolean());
+  auto schema = arrow::schema({field_b, field_c});
+
+  // output fields
+  auto field_result = field("res", decimal_type);
+
+  // build expression.
+  // if (c)
+  //   null
+  // else
+  //   b
+  auto node_a = TreeExprBuilder::MakeNull(decimal_type);
+  auto node_b = TreeExprBuilder::MakeField(field_b);
+  auto node_c = TreeExprBuilder::MakeField(field_c);
+  auto if_node = TreeExprBuilder::MakeIf(node_c, node_a, node_b, decimal_type);
+
+  auto expr = TreeExprBuilder::MakeExpression(if_node, field_result);
+
+  // Build a projector for the expressions.
+  std::shared_ptr<Projector> projector;
+  Status status = Projector::Make(schema, {expr}, TestConfiguration(), 
&projector);
+  DCHECK_OK(status);
+
+  // Create a row-batch with some sample data
+  int num_records = 4;
+
+  auto array_b =
+      MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"2", "3", "4", 
"5"}, scale),
+                            {true, true, true, true});
+
+  auto array_c = MakeArrowArrayBool({true, false, true, false}, {true, true, 
true, true});
+
+  // expected output
+  auto exp =
+      MakeArrowArrayDecimal(decimal_type, MakeDecimalVector({"0", "3", "3", 
"5"}, scale),
+                            {false, true, false, true});
+
+  // prepare input record batch
+  auto in_batch = arrow::RecordBatch::Make(schema, num_records, {array_b, 
array_c});
+
+  // Evaluate expression
+  arrow::ArrayVector outputs;
+  status = projector->Evaluate(*in_batch, pool_, &outputs);
+  DCHECK_OK(status);
+
+  // Validate results
+  EXPECT_ARROW_ARRAY_EQUALS(exp, outputs.at(0));
+}
+
 }  // namespace gandiva
diff --git a/cpp/src/gandiva/tree_expr_builder.cc 
b/cpp/src/gandiva/tree_expr_builder.cc
index a63b700..51c640c 100644
--- a/cpp/src/gandiva/tree_expr_builder.cc
+++ b/cpp/src/gandiva/tree_expr_builder.cc
@@ -99,7 +99,9 @@ NodePtr TreeExprBuilder::MakeNull(DataTypePtr data_type) {
     case arrow::Type::TIMESTAMP:
       return std::make_shared<LiteralNode>(data_type, 
LiteralHolder((int64_t)0), true);
     case arrow::Type::DECIMAL: {
-      DecimalScalar128 literal(0, 0);
+      std::shared_ptr<arrow::DecimalType> decimal_type =
+          arrow::internal::checked_pointer_cast<arrow::DecimalType>(data_type);
+      DecimalScalar128 literal(decimal_type->precision(), 
decimal_type->scale());
       return std::make_shared<LiteralNode>(data_type, LiteralHolder(literal), 
true);
     }
     default:

Reply via email to