You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
querySqlToAndromeda(appendToTable = TRUE) errors with "This ResultSet is closed" after appending #346
When appendToTable = TRUE, querySqlToAndromeda() (and therefore renderTranslateQuerySqlToAndromeda()) appends the
query result to the Andromeda table and then throws an error. The data is appended, but the call fails, so callers
cannot use appendToTable = TRUE.
Happy to do a pull request if useful :)
Reproducible example
connectionDetails <- DatabaseConnector::createConnectionDetails(
dbms = "postgresql",
server = "localhost/postgres",
user = "postgres",
password = Sys.getenv("PASSWORD")
)
connection <- DatabaseConnector::connect(connectionDetails)
andromeda <- Andromeda::andromeda()
DatabaseConnector::querySqlToAndromeda(connection, "SELECT 1 AS x;", andromeda, "t")
DatabaseConnector::querySqlToAndromeda(connection, "SELECT 2 AS x;", andromeda, "t", appendToTable = TRUE)
#> Error executing SQL:
#> org.postgresql.util.PSQLException: This ResultSet is closed.
dplyr::collect(andromeda$t)
#> # A tibble: 2 × 1
#> x
#> <dbl>
#> 1 1
#> 2 2
The second row is appended, but the error is still raised.
Plausible Cause
In R/Andromeda.R (same on main and develop), first is only set to FALSE in the branch that creates the table:
first <- TRUE
while (!DBI::dbHasCompleted(queryResult) || first) {
batch <- DBI::dbFetch(queryResult, n = DBFETCH_BATCH_SIZE)
...
if (first && !appendToTable) {
andromeda[[andromedaTableName]] <- batch
first <- FALSE
} else {
Andromeda::appendToTable(andromeda[[andromedaTableName]], batch)
}
}
With appendToTable = TRUE, the else branch always runs and first stays TRUE. The loop condition
!dbHasCompleted(queryResult) || first therefore stays true after the last batch, and dbFetch() is called again on a
completed result set. The PostgreSQL JDBC driver rejects that with "This ResultSet is closed". I have only tested
PostgreSQL. On a driver that returns an empty data frame instead of erroring, the loop might never end; I haven't
checked.
Suggested fix
Clear first after the first batch in both branches:
if (first && !appendToTable) {
andromeda[[andromedaTableName]] <- batch
} else {
Andromeda::appendToTable(andromeda[[andromedaTableName]], batch)
}
first <- FALSE
Further, a test that calls querySqlToAndromeda() twice on the same table, the second time with appendToTable = TRUE, and checks
the combined row count would cover this.
Activity