Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around missing CAST(x AS INTEGER) in MySQL #1375

Merged
merged 5 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,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)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 22 additions & 3 deletions R/backend-mysql.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -129,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, ...) {
Expand Down
3 changes: 3 additions & 0 deletions man/backend-mysql.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/backend-mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
Code
copy_inline(con_mysql, tibble(x = 1:2, y = letters[1:2])) %>% remote_query()
Output
<SQL> SELECT CAST(`x` AS INTEGER) AS `x`, CAST(`y` AS CHAR) AS `y`
<SQL> 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)
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-backend-mysql.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand All @@ -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())
})

Expand Down
Loading