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

Add translations for base::difftime(), and clock functions add_days(), add_years(), date_build(), get_year(), get_month(), get_day() #1357

Merged
merged 16 commits into from
Jan 9, 2024

Conversation

ablack3
Copy link
Contributor

@ablack3 ablack3 commented Aug 27, 2023

@hadley, @mgirlich. Here is what I'm thinking for two of the clock functions. Once I learn how to make well formed PRs I can add more but I wanted to see what you think about this one first. Can you review and discuss or make suggestions? Thanks!

@hadley
Copy link
Member

hadley commented Sep 15, 2023

Yes, this what I was thinking 😄

Tagging @DavisVaughan in case he has thoughts too.

@DavisVaughan
Copy link
Member

Seems useful to me!


# clock ---------------------------------------------------------------
add_days = function(x, n, ...) {
rlang::check_dots_empty(...)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can omit the rlang namespace. The rlang package is so commonly used that it is usually imported.
And you either do check_dots_empty() or check_dots_empty0(...) but you must not pass ... to check_dots_empty().

Suggested change
rlang::check_dots_empty(...)
check_dots_empty()

@mgirlich
Copy link
Collaborator

@ablack3 Thanks for you PR. Please add a NEWS entry mentioning the issue #1080 and yourself. And please modify your first message and mention that this closes #1080. This way the issue will be automatically closed when this PR is merged.

@mgirlich
Copy link
Collaborator

@ablack3 Do you want to finish the PR? 😄 I'm happy to merge afterwards.

@ablack3
Copy link
Contributor Author

ablack3 commented Nov 8, 2023

@ablack3 Do you want to finish the PR? 😄 I'm happy to merge afterwards.

Yes, I'll finish it this week.

Is there a clock function for calculating the difference between two dates that we can translate to DATEDIFF in SQL?

Should we use difftime in base to generate DATEDIFF?

@hadley
Copy link
Member

hadley commented Nov 8, 2023

Yeah, I think difftime() is a reasonable translation.

@hadley
Copy link
Member

hadley commented Nov 8, 2023

@ablack3 as you're working on this, it would be super useful if you'd take some notes so we could document the process of adding date-time wrappers for other backends (and then we could provide more expectations about how to express date time manipulations in R code for the user)

@ablack3
Copy link
Contributor Author

ablack3 commented Nov 8, 2023

@ablack3 as you're working on this, it would be super useful if you'd take some notes so we could document the process of adding date-time wrappers for other backends (and then we could provide more expectations about how to express date time manipulations in R code for the user)

Yes of course. I'll write some documentation and share it for feedback. It would be nice to have one set of date manipulation functions that translate to many backends.

@ablack3
Copy link
Contributor Author

ablack3 commented Nov 10, 2023

Ok I think this PR is ready. I would expect that you'll have some recommendations so please let me know if you suggested edits to the PR. I will also add similar translations in the bigrquery package and duckdb package in separate PRs. One question, why the backends for bigquery and duckdb are not in the dbplyr package?

I still need to write up the documentation Hadley asked for, possibly in an issue on dplyr.

@ablack3 ablack3 changed the title Add mssql translations for two clock functions Add mssql translations for difftime, clock::add_days, and clock::add_years functions mapping to datediff and dateadd SQL functions Nov 10, 2023
},
add_years = function(x, n, ...) {
check_dots_empty()
sql_expr((!!x + !!n%*INTERVAL%'1 year'))
Copy link
Contributor Author

@ablack3 ablack3 Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgirlich this works but is very awkward. Is there a better way to get the correct SQL here?

This is what I want to write but it is not a valid R expression

sql_expr((!!x + !!n * INTERVAL'1 year'))

The SQL should look like

(`column_name` + 2 * INTERVAL'1 year')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hadley is there a better way to write this expression? !!x + !!n%*INTERVAL%'1 year' seems strange but it's the only way I could come up with that worked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use glue_sql2() since that's SQL expression is distant from any R equivalent.

Copy link
Contributor Author

@ablack3 ablack3 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I tried glue_sql2() instead. Hopefully I used it correctly. I'm using sql_current_con() as the first argument.

}

if (units[1] != "days") {
cli::cli_abort('The only supported value for {.arg units} on SQL backends is "days"')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we could just support the 'days' unit to start. Keep it simple.

@ablack3 ablack3 changed the title Add mssql translations for difftime, clock::add_days, and clock::add_years functions mapping to datediff and dateadd SQL functions Add translations for difftime, clock::add_days, and clock::add_years Nov 10, 2023
Copy link
Member

@hadley hadley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good place to start. Do you want to add a news bullet and we can merge?

@ablack3
Copy link
Contributor Author

ablack3 commented Dec 26, 2023

This seems like a good place to start. Do you want to add a news bullet and we can merge?

Ok I added a few more translations and added a news bullet. Thank you!

@ablack3 ablack3 changed the title Add translations for difftime, clock::add_days, and clock::add_years Add translations for base::difftime(), and clock functions add_days(), add_years(), date_build(), get_year(), get_month(), get_day() Dec 26, 2023
@hadley hadley merged commit 068a88f into tidyverse:main Jan 9, 2024
13 checks passed
@ablack3 ablack3 deleted the clock branch January 24, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants