Skip to content

Commit 6b4e0f6

Browse files
GH-35038: [R] argument order in arrow_table affects object return type (#35039)
* Closes: #35038 Lead-authored-by: Nic Crane <[email protected]> Co-authored-by: Neal Richardson <[email protected]> Signed-off-by: Nic Crane <[email protected]>
1 parent aff876a commit 6b4e0f6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

r/src/table.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ arrow::Status AddMetadataFromDots(SEXP lst, int num_fields,
228228
// "top level" attributes, only relevant if the first object is not named and a data
229229
// frame
230230
cpp11::strings names = Rf_getAttrib(lst, R_NamesSymbol);
231-
if (names[0] == "" && Rf_inherits(VECTOR_ELT(lst, 0), "data.frame")) {
231+
if (names[0] == "" && Rf_inherits(VECTOR_ELT(lst, 0), "data.frame") &&
232+
Rf_xlength(lst) == 1) {
232233
SEXP top_level = metadata[0] = arrow_attributes(VECTOR_ELT(lst, 0), true);
233234
if (!Rf_isNull(top_level) && XLENGTH(top_level) > 0) {
234235
has_top_level_metadata = true;

r/tests/testthat/test-Table.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,22 @@ test_that("as_arrow_table() errors on data.frame with NULL names", {
711711
names(df) <- NULL
712712
expect_error(as_arrow_table(df), "Input data frame columns must be named")
713713
})
714+
715+
test_that("we only preserve metadata of input to arrow_table when passed a single data.frame", {
716+
# data.frame in, data.frame out
717+
df <- data.frame(x = 1)
718+
out1 <- as.data.frame(arrow_table(df))
719+
expect_s3_class(out1, "data.frame", exact = TRUE)
720+
721+
# tibble in, tibble out
722+
tib <- tibble::tibble(x = 1)
723+
out2 <- as.data.frame(arrow_table(tib))
724+
expect_s3_class(out2, c("tbl_df", "tbl", "data.frame"), exact = TRUE)
725+
726+
# GH-35038 - passing in multiple arguments doesn't affect return type
727+
out3 <- as.data.frame(arrow_table(df, name = "1"))
728+
out4 <- as.data.frame(arrow_table(name = "1", df))
729+
730+
expect_s3_class(out3, c("tbl_df", "tbl", "data.frame"), exact = TRUE)
731+
expect_s3_class(out4, c("tbl_df", "tbl", "data.frame"), exact = TRUE)
732+
})

0 commit comments

Comments
 (0)