Skip to content

Commit

Permalink
translations for date_count_between (#1496)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Simon P. Couch <[email protected]>
  • Loading branch information
edward-burn and simonpcouch authored Oct 18, 2024
1 parent 1ff6363 commit 0615a65
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

* `across(everything())` doesn't select grouping columns created via `.by` in
`summarise()` (@mgirlich, #1493).


* New translations of clock function `date_count_between()` for SQL server, Redshift, Snowflake, Postgres, and Spark (@edward-burn, #1495).

* Spark SQL backend now supports persisting tables with
`compute(x, name = I("x.y.z"), temporary = FALSE)` (@zacdav-db, #1502).

Expand Down
12 changes: 12 additions & 0 deletions R/backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,18 @@ simulate_mssql <- function(version = "15.0") {
get_day = function(x) {
sql_expr(DATEPART(DAY, !!x))
},
date_count_between = function(start, end, precision, ..., n = 1L){

check_dots_empty()
if (precision != "day") {
cli_abort("{.arg precision} must be {.val day} on SQL backends.")
}
if (n != 1) {
cli_abort("{.arg n} must be {.val 1} on SQL backends.")
}

sql_expr(DATEDIFF(DAY, !!start, !!end))
},

difftime = function(time1, time2, tz, units = "days") {

Expand Down
12 changes: 12 additions & 0 deletions R/backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,18 @@ sql_translation.PqConnection <- function(con) {
date_build = function(year, month = 1L, day = 1L, ..., invalid = NULL) {
sql_expr(make_date(!!year, !!month, !!day))
},
date_count_between = function(start, end, precision, ..., n = 1L){

check_dots_empty()
if (precision != "day") {
cli_abort("{.arg precision} must be {.val day} on SQL backends.")
}
if (n != 1) {
cli_abort("{.arg n} must be {.val 1} on SQL backends.")
}

sql_expr(!!end - !!start)
},
get_year = function(x) {
sql_expr(date_part('year', !!x))
},
Expand Down
12 changes: 12 additions & 0 deletions R/backend-redshift.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ sql_translation.RedshiftConnection <- function(con) {
get_day = function(x) {
sql_expr(DATE_PART('day', !!x))
},
date_count_between = function(start, end, precision, ..., n = 1L){

check_dots_empty()
if (precision != "day") {
cli_abort("{.arg precision} must be {.val day} on SQL backends.")
}
if (n != 1) {
cli_abort("{.arg n} must be {.val 1} on SQL backends.")
}

sql_expr(DATEDIFF(DAY, !!start, !!end))
},

difftime = function(time1, time2, tz, units = "days") {

Expand Down
12 changes: 12 additions & 0 deletions R/backend-snowflake.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ sql_translation.Snowflake <- function(con) {
get_day = function(x) {
sql_expr(DATE_PART(DAY, !!x))
},
date_count_between = function(start, end, precision, ..., n = 1L){

check_dots_empty()
if (precision != "day") {
cli_abort("{.arg precision} must be {.val day} on SQL backends.")
}
if (n != 1) {
cli_abort("{.arg n} must be {.val 1} on SQL backends.")
}

sql_expr(DATEDIFF(DAY, !!start, !!end))
},

difftime = function(time1, time2, tz, units = "days") {

Expand Down
12 changes: 12 additions & 0 deletions R/backend-spark-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ simulate_spark_sql <- function() simulate_dbi("Spark SQL")
get_day = function(x) {
sql_expr(date_part('DAY', !!x))
},
date_count_between = function(start, end, precision, ..., n = 1L){

check_dots_empty()
if (precision != "day") {
cli_abort("{.arg precision} must be {.val day} on SQL backends.")
}
if (n != 1) {
cli_abort("{.arg n} must be {.val 1} on SQL backends.")
}

sql_expr(datediff(!!end, !!start))
},

difftime = function(time1, time2, tz, units = "days") {

Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-mssql.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ test_that("custom clock functions translated correctly", {
expect_equal(test_translate_sql(get_year(date_column)), sql("DATEPART(YEAR, `date_column`)"))
expect_equal(test_translate_sql(get_month(date_column)), sql("DATEPART(MONTH, `date_column`)"))
expect_equal(test_translate_sql(get_day(date_column)), sql("DATEPART(DAY, `date_column`)"))
expect_equal(test_translate_sql(date_count_between(date_column_1, date_column_2, "day")),
sql("DATEDIFF(DAY, `date_column_1`, `date_column_2`)"))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "year")))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5)))
})

test_that("difftime is translated correctly", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-postgres.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ test_that("custom clock functions translated correctly", {
expect_equal(test_translate_sql(get_year(date_column)), sql("DATE_PART('year', `date_column`)"))
expect_equal(test_translate_sql(get_month(date_column)), sql("DATE_PART('month', `date_column`)"))
expect_equal(test_translate_sql(get_day(date_column)), sql("DATE_PART('day', `date_column`)"))
expect_equal(test_translate_sql(date_count_between(date_column_1, date_column_2, "day")),
sql("`date_column_2` - `date_column_1`"))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "year")))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5)))
})

test_that("difftime is translated correctly", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-redshift.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ test_that("custom clock functions translated correctly", {
expect_equal(test_translate_sql(get_year(date_column)), sql("DATE_PART('year', `date_column`)"))
expect_equal(test_translate_sql(get_month(date_column)), sql("DATE_PART('month', `date_column`)"))
expect_equal(test_translate_sql(get_day(date_column)), sql("DATE_PART('day', `date_column`)"))
expect_equal(test_translate_sql(date_count_between(date_column_1, date_column_2, "day")),
sql("DATEDIFF(DAY, `date_column_1`, `date_column_2`)"))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "year")))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5)))
})

test_that("difftime is translated correctly", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-snowflake.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ test_that("custom clock functions translated correctly", {
expect_equal(test_translate_sql(get_year(date_column)), sql("DATE_PART(YEAR, `date_column`)"))
expect_equal(test_translate_sql(get_month(date_column)), sql("DATE_PART(MONTH, `date_column`)"))
expect_equal(test_translate_sql(get_day(date_column)), sql("DATE_PART(DAY, `date_column`)"))
expect_equal(test_translate_sql(date_count_between(date_column_1, date_column_2, "day")),
sql("DATEDIFF(DAY, `date_column_1`, `date_column_2`)"))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "year")))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5)))
})

test_that("difftime is translated correctly", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-backend-spark-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ test_that("custom clock functions translated correctly", {
expect_equal(test_translate_sql(get_year(date_column)), sql("DATE_PART('YEAR', `date_column`)"))
expect_equal(test_translate_sql(get_month(date_column)), sql("DATE_PART('MONTH', `date_column`)"))
expect_equal(test_translate_sql(get_day(date_column)), sql("DATE_PART('DAY', `date_column`)"))
expect_equal(test_translate_sql(date_count_between(date_column_1, date_column_2, "day")),
sql("DATEDIFF(`date_column_2`, `date_column_1`)"))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "year")))
expect_error(test_translate_sql(date_count_between(date_column_1, date_column_2, "day", n = 5)))
})

test_that("difftime is translated correctly", {
Expand Down

0 comments on commit 0615a65

Please sign in to comment.