Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* `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::cli_abort('The only supported value for {.arg precision} on SQL backends is "day"')
}
if (n != 1) {
cli::cli_abort('The only supported value for {.arg n} on SQL backends is "1"')
}

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::cli_abort('The only supported value for {.arg precision} on SQL backends is "day"')
}
if (n != 1) {
cli::cli_abort('The only supported value for {.arg n} on SQL backends is "1"')
}

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::cli_abort('The only supported value for {.arg precision} on SQL backends is "day"')
}
if (n != 1) {
cli::cli_abort('The only supported value for {.arg n} on SQL backends is "1"')
}

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::cli_abort('The only supported value for {.arg precision} on SQL backends is "day"')
}
if (n != 1) {
cli::cli_abort('The only supported value for {.arg n} on SQL backends is "1"')
}

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::cli_abort('The only supported value for {.arg precision} on SQL backends is "day"')
}
if (n != 1) {
cli::cli_abort('The only supported value for {.arg n} on SQL backends is "1"')
}

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