You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by li...@apache.org on 2022/05/12 20:04:03 UTC

[arrow] branch master updated: ARROW-16425: [C++] Add compute kernel test for scalar array timestamp comparison

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

lidavidm 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 ebfbb08d8e ARROW-16425: [C++] Add compute kernel test for scalar array timestamp comparison
ebfbb08d8e is described below

commit ebfbb08d8e62fc788caab2c3a9958f34cd17a26a
Author: Yaron Gvili <rt...@hotmail.com>
AuthorDate: Thu May 12 16:03:47 2022 -0400

    ARROW-16425: [C++] Add compute kernel test for scalar array timestamp comparison
    
    See https://issues.apache.org/jira/browse/ARROW-16425
    
    Closes #13037 from rtpsw/ARROW-16425
    
    Authored-by: Yaron Gvili <rt...@hotmail.com>
    Signed-off-by: David Li <li...@gmail.com>
---
 .../arrow/compute/kernels/scalar_compare_test.cc   | 59 ++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/cpp/src/arrow/compute/kernels/scalar_compare_test.cc b/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
index f0f2d7e367..682b141829 100644
--- a/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
+++ b/cpp/src/arrow/compute/kernels/scalar_compare_test.cc
@@ -502,6 +502,65 @@ TEST(TestCompareTimestamps, DifferentParameters) {
   }
 }
 
+TEST(TestCompareTimestamps, ScalarArray) {
+  std::string scalar_json = R"("1970-01-02")";
+  std::string array_json = R"(["1970-01-02","2000-02-01",null,"1900-02-28"])";
+
+  struct ArrayCase {
+    Datum side1, side2, expected;
+  };
+  auto CheckArrayCase = [&](std::shared_ptr<DataType> scalar_type,
+                            std::shared_ptr<DataType> array_type, CompareOperator op,
+                            const std::string& expected_json,
+                            const std::string& flip_expected_json) {
+    auto scalar_side = ScalarFromJSON(scalar_type, scalar_json);
+    auto array_side = ArrayFromJSON(array_type, array_json);
+    auto expected = ArrayFromJSON(boolean(), expected_json);
+    auto flip_expected = ArrayFromJSON(boolean(), flip_expected_json);
+    for (const auto& array_case :
+         std::vector<ArrayCase>{{scalar_side, array_side, expected},
+                                {array_side, scalar_side, flip_expected}}) {
+      const auto &lhs = array_case.side1, &rhs = array_case.side2;
+      if (scalar_type->Equals(array_type)) {
+        ASSERT_OK_AND_ASSIGN(Datum result,
+                             CallFunction(CompareOperatorToFunctionName(op), {lhs, rhs}));
+        AssertArraysEqual(*array_case.expected.make_array(), *result.make_array(),
+                          /*verbose=*/true);
+      } else {
+        EXPECT_RAISES_WITH_MESSAGE_THAT(
+            Invalid,
+            ::testing::HasSubstr(
+                "Cannot compare timestamp with timezone to timestamp without timezone"),
+            CallFunction(CompareOperatorToFunctionName(op), {lhs, rhs}));
+      }
+    }
+  };
+
+  for (const auto& unit : TimeUnit::values()) {
+    for (const auto& types :
+         std::vector<std::pair<std::shared_ptr<DataType>, std::shared_ptr<DataType>>>{
+             {timestamp(unit), timestamp(unit)},
+             {timestamp(unit), timestamp(unit, "utc")},
+             {timestamp(unit, "utc"), timestamp(unit)},
+             {timestamp(unit, "utc"), timestamp(unit, "utc")},
+         }) {
+      const auto &t0 = types.first, &t1 = types.second;
+      CheckArrayCase(t0, t1, CompareOperator::EQUAL, "[true, false, null, false]",
+                     "[true, false, null, false]");
+      CheckArrayCase(t0, t1, CompareOperator::NOT_EQUAL, "[false, true, null, true]",
+                     "[false, true, null, true]");
+      CheckArrayCase(t0, t1, CompareOperator::LESS, "[false, true, null, false]",
+                     "[false, false, null, true]");
+      CheckArrayCase(t0, t1, CompareOperator::LESS_EQUAL, "[true, true, null, false]",
+                     "[true, false, null, true]");
+      CheckArrayCase(t0, t1, CompareOperator::GREATER, "[false, false, null, true]",
+                     "[false, true, null, false]");
+      CheckArrayCase(t0, t1, CompareOperator::GREATER_EQUAL, "[true, false, null, true]",
+                     "[true, true, null, false]");
+    }
+  }
+}
+
 template <typename ArrowType>
 class TestCompareDecimal : public ::testing::Test {};
 TYPED_TEST_SUITE(TestCompareDecimal, DecimalArrowTypes);