Skip to content

Commit

Permalink
ARROW-17620: [R] as_arrow_array() ignores type for StructArrays (#14047)
Browse files Browse the repository at this point in the history
As described in https://issues.apache.org/jira/browse/ARROW-17620, `as_arrow_array()` ignores user-provided types passed by the `type` argument.

This PR explicitly adds the `type` name argument to the `Map` call. Currently, the argument gets passed to the ellipsis and gets ignored.

I also added a test. Let me know if there is a better place for it (or a better test).

Lead-authored-by: François Michonneau <[email protected]>
Co-authored-by: Nic Crane <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
  • Loading branch information
fmichonneau and thisisnic authored Sep 12, 2022
1 parent 8c76855 commit 3ddf69e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion r/R/array.R
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ as_arrow_array.data.frame <- function(x, ..., type = NULL) {
fields <- type$fields()
names <- map_chr(fields, "name")
types <- map(fields, "type")
arrays <- Map(as_arrow_array, x, types)
arrays <- Map(as_arrow_array, x, type = types)
names(arrays) <- names

# TODO(ARROW-16266): a hack because there is no StructArray$create() yet
Expand Down
13 changes: 10 additions & 3 deletions r/tests/testthat/test-Array.R
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ test_that("Array$create() can handle data frame with custom struct type (not inf
type <- struct(x = float64(), y = int16())
a <- Array$create(df, type = type)
expect_type_equal(a$type, type)

type <- struct(x = float64(), y = int16(), z = int32())
expect_error(
Array$create(df, type = type),
Expand Down Expand Up @@ -1030,6 +1029,14 @@ test_that("as_arrow_array() default method calls Array$create()", {
)
})

test_that("as_arrow_array respects `type` argument (ARROW-17620)", {
df <- tibble::tibble(x = 1:10, y = 1:10)
type <- struct(x = float64(), y = int16())
a <- Array$create(df, type = type)

expect_type_equal(a, as_arrow_array(df, type = type))
})

test_that("as_arrow_array() works for Array", {
array <- Array$create(logical(), type = null())
expect_identical(as_arrow_array(array), array)
Expand Down Expand Up @@ -1115,7 +1122,7 @@ test_that("as_arrow_array() works for nested extension types", {
nested_plain <- tibble::tibble(x = 1:5)
extension_array <- vctrs_extension_array(nested_plain)
expect_equal(
as_arrow_array(nested, type = extension_array$type),
as_arrow_array(nested_plain, type = extension_array$type),
extension_array
)
})
Expand Down Expand Up @@ -1144,7 +1151,7 @@ test_that("Array$create() calls as_arrow_array() for nested extension types", {
nested_plain <- tibble::tibble(x = 1:5)
extension_array <- vctrs_extension_array(nested_plain)
expect_equal(
Array$create(nested, type = extension_array$type),
Array$create(nested_plain, type = extension_array$type),
extension_array
)
})
Expand Down

0 comments on commit 3ddf69e

Please sign in to comment.