From 1ecc7e09b06a9c8fd95402b5fd0d75c324b92d6f Mon Sep 17 00:00:00 2001 From: arnaud-feldmann Date: Tue, 25 Apr 2023 22:29:54 +0200 Subject: [PATCH 1/4] [R] Implement cumsum for Arrays --- r/R/arrow-datum.R | 2 +- r/tests/testthat/test-compute-arith.R | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index bfebf998a1c..4770b03b9ca 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -151,7 +151,7 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits = 0) { gamma = , digamma = , trigamma = , - cumsum = , + cumsum = eval_array_expression("cumulative_sum_checked", x), cumprod = , cummax = , cummin = , diff --git a/r/tests/testthat/test-compute-arith.R b/r/tests/testthat/test-compute-arith.R index 1f3432363fe..174150a8abc 100644 --- a/r/tests/testthat/test-compute-arith.R +++ b/r/tests/testthat/test-compute-arith.R @@ -175,9 +175,17 @@ 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_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_,1.0))), digits = 10), + Array$create(c(2.3, 1.3, 9.2, NA, NA))) + expect_equal(cumsum(Array$create(-10L)), Array$create(-10L)) + expect_equal(cumsum(Array$create(NA_integer_)), Array$create(NA_integer_)) + expect_equal( + cumsum(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_,3L), 18L))$as_vector(), + c(2L, 9L, 17L, 16L, 18L, 35L, NA_integer_, NA_integer_, NA_integer_) + ) expect_error( - cumsum(Array$create(c(4L, 1L))), + cumprod(Array$create(c(4L, 1L))), "Unsupported operation on `Array`" ) }) From 1fb533dd1fce3745d08019eef5f80baadda93cc3 Mon Sep 17 00:00:00 2001 From: arnaud-feldmann Date: Wed, 26 Apr 2023 12:44:12 +0200 Subject: [PATCH 2/4] add space after comma --- r/tests/testthat/test-compute-arith.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/r/tests/testthat/test-compute-arith.R b/r/tests/testthat/test-compute-arith.R index 174150a8abc..476e30f831c 100644 --- a/r/tests/testthat/test-compute-arith.R +++ b/r/tests/testthat/test-compute-arith.R @@ -175,12 +175,12 @@ 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_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_,1.0))), digits = 10), + expect_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))), digits = 10), Array$create(c(2.3, 1.3, 9.2, NA, NA))) expect_equal(cumsum(Array$create(-10L)), Array$create(-10L)) expect_equal(cumsum(Array$create(NA_integer_)), Array$create(NA_integer_)) expect_equal( - cumsum(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_,3L), 18L))$as_vector(), + cumsum(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 3L), 18L))$as_vector(), c(2L, 9L, 17L, 16L, 18L, 35L, NA_integer_, NA_integer_, NA_integer_) ) From 295b1e6ae8f18535e17eeead23e7a5ce26537035 Mon Sep 17 00:00:00 2001 From: arnaud-feldmann Date: Wed, 26 Apr 2023 12:46:36 +0200 Subject: [PATCH 3/4] 80 characters columns --- r/tests/testthat/test-compute-arith.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/r/tests/testthat/test-compute-arith.R b/r/tests/testthat/test-compute-arith.R index 476e30f831c..0240f18b493 100644 --- a/r/tests/testthat/test-compute-arith.R +++ b/r/tests/testthat/test-compute-arith.R @@ -175,12 +175,15 @@ 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_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))), digits = 10), + expect_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))), + digits = 10), Array$create(c(2.3, 1.3, 9.2, NA, NA))) expect_equal(cumsum(Array$create(-10L)), Array$create(-10L)) expect_equal(cumsum(Array$create(NA_integer_)), Array$create(NA_integer_)) expect_equal( - cumsum(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 3L), 18L))$as_vector(), + cumsum( + ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 3L), 18L) + )$as_vector(), c(2L, 9L, 17L, 16L, 18L, 35L, NA_integer_, NA_integer_, NA_integer_) ) From c3a98322e448ab065d48ee6b982dddac4f89e178 Mon Sep 17 00:00:00 2001 From: Nic Crane Date: Wed, 26 Apr 2023 12:25:59 +0100 Subject: [PATCH 4/4] Update r/tests/testthat/test-compute-arith.R --- r/tests/testthat/test-compute-arith.R | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/r/tests/testthat/test-compute-arith.R b/r/tests/testthat/test-compute-arith.R index 0240f18b493..5cffafe41e6 100644 --- a/r/tests/testthat/test-compute-arith.R +++ b/r/tests/testthat/test-compute-arith.R @@ -175,15 +175,14 @@ 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_equal(round(cumsum(Array$create(c(2.3, -1.0, 7.9, NA_real_, 1.0))), - digits = 10), - Array$create(c(2.3, 1.3, 9.2, NA, NA))) + 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_) + ) expect_equal(cumsum(Array$create(-10L)), Array$create(-10L)) expect_equal(cumsum(Array$create(NA_integer_)), Array$create(NA_integer_)) - expect_equal( - cumsum( - ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 3L), 18L) - )$as_vector(), + expect_as_vector( + cumsum(ChunkedArray$create(c(2L, 7L, 8L), c(-1L, 2L, 17L, NA_integer_, 3L), 18L)), c(2L, 9L, 17L, 16L, 18L, 35L, NA_integer_, NA_integer_, NA_integer_) )