Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add translations for base::difftime(), and clock functions add_days(), add_years(), date_build(), get_year(), get_month(), get_day() #1357

Merged
merged 16 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions R/backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ simulate_mssql <- function(version = "15.0") {
sql_expr(DATEPART(QUARTER, !!x))
}
},

# clock ---------------------------------------------------------------
add_days = function(x, n, ...) {
rlang::check_dots_empty(...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can omit the rlang namespace. The rlang package is so commonly used that it is usually imported.
And you either do check_dots_empty() or check_dots_empty0(...) but you must not pass ... to check_dots_empty().

Suggested change
rlang::check_dots_empty(...)
check_dots_empty()

sql_expr(DATEADD(DAY, !!n, !!x))
},
add_years = function(x, n, ...) {
rlang::check_dots_empty(...)
sql_expr(DATEADD(YEAR, !!n, !!x))
}
)

if (mssql_version(con) >= "11.0") { # MSSQL 2012
Expand Down
2 changes: 1 addition & 1 deletion R/tidyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ partial_eval_sym <- function(sym, data, env) {
}

is_namespaced_dplyr_call <- function(call) {
packages <- c("dplyr", "stringr", "lubridate")
packages <- c("dplyr", "stringr", "lubridate", "clock")
is_symbol(call[[1]], "::") && is_symbol(call[[2]], packages)
}

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ test_that("custom lubridate functions translated correctly", {
expect_error(test_translate_sql(quarter(x, fiscal_start = 5)))
})

test_that("custom clock functions translated correctly", {
local_con(simulate_mssql())
expect_equal(test_translate_sql(add_years(x, 1)), sql("DATEADD(YEAR, 1.0, `x`)"))
expect_equal(test_translate_sql(add_days(x, 1)), sql("DATEADD(DAY, 1.0, `x`)"))
expect_error(test_translate_sql(add_days(x, 1, "dots", "must", "be empty")))
})

test_that("last_value_sql() translated correctly", {
con <- simulate_mssql()
expect_equal(
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-tidyeval.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ test_that("namespaced calls to dplyr functions are stripped", {
# hack to avoid check complaining about not declared imports
expect_equal(partial_eval(rlang::parse_expr("stringr::str_to_lower(x)"), lf), expr(str_to_lower(x)))
expect_equal(partial_eval(rlang::parse_expr("lubridate::today()"), lf), expr(today()))
expect_equal(partial_eval(rlang::parse_expr("clock::add_years(x, 1)"), lf), expr(add_years(x, 1)))
})

test_that("use quosure environment for unevaluted formulas", {
Expand Down
Loading