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

error when writing character fields containing NAs to mysql table #2256

Closed
eduardgrebe opened this issue Nov 17, 2016 · 2 comments · Fixed by #2381
Closed

error when writing character fields containing NAs to mysql table #2256

eduardgrebe opened this issue Nov 17, 2016 · 2 comments · Fixed by #2381
Labels
bug an unexpected problem or unintended behavior

Comments

@eduardgrebe
Copy link

eduardgrebe commented Nov 17, 2016

I get the following error when trying to write a table to a mysql connection with copy_to():

> sanbs_db <- src_mysql("sanbs", host = "localhost", user = "root")
> results_diagnostic.db <- copy_to(sanbs_db, results_diagnostic, temporary = FALSE, indexes = list(c("result_id","donation_id")))
Error in if (n <= 65535) { : missing value where TRUE/FALSE needed

I believe the error occurs because I have true NAs in character fields, and the following check in src-mysql.r produces n=NA instead of the expected maximum length:

db_data_type.MySQLConnection <- function(con, fields, ...) {
  char_type <- function(x) {
    n <- max(nchar(as.character(x), "bytes"))
    if (n <= 65535) {
      paste0("varchar(", n, ")")
    } else {
      "mediumtext"
    }
}

the problem can be demonstrated with:

> stringvec <- c("a",NA,"bb")
> max(nchar(stringvec))
[1] NA
> max(na.omit(nchar(stringvec)))
[1] 2

I'm not sure what this means for writing NULL values into the db table.

@krlmlr
Copy link
Member

krlmlr commented Nov 22, 2016

Thanks, confirmed. Would you like to contribute a test case and perhaps a fix?

@krlmlr krlmlr added bug an unexpected problem or unintended behavior database labels Nov 22, 2016
@eduardgrebe
Copy link
Author

Submitted a pull request with a proposed fix and test case.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants