Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions r/R/dplyr-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,22 @@ nse_funcs$wday <- function(x, label = FALSE, abbr = TRUE, week_start = getOption
Expression$create("day_of_week", x, options = list(one_based_numbering = TRUE, week_start = week_start))

}

nse_funcs$log <- function(x, base = exp(1)) {

if (base == exp(1)) {
return(Expression$create("ln_checked", x))
}

if (base == 2) {
return(Expression$create("log2_checked", x))
}

if (base == 10) {
return(Expression$create("log10_checked", x))
}
# ARROW-13345
stop("`base` values other than exp(1), 2 and 10 not supported in Arrow", call. = FALSE)
}

nse_funcs$logb <- nse_funcs$log
10 changes: 9 additions & 1 deletion r/R/expression.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@
"yday" = "day_of_year",
"hour" = "hour",
# second is defined in dplyr-functions.R
"minute" = "minute"
"minute" = "minute",
"log10" = "log10_checked",
"log2" = "log2_checked",
"log1p" = "log1p_checked",
"sin" = "sin_checked",
"cos" = "cos_checked",
"tan" = "tan_checked",
"asin" = "asin_checked",
"acos" = "acos_checked"
)

.binary_function_map <- list(
Expand Down
6 changes: 3 additions & 3 deletions r/tests/testthat/helper-expectation.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ expect_dplyr_equal <- function(expr,
}

if (!is.null(skip_msg)) {
skip(paste(skip_msg, collpase = "\n"))
skip(paste(skip_msg, collapse = "\n"))
}
}

Expand Down Expand Up @@ -212,7 +212,7 @@ expect_vector_equal <- function(expr, # A vectorized R expression containing `in
}

if (!is.null(skip_msg)) {
skip(paste(skip_msg, collpase = "\n"))
skip(paste(skip_msg, collapse = "\n"))
}
}

Expand Down Expand Up @@ -273,7 +273,7 @@ expect_vector_error <- function(expr, # A vectorized R expression containing `in
}

if (!is.null(skip_msg)) {
skip(paste(skip_msg, collpase = "\n"))
skip(paste(skip_msg, collapse = "\n"))
}
}

Expand Down
109 changes: 109 additions & 0 deletions r/tests/testthat/test-dplyr.R
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,112 @@ test_that("abs()", {
df
)
})

test_that("log functions", {

df <- tibble(x = c(1:10, NA, NA))

expect_dplyr_equal(
input %>%
mutate(y = log(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log(x, base = exp(1))) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log(x, base = 2)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log(x, base = 10)) %>%
collect(),
df
)

expect_error(
nse_funcs$log(Expression$scalar(x), base = 5),
"`base` values other than exp(1), 2 and 10 not supported in Arrow",
fixed = TRUE
)

expect_dplyr_equal(
input %>%
mutate(y = logb(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log1p(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log2(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = log10(x)) %>%
collect(),
df
)

})

test_that("trig functions", {

df <- tibble(x = c(seq(from = 0, to = 1, by = 0.1), NA))

expect_dplyr_equal(
input %>%
mutate(y = sin(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = cos(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = tan(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = asin(x)) %>%
collect(),
df
)

expect_dplyr_equal(
input %>%
mutate(y = acos(x)) %>%
collect(),
df
)

})