jonkeane commented on a change in pull request #12324:
URL: https://github.com/apache/arrow/pull/12324#discussion_r806267955
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,59 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_empty_typed <- concat_arrays(type = int64())
+ expect_true(concat_empty_typed$type == int64())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_int <- concat_arrays(Array$create(1:3), Array$create(4:5))
+ expect_true(concat_int$type == int32())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ concat_int64 <- concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64()),
+ type = int64()
+ )
+ expect_true(concat_int64$type == int64())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ expect_error(
+ concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64())
+ ),
+ "must be identically typed"
+ )
+})
+
+test_that("c() works for Array", {
+ expect_r6_class(c(Array$create(1L), Array$create(1L)), "Array")
+
+ struct <- call_function(
+ "make_struct",
+ Array$create(1L),
+ options = list(field_names = "")
+ )
+ expect_r6_class(c(struct, struct), "StructArray")
+
+ list <- Array$create(list(1))
+ expect_r6_class(c(list, list), "ListArray")
+
+ list <- Array$create(list(), type = large_list_of(float64()))
+ expect_r6_class(c(list, list), "LargeListArray")
+
+ list <- Array$create(list(),type = fixed_size_list_of(float64(), 1L))
+ expect_r6_class(c(list, list), "FixedSizeListArray")
+
+ list <- Array$create(list(),type = map_of(string(), float64()))
Review comment:
```suggestion
list <- Array$create(list(), type = map_of(string(), float64()))
```
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,59 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_empty_typed <- concat_arrays(type = int64())
+ expect_true(concat_empty_typed$type == int64())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_int <- concat_arrays(Array$create(1:3), Array$create(4:5))
+ expect_true(concat_int$type == int32())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ concat_int64 <- concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64()),
+ type = int64()
+ )
+ expect_true(concat_int64$type == int64())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ expect_error(
+ concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64())
+ ),
+ "must be identically typed"
+ )
+})
+
+test_that("c() works for Array", {
+ expect_r6_class(c(Array$create(1L), Array$create(1L)), "Array")
+
+ struct <- call_function(
+ "make_struct",
+ Array$create(1L),
+ options = list(field_names = "")
+ )
+ expect_r6_class(c(struct, struct), "StructArray")
+
+ list <- Array$create(list(1))
+ expect_r6_class(c(list, list), "ListArray")
+
+ list <- Array$create(list(), type = large_list_of(float64()))
+ expect_r6_class(c(list, list), "LargeListArray")
+
+ list <- Array$create(list(),type = fixed_size_list_of(float64(), 1L))
Review comment:
```suggestion
list <- Array$create(list(), type = fixed_size_list_of(float64(), 1L))
```
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,59 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_empty_typed <- concat_arrays(type = int64())
+ expect_true(concat_empty_typed$type == int64())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_int <- concat_arrays(Array$create(1:3), Array$create(4:5))
+ expect_true(concat_int$type == int32())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ concat_int64 <- concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64()),
+ type = int64()
+ )
+ expect_true(concat_int64$type == int64())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ expect_error(
+ concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64())
+ ),
+ "must be identically typed"
+ )
+})
+
+test_that("c() works for Array", {
Review comment:
Could? Should? you also test for things like `c(Array$create(1L), 2L)` I
imagine that would error, but does it with an informative message?
##########
File path: r/R/array.R
##########
@@ -216,6 +216,76 @@ Array$create <- function(x, type = NULL) {
#' @include arrowExports.R
Array$import_from_c <- ImportArray
+
+#' Concatenate zero or more Arrays
+#'
+#' @param ... zero or more [Array] objects to concatenate
+#' @param type An optional `type` describing the desired
+#' type for the final Array.
+#'
+#' @return An [Array]
+#' @export
+#'
+#' @examples
+#' concat_arrays(Array$create(1:3), Array$create(4:5))
+#'
+concat_arrays <- function(..., type = NULL) {
+ dots <- lapply(list2(...), Array$create, type = type)
+
+ if (length(dots) == 0 && is.null(type)) {
+ return(Array$create(logical(), type = null()))
+ } else if (length(dots) == 0) {
+ return(Array$create(logical(), type = null())$cast(type))
+ }
+
+ if (!is.null(type)) {
+ dots <- lapply(dots, function(array) array$cast(type))
+ }
+
+ arrow__Concatenate(dots)
+}
+
+# The c() method uses non-standard dispatch in R
+# and has some peculiarities when multiple types are passed to ....
+# However, with a method defined for all subclasses of Array, it will
+# do what a user expects most of the time.
Review comment:
Out of (morbid?) curiosity: what does it do without the subclasses?
##########
File path: r/R/array.R
##########
@@ -216,6 +216,76 @@ Array$create <- function(x, type = NULL) {
#' @include arrowExports.R
Array$import_from_c <- ImportArray
+
+#' Concatenate zero or more Arrays
+#'
+#' @param ... zero or more [Array] objects to concatenate
+#' @param type An optional `type` describing the desired
+#' type for the final Array.
+#'
+#' @return An [Array]
+#' @export
+#'
+#' @examples
+#' concat_arrays(Array$create(1:3), Array$create(4:5))
+#'
+concat_arrays <- function(..., type = NULL) {
+ dots <- lapply(list2(...), Array$create, type = type)
+
+ if (length(dots) == 0 && is.null(type)) {
+ return(Array$create(logical(), type = null()))
+ } else if (length(dots) == 0) {
+ return(Array$create(logical(), type = null())$cast(type))
+ }
+
+ if (!is.null(type)) {
+ dots <- lapply(dots, function(array) array$cast(type))
+ }
+
+ arrow__Concatenate(dots)
+}
+
+# The c() method uses non-standard dispatch in R
+# and has some peculiarities when multiple types are passed to ....
+# However, with a method defined for all subclasses of Array, it will
+# do what a user expects most of the time.
Review comment:
_nods_ hmmmm yeah that is madness! And if we do define those extra
methods that were here before, does this madness go away? (e.g. if we do `c(
wk::xy(1:2, 1:2), Array$create(1:3))` what happens?)
If we still get weird errors there, I wonder if exposing `c()` is all that
helpful before we do it the "right way"?
We'll export our own concatenate method — so it's possible to use it, but I
suspect that `c()` working only when the first element is an array will be a
less-than-fun experience (with not too much upside anyway!).
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,65 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_empty_typed <- concat_arrays(type = int64())
+ expect_true(concat_empty_typed$type == int64())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_int <- concat_arrays(Array$create(1:3), Array$create(4:5))
+ expect_true(concat_int$type == int32())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ concat_int64 <- concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64()),
+ type = int64()
+ )
+ expect_true(concat_int64$type == int64())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ expect_error(
+ concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64())
+ ),
+ "must be identically typed"
+ )
+})
+
+test_that("concat_arrays() coerces its input to Array", {
+ concat_ints <- concat_arrays(1L, 2L)
+ expect_true(concat_ints$type == int32())
+ expect_true(all(concat_ints == Array$create(c(1L, 2L))))
Review comment:
Could we also add some non-sensical casts here too? (or at least what I
would assume to be nonsensical!
`concat_arrays(Array$create(1L), "foo")` or the like?
##########
File path: r/R/array.R
##########
@@ -216,6 +216,76 @@ Array$create <- function(x, type = NULL) {
#' @include arrowExports.R
Array$import_from_c <- ImportArray
+
+#' Concatenate zero or more Arrays
+#'
+#' @param ... zero or more [Array] objects to concatenate
+#' @param type An optional `type` describing the desired
+#' type for the final Array.
+#'
+#' @return An [Array]
+#' @export
+#'
+#' @examples
+#' concat_arrays(Array$create(1:3), Array$create(4:5))
+#'
+concat_arrays <- function(..., type = NULL) {
+ dots <- lapply(list2(...), Array$create, type = type)
+
+ if (length(dots) == 0 && is.null(type)) {
+ return(Array$create(logical(), type = null()))
+ } else if (length(dots) == 0) {
+ return(Array$create(logical(), type = null())$cast(type))
+ }
+
+ if (!is.null(type)) {
+ dots <- lapply(dots, function(array) array$cast(type))
+ }
+
+ arrow__Concatenate(dots)
+}
+
+# The c() method uses non-standard dispatch in R
+# and has some peculiarities when multiple types are passed to ....
+# However, with a method defined for all subclasses of Array, it will
+# do what a user expects most of the time.
Review comment:
Hmm, maybe this is idiosyncratic but stuffing the arrays themselves into
a vector seems not quite as bad to me, but I can see how that's (probably) not
what people want.
Then again, even without defining them we get odd behaviors depending on the
order already anyway. This is from an arrow install that is not on this branch:
```
> c( wk::xy(1:2, 1:2), Array$create(1:3))
Error: Can't combine 'wk_rcrd' objects that do not have identical classes.
> c(Array$create(1:3), wk::xy(1:2, 1:2))
[[1]]
Array
<int32>
[
1,
2,
3
]
$x
[1] 1 2
$y
[1] 1 2
```
I'm fine keeping it, either way we should mention in our docs that `c()`
will probably be surprising
##########
File path: r/R/array.R
##########
@@ -216,6 +216,49 @@ Array$create <- function(x, type = NULL) {
#' @include arrowExports.R
Array$import_from_c <- ImportArray
+
+#' Concatenate zero or more Arrays
+#'
+#' Concatenates zero or more [Array] objects into a single
+#' array. This operation will copy its input; if you need
+#' the behavior of a single Array but don't need a
Review comment:
```suggestion
#' array. This operation will make a copy of its input; if you need
#' the behavior of a single Array but don't need a
```
this is minor, but I think makes it a tiny bit clearer that it's doing a
copying action (we might even go stronger?)
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,65 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_empty_typed <- concat_arrays(type = int64())
+ expect_true(concat_empty_typed$type == int64())
+ expect_equal(concat_empty$length(), 0L)
+
+ concat_int <- concat_arrays(Array$create(1:3), Array$create(4:5))
+ expect_true(concat_int$type == int32())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ concat_int64 <- concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64()),
+ type = int64()
+ )
+ expect_true(concat_int64$type == int64())
+ expect_true(all(concat_int == Array$create(1:5)))
+
+ expect_error(
+ concat_arrays(
+ Array$create(1:3),
+ Array$create(4:5, type = int64())
+ ),
+ "must be identically typed"
+ )
+})
+
+test_that("concat_arrays() coerces its input to Array", {
+ concat_ints <- concat_arrays(1L, 2L)
+ expect_true(concat_ints$type == int32())
+ expect_true(all(concat_ints == Array$create(c(1L, 2L))))
Review comment:
🎉
##########
File path: r/tests/testthat/test-Array.R
##########
@@ -989,6 +989,75 @@ test_that("auto int64 conversion to int can be disabled
(ARROW-10093)", {
})
})
+test_that("concat_arrays works", {
+ concat_empty <- concat_arrays()
+ expect_true(concat_empty$type == null())
+ expect_equal(concat_empty$length(), 0L)
Review comment:
Thanks for testing this! It is a bit funny, but I'm sure there's some
convoluted reason to need to do this!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]