You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2021/05/27 22:54:21 UTC
[arrow] branch master updated: ARROW-12777: [R] Convert all inputs
to Arrow objects in match_arrow and is_in
This is an automated email from the ASF dual-hosted git repository.
jonkeane pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new bf0f6aa ARROW-12777: [R] Convert all inputs to Arrow objects in match_arrow and is_in
bf0f6aa is described below
commit bf0f6aafc81e9c0d86b015607b8a637f9c4136eb
Author: Nic Crane <th...@gmail.com>
AuthorDate: Thu May 27 17:51:21 2021 -0500
ARROW-12777: [R] Convert all inputs to Arrow objects in match_arrow and is_in
Closes #10383 from thisisnic/ARROW-12777_match_arrow_is_in
Authored-by: Nic Crane <th...@gmail.com>
Signed-off-by: Jonathan Keane <jk...@gmail.com>
---
r/NAMESPACE | 4 ----
r/NEWS.md | 1 +
r/R/compute.R | 25 +++++++++++--------------
r/tests/testthat/test-compute-aggregate.R | 23 +++++++++++++++++++++++
4 files changed, 35 insertions(+), 18 deletions(-)
diff --git a/r/NAMESPACE b/r/NAMESPACE
index d38431d..1510ad8 100644
--- a/r/NAMESPACE
+++ b/r/NAMESPACE
@@ -51,14 +51,10 @@ S3method(is.infinite,ArrowDatum)
S3method(is.na,ArrowDatum)
S3method(is.na,Expression)
S3method(is.nan,ArrowDatum)
-S3method(is_in,ArrowDatum)
-S3method(is_in,default)
S3method(length,ArrowDatum)
S3method(length,ArrowTabular)
S3method(length,Scalar)
S3method(length,Schema)
-S3method(match_arrow,ArrowDatum)
-S3method(match_arrow,default)
S3method(max,ArrowDatum)
S3method(mean,ArrowDatum)
S3method(median,ArrowDatum)
diff --git a/r/NEWS.md b/r/NEWS.md
index d416aa3..931369a 100644
--- a/r/NEWS.md
+++ b/r/NEWS.md
@@ -24,6 +24,7 @@
* `arrow_info()` now includes details on the C++ build, such as compiler version
* `dplyr` queries on `Table` and `RecordBatch` now use the same expression internals as `Dataset` (via `InMemoryDataset`). Among other (mostly internal) benefits that come with this, the print method for `arrow_dplyr_query` now includes the expression and the resulting type of columns derived by `mutate()`.
* Added bindings for the remainder of C data interface: Type, Field, and RecordBatchReader (from the experimental C stream interface). These also have `reticulate::py_to_r()` and `r_to_py()` methods. Along with the addition of the `Scanner$ToRecordBatchReader()` method, you can now build up a Dataset query in R and pass the resulting stream of batches to another tool in process.
+* `match_arrow` now converts `x` into an `Array` if it is not a `Scalar`, `Array` or `ChunkedArray` and no longer dispatches `base::match()`.
# arrow 4.0.0.1
diff --git a/r/R/compute.R b/r/R/compute.R
index 20a2369..f9da04c 100644
--- a/r/R/compute.R
+++ b/r/R/compute.R
@@ -265,13 +265,11 @@ all.ArrowDatum <- function(..., na.rm = FALSE){
#' is_in(Array$create(c(4, 6, 8)), cars_tbl$cyl) # returns Array
#' is_in(ChunkedArray$create(c(4, 6), 8), cars_tbl$cyl) # returns ChunkedArray
#' @export
-match_arrow <- function(x, table, ...) UseMethod("match_arrow")
-
-#' @export
-match_arrow.default <- function(x, table, ...) match(x, table, ...)
-
-#' @export
-match_arrow.ArrowDatum <- function(x, table, ...) {
+match_arrow <- function(x, table, ...) {
+ if (!inherits(x, "ArrowDatum")) {
+ x <- Array$create(x)
+ }
+
if (!inherits(table, c("Array", "ChunkedArray"))) {
table <- Array$create(table)
}
@@ -280,13 +278,12 @@ match_arrow.ArrowDatum <- function(x, table, ...) {
#' @rdname match_arrow
#' @export
-is_in <- function(x, table, ...) UseMethod("is_in")
-
-#' @export
-is_in.default <- function(x, table, ...) x %in% table
-
-#' @export
-is_in.ArrowDatum <- function(x, table, ...) {
+is_in <- function(x, table, ...) {
+
+ if (!inherits(x, "ArrowDatum")) {
+ x <- Array$create(x)
+ }
+
if (!inherits(table, c("Array", "DictionaryArray", "ChunkedArray"))) {
table <- Array$create(table)
}
diff --git a/r/tests/testthat/test-compute-aggregate.R b/r/tests/testthat/test-compute-aggregate.R
index 1e9d21b..25bddde 100644
--- a/r/tests/testthat/test-compute-aggregate.R
+++ b/r/tests/testthat/test-compute-aggregate.R
@@ -342,6 +342,29 @@ test_that("match_arrow", {
ca <- ChunkedArray$create(c(1, 4, 3, 1, 1, 3, 4))
expect_equal(match_arrow(ca, tab), ChunkedArray$create(c(3L, 0L, 1L, 3L, 3L, 1L, 0L)))
+
+ sc <- Scalar$create(3)
+ expect_equal(match_arrow(sc, tab), Scalar$create(1L))
+
+ vec <- c(1,2)
+ expect_equal(match_arrow(vec, tab), Array$create(c(3L, 2L)))
+
+})
+
+test_that("is_in", {
+ a <- Array$create(c(9, 4, 3))
+ tab <- c(4, 3, 2, 1)
+ expect_equal(is_in(a, tab), Array$create(c(FALSE, TRUE, TRUE)))
+
+ ca <- ChunkedArray$create(c(9, 4, 3))
+ expect_equal(is_in(ca, tab), ChunkedArray$create(c(FALSE, TRUE, TRUE)))
+
+ sc <- Scalar$create(3)
+ expect_equal(is_in(sc, tab), Scalar$create(TRUE))
+
+ vec <- c(1,9)
+ expect_equal(is_in(vec, tab), Array$create(c(TRUE, FALSE)))
+
})
test_that("value_counts", {