Skip to content

querySqlToAndromeda(appendToTable = TRUE) errors with "This ResultSet is closed" after appending #346

Open

Description

@fusarolimichele

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.

Environment

  • DatabaseConnector 8.0.0
  • Andromeda 1.2.1
  • PostgreSQL 18.6, JDBC driver 42.7.3
  • R 4.6.0 on macOS (aarch64)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions