-
Notifications
You must be signed in to change notification settings - Fork 28
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
fix/add date-related functions #643
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -275,10 +275,10 @@ sql_translation.duckdb_connection <- function(con) { | |
|
||
# clock | ||
add_days = function(x, n, ...) { | ||
build_sql("DATE_ADD(", !!x, ", INTERVAL '", n ," day')") | ||
build_sql("DATE_ADD(", !!x, ", INTERVAL (", n ,") day)") | ||
}, | ||
add_years = function(x, n, ...) { | ||
build_sql("DATE_ADD(", !!x, ", INTERVAL '", n ," year')") | ||
build_sql("DATE_ADD(", !!x, ", INTERVAL (", n ,") year)") | ||
}, | ||
get_year = function(x) { | ||
build_sql("DATE_PART('year', ", !!x, ")") | ||
|
@@ -302,6 +302,17 @@ sql_translation.duckdb_connection <- function(con) { | |
build_sql("DATEDIFF('day', ", !!start, ", " ,!!end, ")") | ||
|
||
}, | ||
date_build = function(year, month = 1L, day = 1L, ..., invalid = NULL) { | ||
dbplyr:::check_unsupported_arg(invalid, allow_null = TRUE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather not access a function private to dbplyr here. How do other packages handle this? As a last resort, we could vendor a variant of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would you implement such a function? |
||
rlang::check_dots_empty() | ||
build_sql("MAKE_DATE(CAST(", year, " AS INTEGER), CAST(", month, " AS INTEGER), CAST(", day, " AS INTEGER))") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see how |
||
|
||
}, | ||
difftime = function(time1, time2, tz, units = "days") { | ||
dbplyr:::check_unsupported_arg(tz) | ||
dbplyr:::check_unsupported_arg(units, allowed = "days") | ||
build_sql("DATEDIFF('day', ", !!time2, ", " ,!!time1, ")") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we could use the recommended |
||
}, | ||
|
||
# stringr functions | ||
str_c = sql_paste(""), | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -141,11 +141,12 @@ test_that("custom clock functions translated correctly", { | |||||
translate <- function(...) dbplyr::translate_sql(..., con = con) | ||||||
sql <- function(...) dbplyr::sql(...) | ||||||
|
||||||
expect_equal(translate(add_days(x, 1L)), sql(r"{DATE_ADD(x, INTERVAL '1 day')}")) | ||||||
expect_equal(translate(add_days(x, 2L)), sql(r"{DATE_ADD(x, INTERVAL '2 day')}")) | ||||||
m <- 1 | ||||||
expect_equal(translate(add_days(x, m)), sql(r"{DATE_ADD(x, INTERVAL (m) day)}")) | ||||||
expect_equal(translate(add_days(x, 2L)), sql(r"{DATE_ADD(x, INTERVAL (2) day)}")) | ||||||
|
||||||
expect_equal(translate(add_years(x, 1L)), sql(r"{DATE_ADD(x, INTERVAL '1 year')}")) | ||||||
expect_equal(translate(add_years(x, 2L)), sql(r"{DATE_ADD(x, INTERVAL '2 year')}")) | ||||||
expect_equal(translate(add_years(x, m)), sql(r"{DATE_ADD(x, INTERVAL (m) year)}")) | ||||||
expect_equal(translate(add_years(x, 2L)), sql(r"{DATE_ADD(x, INTERVAL (2) year)}")) | ||||||
|
||||||
expect_equal(translate(get_day(x)), sql(r"{DATE_PART('day', x)}")) | ||||||
expect_equal(translate(get_month(x)), sql(r"{DATE_PART('month', x)}")) | ||||||
|
@@ -156,6 +157,10 @@ test_that("custom clock functions translated correctly", { | |||||
precision = "day")), | ||||||
sql(r"{DATEDIFF('day', start, end)}")) | ||||||
|
||||||
expect_equal(translate(difftime(time1 = "time1", time2 = "time2", units = "days")), sql(r"{DATEDIFF('day', time2, time1)}")) | ||||||
expect_equal(translate(date_build(year = 2000, month = 08, day = 08)), sql(r"{MAKE_DATE(CAST(2000.0 AS INTEGER), CAST(8.0 AS INTEGER), CAST(8.0 AS INTEGER))}")) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a proper test:
Suggested change
|
||||||
expect_equal(translate(date_build(year = 2000)), sql(r"{MAKE_DATE(CAST(2000.0 AS INTEGER), CAST(1 AS INTEGER), CAST(1 AS INTEGER))}")) | ||||||
|
||||||
test_data <- data.frame( | ||||||
person = 1L, | ||||||
date_1 = as.Date("2000-01-01"), | ||||||
|
@@ -197,6 +202,9 @@ test_that("custom clock functions translated correctly", { | |||||
precision = "day", | ||||||
n = 5))) | ||||||
|
||||||
expect_error(translate(date_build(year = 2000, month = 08, day = 08, invalid = "next-day"))) | ||||||
expect_error(translate(difftime(time1, time2, units = "secs"))) | ||||||
expect_error(translate(difftime(time1, time2, tz = Sys.timezone()))) | ||||||
}) | ||||||
|
||||||
# stringr functions | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now wonder why this says
!!x
with the bang-bang andn
without.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess is a choice, I think x can be a function to be evaluated in this case, while n can't