Skip to content

Commit

Permalink
Add spark sql translations for clock functions date_build, get_year, …
Browse files Browse the repository at this point in the history
…get_month, get_day.
  • Loading branch information
ablack3 committed Dec 26, 2023
1 parent a468963 commit 2374dad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions R/backend-spark-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ simulate_spark_sql <- function() simulate_dbi("Spark SQL")
check_dots_empty()
sql_expr(add_months(!!!x, !!n*12))
},
date_build = function(year, month = 1L, day = 1L, ..., invalid = NULL) {
sql_expr(make_date(!!year, !!month, !!day))
},
get_year = function(x) {
sql_expr(date_part('YEAR', !!x))
},
get_month = function(x) {
sql_expr(date_part('MONTH', !!x))
},
get_day = function(x) {
sql_expr(date_part('DAY', !!x))
},

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

Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-backend-spark-sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ test_that("custom clock functions translated correctly", {
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")))
expect_equal(test_translate_sql(date_build(2020, 1, 1)), sql("MAKE_DATE(2020.0, 1.0, 1.0)"))
expect_equal(test_translate_sql(date_build(year_column, 1L, 1L)), sql("MAKE_DATE(`year_column`, 1, 1)"))
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`)"))
})

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

0 comments on commit 2374dad

Please sign in to comment.