Skip to content

Translate dbRemoveTable() statements so it works on SQLite - #349

Merged
schuemie merged 1 commit into
OHDSI:developfrom
DG47:fix/dbRemoveTable-translate
Nov 26, 2026
Merged

schuemie merged 1 commit into
OHDSI:developfrom
DG47:fix/dbRemoveTable-translate

Conversation

@DG47

@DG47 DG47 commented Oct 30, 2026

Copy link
Copy Markdown

Fixes #348

DBI::dbRemoveTable() sends a hard-coded TRUNCATE TABLE @table; followed by DROP TABLE @table; directly to the database. SQLite has no TRUNCATE TABLE, so the call fails with near "TRUNCATE": syntax error before the DROP TABLE runs, and the table is left in place. Temp table names (e.g. dbRemoveTable(connection, "#temp")) fail the same way on SQLite and DuckDB because the # prefix is sent verbatim.

Before the 7.0 DBI refactoring the method translated its statements (so SQLite received DELETE FROM); the translation was dropped together with the translation of user-supplied SQL, but these two statements are built by the package itself and must be valid on every supported dialect.

Changes:

  • dbRemoveTable() now renders TRUNCATE TABLE @table; DROP TABLE @table;, translates it with SqlRender::translate() for the connection's dbms, and executes each resulting statement separately (same approach as dropEmulatedTempTables() in R/Sql.R). The statements are still executed one at a time, as required by dbRemoveTable does not drop the table on Spark #310. Translation only applies to the statements the method generates; the DBI query/execute functions are unchanged.
  • Added tests/testthat/test-dbRemoveTable.R, which runs on all configured test servers: inserts a temp table, removes it with dbRemoveTable(), and checks the table is gone. On SQLite and DuckDB it also removes a permanent table in the main schema and checks existsTable() afterwards. Without the fix the SQLite case fails with near "TRUNCATE": syntax error and the DuckDB case with Parser Error: syntax error at or near "#".

Verification (R 4.6.1, macOS arm64):

  • testthat::test_dir("tests/testthat", filter = "dbRemoveTable") against the installed package: fails on develop (SQLite and DuckDB), passes with this change (12 expectations, SQLite and DuckDB).
  • Full testthat::test_dir("tests/testthat"): 43 tests, 0 failures. The only errors are the pre-existing ones that need JDBC drivers / a PostgreSQL server (test-connection.R, test-connectionDetails.R, test-DBItest.R) or a different bigrquery version (test-dbplyr.R), identical on develop.
  • R CMD check --no-tests --ignore-vignettes --no-manual: Status: OK.

I did not edit NEWS.md.

dbRemoveTable() sent a hard-coded TRUNCATE TABLE statement to the
database without translation. SQLite has no TRUNCATE TABLE, so the call
failed with 'near "TRUNCATE": syntax error' before DROP TABLE ran and the
table was left in place. Temp table names ("#temp") were also sent
verbatim.

Render and translate the two statements with SqlRender for the
connection's dbms, as dropEmulatedTempTables() already does, and execute
each resulting statement separately.
@DG47

DG47 commented Nov 21, 2026

Copy link
Copy Markdown
Author

On SQLite, dbRemoveTable() fails with near "TRUNCATE": syntax error and leaves the table in place, because its TRUNCATE TABLE / DROP TABLE statements are sent untranslated. This change runs them through SqlRender::translate() first, the same way dropEmulatedTempTables() does, and fixes #348. Would you have time to review it, @schuemie?

@schuemie
schuemie merged commit 84cd891 into OHDSI:develop Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants