Skip to content

Commit

Permalink
GH-35038: [R] argument order in arrow_table affects object return type (
Browse files Browse the repository at this point in the history
#35039)

* Closes: #35038

Lead-authored-by: Nic Crane <[email protected]>
Co-authored-by: Neal Richardson <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
  • Loading branch information
thisisnic and nealrichardson authored Apr 11, 2023
1 parent aff876a commit 6b4e0f6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion r/src/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ arrow::Status AddMetadataFromDots(SEXP lst, int num_fields,
// "top level" attributes, only relevant if the first object is not named and a data
// frame
cpp11::strings names = Rf_getAttrib(lst, R_NamesSymbol);
if (names[0] == "" && Rf_inherits(VECTOR_ELT(lst, 0), "data.frame")) {
if (names[0] == "" && Rf_inherits(VECTOR_ELT(lst, 0), "data.frame") &&
Rf_xlength(lst) == 1) {
SEXP top_level = metadata[0] = arrow_attributes(VECTOR_ELT(lst, 0), true);
if (!Rf_isNull(top_level) && XLENGTH(top_level) > 0) {
has_top_level_metadata = true;
Expand Down
19 changes: 19 additions & 0 deletions r/tests/testthat/test-Table.R
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,22 @@ test_that("as_arrow_table() errors on data.frame with NULL names", {
names(df) <- NULL
expect_error(as_arrow_table(df), "Input data frame columns must be named")
})

test_that("we only preserve metadata of input to arrow_table when passed a single data.frame", {
# data.frame in, data.frame out
df <- data.frame(x = 1)
out1 <- as.data.frame(arrow_table(df))
expect_s3_class(out1, "data.frame", exact = TRUE)

# tibble in, tibble out
tib <- tibble::tibble(x = 1)
out2 <- as.data.frame(arrow_table(tib))
expect_s3_class(out2, c("tbl_df", "tbl", "data.frame"), exact = TRUE)

# GH-35038 - passing in multiple arguments doesn't affect return type
out3 <- as.data.frame(arrow_table(df, name = "1"))
out4 <- as.data.frame(arrow_table(name = "1", df))

expect_s3_class(out3, c("tbl_df", "tbl", "data.frame"), exact = TRUE)
expect_s3_class(out4, c("tbl_df", "tbl", "data.frame"), exact = TRUE)
})

0 comments on commit 6b4e0f6

Please sign in to comment.