]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file | |
3 | // distributed with this work for additional information | |
4 | // regarding copyright ownership. The ASF licenses this file | |
5 | // to you under the Apache License, Version 2.0 (the | |
6 | // "License"); you may not use this file except in compliance | |
7 | // with the License. You may obtain a copy of the License at | |
8 | // | |
9 | // http://www.apache.org/licenses/LICENSE-2.0 | |
10 | // | |
11 | // Unless required by applicable law or agreed to in writing, | |
12 | // software distributed under the License is distributed on an | |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | // KIND, either express or implied. See the License for the | |
15 | // specific language governing permissions and limitations | |
16 | // under the License. | |
17 | ||
18 | #include <memory> | |
19 | #include <vector> | |
20 | ||
21 | #include "arrow/testing/gtest_util.h" | |
22 | ||
23 | #include "gandiva/execution_context.h" | |
24 | #include "gandiva/to_date_holder.h" | |
25 | #include "precompiled/epoch_time_point.h" | |
26 | ||
27 | #include <gtest/gtest.h> | |
28 | ||
29 | namespace gandiva { | |
30 | ||
31 | class TestToDateHolder : public ::testing::Test { | |
32 | public: | |
33 | FunctionNode BuildToDate(std::string pattern) { | |
34 | auto field = std::make_shared<FieldNode>(arrow::field("in", arrow::utf8())); | |
35 | auto pattern_node = | |
36 | std::make_shared<LiteralNode>(arrow::utf8(), LiteralHolder(pattern), false); | |
37 | auto suppress_error_node = | |
38 | std::make_shared<LiteralNode>(arrow::int32(), LiteralHolder(0), false); | |
39 | return FunctionNode("to_date_utf8_utf8_int32", | |
40 | {field, pattern_node, suppress_error_node}, arrow::int64()); | |
41 | } | |
42 | ||
43 | protected: | |
44 | ExecutionContext execution_context_; | |
45 | }; | |
46 | ||
47 | TEST_F(TestToDateHolder, TestSimpleDateTime) { | |
48 | std::shared_ptr<ToDateHolder> to_date_holder; | |
49 | ASSERT_OK(ToDateHolder::Make("YYYY-MM-DD HH:MI:SS", 1, &to_date_holder)); | |
50 | ||
51 | auto& to_date = *to_date_holder; | |
52 | bool out_valid; | |
53 | std::string s("1986-12-01 01:01:01"); | |
54 | int64_t millis_since_epoch = | |
55 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
56 | EXPECT_EQ(millis_since_epoch, 533779200000); | |
57 | ||
58 | s = std::string("1986-12-01 01:01:01.11"); | |
59 | millis_since_epoch = | |
60 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
61 | EXPECT_EQ(millis_since_epoch, 533779200000); | |
62 | ||
63 | s = std::string("1986-12-01 01:01:01 +0800"); | |
64 | millis_since_epoch = | |
65 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
66 | EXPECT_EQ(millis_since_epoch, 533779200000); | |
67 | ||
68 | #if 0 | |
69 | // TODO : this fails parsing with date::parse and strptime on linux | |
70 | s = std::string("1886-12-01 00:00:00"); | |
71 | millis_since_epoch = | |
72 | to_date(&execution_context_, s.data(), (int) s.length(), true, &out_valid); | |
73 | EXPECT_EQ(out_valid, true); | |
74 | EXPECT_EQ(millis_since_epoch, -2621894400000); | |
75 | #endif | |
76 | ||
77 | s = std::string("1886-12-01 01:01:01"); | |
78 | millis_since_epoch = | |
79 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
80 | EXPECT_EQ(millis_since_epoch, -2621894400000); | |
81 | ||
82 | s = std::string("1986-12-11 01:30:00"); | |
83 | millis_since_epoch = | |
84 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
85 | EXPECT_EQ(millis_since_epoch, 534643200000); | |
86 | } | |
87 | ||
88 | TEST_F(TestToDateHolder, TestSimpleDate) { | |
89 | std::shared_ptr<ToDateHolder> to_date_holder; | |
90 | ASSERT_OK(ToDateHolder::Make("YYYY-MM-DD", 1, &to_date_holder)); | |
91 | ||
92 | auto& to_date = *to_date_holder; | |
93 | bool out_valid; | |
94 | std::string s("1986-12-01"); | |
95 | int64_t millis_since_epoch = | |
96 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
97 | EXPECT_EQ(millis_since_epoch, 533779200000); | |
98 | ||
99 | s = std::string("1986-12-01"); | |
100 | millis_since_epoch = | |
101 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
102 | EXPECT_EQ(millis_since_epoch, 533779200000); | |
103 | ||
104 | s = std::string("1886-12-1"); | |
105 | millis_since_epoch = | |
106 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
107 | EXPECT_EQ(millis_since_epoch, -2621894400000); | |
108 | ||
109 | s = std::string("2012-12-1"); | |
110 | millis_since_epoch = | |
111 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
112 | EXPECT_EQ(millis_since_epoch, 1354320000000); | |
113 | ||
114 | // wrong month. should return 0 since we are suppressing errors. | |
115 | s = std::string("1986-21-01 01:01:01 +0800"); | |
116 | millis_since_epoch = | |
117 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
118 | EXPECT_EQ(millis_since_epoch, 0); | |
119 | } | |
120 | ||
121 | TEST_F(TestToDateHolder, TestSimpleDateTimeError) { | |
122 | std::shared_ptr<ToDateHolder> to_date_holder; | |
123 | ||
124 | auto status = ToDateHolder::Make("YYYY-MM-DD HH:MI:SS", 0, &to_date_holder); | |
125 | EXPECT_EQ(status.ok(), true) << status.message(); | |
126 | auto& to_date = *to_date_holder; | |
127 | bool out_valid; | |
128 | ||
129 | std::string s("1986-01-40 01:01:01 +0800"); | |
130 | int64_t millis_since_epoch = | |
131 | to_date(&execution_context_, s.data(), (int)s.length(), true, &out_valid); | |
132 | EXPECT_EQ(0, millis_since_epoch); | |
133 | std::string expected_error = | |
134 | "Error parsing value 1986-01-40 01:01:01 +0800 for given format"; | |
135 | EXPECT_TRUE(execution_context_.get_error().find(expected_error) != std::string::npos) | |
136 | << status.message(); | |
137 | ||
138 | // not valid should not return error | |
139 | execution_context_.Reset(); | |
140 | millis_since_epoch = to_date(&execution_context_, "nullptr", 7, false, &out_valid); | |
141 | EXPECT_EQ(millis_since_epoch, 0); | |
142 | EXPECT_TRUE(execution_context_.has_error() == false); | |
143 | } | |
144 | ||
145 | TEST_F(TestToDateHolder, TestSimpleDateTimeMakeError) { | |
146 | std::shared_ptr<ToDateHolder> to_date_holder; | |
147 | // reject time stamps for now. | |
148 | auto status = ToDateHolder::Make("YYYY-MM-DD HH:MI:SS tzo", 0, &to_date_holder); | |
149 | EXPECT_EQ(status.IsInvalid(), true) << status.message(); | |
150 | } | |
151 | ||
152 | } // namespace gandiva |