From 0980c42b5a70741e05868b2df6fe32c8dd06d090 Mon Sep 17 00:00:00 2001 From: "Christopher D. Higgins" <40569964+higgicd@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:11:56 -0400 Subject: [PATCH 1/3] ARROW-16871: [R] Implement exp() and sqrt() in Arrow dplyr queries implement sqrt and exp dplyr bindings --- r/R/arrow-datum.R | 2 +- r/R/dplyr-funcs-math.R | 17 +++++++++++++++++ r/tests/testthat/test-dplyr-funcs-math.R | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/r/R/arrow-datum.R b/r/R/arrow-datum.R index 4ec5f8f9d65..39362628bb4 100644 --- a/r/R/arrow-datum.R +++ b/r/R/arrow-datum.R @@ -123,7 +123,7 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits = 0) { x, options = list(ndigits = digits, round_mode = RoundMode$HALF_TO_EVEN) ), - sqrt = eval_array_expression("power_checked", x, 0.5), + sqrt = eval_array_expression("sqrt_checked", x), exp = eval_array_expression("power_checked", exp(1), x), signif = , expm1 = , diff --git a/r/R/dplyr-funcs-math.R b/r/R/dplyr-funcs-math.R index b92c202d048..b324d4bd388 100644 --- a/r/R/dplyr-funcs-math.R +++ b/r/R/dplyr-funcs-math.R @@ -80,4 +80,21 @@ register_bindings_math <- function() { options = list(ndigits = digits, round_mode = RoundMode$HALF_TO_EVEN) ) }) + + register_binding("sqrt", function(x, ...) { + # accepts and ignores ... for consistency with base::trunc() + build_expr( + "sqrt_checked", + x + ) + }) + + register_binding("exp", function(x, ...) { + # accepts and ignores ... for consistency with base::trunc() + build_expr( + "power_checked", + exp(1), + x + ) + }) } diff --git a/r/tests/testthat/test-dplyr-funcs-math.R b/r/tests/testthat/test-dplyr-funcs-math.R index dd982c99428..5bee20983b6 100644 --- a/r/tests/testthat/test-dplyr-funcs-math.R +++ b/r/tests/testthat/test-dplyr-funcs-math.R @@ -330,3 +330,25 @@ test_that("floor division maintains type consistency with R", { df ) }) + +test_that("exp()", { + df <- tibble(x = c(1:5, NA)) + + compare_dplyr_binding( + .input %>% + mutate(y = exp(x)) %>% + collect(), + df + ) +}) + +test_that("sqrt()", { + df <- tibble(x = c(1:5, NA)) + + compare_dplyr_binding( + .input %>% + mutate(y = sqrt(x)) %>% + collect(), + df + ) +}) From 6d99e86d6ed3e09ba7cc72047f57dea1082133f9 Mon Sep 17 00:00:00 2001 From: "Christopher D. Higgins" <40569964+higgicd@users.noreply.github.com> Date: Tue, 5 Jul 2022 14:35:02 -0400 Subject: [PATCH 2/3] ARROW-16871: [R] Implement exp() and sqrt() in Arrow dplyr queries remove `...` from function signatures --- r/R/dplyr-funcs-math.R | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/r/R/dplyr-funcs-math.R b/r/R/dplyr-funcs-math.R index b324d4bd388..17d835c81fc 100644 --- a/r/R/dplyr-funcs-math.R +++ b/r/R/dplyr-funcs-math.R @@ -81,16 +81,14 @@ register_bindings_math <- function() { ) }) - register_binding("sqrt", function(x, ...) { - # accepts and ignores ... for consistency with base::trunc() + register_binding("sqrt", function(x) { build_expr( "sqrt_checked", x ) }) - register_binding("exp", function(x, ...) { - # accepts and ignores ... for consistency with base::trunc() + register_binding("exp", function(x) { build_expr( "power_checked", exp(1), From c210871d95ae942f299f7e4587de38b4af2963c9 Mon Sep 17 00:00:00 2001 From: "Christopher D. Higgins" <40569964+higgicd@users.noreply.github.com> Date: Tue, 5 Jul 2022 15:19:57 -0400 Subject: [PATCH 3/3] address trailing whitespace warnings clean up code to address trailing whitespace warnings generated by `lintr` --- r/R/dplyr-funcs-math.R | 6 +++--- r/tests/testthat/test-dplyr-funcs-math.R | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/r/R/dplyr-funcs-math.R b/r/R/dplyr-funcs-math.R index 17d835c81fc..0ba2ddc856e 100644 --- a/r/R/dplyr-funcs-math.R +++ b/r/R/dplyr-funcs-math.R @@ -80,18 +80,18 @@ register_bindings_math <- function() { options = list(ndigits = digits, round_mode = RoundMode$HALF_TO_EVEN) ) }) - + register_binding("sqrt", function(x) { build_expr( "sqrt_checked", x ) }) - + register_binding("exp", function(x) { build_expr( "power_checked", - exp(1), + exp(1), x ) }) diff --git a/r/tests/testthat/test-dplyr-funcs-math.R b/r/tests/testthat/test-dplyr-funcs-math.R index 5bee20983b6..47a9f0b7c02 100644 --- a/r/tests/testthat/test-dplyr-funcs-math.R +++ b/r/tests/testthat/test-dplyr-funcs-math.R @@ -333,7 +333,7 @@ test_that("floor division maintains type consistency with R", { test_that("exp()", { df <- tibble(x = c(1:5, NA)) - + compare_dplyr_binding( .input %>% mutate(y = exp(x)) %>% @@ -344,7 +344,7 @@ test_that("exp()", { test_that("sqrt()", { df <- tibble(x = c(1:5, NA)) - + compare_dplyr_binding( .input %>% mutate(y = sqrt(x)) %>%