]>
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 | test_that("null type works as expected", { | |
19 | x <- null() | |
20 | expect_equal(x$id, 0L) | |
21 | expect_equal(x$name, "null") | |
22 | expect_equal(x$ToString(), "null") | |
23 | expect_true(x == x) | |
24 | expect_false(x == int8()) | |
25 | expect_equal(x$num_fields, 0L) | |
26 | expect_equal(x$fields(), list()) | |
27 | }) | |
28 | ||
29 | test_that("boolean type work as expected", { | |
30 | x <- boolean() | |
31 | expect_equal(x$id, Type$BOOL) | |
32 | expect_equal(x$name, "bool") | |
33 | expect_equal(x$ToString(), "bool") | |
34 | expect_true(x == x) | |
35 | expect_false(x == null()) | |
36 | expect_equal(x$num_fields, 0L) | |
37 | expect_equal(x$fields(), list()) | |
38 | expect_equal(x$bit_width, 1L) | |
39 | }) | |
40 | ||
41 | test_that("int types works as expected", { | |
42 | x <- uint8() | |
43 | expect_equal(x$id, Type$UINT8) | |
44 | expect_equal(x$name, "uint8") | |
45 | expect_equal(x$ToString(), "uint8") | |
46 | expect_true(x == x) | |
47 | expect_false(x == null()) | |
48 | expect_equal(x$num_fields, 0L) | |
49 | expect_equal(x$fields(), list()) | |
50 | expect_equal(x$bit_width, 8L) | |
51 | ||
52 | x <- int8() | |
53 | expect_equal(x$id, Type$INT8) | |
54 | expect_equal(x$name, "int8") | |
55 | expect_equal(x$ToString(), "int8") | |
56 | expect_true(x == x) | |
57 | expect_false(x == null()) | |
58 | expect_equal(x$num_fields, 0L) | |
59 | expect_equal(x$fields(), list()) | |
60 | expect_equal(x$bit_width, 8L) | |
61 | ||
62 | x <- uint16() | |
63 | expect_equal(x$id, Type$UINT16) | |
64 | expect_equal(x$name, "uint16") | |
65 | expect_equal(x$ToString(), "uint16") | |
66 | expect_true(x == x) | |
67 | expect_false(x == null()) | |
68 | expect_equal(x$num_fields, 0L) | |
69 | expect_equal(x$fields(), list()) | |
70 | expect_equal(x$bit_width, 16L) | |
71 | ||
72 | x <- int16() | |
73 | expect_equal(x$id, Type$INT16) | |
74 | expect_equal(x$name, "int16") | |
75 | expect_equal(x$ToString(), "int16") | |
76 | expect_true(x == x) | |
77 | expect_false(x == null()) | |
78 | expect_equal(x$num_fields, 0L) | |
79 | expect_equal(x$fields(), list()) | |
80 | expect_equal(x$bit_width, 16L) | |
81 | ||
82 | x <- uint32() | |
83 | expect_equal(x$id, Type$UINT32) | |
84 | expect_equal(x$name, "uint32") | |
85 | expect_equal(x$ToString(), "uint32") | |
86 | expect_true(x == x) | |
87 | expect_false(x == null()) | |
88 | expect_equal(x$num_fields, 0L) | |
89 | expect_equal(x$fields(), list()) | |
90 | expect_equal(x$bit_width, 32L) | |
91 | ||
92 | x <- int32() | |
93 | expect_equal(x$id, Type$INT32) | |
94 | expect_equal(x$name, "int32") | |
95 | expect_equal(x$ToString(), "int32") | |
96 | expect_true(x == x) | |
97 | expect_false(x == null()) | |
98 | expect_equal(x$num_fields, 0L) | |
99 | expect_equal(x$fields(), list()) | |
100 | expect_equal(x$bit_width, 32L) | |
101 | ||
102 | x <- uint64() | |
103 | expect_equal(x$id, Type$UINT64) | |
104 | expect_equal(x$name, "uint64") | |
105 | expect_equal(x$ToString(), "uint64") | |
106 | expect_true(x == x) | |
107 | expect_false(x == null()) | |
108 | expect_equal(x$num_fields, 0L) | |
109 | expect_equal(x$fields(), list()) | |
110 | expect_equal(x$bit_width, 64L) | |
111 | ||
112 | x <- int64() | |
113 | expect_equal(x$id, Type$INT64) | |
114 | expect_equal(x$name, "int64") | |
115 | expect_equal(x$ToString(), "int64") | |
116 | expect_true(x == x) | |
117 | expect_false(x == null()) | |
118 | expect_equal(x$num_fields, 0L) | |
119 | expect_equal(x$fields(), list()) | |
120 | expect_equal(x$bit_width, 64L) | |
121 | }) | |
122 | ||
123 | test_that("float types work as expected", { | |
124 | x <- float16() | |
125 | expect_equal(x$id, Type$HALF_FLOAT) | |
126 | expect_equal(x$name, "halffloat") | |
127 | expect_equal(x$ToString(), "halffloat") | |
128 | expect_true(x == x) | |
129 | expect_false(x == null()) | |
130 | expect_equal(x$num_fields, 0L) | |
131 | expect_equal(x$fields(), list()) | |
132 | expect_equal(x$bit_width, 16L) | |
133 | ||
134 | x <- float32() | |
135 | expect_equal(x$id, Type$FLOAT) | |
136 | expect_equal(x$name, "float") | |
137 | expect_equal(x$ToString(), "float") | |
138 | expect_true(x == x) | |
139 | expect_false(x == null()) | |
140 | expect_equal(x$num_fields, 0L) | |
141 | expect_equal(x$fields(), list()) | |
142 | expect_equal(x$bit_width, 32L) | |
143 | ||
144 | x <- float64() | |
145 | expect_equal(x$id, Type$DOUBLE) | |
146 | expect_equal(x$name, "double") | |
147 | expect_equal(x$ToString(), "double") | |
148 | expect_true(x == x) | |
149 | expect_false(x == null()) | |
150 | expect_equal(x$num_fields, 0L) | |
151 | expect_equal(x$fields(), list()) | |
152 | expect_equal(x$bit_width, 64L) | |
153 | }) | |
154 | ||
155 | test_that("utf8 type works as expected", { | |
156 | x <- utf8() | |
157 | expect_equal(x$id, Type$STRING) | |
158 | expect_equal(x$name, "utf8") | |
159 | expect_equal(x$ToString(), "string") | |
160 | expect_true(x == x) | |
161 | expect_false(x == null()) | |
162 | expect_equal(x$num_fields, 0L) | |
163 | expect_equal(x$fields(), list()) | |
164 | }) | |
165 | ||
166 | test_that("date types work as expected", { | |
167 | x <- date32() | |
168 | expect_equal(x$id, Type$DATE32) | |
169 | expect_equal(x$name, "date32") | |
170 | expect_equal(x$ToString(), "date32[day]") | |
171 | expect_true(x == x) | |
172 | expect_false(x == null()) | |
173 | expect_equal(x$num_fields, 0L) | |
174 | expect_equal(x$fields(), list()) | |
175 | expect_equal(x$unit(), unclass(DateUnit$DAY)) | |
176 | ||
177 | x <- date64() | |
178 | expect_equal(x$id, Type$DATE64) | |
179 | expect_equal(x$name, "date64") | |
180 | expect_equal(x$ToString(), "date64[ms]") | |
181 | expect_true(x == x) | |
182 | expect_false(x == null()) | |
183 | expect_equal(x$num_fields, 0L) | |
184 | expect_equal(x$fields(), list()) | |
185 | expect_equal(x$unit(), unclass(DateUnit$MILLI)) | |
186 | }) | |
187 | ||
188 | test_that("timestamp type works as expected", { | |
189 | x <- timestamp(TimeUnit$SECOND) | |
190 | expect_equal(x$id, Type$TIMESTAMP) | |
191 | expect_equal(x$name, "timestamp") | |
192 | expect_equal(x$ToString(), "timestamp[s]") | |
193 | expect_true(x == x) | |
194 | expect_false(x == null()) | |
195 | expect_equal(x$num_fields, 0L) | |
196 | expect_equal(x$fields(), list()) | |
197 | expect_equal(x$bit_width, 64L) | |
198 | expect_equal(x$timezone(), "") | |
199 | expect_equal(x$unit(), unclass(TimeUnit$SECOND)) | |
200 | ||
201 | x <- timestamp(TimeUnit$MILLI) | |
202 | expect_equal(x$id, Type$TIMESTAMP) | |
203 | expect_equal(x$name, "timestamp") | |
204 | expect_equal(x$ToString(), "timestamp[ms]") | |
205 | expect_true(x == x) | |
206 | expect_false(x == null()) | |
207 | expect_equal(x$num_fields, 0L) | |
208 | expect_equal(x$fields(), list()) | |
209 | expect_equal(x$bit_width, 64L) | |
210 | expect_equal(x$timezone(), "") | |
211 | expect_equal(x$unit(), unclass(TimeUnit$MILLI)) | |
212 | ||
213 | x <- timestamp(TimeUnit$MICRO) | |
214 | expect_equal(x$id, Type$TIMESTAMP) | |
215 | expect_equal(x$name, "timestamp") | |
216 | expect_equal(x$ToString(), "timestamp[us]") | |
217 | expect_true(x == x) | |
218 | expect_false(x == null()) | |
219 | expect_equal(x$num_fields, 0L) | |
220 | expect_equal(x$fields(), list()) | |
221 | expect_equal(x$bit_width, 64L) | |
222 | expect_equal(x$timezone(), "") | |
223 | expect_equal(x$unit(), unclass(TimeUnit$MICRO)) | |
224 | ||
225 | x <- timestamp(TimeUnit$NANO) | |
226 | expect_equal(x$id, Type$TIMESTAMP) | |
227 | expect_equal(x$name, "timestamp") | |
228 | expect_equal(x$ToString(), "timestamp[ns]") | |
229 | expect_true(x == x) | |
230 | expect_false(x == null()) | |
231 | expect_equal(x$num_fields, 0L) | |
232 | expect_equal(x$fields(), list()) | |
233 | expect_equal(x$bit_width, 64L) | |
234 | expect_equal(x$timezone(), "") | |
235 | expect_equal(x$unit(), unclass(TimeUnit$NANO)) | |
236 | }) | |
237 | ||
238 | test_that("timestamp with timezone", { | |
239 | expect_equal(timestamp(timezone = "EST")$ToString(), "timestamp[s, tz=EST]") | |
240 | }) | |
241 | ||
242 | test_that("time32 types work as expected", { | |
243 | x <- time32(TimeUnit$SECOND) | |
244 | expect_equal(x$id, Type$TIME32) | |
245 | expect_equal(x$name, "time32") | |
246 | expect_equal(x$ToString(), "time32[s]") | |
247 | expect_true(x == x) | |
248 | expect_false(x == null()) | |
249 | expect_equal(x$num_fields, 0L) | |
250 | expect_equal(x$fields(), list()) | |
251 | expect_equal(x$bit_width, 32L) | |
252 | expect_equal(x$unit(), unclass(TimeUnit$SECOND)) | |
253 | ||
254 | x <- time32(TimeUnit$MILLI) | |
255 | expect_equal(x$id, Type$TIME32) | |
256 | expect_equal(x$name, "time32") | |
257 | expect_equal(x$ToString(), "time32[ms]") | |
258 | expect_true(x == x) | |
259 | expect_false(x == null()) | |
260 | expect_equal(x$num_fields, 0L) | |
261 | expect_equal(x$fields(), list()) | |
262 | expect_equal(x$bit_width, 32L) | |
263 | expect_equal(x$unit(), unclass(TimeUnit$MILLI)) | |
264 | }) | |
265 | ||
266 | test_that("time64 types work as expected", { | |
267 | x <- time64(TimeUnit$MICRO) | |
268 | expect_equal(x$id, Type$TIME64) | |
269 | expect_equal(x$name, "time64") | |
270 | expect_equal(x$ToString(), "time64[us]") | |
271 | expect_true(x == x) | |
272 | expect_false(x == null()) | |
273 | expect_equal(x$num_fields, 0L) | |
274 | expect_equal(x$fields(), list()) | |
275 | expect_equal(x$bit_width, 64L) | |
276 | expect_equal(x$unit(), unclass(TimeUnit$MICRO)) | |
277 | ||
278 | x <- time64(TimeUnit$NANO) | |
279 | expect_equal(x$id, Type$TIME64) | |
280 | expect_equal(x$name, "time64") | |
281 | expect_equal(x$ToString(), "time64[ns]") | |
282 | expect_true(x == x) | |
283 | expect_false(x == null()) | |
284 | expect_equal(x$num_fields, 0L) | |
285 | expect_equal(x$fields(), list()) | |
286 | expect_equal(x$bit_width, 64L) | |
287 | expect_equal(x$unit(), unclass(TimeUnit$NANO)) | |
288 | }) | |
289 | ||
290 | test_that("time type unit validation", { | |
291 | expect_equal(time32(TimeUnit$SECOND), time32("s")) | |
292 | expect_equal(time32(TimeUnit$MILLI), time32("ms")) | |
293 | expect_equal(time32(), time32(TimeUnit$MILLI)) | |
294 | expect_error(time32(4), '"unit" should be one of 1 or 0') | |
295 | expect_error(time32(NULL), '"unit" should be one of "ms" or "s"') | |
296 | expect_match_arg_error(time32("years")) | |
297 | ||
298 | expect_equal(time64(TimeUnit$NANO), time64("n")) | |
299 | expect_equal(time64(TimeUnit$MICRO), time64("us")) | |
300 | expect_equal(time64(), time64(TimeUnit$NANO)) | |
301 | expect_error(time64(4), '"unit" should be one of 3 or 2') | |
302 | expect_error(time64(NULL), '"unit" should be one of "ns" or "us"') | |
303 | expect_match_arg_error(time64("years")) | |
304 | }) | |
305 | ||
306 | test_that("timestamp type input validation", { | |
307 | expect_equal(timestamp("ms"), timestamp(TimeUnit$MILLI)) | |
308 | expect_equal(timestamp(), timestamp(TimeUnit$SECOND)) | |
309 | expect_error( | |
310 | timestamp(NULL), | |
311 | '"unit" should be one of "ns", "us", "ms", or "s"' | |
312 | ) | |
313 | expect_error( | |
314 | timestamp(timezone = 1231231), | |
315 | "timezone is not a string" | |
316 | ) | |
317 | expect_error( | |
318 | timestamp(timezone = c("not", "a", "timezone")), | |
319 | "timezone is not a string" | |
320 | ) | |
321 | }) | |
322 | ||
323 | test_that("list type works as expected", { | |
324 | x <- list_of(int32()) | |
325 | expect_equal(x$id, Type$LIST) | |
326 | expect_equal(x$name, "list") | |
327 | expect_equal(x$ToString(), "list<item: int32>") | |
328 | expect_true(x == x) | |
329 | expect_false(x == null()) | |
330 | expect_equal(x$num_fields, 1L) | |
331 | expect_equal( | |
332 | x$fields()[[1]], | |
333 | field("item", int32()) | |
334 | ) | |
335 | expect_equal(x$value_type, int32()) | |
336 | expect_equal(x$value_field, field("item", int32())) | |
337 | }) | |
338 | ||
339 | test_that("struct type works as expected", { | |
340 | x <- struct(x = int32(), y = boolean()) | |
341 | expect_equal(x$id, Type$STRUCT) | |
342 | expect_equal(x$name, "struct") | |
343 | expect_equal(x$ToString(), "struct<x: int32, y: bool>") | |
344 | expect_true(x == x) | |
345 | expect_false(x == null()) | |
346 | expect_equal(x$num_fields, 2L) | |
347 | expect_equal( | |
348 | x$fields()[[1]], | |
349 | field("x", int32()) | |
350 | ) | |
351 | expect_equal( | |
352 | x$fields()[[2]], | |
353 | field("y", boolean()) | |
354 | ) | |
355 | expect_equal(x$GetFieldIndex("x"), 0L) | |
356 | expect_equal(x$GetFieldIndex("y"), 1L) | |
357 | expect_equal(x$GetFieldIndex("z"), -1L) | |
358 | ||
359 | expect_equal(x$GetFieldByName("x"), field("x", int32())) | |
360 | expect_equal(x$GetFieldByName("y"), field("y", boolean())) | |
361 | expect_null(x$GetFieldByName("z")) | |
362 | }) | |
363 | ||
364 | test_that("DictionaryType works as expected (ARROW-3355)", { | |
365 | d <- dictionary(int32(), utf8()) | |
366 | expect_equal(d, d) | |
367 | expect_true(d == d) | |
368 | expect_false(d == int32()) | |
369 | expect_equal(d$id, Type$DICTIONARY) | |
370 | expect_equal(d$bit_width, 32L) | |
371 | expect_equal(d$ToString(), "dictionary<values=string, indices=int32>") | |
372 | expect_equal(d$index_type, int32()) | |
373 | expect_equal(d$value_type, utf8()) | |
374 | ord <- dictionary(ordered = TRUE) | |
375 | expect_equal(ord$ToString(), "dictionary<values=string, indices=int32, ordered>") | |
376 | }) | |
377 | ||
378 | test_that("DictionaryType validation", { | |
379 | expect_error( | |
380 | dictionary(utf8(), int32()), | |
381 | "Dictionary index type should be .*integer, got string" | |
382 | ) | |
383 | expect_error(dictionary(4, utf8()), 'index_type must be a "DataType"') | |
384 | expect_error(dictionary(int8(), "strings"), 'value_type must be a "DataType"') | |
385 | }) | |
386 | ||
387 | test_that("decimal type and validation", { | |
388 | expect_error(decimal()) | |
389 | expect_error(decimal("four"), '"precision" must be an integer') | |
390 | expect_error(decimal(4)) | |
391 | expect_error(decimal(4, "two"), '"scale" must be an integer') | |
392 | expect_error(decimal(NA, 2), '"precision" must be an integer') | |
393 | expect_error(decimal(0, 2), "Invalid: Decimal precision out of range: 0") | |
394 | expect_error(decimal(100, 2), "Invalid: Decimal precision out of range: 100") | |
395 | expect_error(decimal(4, NA), '"scale" must be an integer') | |
396 | ||
397 | expect_r6_class(decimal(4, 2), "Decimal128Type") | |
398 | }) | |
399 | ||
400 | test_that("Binary", { | |
401 | expect_r6_class(binary(), "Binary") | |
402 | expect_equal(binary()$ToString(), "binary") | |
403 | }) | |
404 | ||
405 | test_that("FixedSizeBinary", { | |
406 | expect_r6_class(fixed_size_binary(4), "FixedSizeBinary") | |
407 | expect_equal(fixed_size_binary(4)$ToString(), "fixed_size_binary[4]") | |
408 | ||
409 | # input validation | |
410 | expect_error(fixed_size_binary(NA), "'byte_width' cannot be NA") | |
411 | expect_error(fixed_size_binary(-1), "'byte_width' must be > 0") | |
412 | expect_error(fixed_size_binary("four")) | |
413 | expect_error(fixed_size_binary(c(2, 4))) | |
414 | }) | |
415 | ||
416 | test_that("DataType to C-interface", { | |
417 | datatype <- timestamp("ms", timezone = "Pacific/Marquesas") | |
418 | ||
419 | # export the datatype via the C-interface | |
420 | ptr <- allocate_arrow_schema() | |
421 | datatype$export_to_c(ptr) | |
422 | ||
423 | # then import it and check that the roundtripped value is the same | |
424 | circle <- DataType$import_from_c(ptr) | |
425 | expect_equal(circle, datatype) | |
426 | ||
427 | # must clean up the pointer or we leak | |
428 | delete_arrow_schema(ptr) | |
429 | }) |