Skip to content

Add DuckDB support - #68

Merged
gkennos merged 22 commits into
mainfrom
65-duckdb-support
Nov 5, 2026
Merged

gkennos merged 22 commits into
mainfrom
65-duckdb-support

Conversation

@nicoloesch

@nicoloesch nicoloesch commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Changes

  • Adds DuckDB support through a new duckdb extra.
  • Covers table creation, vocabulary loading, schema info/doctor/reconcile, foreign-key and index maintenance, and backup/restore.
  • Features DuckDB can't support raise FeatureNotSupportedError.
  • The cyclic and self-referential foreign keys are marked use_alter=True.
  • These are the domain, vocabulary and concept class keys to concept, the relationship reverse key, the episode parent key, and the visit preceding and parent keys.
  • DuckDB can't add those 8 keys after table creation, so it enforces the other 169 of 177.
  • The 8 unenforced keys are checked by anti-join in foreign-keys validate, reconcile and doctor --deep.
  • PostgreSQL and SQLite keep the same constraints and names.
  • Concept now loads before Relationship.
  • A cyclic table loaded alone into an empty database is validated and rolled back on failure.
  • On DuckDB, reloading a populated concept, person, visit_occurrence, provider or care_site with replace is rejected up front, with a message pointing to upsert.
  • DuckDB backup writes each table to one Parquet file in a directory, in-process.
  • DuckDB restore loads row data into existing tables in dependency order and resets owned sequences.
  • cache_scope now resolves relative DuckDB file paths the same way as SQLite ones.
  • Breaking: person.visit_occurrence_id and person.visit_detail_id are removed on all dialects, to match CDM 5.4 and break the person/visit cycle.
  • Existing databases keep those columns until reconcile flags them, so check them for populated rows before migrating.

Adapts to orm-loader 3

  • Requires orm-loader>=3.0.0,<4 (One file-to-database pipeline with a single conversion policy orm-loader#46), which replaces the CSV loaders with one file pipeline.
  • CDMTableBase and the maintenance code use FileLoadableTableInterface and FileTableProtocol, and call load_file.
  • Loader backends are resolved with resolved, and load_vocab passes it to load_file, so DuckDB can classify the foreign keys it cannot enforce.
  • load_vocab validates the loaded tables with validate_foreign_keys instead of validate_deferred_fks.
  • The load no longer retries after creating a missing staging table, because load_file creates and drops its own.
  • Breaking: the insert_if_empty merge strategy is now insert, and --merge-strategy insert inserts without checking that the target is empty. A key collision raises the database error, and the crash-retry truncation applies to insert.
  • Breaking: --staging-chunk-size and the auto value of --quote-mode are removed. The default quote mode is by_delimiter.
  • The installation docs and the full-text test use relaxed_foreign_keys(session) instead of the old bulk load context.

Checklist

  • Applied exactly one label (breaking, feature, fix, dependencies, or chore)
  • Tests pass locally (uv run pytest -q)
  • Lint passes (uv run ruff check .)

@gkennos
gkennos self-requested a review October 23, 2026 23:26
MaintenanceContext._foreign_keys_creatable() and reconcile_schema()'s
FK-missing check only consulted resolved's static schema-translate map,
so a tag claimed only via create_engine(schema_claims=[...]) raised
UnregisteredSchemaTagError even though the connection itself already
resolves it. Both now build physical_schemas from the connection's own
claimed tags before calling foreign_key_can_span().
…to loader backends, staging chunk size and staging retry removed
Comment thread omop_alchemy/maintenance/cli_vocab.py Resolved
Comment thread tests/conftest.py Resolved
Comment thread tests/test_duckdb_load_vocab_source.py Resolved
Comment thread tests/test_load_vocab_postgres.py Resolved
Comment thread omop_alchemy/maintenance/cli_vocab.py Resolved
- A failed foreign key validation after a load raises VocabularyLoadError
  saying the rows are committed and how to recover
- --merge-batch-size help and docs say earlier batches stay committed on
  PostgreSQL and DuckDB
- Fixtures add concept_ancestor, concept_relationship and concept_synonym
  rows, including quoted and non-ASCII names; Postgres load tests cover
  bulk and non-bulk mode

@gkennos gkennos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed changes passed local validation across SQLite, DuckDB and PostgreSQL on both SQLAlchemy versions

Happy with the local DOUBLE normalisation workaround + cleanup tracked in #71

@gkennos
gkennos merged commit 658a98e into main Nov 5, 2026
5 checks passed
@gkennos
gkennos deleted the 65-duckdb-support branch November 5, 2026 06:15
nicoloesch added a commit that referenced this pull request Nov 7, 2026
Pulls in DuckDB support (#68). Resolved conflicts in cdm_table_base.py,
person.py, and maintenance/tables.py by keeping this branch's
CDMTableBase.__init_subclass__ auto-tagging and the extension-owner
field, while keeping main's new MaintenanceTable.model_cls field.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking Incompatible API change. MAJOR: x+1.y.z

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add DuckDB dialect support

2 participants