]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/r/tests/testthat/test-python.R
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / r / tests / testthat / test-python.R
CommitLineData
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
18test_that("install_pyarrow", {
19 skip_on_cran()
20 skip_if_offline()
21 skip_if_not_dev_mode()
22 # Windows CI machine doesn't pick up the right python or something
23 skip_on_os("windows")
24 skip_if_not_installed("reticulate")
25
26 venv <- try(reticulate::virtualenv_create("arrow-test"))
27 # Bail out if virtualenv isn't available
28 skip_if(inherits(venv, "try-error"))
29 expect_error(install_pyarrow("arrow-test", nightly = TRUE), NA)
30 # Set this up for the following tests
31 reticulate::use_virtualenv("arrow-test")
32})
33
34skip_if_no_pyarrow()
35
36test_that("Array from Python", {
37 pa <- reticulate::import("pyarrow")
38 py <- pa$array(c(1, 2, 3))
39 expect_equal(py, Array$create(c(1, 2, 3)))
40})
41
42test_that("Array to Python", {
43 pa <- reticulate::import("pyarrow", convert = FALSE)
44 r <- Array$create(c(1, 2, 3))
45 py <- pa$concat_arrays(list(r))
46 expect_s3_class(py, "pyarrow.lib.Array")
47 expect_equal(reticulate::py_to_r(py), r)
48})
49
50test_that("RecordBatch to/from Python", {
51 pa <- reticulate::import("pyarrow", convert = FALSE)
52 batch <- record_batch(col1 = c(1, 2, 3), col2 = letters[1:3])
53 py <- reticulate::r_to_py(batch)
54 expect_s3_class(py, "pyarrow.lib.RecordBatch")
55 expect_equal(reticulate::py_to_r(py), batch)
56})
57
58test_that("Table and ChunkedArray from Python", {
59 pa <- reticulate::import("pyarrow", convert = FALSE)
60 batch <- record_batch(col1 = c(1, 2, 3), col2 = letters[1:3])
61 tab <- Table$create(batch, batch)
62 pybatch <- reticulate::r_to_py(batch)
63 pytab <- pa$Table$from_batches(list(pybatch, pybatch))
64 expect_s3_class(pytab, "pyarrow.lib.Table")
65 expect_s3_class(pytab[0], "pyarrow.lib.ChunkedArray")
66 expect_equal(reticulate::py_to_r(pytab[0]), tab$col1)
67 expect_equal(reticulate::py_to_r(pytab), tab)
68})
69
70test_that("Table and ChunkedArray to Python", {
71 batch <- record_batch(col1 = c(1, 2, 3), col2 = letters[1:3])
72 tab <- Table$create(batch, batch)
73
74 pychunked <- reticulate::r_to_py(tab$col1)
75 expect_s3_class(pychunked, "pyarrow.lib.ChunkedArray")
76 expect_equal(reticulate::py_to_r(pychunked), tab$col1)
77
78 pytab <- reticulate::r_to_py(tab)
79 expect_s3_class(pytab, "pyarrow.lib.Table")
80 expect_equal(reticulate::py_to_r(pytab), tab)
81})
82
83test_that("RecordBatch with metadata roundtrip", {
84 batch <- RecordBatch$create(example_with_times)
85 pybatch <- reticulate::r_to_py(batch)
86 expect_s3_class(pybatch, "pyarrow.lib.RecordBatch")
87 expect_equal(reticulate::py_to_r(pybatch), batch)
88 expect_identical(as.data.frame(reticulate::py_to_r(pybatch)), example_with_times)
89})
90
91test_that("Table with metadata roundtrip", {
92 tab <- Table$create(example_with_times)
93 pytab <- reticulate::r_to_py(tab)
94 expect_s3_class(pytab, "pyarrow.lib.Table")
95 expect_equal(reticulate::py_to_r(pytab), tab)
96 expect_identical(as.data.frame(reticulate::py_to_r(pytab)), example_with_times)
97})
98
99test_that("DataType roundtrip", {
100 r <- timestamp("ms", timezone = "Pacific/Marquesas")
101 py <- reticulate::r_to_py(r)
102 expect_s3_class(py, "pyarrow.lib.DataType")
103 expect_equal(reticulate::py_to_r(py), r)
104})
105
106test_that("Field roundtrip", {
107 r <- field("x", time32("s"))
108 py <- reticulate::r_to_py(r)
109 expect_s3_class(py, "pyarrow.lib.Field")
110 expect_equal(reticulate::py_to_r(py), r)
111})
112
113test_that("RecordBatchReader to python", {
114 library(dplyr)
115
116 tab <- Table$create(example_data)
117 scan <- tab %>%
118 select(int, lgl) %>%
119 filter(int > 6) %>%
120 Scanner$create()
121 reader <- scan$ToRecordBatchReader()
122 pyreader <- reticulate::r_to_py(reader)
123 expect_s3_class(pyreader, "pyarrow.lib.RecordBatchReader")
124 pytab <- pyreader$read_all()
125 expect_s3_class(pytab, "pyarrow.lib.Table")
126 back_to_r <- reticulate::py_to_r(pytab)
127 expect_r6_class(back_to_r, "Table")
128 expect_identical(
129 as.data.frame(back_to_r),
130 example_data %>%
131 select(int, lgl) %>%
132 filter(int > 6)
133 )
134})
135
136test_that("RecordBatchReader from python", {
137 tab <- Table$create(example_data)
138 scan <- Scanner$create(tab)
139 reader <- scan$ToRecordBatchReader()
140 pyreader <- reticulate::r_to_py(reader)
141 back_to_r <- reticulate::py_to_r(pyreader)
142 rt_table <- back_to_r$read_table()
143 expect_r6_class(rt_table, "Table")
144 expect_identical(as.data.frame(rt_table), example_data)
145})