- Notifications
You must be signed in to change notification settings - Fork47
fix/add date-related functions#643
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Conversation
krlmlr left a comment
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.
| # 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 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 thinkx can be a function to be evaluated in this case, whilen can't
| }, | ||
| 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 ofcheck_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?
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.
Could be a newdbplyr.R orcheck_unsupported_arg.R .
| 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 howSELECT MAKE_DATE(2022.0, 3.0, 8.0); fails in duckdb. Still, casting shouldn't be a concern toMAKE_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 recommendedsql_expr() in the translation here and above.
Uh oh!
There was an error while loading.Please reload this page.
3e5d5fa tof344576Comparekrlmlr commentedSep 7, 2025
@IoannaNika: Do you want to take another stab? |
IoannaNika commentedSep 8, 2025
@krlmlr I can try to make some time to work on this. Should I work on a separate PR? If I recall correctly the tasks remaining are to add support for these functions:
|
krlmlr commentedSep 8, 2025
Thanks. The comments above are still valid, adding to this PR is fine. |
IoannaNika commentedSep 15, 2025
@krlmlr I have issues building the package, I get some error:
Do you know why that is? |
krlmlr commentedSep 15, 2025
When the code changes, I often need |
923f7d9 to69dbe63CompareIoannaNika commentedOct 15, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I think It's working now@krlmlr library(duckdb)#> Loading required package: DBIlibrary(clock)library(DBI)library(dplyr)#>#> Attaching package: 'dplyr'#> The following objects are masked from 'package:stats':#>#> filter, lag#> The following objects are masked from 'package:base':#>#> intersect, setdiff, setequal, unioncon<-DBI::dbConnect(duckdb::duckdb())date_df<-dplyr::tibble(id=1)DBI::dbWriteTable(con,"tmpdate",date_df)date_tbl<-dplyr::tbl(con,"tmpdate")df<-dplyr::mutate(date_tbl,date7=clock::date_build(as.integer(1999)),date8=clock::date_build(as.integer(2000), as.integer(1), as.integer(1)))df#> # Source: SQL [?? x 3]#> # Database: DuckDB 1.4.2-dev16 [root@Darwin 24.0.0:R 4.4.1/:memory:]#> id date7 date8#> <dbl> <date> <date>#> 1 1 1999-01-01 2000-01-01df<-dplyr::mutate(df,diff_time= difftime(date8,date7))df#> # Source: SQL [?? x 4]#> # Database: DuckDB 1.4.2-dev16 [root@Darwin 24.0.0:R 4.4.1/:memory:]#> id date7 date8 diff_time#> <dbl> <date> <date> <dbl>#> 1 1 1999-01-01 2000-01-01 365 Created on 2025-10-15 withreprex v2.1.1 |
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 documentationhttps://duckdb.org/docs/sql/functions/date.html#date_adddate-interval)
Created on 2024-12-12 withreprex v2.1.1