Skip to content

dbRemoveTable() fails on SQLite with 'near "TRUNCATE": syntax error' and leaves the table in place #348

Open

Description

@DG47

Describe the bug

DBI::dbRemoveTable() on a DatabaseConnector SQLite connection fails and does not drop the table. The method sends a hard-coded TRUNCATE TABLE statement to the database without translating it, and SQLite has no TRUNCATE TABLE statement. Since the TRUNCATE runs before the DROP TABLE, the error aborts the call and the table remains.

The same happens when passing a temp table name (e.g. dbRemoveTable(connection, "#temp"), as used in extras/TestCode.R): the # prefix is sent to the database verbatim, which SQLite and DuckDB reject with a parser error.

In DatabaseConnector 6.x dbRemoveTable() translated its statements, so TRUNCATE TABLE became DELETE FROM on SQLite. Since the 7.0 DBI refactoring (commit 5969f2f "Removing all translation from DBI functions") the method sends the raw statements. dropEmulatedTempTables() in R/Sql.R runs the same TRUNCATE TABLE ...; DROP TABLE ...; pattern but translates it with SqlRender::translate() first, so it keeps working on SQLite.

To Reproduce

library(DatabaseConnector)
connection <- connect(dbms = "sqlite", server = tempfile(fileext = ".sqlite"))
insertTable(connection, databaseSchema = "main", tableName = "my_table", data = data.frame(x = 1:3))

DBI::dbRemoveTable(connection, "my_table", databaseSchema = "main")
#> Error: near "TRUNCATE": syntax error

getTableNames(connection, "main")
#> [1] "my_table"

disconnect(connection)

The same call on a DuckDB connection succeeds because DuckDB accepts TRUNCATE TABLE.

Expected behavior

dbRemoveTable() returns TRUE and the table no longer exists, on every platform DatabaseConnector supports (SQLite included).

Environment

  • DatabaseConnector 8.0.0 (develop, commit c7a5bed) and 7.x
  • SqlRender 1.19.7
  • RSQLite 2.4.x, duckdb 1.x
  • R 4.6.1 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