BUG: Save and restore user SQLite adapters in to_sql() (GH#64337)#66297
Open
q121212 wants to merge 3 commits into
Open
BUG: Save and restore user SQLite adapters in to_sql() (GH#64337)#66297q121212 wants to merge 3 commits into
q121212 wants to merge 3 commits into
Conversation
Every call to DataFrame.to_sql() with a SQLite connection created a new SQLiteTable instance whose __init__ unconditionally called sqlite3.register_adapter() and sqlite3.register_converter(). These are global mutations, overwriting any custom adapters/converters the user may have registered. Fix: use a module-level _SQLITE_ADAPTERS_REGISTERED flag so adapters and converters are registered only once per process. After the first to_sql() call, subsequent calls are no-ops. This preserves pandas' required adapters (needed because Python 3.12+ removed default datetime adapters) while not stomping on user handlers.
Every to_sql() call unconditionally registered pandas' own adapters and converters via sqlite3.register_adapter()/register_converter(), overwriting any custom handlers the user may have set up. Fix: save the currently registered adapters (for time, date, datetime) and converters (for 'date', 'timestamp') before registering pandas' own, then restore them after the insert operation completes. This handles both cases: - User registered adapters BEFORE the first to_sql() — restored - User registered adapters AFTER a to_sql() — never overwritten on subsequent calls Restore happens in a finally block in _execute_insert() and _execute_insert_multi() to ensure cleanup even on error.
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.
Closes #64337
This is a more comprehensive fix that preserves user-registered adapters and
converters even when they were registered before the first
to_sql()call.How it works
In
SQLiteTable.__init__(), save the currently registered adapters(for
time,date,datetime) and converters (for"date","timestamp")that pandas will override.
Register pandas' adapters/converters as before (needed for the insert
operation to work on Python 3.12+).
After
_execute_insert()or_execute_insert_multi()returns, restorethe user's original adapters/converters via a
finallyblock.Changes
File:
pandas/io/sql.pySQLiteTable.__init__()— save existing adapters/converters beforeregistering pandas' own
_restore_date_adapters()method — restores saved handlers_execute_insert()— wrapssuper()call with try/finally thatcalls
_restore_date_adapters()_execute_insert_multi()— same patternAdded tests
File:
pandas/tests/io/test_sql.pytest_to_sql_restores_user_adapters_after_insert— custom adapterregistered BEFORE to_sql() is restored after the operation
test_to_sql_restores_user_converters_after_insert— same for converterstest_to_sql_restores_no_crash_when_no_prior_registration— edge case:no prior user registration, should not crash on restore
Comparison with simple version
Checklist