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

function translations for DATEDIFF and DATEADD #1080

Open
ablack3 opened this issue Dec 15, 2022 · 4 comments
Open

function translations for DATEDIFF and DATEADD #1080

ablack3 opened this issue Dec 15, 2022 · 4 comments
Labels
feature a feature request or enhancement
Milestone

Comments

@ablack3
Copy link
Contributor

ablack3 commented Dec 15, 2022

Feature Request

DATEDIFF and DATEADD are commonly used SQL functions with slightly different syntax on different database platforms. I would like to use dbplyr's translation approach to generate SQL statements DATEDIFF and DATEADD that are correct for various database platforms.

I can use sql passthrough but then the dplyr code won't be translated and work on multiple database platforms. I would like to write one dplyr expression to calculate the difference between dates (or addition of an interval to a date) that will be translated correctly on both sql server and postgres. Below is a reprex that generates correct SQL on postgres but not sql server.

library(tidyverse)
df <- tibble(date1 = as.Date("2020-02-02"), date2 = as.Date("2020-02-03"))
db <- dbplyr::lazy_frame(df, con = dbplyr::simulate_postgres())

# Date add
db %>% 
  mutate(date3 = date1 + lubridate::years(1)) %>% 
  show_query()
#> <SQL>
#> SELECT *, `date1` + CAST('1 years' AS INTERVAL) AS `date3`
#> FROM `df`

# Date diff
db %>% 
  mutate(days = date2 - date1) %>% 
  show_query()
#> <SQL>
#> SELECT *, `date2` - `date1` AS `days`
#> FROM `df`

db <- dbplyr::lazy_frame(df, con = dbplyr::simulate_mssql())

# Date add
db %>% 
  mutate(date3 = date1 + lubridate::years(1)) %>% 
  show_query()
#> <SQL>
#> SELECT *, `date1` + years(1.0) AS `date3`
#> FROM `df`

# Date diff
db %>% 
  mutate(days = date2 - date1) %>% 
  show_query()
#> <SQL>
#> SELECT *, `date2` - `date1` AS `days`
#> FROM `df`

Created on 2022-12-15 with reprex v2.0.2

@mgirlich
Copy link
Collaborator

dbplyr doesn't know the column types, so it is not possible to simply translate + and - correctly for dates. Therefore, a custom function like date_add() and date_diff() would be necessary. Unfortunately, this would probably feel a bit artificial as this isn't required in R. Also, this would be quite hard to find. So, I'm not sure it is worthing adding such a function.

@hadley What's your opinion on this?

@ablack3
Copy link
Contributor Author

ablack3 commented Dec 22, 2022

What about translating the R function difftime to DATEDIFF?

Is it possible/recommended for me to extend dbplyr function translation and add date_add and date_diff R functions in my own package with SQL translations by creating a custom sql translation environment?

@hadley
Copy link
Member

hadley commented Dec 22, 2022

Yeah, I can't see an easy way for dbplyr to support this currently — we don't know the column types so we can't make +/- work and we don't have an extension mechanism so that another package could define date_add() and date_diff().

@ablack3 ablack3 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2022
@ablack3
Copy link
Contributor Author

ablack3 commented Aug 27, 2023

I'm going to reopen as we explore the possibility of implementing translations for the clock functions. I've opened an initial PR here. Thanks for your help with this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants