-
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?
Conversation
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.
Thanks! The bug fix and the new features could be two separate PRs. Also, medium-term, I'd like to switch to snapshot tests for the SQL translations, this PR could be a start.
@@ -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)") |
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 and n
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
@@ -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 comment
The 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 check_unsupported_arg()
here.
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.
Where would you implement such a function?
date_build = function(year, month = 1L, day = 1L, ..., invalid = NULL) { | ||
dbplyr:::check_unsupported_arg(invalid, allow_null = TRUE) | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I see how SELECT MAKE_DATE(2022.0, 3.0, 8.0);
fails in duckdb. Still, casting shouldn't be a concern to MAKE_DATE()
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could use the recommended sql_expr()
in the translation here and above.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
For a proper test:
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))}")) | |
expect_equal(translate(date_build(year = 2000L, month = 8L, day = 8L)), sql(r"{MAKE_DATE(CAST(2000.0 AS INTEGER), CAST(8.0 AS INTEGER), CAST(8.0 AS INTEGER))}")) |
Added support for:
Fixed:
The issue is shown below. When the functions are used with a variable being passed for the interval number to be added, the translation is wrong. Parenthesis are needed to support variables (see documentation https://duckdb.org/docs/sql/functions/date.html#date_adddate-interval)
Created on 2024-12-12 with reprex v2.1.1