diff --git a/r/R/metadata.R b/r/R/metadata.R index 747f08069e4..6a54b3e3842 100644 --- a/r/R/metadata.R +++ b/r/R/metadata.R @@ -86,9 +86,11 @@ apply_arrow_r_metadata <- function(x, r_metadata) { call. = FALSE ) } else { - x <- map2(x, columns_metadata, function(.x, .y) { - apply_arrow_r_metadata(.x, .y) - }) + if (length(x) > 0) { + x <- map2(x, columns_metadata, function(.x, .y) { + apply_arrow_r_metadata(.x, .y) + }) + } } x } diff --git a/r/tests/testthat/test-metadata.R b/r/tests/testthat/test-metadata.R index 21b7ebe11a1..4cf8e49af1b 100644 --- a/r/tests/testthat/test-metadata.R +++ b/r/tests/testthat/test-metadata.R @@ -254,8 +254,6 @@ test_that("Row-level metadata (does not) roundtrip in datasets", { skip_if_not_available("dataset") skip_if_not_available("parquet") - library(dplyr, warn.conflicts = FALSE) - df <- tibble::tibble( metadata = list( structure(1, my_value_as_attr = 1), @@ -269,39 +267,36 @@ test_that("Row-level metadata (does not) roundtrip in datasets", { dst_dir <- make_temp_dir() - withr::with_options( - list("arrow.preserve_row_level_metadata" = TRUE), - { - expect_warning( - write_dataset(df, dst_dir, partitioning = "part"), - "Row-level metadata is not compatible with datasets and will be discarded" - ) - - # Reset directory as previous write will have created some files and the default - # behavior is to error on existing - dst_dir <- make_temp_dir() - # but we need to write a dataset with row-level metadata to make sure when - # reading ones that have been written with them we warn appropriately - fake_func_name <- write_dataset - fake_func_name(df, dst_dir, partitioning = "part") - - ds <- open_dataset(dst_dir) - expect_warning( - df_from_ds <- collect(ds), - "Row-level metadata is not compatible with this operation and has been ignored" - ) - expect_equal( - arrange(df_from_ds, int), - arrange(df, int), - ignore_attr = TRUE - ) - - # however there is *no* warning if we don't select the metadata column - expect_warning( - df_from_ds <- ds %>% select(int) %>% collect(), - NA - ) - } + withr::local_options("arrow.preserve_row_level_metadata" = TRUE) + + expect_warning( + write_dataset(df, dst_dir, partitioning = "part"), + "Row-level metadata is not compatible with datasets and will be discarded" + ) + + # Reset directory as previous write will have created some files and the default + # behavior is to error on existing + dst_dir <- make_temp_dir() + # but we need to write a dataset with row-level metadata to make sure when + # reading ones that have been written with them we warn appropriately + fake_func_name <- write_dataset + fake_func_name(df, dst_dir, partitioning = "part") + + ds <- open_dataset(dst_dir) + expect_warning( + df_from_ds <- collect(ds), + "Row-level metadata is not compatible with this operation and has been ignored" + ) + expect_equal( + dplyr::arrange(df_from_ds, int), + dplyr::arrange(df, int), + ignore_attr = TRUE + ) + + # however there is *no* warning if we don't select the metadata column + expect_warning( + df_from_ds <- ds %>% dplyr::select(int) %>% dplyr::collect(), + NA ) })