Skip to content

Commit 1f9db90

Browse files
hadleykou
authored andcommitted
MINOR: [R] Fix for dev purrr (#14581)
The recycling rules in map2() are now stricter, so we need to check that `x` actually has columns before applying the metadata. I also mildly refactored the test to make it easier to run in isolation; I'm happy to revert those changes if desired. Authored-by: Hadley Wickham <h.wickham@gmail.com> Signed-off-by: Neal Richardson <neal.p.richardson@gmail.com>
1 parent 8f47806 commit 1f9db90

2 files changed

Lines changed: 35 additions & 38 deletions

File tree

r/R/metadata.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ apply_arrow_r_metadata <- function(x, r_metadata) {
8686
call. = FALSE
8787
)
8888
} else {
89-
x <- map2(x, columns_metadata, function(.x, .y) {
90-
apply_arrow_r_metadata(.x, .y)
91-
})
89+
if (length(x) > 0) {
90+
x <- map2(x, columns_metadata, function(.x, .y) {
91+
apply_arrow_r_metadata(.x, .y)
92+
})
93+
}
9294
}
9395
x
9496
}

r/tests/testthat/test-metadata.R

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,6 @@ test_that("Row-level metadata (does not) roundtrip in datasets", {
254254
skip_if_not_available("dataset")
255255
skip_if_not_available("parquet")
256256

257-
library(dplyr, warn.conflicts = FALSE)
258-
259257
df <- tibble::tibble(
260258
metadata = list(
261259
structure(1, my_value_as_attr = 1),
@@ -269,39 +267,36 @@ test_that("Row-level metadata (does not) roundtrip in datasets", {
269267

270268
dst_dir <- make_temp_dir()
271269

272-
withr::with_options(
273-
list("arrow.preserve_row_level_metadata" = TRUE),
274-
{
275-
expect_warning(
276-
write_dataset(df, dst_dir, partitioning = "part"),
277-
"Row-level metadata is not compatible with datasets and will be discarded"
278-
)
279-
280-
# Reset directory as previous write will have created some files and the default
281-
# behavior is to error on existing
282-
dst_dir <- make_temp_dir()
283-
# but we need to write a dataset with row-level metadata to make sure when
284-
# reading ones that have been written with them we warn appropriately
285-
fake_func_name <- write_dataset
286-
fake_func_name(df, dst_dir, partitioning = "part")
287-
288-
ds <- open_dataset(dst_dir)
289-
expect_warning(
290-
df_from_ds <- collect(ds),
291-
"Row-level metadata is not compatible with this operation and has been ignored"
292-
)
293-
expect_equal(
294-
arrange(df_from_ds, int),
295-
arrange(df, int),
296-
ignore_attr = TRUE
297-
)
298-
299-
# however there is *no* warning if we don't select the metadata column
300-
expect_warning(
301-
df_from_ds <- ds %>% select(int) %>% collect(),
302-
NA
303-
)
304-
}
270+
withr::local_options("arrow.preserve_row_level_metadata" = TRUE)
271+
272+
expect_warning(
273+
write_dataset(df, dst_dir, partitioning = "part"),
274+
"Row-level metadata is not compatible with datasets and will be discarded"
275+
)
276+
277+
# Reset directory as previous write will have created some files and the default
278+
# behavior is to error on existing
279+
dst_dir <- make_temp_dir()
280+
# but we need to write a dataset with row-level metadata to make sure when
281+
# reading ones that have been written with them we warn appropriately
282+
fake_func_name <- write_dataset
283+
fake_func_name(df, dst_dir, partitioning = "part")
284+
285+
ds <- open_dataset(dst_dir)
286+
expect_warning(
287+
df_from_ds <- collect(ds),
288+
"Row-level metadata is not compatible with this operation and has been ignored"
289+
)
290+
expect_equal(
291+
dplyr::arrange(df_from_ds, int),
292+
dplyr::arrange(df, int),
293+
ignore_attr = TRUE
294+
)
295+
296+
# however there is *no* warning if we don't select the metadata column
297+
expect_warning(
298+
df_from_ds <- ds %>% dplyr::select(int) %>% dplyr::collect(),
299+
NA
305300
)
306301
})
307302

0 commit comments

Comments
 (0)