Skip to content

Commit 2f53ebf

Browse files
Additional tests for read_arrow / write_arrow
1 parent 4237c32 commit 2f53ebf

File tree

7 files changed

+84
-4
lines changed

7 files changed

+84
-4
lines changed

r/DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Imports:
2525
tibble,
2626
crayon
2727
Roxygen: list(markdown = TRUE)
28-
RoxygenNote: 6.0.1.9000
28+
RoxygenNote: 6.1.0.9000
2929
Suggests:
3030
testthat
3131
Collate:

r/R/RcppExports.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ Column__null_count <- function(column) {
141141
.Call(`_arrow_Column__null_count`, column)
142142
}
143143

144+
Column__type <- function(column) {
145+
.Call(`_arrow_Column__type`, column)
146+
}
147+
144148
Field_initialize <- function(name, type, nullable = TRUE) {
145149
.Call(`_arrow_Field_initialize`, name, type, nullable)
146150
}

r/R/array.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ read_record_batch <- function(path){
9494
`arrow::Column` <- R6Class("arrow::Column", inherit = `arrow::Object`,
9595
public = list(
9696
length = function() Column__length(self),
97-
null_count = function() Column__null_count(self)
97+
null_count = function() Column__null_count(self),
98+
type = function() `arrow::DataType`$dispatch(Column__type(self))
9899
)
99100
)
100101

r/src/RcppExports.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,17 @@ BEGIN_RCPP
399399
return rcpp_result_gen;
400400
END_RCPP
401401
}
402+
// Column__type
403+
std::shared_ptr<arrow::DataType> Column__type(const std::shared_ptr<arrow::Column>& column);
404+
RcppExport SEXP _arrow_Column__type(SEXP columnSEXP) {
405+
BEGIN_RCPP
406+
Rcpp::RObject rcpp_result_gen;
407+
Rcpp::RNGScope rcpp_rngScope_gen;
408+
Rcpp::traits::input_parameter< const std::shared_ptr<arrow::Column>& >::type column(columnSEXP);
409+
rcpp_result_gen = Rcpp::wrap(Column__type(column));
410+
return rcpp_result_gen;
411+
END_RCPP
412+
}
402413
// Field_initialize
403414
std::shared_ptr<arrow::Field> Field_initialize(const std::string& name, const std::shared_ptr<arrow::DataType>& type, bool nullable);
404415
RcppExport SEXP _arrow_Field_initialize(SEXP nameSEXP, SEXP typeSEXP, SEXP nullableSEXP) {
@@ -996,6 +1007,7 @@ static const R_CallMethodDef CallEntries[] = {
9961007
{"_arrow_Table__column", (DL_FUNC) &_arrow_Table__column, 2},
9971008
{"_arrow_Column__length", (DL_FUNC) &_arrow_Column__length, 1},
9981009
{"_arrow_Column__null_count", (DL_FUNC) &_arrow_Column__null_count, 1},
1010+
{"_arrow_Column__type", (DL_FUNC) &_arrow_Column__type, 1},
9991011
{"_arrow_Field_initialize", (DL_FUNC) &_arrow_Field_initialize, 3},
10001012
{"_arrow_Field_ToString", (DL_FUNC) &_arrow_Field_ToString, 1},
10011013
{"_arrow_Field_name", (DL_FUNC) &_arrow_Field_name, 1},

r/src/buffer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ List RecordBatch_to_dataframe(const std::shared_ptr<arrow::RecordBatch>& batch){
233233
names[i] = batch->column_name(i);
234234
}
235235
tbl.attr("names") = names;
236-
tbl.attr("class") = CharacterVector::create("tbf_df", "tbl", "data.frame");
236+
tbl.attr("class") = CharacterVector::create("tbl_df", "tbl", "data.frame");
237237
tbl.attr("row.names") = IntegerVector::create(NA_INTEGER, -nr);
238238
return tbl;
239239
}
@@ -341,7 +341,7 @@ List Table_to_dataframe(const std::shared_ptr<arrow::Table>& table){
341341
names[i] = column->name();
342342
}
343343
tbl.attr("names") = names;
344-
tbl.attr("class") = CharacterVector::create("tbf_df", "tbl", "data.frame");
344+
tbl.attr("class") = CharacterVector::create("tbl_df", "tbl", "data.frame");
345345
tbl.attr("row.names") = IntegerVector::create(NA_INTEGER, -nr);
346346
return tbl;
347347
}
@@ -360,3 +360,8 @@ int Column__length(const std::shared_ptr<arrow::Column>& column) {
360360
int Column__null_count(const std::shared_ptr<arrow::Column>& column) {
361361
return column->null_count();
362362
}
363+
364+
// [[Rcpp::export]]
365+
std::shared_ptr<arrow::DataType> Column__type(const std::shared_ptr<arrow::Column>& column) {
366+
return column->type();
367+
}

r/tests/testthat.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717

1818
library(testthat)
1919
library(arrow)
20+
library(tibble)
2021

2122
test_check("arrow")

r/tests/testthat/test-read-write.R

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
context("test-read-write")
2+
3+
test_that("arrow::table round trip", {
4+
tbl <- tibble(int = 1:10, dbl = as.numeric(1:10), raw = as.raw(1:10))
5+
6+
tab <- arrow::table(tbl)
7+
expect_equal(tab$num_columns(), 3L)
8+
expect_equal(tab$num_rows(), 10L)
9+
10+
expect_equal(tab$column(0)$length(), 10L)
11+
expect_equal(tab$column(1)$length(), 10L)
12+
expect_equal(tab$column(2)$length(), 10L)
13+
14+
expect_equal(tab$column(0)$null_count(), 0L)
15+
expect_equal(tab$column(1)$null_count(), 0L)
16+
expect_equal(tab$column(2)$null_count(), 0L)
17+
18+
expect_equal(tab$column(0)$type(), int32())
19+
expect_equal(tab$column(1)$type(), float64())
20+
expect_equal(tab$column(2)$type(), int8())
21+
22+
tf <- tempfile(); on.exit(unlink(tf))
23+
write_arrow(tbl, path = tf)
24+
25+
res <- read_arrow(tf)
26+
expect_identical(tbl, res)
27+
})
28+
29+
test_that("arrow::table round trip handles NA in integer and numeric", {
30+
tbl <- tibble(int = c(NA, 2:10), dbl = as.numeric(c(1:5, NA, 7:9, NA)), raw = as.raw(1:10))
31+
32+
tab <- arrow::table(tbl)
33+
expect_equal(tab$num_columns(), 3L)
34+
expect_equal(tab$num_rows(), 10L)
35+
36+
expect_equal(tab$column(0)$length(), 10L)
37+
expect_equal(tab$column(1)$length(), 10L)
38+
expect_equal(tab$column(2)$length(), 10L)
39+
40+
expect_equal(tab$column(0)$null_count(), 1L)
41+
expect_equal(tab$column(1)$null_count(), 2L)
42+
expect_equal(tab$column(2)$null_count(), 0L)
43+
44+
expect_equal(tab$column(0)$type(), int32())
45+
expect_equal(tab$column(1)$type(), float64())
46+
expect_equal(tab$column(2)$type(), int8())
47+
48+
tf <- tempfile(); on.exit(unlink(tf))
49+
write_arrow(tbl, path = tf)
50+
51+
res <- read_arrow(tf)
52+
expect_identical(tbl, res)
53+
expect_true(is.na(res$int[1]))
54+
expect_true(is.na(res$dbl[6]))
55+
expect_true(is.na(res$dbl[10]))
56+
})
57+

0 commit comments

Comments
 (0)