Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 14 additions & 6 deletions r/R/record-batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ as.data.frame.RecordBatch <- function(x, row.names = NULL, optional = FALSE, ...

apply_arrow_r_metadata <- function(x, r_metadata) {
tryCatch({
columns_metadata <- r_metadata$columns
if (is.data.frame(x)) {
if (length(names(x)) && !is.null(columns_metadata)) {
for (name in intersect(names(columns_metadata), names(x))) {
x[[name]] <- apply_arrow_r_metadata(x[[name]], columns_metadata[[name]])
}
}
} else if(is.list(x) && !inherits(x, "POSIXlt") && !is.null(columns_metadata)) {
x <- map2(x, columns_metadata, function(.x, .y) {
apply_arrow_r_metadata(.x, .y)
})
x
}
Copy link
Member

Choose a reason for hiding this comment

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

Could we move !is.null(columns_metadata) up to line 290 instead of having it on both 291 and 296?


if (!is.null(r_metadata$attributes)) {
attributes(x)[names(r_metadata$attributes)] <- r_metadata$attributes
if (inherits(x, "POSIXlt")) {
Expand All @@ -297,12 +311,6 @@ apply_arrow_r_metadata <- function(x, r_metadata) {
}
}

columns_metadata <- r_metadata$columns
if (length(names(x)) && !is.null(columns_metadata)) {
for (name in intersect(names(columns_metadata), names(x))) {
x[[name]] <- apply_arrow_r_metadata(x[[name]], columns_metadata[[name]])
}
}
}, error = function(e) {
warning("Invalid metadata$r", call. = FALSE)
})
Expand Down
19 changes: 16 additions & 3 deletions r/R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,24 @@ arrow_attributes <- function(x, only_top_level = FALSE) {

if (is.data.frame(x)) {
columns <- map(x, arrow_attributes)
if (length(att) || !all(map_lgl(columns, is.null))) {
out <- if (length(att) || !all(map_lgl(columns, is.null))) {
list(attributes = att, columns = columns)
}
} else if (length(att)) {
list(attributes = att, columns = NULL)
return(out)
}

columns <- NULL
if (is.list(x) && !inherits(x, "POSIXlt")) {
# for list columns, we also keep attributes of each
# element in columns
columns <- map(x, arrow_attributes)
if (all(map_lgl(columns, is.null))) {
columns <- NULL
}
}

if (length(att) || !is.null(columns)) {
list(attributes = att, columns = columns)
} else {
NULL
}
Expand Down
7 changes: 7 additions & 0 deletions r/tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ 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 of list elements (ARROW-10386)", {
df <- data.frame(x = I(list(structure(1, foo = "bar"), structure(2, foo = "bar"))))
tab <- Table$create(df)
expect_identical(attr(as.data.frame(tab)$x[[1]], "foo"), "bar")
expect_identical(attr(as.data.frame(tab)$x[[2]], "foo"), "bar")
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it would be clearer to have different attributes on each item/row to make it super obvious that we're not picking the attributes of the first item/row and copying them for the whole column.

})