Skip to content

Commit 8b0ebb9

Browse files
amoebathisisnic
andauthored
GH-44953: [R] Add R bindings for new compute functions (#44971)
### Rationale for this change Adds R bindings to newly added hyperbolic trig and Expm1 compute functions. See #44953. Bindings prefer calling `_checked` variants of compute functions where applicable which follows how existing bindings have been implemented. ### What changes are included in this PR? - [x] R bindings for hyperbolic trig compute functions - [x] R bindings for Expm1(Exponent) compute function - [x] Adds missing dplyr binding for `base::atan` - [x] Adds missing documentation for atan (not directly related to this PR) - [x] Updates to NEWS.md ### Are these changes tested? Yes. ### Are there any user-facing changes? New compute bindings in R. * GitHub Issue: #44953 Lead-authored-by: Bryce Mecum <[email protected]> Co-authored-by: Nic Crane <[email protected]> Signed-off-by: Nic Crane <[email protected]>
1 parent aea0a8c commit 8b0ebb9

9 files changed

Lines changed: 182 additions & 59 deletions

File tree

‎r/NEWS.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
# arrow 20.0.0.9000
2121

22+
## Minor improvements and fixes
23+
24+
- Added bindings for atan, sinh, cosh, tanh, asinh, acosh, and tanh, and expm1 (#44953)
25+
2226
# arrow 20.0.0
2327

2428
# arrow 19.0.1.1

‎r/R/arrow-datum.R‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits = 0) {
128128
log10 = eval_array_expression("log10_checked", x),
129129
log2 = eval_array_expression("log2_checked", x),
130130
log1p = eval_array_expression("log1p_checked", x),
131+
expm1 = eval_array_expression("expm1", x),
131132
round = eval_array_expression(
132133
"round",
133134
x,
@@ -139,17 +140,16 @@ Math.ArrowDatum <- function(x, ..., base = exp(1), digits = 0) {
139140
cumprod = eval_array_expression("cumulative_prod_checked", x),
140141
cummax = eval_array_expression("cumulative_max", x),
141142
cummin = eval_array_expression("cumulative_min", x),
143+
cosh = eval_array_expression("cosh", x),
144+
sinh = eval_array_expression("sinh", x),
145+
tanh = eval_array_expression("tanh", x),
146+
acosh = eval_array_expression("acosh_checked", x),
147+
asinh = eval_array_expression("asinh", x),
148+
atanh = eval_array_expression("atanh_checked", x),
142149
signif = ,
143-
expm1 = ,
144150
cospi = ,
145151
sinpi = ,
146152
tanpi = ,
147-
cosh = ,
148-
sinh = ,
149-
tanh = ,
150-
acosh = ,
151-
asinh = ,
152-
atanh = ,
153153
lgamma = ,
154154
gamma = ,
155155
digamma = ,

‎r/R/dplyr-funcs-doc.R‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#'
2222
#' The `arrow` package contains methods for 37 `dplyr` table functions, many of
2323
#' which are "verbs" that do transformations to one or more tables.
24-
#' The package also has mappings of 212 R functions to the corresponding
24+
#' The package also has mappings of 221 R functions to the corresponding
2525
#' functions in the Arrow compute library. These allow you to write code inside
2626
#' of `dplyr` methods that call R functions, including many in packages like
2727
#' `stringr` and `lubridate`, and they will get translated to Arrow and run
@@ -42,7 +42,7 @@
4242
#' * [`collect()`][dplyr::collect()]
4343
#' * [`compute()`][dplyr::compute()]
4444
#' * [`count()`][dplyr::count()]
45-
#' * [`distinct()`][dplyr::distinct()]: `.keep_all = TRUE` not supported
45+
#' * [`distinct()`][dplyr::distinct()]: `.keep_all = TRUE` returns a non-missing value if present, only returning missing values if all are missing.
4646
#' * [`explain()`][dplyr::explain()]
4747
#' * [`filter()`][dplyr::filter()]
4848
#' * [`full_join()`][dplyr::full_join()]: the `copy` argument is ignored
@@ -83,7 +83,7 @@
8383
#' Functions can be called either as `pkg::fun()` or just `fun()`, i.e. both
8484
#' `str_sub()` and `stringr::str_sub()` work.
8585
#'
86-
#' In addition to these functions, you can call any of Arrow's 262 compute
86+
#' In addition to these functions, you can call any of Arrow's 271 compute
8787
#' functions directly. Arrow has many functions that don't map to an existing R
8888
#' function. In other cases where there is an R function mapping, you can still
8989
#' call the Arrow function directly if you don't want the adaptations that the R
@@ -96,6 +96,7 @@
9696
#'
9797
#' * [`add_filename()`][arrow::add_filename()]
9898
#' * [`cast()`][arrow::cast()]
99+
#' * [`one()`][arrow::one()]
99100
#'
100101
#' ## base
101102
#'
@@ -119,6 +120,7 @@
119120
#' * [`^`][^()]
120121
#' * [`abs()`][base::abs()]
121122
#' * [`acos()`][base::acos()]
123+
#' * [`acosh()`][base::acosh()]
122124
#' * [`all()`][base::all()]
123125
#' * [`any()`][base::any()]
124126
#' * [`as.Date()`][base::as.Date()]: Multiple `tryFormats` not supported in Arrow.
@@ -130,14 +132,19 @@
130132
#' * [`as.logical()`][base::as.logical()]
131133
#' * [`as.numeric()`][base::as.numeric()]
132134
#' * [`asin()`][base::asin()]
135+
#' * [`asinh()`][base::asinh()]
136+
#' * [`atan()`][base::atan()]
137+
#' * [`atanh()`][base::atanh()]
133138
#' * [`ceiling()`][base::ceiling()]
134139
#' * [`cos()`][base::cos()]
140+
#' * [`cosh()`][base::cosh()]
135141
#' * [`data.frame()`][base::data.frame()]: `row.names` and `check.rows` arguments not supported;
136142
#' `stringsAsFactors` must be `FALSE`
137143
#' * [`difftime()`][base::difftime()]: only supports `units = "secs"` (the default);
138144
#' `tz` argument not supported
139145
#' * [`endsWith()`][base::endsWith()]
140146
#' * [`exp()`][base::exp()]
147+
#' * [`expm1()`][base::expm1()]
141148
#' * [`floor()`][base::floor()]
142149
#' * [`format()`][base::format()]
143150
#' * [`grepl()`][base::grepl()]
@@ -171,6 +178,7 @@
171178
#' * [`round()`][base::round()]
172179
#' * [`sign()`][base::sign()]
173180
#' * [`sin()`][base::sin()]
181+
#' * [`sinh()`][base::sinh()]
174182
#' * [`sqrt()`][base::sqrt()]
175183
#' * [`startsWith()`][base::startsWith()]
176184
#' * [`strftime()`][base::strftime()]
@@ -183,6 +191,7 @@
183191
#' * [`substring()`][base::substring()]
184192
#' * [`sum()`][base::sum()]
185193
#' * [`tan()`][base::tan()]
194+
#' * [`tanh()`][base::tanh()]
186195
#' * [`tolower()`][base::tolower()]
187196
#' * [`toupper()`][base::toupper()]
188197
#' * [`trunc()`][base::trunc()]

‎r/R/dplyr-funcs-simple.R‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,22 @@
3232
"base::log1p" = "log1p_checked",
3333
"base::log2" = "log2_checked",
3434
"base::sign" = "sign",
35+
"base::expm1" = "expm1",
3536
# trunc is defined in dplyr-functions.R
3637

3738
# trigonometric functions
3839
"base::acos" = "acos_checked",
3940
"base::asin" = "asin_checked",
4041
"base::cos" = "cos_checked",
42+
"base::atan" = "atan",
4143
"base::sin" = "sin_checked",
4244
"base::tan" = "tan_checked",
45+
"base::cosh" = "cosh",
46+
"base::sinh" = "sinh",
47+
"base::tanh" = "tanh",
48+
"base::acosh" = "acosh_checked",
49+
"base::asinh" = "asinh",
50+
"base::atanh" = "atanh_checked",
4351

4452
# logical functions
4553
"!" = "invert",

‎r/extra-tests/helpers.R‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎r/extra-tests/test-read-files.R‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
library(arrow)
1919
library(testthat)
2020

21+
source("tests/testthat/helper-skip.R")
22+
2123
pq_file <- "files/ex_data.parquet"
2224

2325
test_that("Can read the file (parquet)", {
@@ -30,7 +32,7 @@ test_that("Can read the file (parquet)", {
3032

3133
### Parquet
3234
test_that("Can see the metadata (parquet)", {
33-
skip_if_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
35+
skip_if_arrow_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
3436

3537
df <- read_parquet(pq_file)
3638
expect_s3_class(df, "tbl")
@@ -74,7 +76,7 @@ for (comp in c("lz4", "uncompressed", "zstd")) {
7476
})
7577

7678
test_that(paste0("Can see the metadata (feather ", comp, ")"), {
77-
skip_if_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
79+
skip_if_arrow_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
7880

7981
df <- read_feather(feather_file)
8082
expect_s3_class(df, "tbl")
@@ -132,7 +134,7 @@ test_that("Can read the file (parquet)", {
132134
})
133135

134136
test_that("Can see the metadata (stream)", {
135-
skip_if_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
137+
skip_if_arrow_version_less_than("2.0.0", "Version 1.0.1 can't read new version metadata.")
136138
df <- read_ipc_stream(stream_file)
137139

138140
expect_s3_class(df, "tbl")

‎r/tests/testthat/helper-skip.R‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,22 @@ skip_on_python_older_than <- function(python_version) {
130130
}
131131
}
132132

133+
skip_if_arrow_version_less_than <- function(version, msg) {
134+
if (arrow_cpp_version() < numeric_version(version)) {
135+
skip(msg)
136+
}
137+
}
138+
139+
skip_if_arrow_version_equals <- function(version, msg) {
140+
if (arrow_cpp_version() == numeric_version(version)) {
141+
skip(msg)
142+
}
143+
}
144+
145+
arrow_cpp_version <- function() {
146+
numeric_version(gsub("-SNAPSHOT", "", arrow::arrow_info()$build_info["cpp_version"]))
147+
}
148+
133149
process_is_running <- function(x) {
134150
if (force_tests()) {
135151
# Return TRUE as this is used as a condition in an if statement

‎r/tests/testthat/test-compute-arith.R‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,32 @@ test_that("Math group generics work on Array objects", {
223223
)
224224

225225
expect_error(signif(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
226-
expect_error(expm1(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
227226

228227
expect_error(cospi(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
229228
expect_error(sinpi(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
230229
expect_error(tanpi(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
231230

232-
expect_error(cosh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
233-
expect_error(sinh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
234-
expect_error(tanh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
235-
236-
expect_error(acosh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
237-
expect_error(asinh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
238-
expect_error(atanh(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
239-
240231
expect_error(lgamma(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
241232
expect_error(gamma(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
242233
expect_error(digamma(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
243234
expect_error(trigamma(Array$create(c(4L, 1L))), "Unsupported operation on `Array`")
244235
})
236+
237+
test_that("hyperbolic trig functions work on Array objects", {
238+
skip_if_arrow_version_less_than("18.1.0.9000", "Hyperbolic trig functions not available until version 19.")
239+
240+
expect_equal(sinh(Array$create(c(0.6, 0.9))), Array$create(sinh(c(0.6, 0.9))))
241+
expect_equal(cosh(Array$create(c(0.6, 0.9))), Array$create(cosh(c(0.6, 0.9))))
242+
expect_equal(tanh(Array$create(c(0.6, 0.9))), Array$create(tanh(c(0.6, 0.9))))
243+
expect_equal(asinh(Array$create(c(0.6, 0.9))), Array$create(asinh(c(0.6, 0.9))))
244+
expect_error(acosh(Array$create(c(0.6, 0.9))), "Invalid: domain error")
245+
expect_equal(acosh(Array$create(c(1, 2))), Array$create(acosh(c(1, 2))))
246+
expect_error(atanh(Array$create(c(-1, 1))), "Invalid: domain error")
247+
expect_equal(atanh(Array$create(c(0.6, 0.9))), Array$create(atanh(c(0.6, 0.9))))
248+
})
249+
250+
test_that("expm1 works on Array objects", {
251+
skip_if_arrow_version_less_than("18.1.0.9000", "expm1 not available until version 19.")
252+
253+
expect_equal(expm1(Array$create(c(0.00000001, 10))), Array$create(expm1(c(0.00000001, 10))))
254+
})

0 commit comments

Comments
 (0)