diff --git a/R/relational-duckdb.R b/R/relational-duckdb.R index fb857efe..5c18bb9d 100644 --- a/R/relational-duckdb.R +++ b/R/relational-duckdb.R @@ -27,8 +27,8 @@ duckplyr_macros <- c( "is.na" = "(x) AS (x IS NULL)", "n" = "() AS CAST(COUNT(*) AS int32)", # - "log10" = "(x) AS log(x)", - "log" = "(x) AS ln(x)", + "log10" = "(x) AS CASE WHEN x < 0 THEN CAST('NaN' AS double) WHEN x = 0 THEN CAST('-Inf' AS double) ELSE log(x) END", + "log" = "(x) AS CASE WHEN x < 0 THEN CAST('NaN' AS double) WHEN x = 0 THEN CAST('-Inf' AS double) ELSE ln(x) END", # TPCH # https://github.com/duckdb/duckdb/discussions/8599 diff --git a/tests/testthat/test-as_duckplyr_df.R b/tests/testthat/test-as_duckplyr_df.R index 9379ed5b..68d285eb 100644 --- a/tests/testthat/test-as_duckplyr_df.R +++ b/tests/testthat/test-as_duckplyr_df.R @@ -1304,6 +1304,34 @@ test_that("as_duckplyr_df() and mutate(c = 0, d = 0, e = c / d)", { expect_equal(pre, post) }) + +test_that("as_duckplyr_df() and mutate(c = 0, d = -1, e = log(c), f = log(d))", { + # Data + test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3)) + + # Run + pre <- test_df %>% as_duckplyr_df() %>% mutate(c = 0, d = -1, e = log(c), f = log(d)) + # FIXME: This is overwritten by autogeneration but necessary, find better solution + post <- test_df %>% mutate(c = 0, d = -1, e = log(c), f = suppressWarnings(log(d))) %>% as_duckplyr_df() + + # Compare + expect_equal(pre, post) +}) + + +test_that("as_duckplyr_df() and mutate(c = 0, d = -1, e = log(c), f = log10(d))", { + # Data + test_df <- data.frame(a = 1:6 + 0, b = 2, g = rep(1:3, 1:3)) + + # Run + pre <- test_df %>% as_duckplyr_df() %>% mutate(c = 0, d = -1, e = log(c), f = log10(d)) + # FIXME: This is overwritten by autogeneration but necessary, find better solution + post <- test_df %>% mutate(c = 0, d = -1, e = log(c), f = suppressWarnings(log10(d))) %>% as_duckplyr_df() + + # Compare + expect_equal(pre, post) +}) + test_that("as_duckplyr_df() and nest_by()", { withr::local_envvar(DUCKPLYR_FORCE = "FALSE") diff --git a/tools/00-funs.R b/tools/00-funs.R index 57104d15..1d138b38 100644 --- a/tools/00-funs.R +++ b/tools/00-funs.R @@ -601,6 +601,10 @@ test_extra_arg_map <- list( # Division by zero "c = 0, d = 0, e = c / d", + # Negative log + "c = 0, d = -1, e = log(c), f = log(d)", + "c = 0, d = -1, e = log(c), f = log10(d)", + NULL ), nest_join = "join_by(a)",