]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/r/tests/testthat/test-dplyr-funcs-datetime.R
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / tests / testthat / test-dplyr-funcs-datetime.R
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 skip_if_not_available("dataset")
19
20 library(lubridate, warn.conflicts = FALSE)
21 library(dplyr, warn.conflicts = FALSE)
22
23 # base::strptime() defaults to local timezone
24 # but arrow's strptime defaults to UTC.
25 # So that tests are consistent, set the local timezone to UTC
26 # TODO: consider reevaluating this workaround after ARROW-12980
27 withr::local_timezone("UTC")
28
29 # TODO: We should test on windows once ARROW-13168 is resolved.
30 if (tolower(Sys.info()[["sysname"]]) == "windows") {
31 test_date <- as.POSIXct("2017-01-01 00:00:11.3456789", tz = "")
32 } else {
33 test_date <- as.POSIXct("2017-01-01 00:00:11.3456789", tz = "Pacific/Marquesas")
34 }
35
36
37 test_df <- tibble::tibble(
38 # test_date + 1 turns the tzone = "" to NULL, which is functionally equivalent
39 # so we can run some tests on Windows, but this skirts around
40 # https://issues.apache.org/jira/browse/ARROW-13588
41 # That issue is tough because in C++, "" is the "no timezone" value
42 # due to static typing, so we can't distinguish a literal "" from NULL
43 datetime = c(test_date, NA) + 1,
44 date = c(as.Date("2021-09-09"), NA)
45 )
46
47 # These tests test component extraction from timestamp objects
48
49 test_that("extract year from timestamp", {
50 compare_dplyr_binding(
51 .input %>%
52 mutate(x = year(datetime)) %>%
53 collect(),
54 test_df
55 )
56 })
57
58 test_that("extract isoyear from timestamp", {
59 compare_dplyr_binding(
60 .input %>%
61 mutate(x = isoyear(datetime)) %>%
62 collect(),
63 test_df
64 )
65 })
66
67 test_that("extract quarter from timestamp", {
68 compare_dplyr_binding(
69 .input %>%
70 mutate(x = quarter(datetime)) %>%
71 collect(),
72 test_df
73 )
74 })
75
76 test_that("extract month from timestamp", {
77 compare_dplyr_binding(
78 .input %>%
79 mutate(x = month(datetime)) %>%
80 collect(),
81 test_df
82 )
83 })
84
85 test_that("extract isoweek from timestamp", {
86 compare_dplyr_binding(
87 .input %>%
88 mutate(x = isoweek(datetime)) %>%
89 collect(),
90 test_df
91 )
92 })
93
94 test_that("extract epiweek from timestamp", {
95 compare_dplyr_binding(
96 .input %>%
97 mutate(x = epiweek(datetime)) %>%
98 collect(),
99 test_df
100 )
101 })
102
103 test_that("extract day from timestamp", {
104 compare_dplyr_binding(
105 .input %>%
106 mutate(x = day(datetime)) %>%
107 collect(),
108 test_df
109 )
110 })
111
112 test_that("extract wday from timestamp", {
113 compare_dplyr_binding(
114 .input %>%
115 mutate(x = wday(datetime)) %>%
116 collect(),
117 test_df
118 )
119
120 compare_dplyr_binding(
121 .input %>%
122 mutate(x = wday(date, week_start = 3)) %>%
123 collect(),
124 test_df
125 )
126
127 compare_dplyr_binding(
128 .input %>%
129 mutate(x = wday(date, week_start = 1)) %>%
130 collect(),
131 test_df
132 )
133
134 skip_on_os("windows") # https://issues.apache.org/jira/browse/ARROW-13168
135
136 compare_dplyr_binding(
137 .input %>%
138 mutate(x = wday(date, label = TRUE)) %>%
139 mutate(x = as.character(x)) %>%
140 collect(),
141 test_df
142 )
143
144 compare_dplyr_binding(
145 .input %>%
146 mutate(x = wday(datetime, label = TRUE, abbr = TRUE)) %>%
147 mutate(x = as.character(x)) %>%
148 collect(),
149 test_df
150 )
151 })
152
153 test_that("extract yday from timestamp", {
154 compare_dplyr_binding(
155 .input %>%
156 mutate(x = yday(datetime)) %>%
157 collect(),
158 test_df
159 )
160 })
161
162 test_that("extract hour from timestamp", {
163 compare_dplyr_binding(
164 .input %>%
165 mutate(x = hour(datetime)) %>%
166 collect(),
167 test_df
168 )
169 })
170
171 test_that("extract minute from timestamp", {
172 compare_dplyr_binding(
173 .input %>%
174 mutate(x = minute(datetime)) %>%
175 collect(),
176 test_df
177 )
178 })
179
180 test_that("extract second from timestamp", {
181 compare_dplyr_binding(
182 .input %>%
183 mutate(x = second(datetime)) %>%
184 collect(),
185 test_df,
186 # arrow supports nanosecond resolution but lubridate does not
187 tolerance = 1e-6
188 )
189 })
190
191 # These tests test extraction of components from date32 objects
192
193 test_that("extract year from date", {
194 compare_dplyr_binding(
195 .input %>%
196 mutate(x = year(date)) %>%
197 collect(),
198 test_df
199 )
200 })
201
202 test_that("extract isoyear from date", {
203 compare_dplyr_binding(
204 .input %>%
205 mutate(x = isoyear(date)) %>%
206 collect(),
207 test_df
208 )
209 })
210
211 test_that("extract quarter from date", {
212 compare_dplyr_binding(
213 .input %>%
214 mutate(x = quarter(date)) %>%
215 collect(),
216 test_df
217 )
218 })
219
220 test_that("extract month from date", {
221 compare_dplyr_binding(
222 .input %>%
223 mutate(x = month(date)) %>%
224 collect(),
225 test_df
226 )
227 })
228
229 test_that("extract isoweek from date", {
230 compare_dplyr_binding(
231 .input %>%
232 mutate(x = isoweek(date)) %>%
233 collect(),
234 test_df
235 )
236 })
237
238 test_that("extract epiweek from date", {
239 compare_dplyr_binding(
240 .input %>%
241 mutate(x = epiweek(date)) %>%
242 collect(),
243 test_df
244 )
245 })
246
247 test_that("extract day from date", {
248 compare_dplyr_binding(
249 .input %>%
250 mutate(x = day(date)) %>%
251 collect(),
252 test_df
253 )
254 })
255
256 test_that("extract wday from date", {
257 compare_dplyr_binding(
258 .input %>%
259 mutate(x = wday(date)) %>%
260 collect(),
261 test_df
262 )
263
264 compare_dplyr_binding(
265 .input %>%
266 mutate(x = wday(date, week_start = 3)) %>%
267 collect(),
268 test_df
269 )
270
271 compare_dplyr_binding(
272 .input %>%
273 mutate(x = wday(date, week_start = 1)) %>%
274 collect(),
275 test_df
276 )
277
278 skip_on_os("windows") # https://issues.apache.org/jira/browse/ARROW-13168
279
280 compare_dplyr_binding(
281 .input %>%
282 mutate(x = wday(date, label = TRUE, abbr = TRUE)) %>%
283 mutate(x = as.character(x)) %>%
284 collect(),
285 test_df
286 )
287
288 compare_dplyr_binding(
289 .input %>%
290 mutate(x = wday(date, label = TRUE)) %>%
291 mutate(x = as.character(x)) %>%
292 collect(),
293 test_df
294 )
295 })
296
297 test_that("extract yday from date", {
298 compare_dplyr_binding(
299 .input %>%
300 mutate(x = yday(date)) %>%
301 collect(),
302 test_df
303 )
304 })