From 62c5eca98b330fce8e1cdf73dead7408f5b05e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 6 Oct 2023 17:17:04 +0200 Subject: [PATCH 1/4] Introduce simulate_mariadb() --- NAMESPACE | 1 + R/backend-mysql.R | 6 +++++- man/backend-mysql.Rd | 3 +++ tests/testthat/test-backend-mysql.R | 4 ++-- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index a9bff8dd8..1f174d463 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -493,6 +493,7 @@ export(simulate_dbi) export(simulate_hana) export(simulate_hive) export(simulate_impala) +export(simulate_mariadb) export(simulate_mssql) export(simulate_mysql) export(simulate_odbc) diff --git a/R/backend-mysql.R b/R/backend-mysql.R index 045bc1cde..85f7bdea8 100644 --- a/R/backend-mysql.R +++ b/R/backend-mysql.R @@ -24,7 +24,11 @@ NULL #' @export #' @rdname backend-mysql -simulate_mysql <- function() simulate_dbi("MariaDBConnection") +simulate_mysql <- function() simulate_dbi("MySQLConnection") + +#' @export +#' @rdname backend-mysql +simulate_mariadb <- function() simulate_dbi("MariaDBConnection") #' @export dbplyr_edition.MariaDBConnection <- function(con) { diff --git a/man/backend-mysql.Rd b/man/backend-mysql.Rd index 7193e4c33..96022416b 100644 --- a/man/backend-mysql.Rd +++ b/man/backend-mysql.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/backend-mysql.R \name{backend-mysql} \alias{simulate_mysql} +\alias{simulate_mariadb} \title{Backend: MySQL/MariaDB} \usage{ simulate_mysql() + +simulate_mariadb() } \description{ See \code{vignette("translation-function")} and \code{vignette("translation-verb")} for diff --git a/tests/testthat/test-backend-mysql.R b/tests/testthat/test-backend-mysql.R index 163328246..14426e912 100644 --- a/tests/testthat/test-backend-mysql.R +++ b/tests/testthat/test-backend-mysql.R @@ -44,7 +44,7 @@ test_that("custom stringr functions translated correctly", { # verbs ------------------------------------------------------------------- test_that("generates custom sql", { - con_maria <- simulate_mysql() + con_maria <- simulate_mariadb() expect_snapshot(sql_table_analyze(con_maria, in_schema("schema", "tbl"))) expect_snapshot(sql_query_explain(con_maria, sql("SELECT * FROM table"))) @@ -57,7 +57,7 @@ test_that("generates custom sql", { expect_snapshot(copy_inline(con_maria, tibble(x = 1:2, y = letters[1:2])) %>% remote_query()) - con_mysql <- simulate_dbi("MySQLConnection") + con_mysql <- simulate_mysql() expect_snapshot(copy_inline(con_mysql, tibble(x = 1:2, y = letters[1:2])) %>% remote_query()) }) From b84be01cd28016569e259a712ec6f8c36087de3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 6 Oct 2023 17:18:47 +0200 Subject: [PATCH 2/4] Work around missing CAST(x AS INTEGER) in MySQL --- R/backend-mysql.R | 19 +++++++++++++++++-- tests/testthat/_snaps/backend-mysql.md | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/R/backend-mysql.R b/R/backend-mysql.R index 85f7bdea8..e1ddab659 100644 --- a/R/backend-mysql.R +++ b/R/backend-mysql.R @@ -133,9 +133,24 @@ sql_translation.MariaDBConnection <- function(con) { } #' @export -sql_translation.MySQL <- sql_translation.MariaDBConnection +sql_translation.MySQL <- function(con) { + maria <- unclass(sql_translation.MariaDBConnection()) + sql_variant( + sql_translator(.parent = maria$scalar, + # MySQL doesn't support casting to INTEGER or BIGINT. + as.integer = function(x) { + sql_expr(TRUNCATE(CAST(!!x %AS% DOUBLE), 0L)) + }, + as.integer64 = function(x) { + sql_expr(TRUNCATE(CAST(!!x %AS% DOUBLE), 0L)) + }, + ), + maria$aggregate, + maria$window + ) +} #' @export -sql_translation.MySQLConnection <- sql_translation.MariaDBConnection +sql_translation.MySQLConnection <- sql_translation.MySQL #' @export sql_table_analyze.MariaDBConnection <- function(con, table, ...) { diff --git a/tests/testthat/_snaps/backend-mysql.md b/tests/testthat/_snaps/backend-mysql.md index c0f110391..a389acf46 100644 --- a/tests/testthat/_snaps/backend-mysql.md +++ b/tests/testthat/_snaps/backend-mysql.md @@ -64,7 +64,7 @@ Code copy_inline(con_mysql, tibble(x = 1:2, y = letters[1:2])) %>% remote_query() Output - SELECT CAST(`x` AS INTEGER) AS `x`, CAST(`y` AS CHAR) AS `y` + SELECT TRUNCATE(CAST(`x` AS DOUBLE), 0) AS `x`, CAST(`y` AS CHAR) AS `y` FROM ( SELECT NULL AS `x`, NULL AS `y` WHERE (0 = 1) From effc59726b13ac73609bc8dad1a688004d23a32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Fri, 6 Oct 2023 17:22:14 +0200 Subject: [PATCH 3/4] NEWS --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index de9636442..385056c48 100644 --- a/NEWS.md +++ b/NEWS.md @@ -134,7 +134,8 @@ * `rows_*()` use the column types of `x` when auto copying `y` (@mgirlich, #1327). * `copy_inline()` now works (@mgirlich, #1188). * Fix translation of `as.numeric()`, `as.POSIXct()`, `as_datetime()`, and - `as.integer64()` (@avsdev-cw, #1189). + `as.integer64()` (@avsdev-cw, #1189), and `as.integer()` for MySQL (@krlmlr, #1375). + * New `simulate_mariadb()` (@krlmlr, #1375). * MS SQL: * `row_number()` now works when no order is specified (@ejneer, @fh-mthomson, #1332) From 2b32499008e85e14ac93bcc5beee2c3877412160 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Fri, 27 Oct 2023 08:15:52 +0000 Subject: [PATCH 4/4] Fix News --- NEWS.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index d18f0b3d4..4cc094b3a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,15 @@ # dbplyr (development version) +## Minor improvements and bug fixes + * `sql_translator()` now checks for duplicated definitions (@krlmlr, #1374). +## Backend specific improvements + +* MySQL/MariaDB: + * Fix translation of `as.integer()` for MySQL (@krlmlr, #1375). + * New `simulate_mariadb()` (@krlmlr, #1375). + # dbplyr 2.4.0 ## Breaking changes @@ -145,8 +153,7 @@ * `rows_*()` use the column types of `x` when auto copying `y` (@mgirlich, #1327). * `copy_inline()` now works (@mgirlich, #1188). * Fix translation of `as.numeric()`, `as.POSIXct()`, `as_datetime()`, and - `as.integer64()` (@avsdev-cw, #1189), and `as.integer()` for MySQL (@krlmlr, #1375). - * New `simulate_mariadb()` (@krlmlr, #1375). + `as.integer64()` (@avsdev-cw, #1189). * MS SQL: * `row_number()` now works when no order is specified (@ejneer, @fh-mthomson, #1332)