Skip to content

Commit

Permalink
Add mssql translations for two clock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ablack3 committed Aug 27, 2023
1 parent 9ff37e0 commit 77df050
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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(...)
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
13 changes: 13 additions & 0 deletions tests/testthat/test-backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ 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, 1L)), sql("DATEADD(YEAR, 1, `x`)"))
expect_equal(test_translate_sql(add_years(x, 1)), sql("DATEADD(YEAR, 1.0, `x`)"))
expect_equal(test_translate_sql(add_days(x, 1L)), sql("DATEADD(DAY, 1, `x`)"))
expect_equal(test_translate_sql(add_days(x, 1)), sql("DATEADD(DAY, 1.0, `x`)"))

expect_equal(test_translate_sql(clock::add_years(x, 1)), sql("DATEADD(YEAR, 1.0, `x`)"))
expect_equal(test_translate_sql(clock::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

0 comments on commit 77df050

Please sign in to comment.