Skip to content

Commit

Permalink
address as.double() and as.character() errors with Teradata (#1545)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: simonpcouch <[email protected]>
  • Loading branch information
rplsmn and simonpcouch authored Oct 24, 2024
1 parent 0615a65 commit 7fb52d7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dbplyr (development version)

* Translations for `as.double()` and `as.character()` with Teradata previously
raised errors and are now correct (@rplsmn, #1545).

* Translations of `difftime()` for Postgres, SQL server, Redshift, and Snowflake
previously returned the wrong sign and are now correct (@edward-burn, #1532).

Expand Down
8 changes: 6 additions & 2 deletions R/backend-teradata.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ sql_translation.Teradata <- function(con) {
digits <- vctrs::vec_cast(digits, integer())
sql_expr(CAST(!!x %as% DECIMAL(12L, !!digits)))
},
as.double = sql_cast("NUMERIC"),
as.character = sql_cast("VARCHAR(MAX)"),
as.double = sql_cast("FLOAT"),
as.character = function(x, nchar = 255L) {
check_number_whole(nchar, min = 0, max = 64000)
nchar <- vctrs::vec_cast(nchar, integer())
sql_expr(CAST(!!x %as% VARCHAR(!!nchar)))
},
as.Date = teradata_as_date,
log10 = sql_prefix("LOG"),
log = sql_log(),
Expand Down
5 changes: 3 additions & 2 deletions tests/testthat/test-backend-teradata.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ test_that("custom scalar translated correctly", {
expect_equal(test_translate_sql(x != y), sql("`x` <> `y`"))
expect_equal(test_translate_sql(as.numeric(x)), sql("CAST(`x` AS DECIMAL(12, 9))"))
expect_equal(test_translate_sql(as.numeric(x, 8)), sql("CAST(`x` AS DECIMAL(12, 8))"))
expect_equal(test_translate_sql(as.double(x)), sql("CAST(`x` AS NUMERIC)"))
expect_equal(test_translate_sql(as.character(x)), sql("CAST(`x` AS VARCHAR(MAX))"))
expect_equal(test_translate_sql(as.double(x)), sql("CAST(`x` AS FLOAT)"))
expect_equal(test_translate_sql(as.character(x)), sql("CAST(`x` AS VARCHAR(255))"))
expect_equal(test_translate_sql(as.character(x, 12)), sql("CAST(`x` AS VARCHAR(12))"))
expect_equal(test_translate_sql(log(x)), sql("LN(`x`)"))
expect_equal(test_translate_sql(cot(x)), sql("1 / TAN(`x`)"))
expect_equal(test_translate_sql(nchar(x)), sql("CHARACTER_LENGTH(`x`)"))
Expand Down

0 comments on commit 7fb52d7

Please sign in to comment.