-
Notifications
You must be signed in to change notification settings - Fork 173
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
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
77df050
Add mssql translations for two clock functions
ablack3 8eaa86e
strip namespace for clock function translation
ablack3 76e7146
fix merge conflict
ablack3 07e1a5a
add datediff translation on mssql
ablack3 9356883
only support units='days' in difftime sql translation
ablack3 78d5e54
add difftime, add_years, and add_days translations for oracle
ablack3 1109f8b
add date function translations for postgres
ablack3 b0e457b
Add date function translations on redshift
ablack3 93fb131
add date function translations for snowflake and spark
ablack3 e770da7
Merge branch 'main' of github.com:tidyverse/dbplyr into clock
ablack3 6e8a92c
add date_build, get_year, get_month, get_day translations for postgres.
ablack3 0693ef1
Add redshift clock function translations for date_build, get_year, ge…
ablack3 477179e
add snowflake translations for clock functions date_build, get_year, …
ablack3 a468963
Add mssql clock translations for date_build, get_year, get_month, get…
ablack3 2374dad
Add spark sql translations for clock functions date_build, get_year, …
ablack3 12b06cf
update News
ablack3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,29 @@ sql_translation.PqConnection <- function(con) { | |
) | ||
sql_expr(DATE_TRUNC(!!unit, !!x)) | ||
}, | ||
|
||
# clock --------------------------------------------------------------- | ||
add_days = function(x, n, ...) { | ||
check_dots_empty() | ||
sql_expr((!!x + !!n%*INTERVAL%'1 day')) | ||
}, | ||
add_years = function(x, n, ...) { | ||
check_dots_empty() | ||
sql_expr((!!x + !!n%*INTERVAL%'1 year')) | ||
}, | ||
|
||
difftime = function(time1, time2, tz, units = "days") { | ||
|
||
if (!missing(tz)) { | ||
cli::cli_abort("The {.arg tz} argument is not supported for SQL backends.") | ||
} | ||
|
||
if (units[1] != "days") { | ||
cli::cli_abort('The only supported value for {.arg units} on SQL backends is "days"') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
||
sql_expr((CAST(!!time2 %AS% DATE) - CAST(!!time1 %AS% DATE))) | ||
}, | ||
), | ||
sql_translator(.parent = base_agg, | ||
cor = sql_aggregate_2("CORR"), | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
test_that("custom clock functions translated correctly", { | ||
local_con(simulate_spark_sql()) | ||
expect_equal(test_translate_sql(add_years(x, 1)), sql("ADD_MONTHS('`x`', 1.0 * 12.0)")) | ||
expect_equal(test_translate_sql(add_days(x, 1)), sql("DATE_ADD(`x`, 1.0)")) | ||
expect_error(test_translate_sql(add_days(x, 1, "dots", "must", "be empty"))) | ||
}) | ||
|
||
test_that("difftime is translated correctly", { | ||
local_con(simulate_spark_sql()) | ||
expect_equal(test_translate_sql(difftime(start_date, end_date, units = "days")), sql("DATEDIFF(`end_date`, `start_date`)")) | ||
expect_equal(test_translate_sql(difftime(start_date, end_date)), sql("DATEDIFF(`end_date`, `start_date`)")) | ||
|
||
expect_error(test_translate_sql(difftime(start_date, end_date, units = "auto"))) | ||
expect_error(test_translate_sql(difftime(start_date, end_date, tz = "UTC", units = "days"))) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@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
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.
@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.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 probably use
glue_sql2()
since that's SQL expression is distant from any R equivalent.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.
Ok I tried
glue_sql2()
instead. Hopefully I used it correctly. I'm usingsql_current_con()
as the first argument.