anthonylouisbsb commented on a change in pull request #10137:
URL: https://github.com/apache/arrow/pull/10137#discussion_r756498235



##########
File path: cpp/src/gandiva/interval_holder_test.cc
##########
@@ -0,0 +1,423 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "gandiva/interval_holder.h"
+
+#include <gtest/gtest.h>
+
+#include <memory>
+#include <vector>
+
+#include "gandiva/execution_context.h"
+
+namespace gandiva {
+
+class TestIntervalHolder : public ::testing::Test {
+ protected:
+  ExecutionContext execution_context_;
+};
+
+TEST_F(TestIntervalHolder, TestMatchAllPeriods) {
+  std::shared_ptr<IntervalDaysHolder> interval_days_holder;
+  std::shared_ptr<IntervalYearsHolder> interval_years_holder;
+
+  auto status = IntervalDaysHolder::Make(0, &interval_days_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  status = IntervalYearsHolder::Make(0, &interval_years_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  auto& cast_interval_day = *interval_days_holder;
+  auto& cast_interval_year = *interval_years_holder;
+
+  // Pass only numbers to cast
+  bool out_valid;
+  std::string data("73834992");
+  int64_t response = cast_interval_day(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  int32_t response_interval_yrs =
+      cast_interval_year(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass only years and days to cast
+  data = "P12Y15D";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GT(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass years and days and months to cast
+  data = "P12Y2M15D";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GT(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass days and months to cast
+  data = "P5M13D";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GT(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass all possible fields cast
+  data = "P2Y5M13DT10H42M21S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GT(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass only time fields cast
+  data = "PT10H42M21S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GE(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass only time fields to cast without hours
+  data = "PT42M21S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GE(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  // Pass only weeks to cast
+  data = "P25W";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_GT(response, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_GE(response_interval_yrs, 0);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+
+  execution_context_.Reset();
+}
+
+TEST_F(TestIntervalHolder, TestMatchErrorsForCastIntervalDay) {
+  std::shared_ptr<IntervalDaysHolder> interval_days_holder;
+  std::shared_ptr<IntervalYearsHolder> interval_years_holder;
+
+  auto status = IntervalDaysHolder::Make(0, &interval_days_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  status = IntervalYearsHolder::Make(0, &interval_years_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  auto& cast_interval_day = *interval_days_holder;
+  auto& cast_interval_year = *interval_years_holder;
+
+  // Pass an empty string
+  bool out_valid;
+  std::string data(" ");
+  int64_t response = cast_interval_day(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  int32_t response_interval_yrs =
+      cast_interval_year(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Pass only days before years
+  data = "P15D12Y";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Pass years and days and months in wrong order
+  data = "P12M15D2Y";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Forget the P in the first position
+  data = "5M13D";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Use m instead M in the period format
+  data = "P2Y5M13DT10H42m21S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Does not pass the T when defining only time fields
+  data = "P10H42M21S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  // Pass weeks with other variables
+  data = "P2Y25W2M3D";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_EQ(response, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+
+  response_interval_yrs = cast_interval_year(&execution_context_, data, true, 
&out_valid);
+  EXPECT_EQ(response_interval_yrs, 0);
+  EXPECT_FALSE(out_valid);
+  EXPECT_TRUE(execution_context_.has_error());
+
+  execution_context_.Reset();
+}
+
+TEST_F(TestIntervalHolder, TestUsingWeekFormatterForCastIntervalDay) {
+  std::shared_ptr<IntervalDaysHolder> interval_holder;
+
+  auto status = IntervalDaysHolder::Make(0, &interval_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  auto& cast_interval_day = *interval_holder;
+
+  bool out_valid;
+  std::string data("P1W");
+  int64_t response = cast_interval_day(&execution_context_, data, true, 
&out_valid);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, 7);
+
+  data = "P10W";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, 70);
+
+  execution_context_.Reset();
+}
+
+TEST_F(TestIntervalHolder, TestUsingCompleteFormatterForCastIntervalDay) {
+  std::shared_ptr<IntervalDaysHolder> interval_holder;
+
+  auto status = IntervalDaysHolder::Make(0, &interval_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  auto& cast_interval_day = *interval_holder;
+
+  bool out_valid;
+  std::string data("1742461111");
+  int64_t response = cast_interval_day(&execution_context_, data, true, 
&out_valid);
+  int64_t qty_days_in_response = 20;
+  int64_t qty_millis_in_response = 14461111;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  data = "P1Y1M1DT1H1M1S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  qty_days_in_response = 1;
+  qty_millis_in_response = 3661000;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  data = "PT48H1M1S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  qty_days_in_response = 2;
+  qty_millis_in_response = 61000;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  data = "PT1S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  qty_days_in_response = 0;
+  qty_millis_in_response = 1000;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  data = "P10DT1S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  qty_days_in_response = 10;
+  qty_millis_in_response = 1000;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  execution_context_.Reset();
+
+  data = "P0DT0S";
+  response = cast_interval_day(&execution_context_, data, true, &out_valid);
+  qty_days_in_response = 0;
+  qty_millis_in_response = 0;
+  EXPECT_TRUE(out_valid);
+  EXPECT_FALSE(execution_context_.has_error());
+  EXPECT_EQ(response, (qty_millis_in_response << 32) | qty_days_in_response);
+
+  execution_context_.Reset();
+}
+
+TEST_F(TestIntervalHolder, TestUsingCompleteFormatterForCastIntervalYear) {
+  std::shared_ptr<IntervalYearsHolder> interval_years_holder;
+
+  auto status = IntervalYearsHolder::Make(0, &interval_years_holder);
+  EXPECT_EQ(status.ok(), true) << status.message();
+
+  auto& cast_interval_years = *interval_years_holder;
+
+  bool out_valid;
+  std::string data("65851111");

Review comment:
       The comments were wrong, the input is the number of months. I fixed the 
comment in the function.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to