Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions r/R/record-batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ as.data.frame.RecordBatch <- function(x, row.names = NULL, optional = FALSE, ...
}

.serialize_arrow_r_metadata <- function(x) {
# drop problems attributes (most likely from readr)
if ("attributes" %in% names(x) &&
"problems" %in% names(x[["attributes"]]) ) {
x[["attributes"]][["problems"]] <- NULL
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can safely just

Suggested change
if ("attributes" %in% names(x) &&
"problems" %in% names(x[["attributes"]]) ) {
x[["attributes"]][["problems"]] <- NULL
}
x[["attributes"]][["problems"]] <- NULL
> x <- list()
> x
list()
> x$attributes$problems <- NULL
> x
list()
> x[["attributes"]][["problems"]] <- NULL
> x
list()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it turns out just NULLing doesn’t quite work with the tests for garbage metadata ("garbage"[["attributes"]][["problems"]] <- NULL errors. I can either test that the attributes are a list before this or alter our garbage tests to send in a list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we validate/assert is.list() in this function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can do that. Though one of the existing tests fails here — the one right above it works which might be sufficient, is there something that lines 76-81 is testing that we aren't covering with lines 70-75?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to look into the implementation to be sure, but I think the intent of the first test should be that we don't save garbage r metadata and the second would be that, if someone happened to have bad metadata stored in the r key (we can't prevent some other data generating process from doing that), that we don't crash on loading it. It's not clear that that's actually what those tests are doing, but that's what I think they should do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, cool — I'll rewrite the second test to avoid using .serialize_arrow_r_metadata() so that we don't error there, but still keep the testing-serialized-data aspect of that test.

rawToChar(serialize(x, NULL, ascii = TRUE))
}

Expand Down
20 changes: 20 additions & 0 deletions r/tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,23 @@ test_that("metadata keeps attribute of top level data frame", {
expect_identical(attr(as.data.frame(tab), "foo"), "bar")
expect_identical(as.data.frame(tab), df)
})

test_that("metadata drops readr's problems attribute", {
readr_like <- tibble::tibble(
dbl = 1.1,
not_here = NA_character_
)
attributes(readr_like) <- append(
attributes(readr_like),
list(problems = tibble::tibble(
row = 1L,
col = NA_character_,
expected = "2 columns",
actual = "1 columns",
file = "'test'"
))
)

tab <- Table$create(readr_like)
expect_null(attr(as.data.frame(tab), "problems"))
})