From 665eedd2eaadd1e3a573468f72cddf22eb585695 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20SIMON?= Date: Mon, 14 Oct 2024 15:28:35 +0200 Subject: [PATCH 1/6] Update numeric to float for double --- R/backend-teradata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/backend-teradata.R b/R/backend-teradata.R index ba17d3a45..ebc7cb1ab 100644 --- a/R/backend-teradata.R +++ b/R/backend-teradata.R @@ -109,7 +109,7 @@ 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.double = sql_cast("FLOAT"), as.character = sql_cast("VARCHAR(MAX)"), as.Date = teradata_as_date, log10 = sql_prefix("LOG"), From c8989243b3d53e464bc994f05b74e91c3fcea716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20SIMON?= Date: Mon, 14 Oct 2024 15:38:53 +0200 Subject: [PATCH 2/6] Update as.character MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ajout d'un argument pour le nombre chars Par défaut 255 Corrige le bug --- R/backend-teradata.R | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/R/backend-teradata.R b/R/backend-teradata.R index ebc7cb1ab..d507cc942 100644 --- a/R/backend-teradata.R +++ b/R/backend-teradata.R @@ -110,7 +110,10 @@ sql_translation.Teradata <- function(con) { sql_expr(CAST(!!x %as% DECIMAL(12L, !!digits))) }, as.double = sql_cast("FLOAT"), - as.character = sql_cast("VARCHAR(MAX)"), + as.character = function(x, nchar = 255L) { + 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(), From e66987ef4973b17ad2da1afbb75254d41ecd1bb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20SIMON?= Date: Wed, 16 Oct 2024 09:19:03 +0200 Subject: [PATCH 3/6] Add tests for changed td translations Modified the test for as.double Modified and added a new test for as.character --- tests/testthat/test-backend-teradata.R | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-backend-teradata.R b/tests/testthat/test-backend-teradata.R index c1883097e..6595979b9 100644 --- a/tests/testthat/test-backend-teradata.R +++ b/tests/testthat/test-backend-teradata.R @@ -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`)")) From a49c4006698e7c8a239e0395b2f390559cee78d4 Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Thu, 24 Oct 2024 07:48:25 -0500 Subject: [PATCH 4/6] add NEWS entry --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 7208d0936..6ebcfb0f8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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). From 5c40830b0ea4dd2f207e4a4bd6bd16a98c9802cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Simon?= <74007913+rplsmn@users.noreply.github.com> Date: Thu, 24 Oct 2024 15:58:59 +0200 Subject: [PATCH 5/6] Update R/backend-teradata.R Co-authored-by: Simon P. Couch --- R/backend-teradata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/backend-teradata.R b/R/backend-teradata.R index d507cc942..fe6bb566e 100644 --- a/R/backend-teradata.R +++ b/R/backend-teradata.R @@ -111,7 +111,7 @@ sql_translation.Teradata <- function(con) { }, as.double = sql_cast("FLOAT"), as.character = function(x, nchar = 255L) { - nchar <- vctrs::vec_cast(nchar, integer()) + check_number_whole(nchar, min = 0, max = 64000) sql_expr(CAST(!!x %as% VARCHAR(!!nchar))) }, as.Date = teradata_as_date, From b59651addb548ca2900414feeb8e5b626289301a Mon Sep 17 00:00:00 2001 From: simonpcouch Date: Thu, 24 Oct 2024 09:27:45 -0500 Subject: [PATCH 6/6] cast to integer after type checking --- R/backend-teradata.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/backend-teradata.R b/R/backend-teradata.R index fe6bb566e..e5fc0c861 100644 --- a/R/backend-teradata.R +++ b/R/backend-teradata.R @@ -112,6 +112,7 @@ sql_translation.Teradata <- function(con) { 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,