Repository navigation
Translate dbRemoveTable() statements so it works on SQLite - #349
Merged
Merged
Conversation
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.
Author
|
On SQLite, |
schuemie
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #348
DBI::dbRemoveTable()sends a hard-codedTRUNCATE TABLE @table;followed byDROP TABLE @table;directly to the database. SQLite has noTRUNCATE TABLE, so the call fails withnear "TRUNCATE": syntax errorbefore theDROP TABLEruns, 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 rendersTRUNCATE TABLE @table; DROP TABLE @table;, translates it withSqlRender::translate()for the connection's dbms, and executes each resulting statement separately (same approach asdropEmulatedTempTables()inR/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.tests/testthat/test-dbRemoveTable.R, which runs on all configured test servers: inserts a temp table, removes it withdbRemoveTable(), and checks the table is gone. On SQLite and DuckDB it also removes a permanent table in themainschema and checksexistsTable()afterwards. Without the fix the SQLite case fails withnear "TRUNCATE": syntax errorand the DuckDB case withParser 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 ondevelop(SQLite and DuckDB), passes with this change (12 expectations, SQLite and DuckDB).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 differentbigrqueryversion (test-dbplyr.R), identical ondevelop.R CMD check --no-tests --ignore-vignettes --no-manual: Status: OK.I did not edit NEWS.md.