Skip to content

Commit

Permalink
Work around missing CAST(x AS INTEGER) in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Oct 6, 2023
1 parent 62c5eca commit b84be01
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions R/backend-mysql.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
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

0 comments on commit b84be01

Please sign in to comment.