Skip to content

Commit

Permalink
Fix all things broken in PR #347
Browse files Browse the repository at this point in the history
  • Loading branch information
schuemie committed Mar 20, 2024
1 parent f3391bb commit ae7b974
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
26 changes: 13 additions & 13 deletions inst/csv/replacementPatterns.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1080,19 +1080,19 @@ spark,"HASHBYTES('MD5',@a)","MD5(@a)"
spark,"CONVERT(VARBINARY, CONCAT('0x', @a), 1)","CAST(CONCAT('x', @a) AS BIT(32))"
spark,"CONVERT(DATE, @a)","TO_DATE(@a, 'yyyy-MM-dd')"
spark,"DATEPART(@part, @date)","DATE_PART('@part', @date)"
spark,"DATEADD(second,@seconds,@datetime)",DATE_ADD(second,@seconds,@datetime)
spark,"DATEADD(minute,@minutes,@datetime)",DATE_ADD(minute,@minutes,@datetime)
spark,"DATEADD(hour,@hours,@datetime)",DATE_ADD(hour,@hours,@datetime)
spark,"DATEADD(d,@days,@date)",DATE_ADD(day,@days,@date)
spark,"DATEADD(dd,@days,@date)",DATE_ADD(day,@days,@date)
spark,"DATEADD(day,@days,@date)",DATE_ADD(day,@days,@date)
spark,"DATEADD(m,@months,@date)",DATE_ADD(month,@months,@date)
spark,"DATEADD(mm,@months,@date)",DATE_ADD(month,@months,@date)
spark,"DATEADD(month,@months,@date)",DATE_ADD(month,@months,@date)
spark,"DATEADD(yy,@years,@date)",DATE_ADD(year,@years,@date)
spark,"DATEADD(yyyy,@years,@date)",DATE_ADD(year,@years,@date)
spark,"DATEADD(year,@years,@date)",DATE_ADD(year,@years,@date)
spark,"date_add(@date, @(-?[0-9]+)a.0)","date_add(@date, @a)"
spark,"DATEADD(second,@seconds,@datetime)","DATE_ADD(second,@seconds,@datetime)"
spark,"DATEADD(minute,@minutes,@datetime)","DATE_ADD(minute,@minutes,@datetime)"
spark,"DATEADD(hour,@hours,@datetime)","DATE_ADD(hour,@hours,@datetime)"
spark,"DATEADD(d,@days,@date)","DATE_ADD(day,@days,@date)"
spark,"DATEADD(dd,@days,@date)","DATE_ADD(day,@days,@date)"
spark,"DATEADD(day,@days,@date)","DATE_ADD(day,@days,@date)"
spark,"DATEADD(m,@months,@date)","DATE_ADD(month,@months,@date)"
spark,"DATEADD(mm,@months,@date)","DATE_ADD(month,@months,@date)"
spark,"DATEADD(month,@months,@date)","DATE_ADD(month,@months,@date)"
spark,"DATEADD(yy,@years,@date)","DATE_ADD(year,@years,@date)"
spark,"DATEADD(yyyy,@years,@date)","DATE_ADD(year,@years,@date)"
spark,"DATEADD(year,@years,@date)","DATE_ADD(year,@years,@date)"
spark,"DATE_ADD(@part,@(-?[0-9]+)a.0,@date)","DATE_ADD(@part,@a,@date)"
spark,INTERVAL @(-?[0-9]+)a.0,INTERVAL @a
spark,"DATEDIFF(second,@start,@end)","datediff(second,@end,@start)"
spark,"DATEDIFF(minute,@start,@end)","datediff(minute,@end,@start)"
Expand Down
35 changes: 18 additions & 17 deletions tests/testthat/test-translate-spark.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,78 +59,78 @@ test_that("translate sql server -> spark dateadd", {
sql <- translate("SELECT dateadd(second, -1 * 2, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' - INTERVAL 2 second)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(second,-1 * 2,'2019-01-01 00:00:00')")

sql <- translate("SELECT dateadd(minute, -1 * 3, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' - INTERVAL 3 minute)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(minute,-1 * 3,'2019-01-01 00:00:00')")

sql <- translate("SELECT dateadd(hour, -1 * 4, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' - INTERVAL 4 hour)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(hour,-1 * 4,'2019-01-01 00:00:00')")

# Positive intervals have typical translation patterns
sql <- translate("SELECT dateadd(second, 1, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' + INTERVAL 1 second)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(second,1,'2019-01-01 00:00:00')")

sql <- translate("SELECT dateadd(minute, 1, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' + INTERVAL 1 minute)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(minute,1,'2019-01-01 00:00:00')")

sql <- translate("SELECT dateadd(hour, 1, '2019-01-01 00:00:00')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01 00:00:00' + INTERVAL 1 hour)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(hour,1,'2019-01-01 00:00:00')")

sql <- translate("SELECT dateadd(d, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT date_add('2019-01-01', 1)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')")

sql <- translate("SELECT dateadd(dd, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT date_add('2019-01-01', 1)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')")

sql <- translate("SELECT dateadd(day, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT date_add('2019-01-01', 1)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(day,1,'2019-01-01')")

sql <- translate("SELECT dateadd(m, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 month)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')")

sql <- translate("SELECT dateadd(mm, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 month)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')")

sql <- translate("SELECT dateadd(month, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 month)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(month,1,'2019-01-01')")

sql <- translate("SELECT dateadd(yy, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 year)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')")

sql <- translate("SELECT dateadd(yyyy, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 year)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')")

sql <- translate("SELECT dateadd(year, 1, '2019-01-01')",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "SELECT ('2019-01-01' + INTERVAL 1 year)")
expect_equal_ignore_spaces(sql, "SELECT DATE_ADD(year,1,'2019-01-01')")
})


Expand Down Expand Up @@ -453,14 +453,14 @@ test_that("translate sql server -> spark DATEADD DAY with float", {
sql <- translate("select DATEADD(DAY, 1.0, some_date) from my_table;",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "select date_add(some_date, 1) from my_table;")
expect_equal_ignore_spaces(sql, "select DATE_ADD(day, 1, some_date) from my_table;")
})

test_that("translate sql server -> spark DATEADD YEAR with float", {
sql <- translate("select DATEADD(YEAR, 1.0, some_date) from my_table;",
targetDialect = "spark"
)
expect_equal_ignore_spaces(sql, "select (some_date + INTERVAL 1 year) from my_table;")
expect_equal_ignore_spaces(sql, "select DATE_ADD(year, 1, some_date) from my_table;")
})

test_that("translate sql server -> spark DATEADD YEAR with float", {
Expand All @@ -479,3 +479,4 @@ test_that("translate sql server -> spark add column with default", {
sql <- translate("ALTER TABLE mytable ADD COLUMN mycol int DEFAULT 0;", targetDialect = "spark")
expect_equal_ignore_spaces(sql, "ALTER TABLE mytable ADD COLUMN mycol int; \n ALTER TABLE mytable SET TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported'); \n ALTER TABLE mytable ALTER COLUMN mycol SET DEFAULT 0;")
})

0 comments on commit ae7b974

Please sign in to comment.