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

fix/add date-related functions #643

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions R/backend-dbplyr__duckdb_connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
Copy link
Collaborator

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.

Copy link
Author

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

},
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, ")")
Expand All @@ -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)
Copy link
Collaborator

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.

Copy link
Author

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?

rlang::check_dots_empty()
build_sql("MAKE_DATE(CAST(", year, " AS INTEGER), CAST(", month, " AS INTEGER), CAST(", day, " AS INTEGER))")
Copy link
Collaborator

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, ")")
Copy link
Collaborator

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.

},

# stringr functions
str_c = sql_paste(""),
Expand Down
16 changes: 12 additions & 4 deletions tests/testthat/test-backend-dbplyr__duckdb_connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"))
Expand All @@ -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))}"))
Copy link
Collaborator

Choose a reason for hiding this comment

The 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, 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))}"))

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"),
Expand Down Expand Up @@ -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
Expand Down