Skip to content

Commit

Permalink
Fix class option
Browse files Browse the repository at this point in the history
Closes #104.
  • Loading branch information
gaborcsardi committed Jan 24, 2025
1 parent 3667da9 commit d879e89
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
* `read_parquet()` now reads the `FLOAT16` logical type as a real (double)
vector.

* The `class` argument of `parquet_options()` and the `nanoparquet.class`
option now work again (#104).

# nanoparquet 0.3.1

* This version fixes a `write_parquet()` crash (#73).
Expand Down
4 changes: 4 additions & 0 deletions R/read-parquet.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ post_process_read_result <- function(res, file, options, col_select) {
)
}

if (length(options[["class"]]) > 0) {
class(res) <- unique(c(options[["class"]], class(res)))
}

res
}

Expand Down
5 changes: 2 additions & 3 deletions src/RParquetReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,9 +2065,8 @@ void RParquetReader::create_df() {
Rf_setAttrib(columns, R_RowNamesSymbol, rnms);
UNPROTECT(1);

SEXP cls = PROTECT(Rf_allocVector(STRSXP, 2));
SET_STRING_ELT(cls, 0, Rf_mkCharCE("tbl", CE_UTF8));
SET_STRING_ELT(cls, 1, Rf_mkCharCE("data.frame", CE_UTF8));
SEXP cls = PROTECT(Rf_allocVector(STRSXP, 1));
SET_STRING_ELT(cls, 0, Rf_mkCharCE("data.frame", CE_UTF8));
Rf_setAttrib(columns, R_ClassSymbol, cls);
UNPROTECT(1);
}
17 changes: 17 additions & 0 deletions tests/testthat/test-read-parquet-5.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ test_that("read a subset, factor to test Arrow metadata", {
df[, 14, drop = FALSE]
)
})

test_that("class", {
withr::local_options(nanoparquet.class = NULL)
tmp <- tempfile(fileext = ".parquet")
on.exit(unlink(tmp), add = TRUE)
write_parquet(test_df(), tmp)
expect_equal(class(read_parquet(tmp)), c("tbl", "data.frame"))
expect_equal(
class(read_parquet(
tmp,
options = parquet_options(class = c("foo", "bar", "data.frame"))
)),
c("foo", "bar", "data.frame")
)
withr::local_options(nanoparquet.class = "foobar")
expect_equal(class(read_parquet(tmp)), c("foobar", "data.frame"))
})

0 comments on commit d879e89

Please sign in to comment.