This is an automated email from the ASF dual-hosted git repository.

jonkeane pushed a commit to branch maint-17.0.0-r
in repository https://gitbox.apache.org/repos/asf/arrow.git

commit bcb094d9c157d78d2715a39d2b8c7fc4cfdea003
Author: Patrick Aboyoun <[email protected]>
AuthorDate: Sun Jul 7 09:59:14 2024 -0700

    GH-43163: [R] Fix bindings in Math group generics (#43162)
    
    
    
    ### Rationale for this change
    
    When support was added for `cumsum` in the `Math` group generics it 
mistakenly mapped `signif`, `expm1`, `log1p`, `cospi`, `sinpi`, `tanpi`, 
`cosh`, `sinh`, `tanh`, `acosh`, `asinh`, `atanh`, `lgamma`, `gamma`, 
`digamma`, and `trigamma` to the `cumulative_sum_checked` arrow function. This 
PR corrects that mistake and well as adds support for `log2`, `log1p`, 
`cumprod`, `cummax`, and `cummin`.
    
    ### What changes are included in this PR?
    
    It contains the following changes:
    1. `acos`, `asin`, `cos`, `sin`, `tan` now map to the `*_checked` arrow 
function variants
    2. `log2` maps to the `log2_checked` arrow function
    3. `log1p` maps to the `log1p_checked` arrow function
    4. `cumprod` maps to the `cumulative_prod_checked` arrow function
    5. `cummax` maps to the `cumulative_max` arrow function
    6. `cummin` maps to the `cumulative_min` arrow function
    7. `signif`, `expm1`, `cospi`, `sinpi`, `tanpi`, `cosh`, `sinh`, `tanh`, 
`acosh`, `asinh`, `atanh`, `lgamma`, `gamma`, `digamma`, and `trigamma` 
properly throw an unsupported operation error
    
    ### Are these changes tested?
    
    Yes, tests were added to "Math group generics work on Array objects" in 
`arrow/r/tests/testthat/test-compute-arith.R`
    
    ### Are there any user-facing changes?
    
    No
    
    * GitHub Issue: #43163
    
    Authored-by: Patrick Aboyoun <[email protected]>
    Signed-off-by: Jonathan Keane <[email protected]>
---
 r/R/arrow-datum.R                     | 31 +++++++++----------
 r/tests/testthat/test-compute-arith.R | 57 +++++++++++++++++++++++++++++++++--
 2 files changed, 69 insertions(+), 19 deletions(-)

diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R
index 4770b03b9c..ba513ef470 100644
--- a/r/R/arrow-datum.R
+++ b/r/R/arrow-datum.R
@@ -115,19 +115,19 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits 
= 0) {
   switch(.Generic,
     abs = eval_array_expression("abs_checked", x),
     ceiling = eval_array_expression("ceil", x),
-    sign = ,
-    floor = ,
-    trunc = ,
-    acos = ,
-    asin = ,
-    atan = ,
-    cos = ,
-    sin = ,
-    tan = {
-      eval_array_expression(.Generic, x)
-    },
+    sign = eval_array_expression("sign", x),
+    floor = eval_array_expression("floor", x),
+    trunc = eval_array_expression("trunc", x),
+    acos = eval_array_expression("acos_checked", x),
+    asin = eval_array_expression("asin_checked", x),
+    atan = eval_array_expression("atan", x),
+    cos = eval_array_expression("cos_checked", x),
+    sin = eval_array_expression("sin_checked", x),
+    tan = eval_array_expression("tan_checked", x),
     log = eval_array_expression("logb_checked", x, base),
     log10 = eval_array_expression("log10_checked", x),
+    log2 = eval_array_expression("log2_checked", x),
+    log1p = eval_array_expression("log1p_checked", x),
     round = eval_array_expression(
       "round",
       x,
@@ -135,9 +135,12 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits 
= 0) {
     ),
     sqrt = eval_array_expression("sqrt_checked", x),
     exp = eval_array_expression("power_checked", exp(1), x),
+    cumsum = eval_array_expression("cumulative_sum_checked", x),
+    cumprod = eval_array_expression("cumulative_prod_checked", x),
+    cummax = eval_array_expression("cumulative_max", x),
+    cummin = eval_array_expression("cumulative_min", x),
     signif = ,
     expm1 = ,
-    log1p = ,
     cospi = ,
     sinpi = ,
     tanpi = ,
@@ -151,10 +154,6 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits 
= 0) {
     gamma = ,
     digamma = ,
     trigamma = ,
-    cumsum = eval_array_expression("cumulative_sum_checked", x),
-    cumprod = ,
-    cummax = ,
-    cummin = ,
     stop(paste0("Unsupported operation on `", class(x)[1L], "` : "), .Generic, 
call. = FALSE)
   )
 }
diff --git a/r/tests/testthat/test-compute-arith.R 
b/r/tests/testthat/test-compute-arith.R
index 5cffafe41e..bbdcb10a6b 100644
--- a/r/tests/testthat/test-compute-arith.R
+++ b/r/tests/testthat/test-compute-arith.R
@@ -162,6 +162,8 @@ test_that("Math group generics work on Array objects", {
     Array$create(log(c(0.6, 2.1), base = 2))
   )
   expect_equal(log10(Array$create(c(0.6, 2.1))), Array$create(log10(c(0.6, 
2.1))))
+  expect_equal(log2(Array$create(c(0.6, 2.1))), Array$create(log2(c(0.6, 
2.1))))
+  expect_equal(log1p(Array$create(c(0.6, 2.1, 0))), Array$create(log1p(c(0.6, 
2.1, 0))))
 
   expect_equal(round(Array$create(c(0.6, 2.1))), Array$create(c(1, 2)))
   expect_equal(
@@ -175,6 +177,7 @@ test_that("Math group generics work on Array objects", {
     round(exp(Array$create(c(2L, 1L))), digits = 10),
     Array$create(round(exp(c(2L, 1L)), 10))
   )
+
   expect_as_vector(
     cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))),
     c(2.3, 1.3, 9.2, NA_real_, NA_real_)
@@ -186,8 +189,56 @@ test_that("Math group generics work on Array objects", {
     c(2L, 9L, 17L, 16L, 18L, 35L, NA_integer_, NA_integer_, NA_integer_)
   )
 
-  expect_error(
-    cumprod(Array$create(c(4L, 1L))),
-    "Unsupported operation on `Array`"
+  expect_as_vector(
+    cumprod(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))),
+    c(2.3, -2.3, -18.17, NA_real_, NA_real_)
+  )
+  expect_equal(cumprod(Array$create(-10L)), Array$create(-10L))
+  expect_equal(cumprod(Array$create(NA_integer_)), Array$create(NA_integer_))
+  expect_as_vector(
+    cumprod(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 
3L), 18L)),
+    c(2L, 14L, 112L, -112L, -224L, -3808L, NA_integer_, NA_integer_, 
NA_integer_)
+  )
+
+  expect_as_vector(
+    cummax(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))),
+    c(2.3, 2.3, 7.9, NA_real_, NA_real_)
   )
+  expect_equal(cummax(Array$create(-10L)), Array$create(-10L))
+  expect_equal(cummax(Array$create(NA_integer_)), Array$create(NA_integer_))
+  expect_as_vector(
+    cummax(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 
3L), 18L)),
+    c(2L, 7L, 8L, 8L, 8L, 17L, NA_integer_, NA_integer_, NA_integer_)
+  )
+
+  expect_as_vector(
+    cummin(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))),
+    c(2.3, -1, -1, NA_real_, NA_real_)
+  )
+  expect_equal(cummin(Array$create(-10L)), Array$create(-10L))
+  expect_equal(cummin(Array$create(NA_integer_)), Array$create(NA_integer_))
+  expect_as_vector(
+    cummin(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 
3L), 18L)),
+    c(2L, 2L, 2L, -1L, -1L, -1L, NA_integer_, NA_integer_, NA_integer_)
+  )
+
+  expect_error(signif(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(expm1(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+
+  expect_error(cospi(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(sinpi(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(tanpi(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+
+  expect_error(cosh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(sinh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(tanh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+
+  expect_error(acosh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(asinh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(atanh(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+
+  expect_error(lgamma(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(gamma(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(digamma(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
+  expect_error(trigamma(Array$create(c(4L, 1L))), "Unsupported operation on 
`Array`")
 })

Reply via email to