Add bulk_insert dialect hook with DuckDB CSV implementation#57
Merged
Conversation
Introduces DatabaseDialect.bulk_insert(conn, table, records) as the single insertion point for temp-table loading. The base implementation falls back to SQLAlchemy executemany (no behaviour change for PostgreSQL, MySQL, MSSQL). DuckDBDialect overrides it with a write-to-tempfile / read_csv approach that is significantly faster for large payloads: - Records are serialised to a NamedTemporaryFile CSV (stdlib csv, no extra dependencies). - read_csv is called with all_varchar=true; each column is then explicitly CAST to its target DuckDB type (BIGINT, DOUBLE, TIMESTAMPTZ, BOOLEAN, …) in the SELECT clause, avoiding auto_detect type mis-identification. - LargeBinary (record-hash) columns are hex-encoded in the CSV and decoded with unhex() in SQL. - SQLAlchemy Python-side scalar defaults (e.g. default=False on temp_exists) are materialised manually before writing the CSV, matching the behaviour of executemany. - The temp file is deleted in a finally block even when an error occurs. document.py: insert_into_temp_tables now calls dialect.bulk_insert(conn, query.table, records) instead of conn.execute(query, records) directly. Tests: new tests/test_bulk_insert.py covers base-class fallback, numeric types (incl. BigInteger/SmallInteger subclass ordering), boolean, datetime, binary, scalar defaults, and empty-records no-op.
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.
Introduces DatabaseDialect.bulk_insert(conn, table, records) as the single insertion point for temp-table loading. The base implementation falls back to SQLAlchemy executemany (no behaviour change for PostgreSQL, MySQL, MSSQL). DuckDBDialect overrides it with a write-to-tempfile / read_csv approach that is significantly faster for large payloads:
document.py: insert_into_temp_tables now calls
dialect.bulk_insert(conn, query.table, records) instead of conn.execute(query, records) directly.
Tests: new tests/test_bulk_insert.py covers base-class fallback, numeric types (incl. BigInteger/SmallInteger subclass ordering), boolean, datetime, binary, scalar defaults, and empty-records no-op.