Skip to content

feat(dialect): add pluggable SqlDialect trait and DynDialect - #404

Merged
KKould merged 2 commits into
KipData:mainfrom
alexskinner:upstream/dialect-registry
Nov 21, 2026
Merged

KKould merged 2 commits into
KipData:mainfrom
alexskinner:upstream/dialect-registry

Conversation

@alexskinner

@alexskinner alexskinner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Part of #403.

What

  • Adds src/dialect/mod.rs with the SqlDialect trait (name, parser_dialect, rewrite, map_data_type, map_column_type), GenericSqlDialect and DynDialect.
  • Database<S, D: SqlDialect = GenericSqlDialect> (and Transaction<'a, S, D = GenericSqlDialect>, DataBaseBuilder<D = GenericSqlDialect>) own one dialect, set at build time with DataBaseBuilder::dialect(d) and fixed after that. Without .dialect(..), a database uses GenericSqlDialect, statically dispatched, and behaves exactly as on main. Parsing uses its parser dialect and runs its rewrite on each statement. The binder gets it by reference: column definitions resolve types through map_column_type, and CAST and typed literals through map_data_type.
  • DynDialect is an opt-in for choosing the dialect at runtime, passed with .dialect(dynamic). It follows your sketch: new() starts with the generic dialect registered and current, register<D: SqlDialect + Send + Sync + 'static>(d) adds or replaces one by name, switch(&mut self, name) -> bool picks the current one, and current() -> &dyn SqlDialect returns it. It implements SqlDialect by forwarding every hook to current().
  • There is no registry lookup or thread-local. The dialect lives on the Database, and Database stays Send + Sync.
  • Code that doesn't mention a dialect compiles unchanged, because the type parameter defaults to GenericSqlDialect. Every hook defaults to current behaviour.
  • Rustdoc on the public extension API (SqlDialect, DynDialect, DataBaseBuilder::dialect) and a section in docs/features.md.

Why

This lets an external crate supply dialect behaviour without changing core. A host that knows its dialect at compile time passes the concrete type. A host that picks it from configuration, for example one Database per datasource, opts in to DynDialect, registers its dialects and switches before building. See #403.

Testing

  • No test file is added. Two unit tests in src/dialect/mod.rs: one builds a database with a test dialect via .dialect(..) and checks column-type mapping and statement rewriting end to end (and that the generic build rejects the custom type); the other checks DynDialect register/switch, including an unknown name, and runs a query through the switched dialect. Parser and generic behaviour stay covered by the existing sqllogictest files.
  • make test (649 passed, 2 ignored), make test-async (594 passed), make test-slt and make test-slt-async (198 files each), make fmt and all four make clippy invocations pass. make test-wasm passes too.

Notes for review

  • One commit on current main (0.7.1), no new dependencies.
  • The binder holds &dyn SqlDialect rather than a third type parameter, so Binder and its many impls aren't monomorphised per dialect. Hooks run once per bound item, not per row, so that costs one virtual call each. Parsing and rewrite are called on D directly.
  • DataBaseBuilder::path builds the default (generic) builder, and .dialect(d) turns it into a DataBaseBuilder<D>. All other builder methods and build_* work for any D. switch takes &mut self, so a DynDialect's dialect is chosen before the database is built. A &mut self method on Database could expose it later if that's useful.
  • Follow-up PRs (feat(dialect): add comparison coercion and boolean text hooks #406-feat(dialect): add session transaction control #409) add the remaining hooks, one per PR, each on top of this commit.

Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/mod.rs Resolved
Comment thread src/dialect/mod.rs Resolved
@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.35793% with 18 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.99%. Comparing base (a447022) to head (8e77f05).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/dialect/mod.rs 88.88% 18 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #404      +/-   ##
==========================================
+ Coverage   92.97%   92.99%   +0.01%     
==========================================
  Files         274      275       +1     
  Lines       38990    39243     +253     
==========================================
+ Hits        36252    36495     +243     
- Misses       2738     2748      +10     
Flag Coverage Δ
rust 92.99% <93.35%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/binder/aggregate.rs 96.65% <ø> (ø)
src/binder/create_table.rs 78.12% <ø> (ø)
src/binder/mod.rs 95.18% <100.00%> (+0.13%) ⬆️
src/binder/parser.rs 90.34% <100.00%> (+0.25%) ⬆️
src/db.rs 94.19% <100.00%> (+0.66%) ⬆️
src/db/program.rs 82.60% <100.00%> (ø)
src/optimizer/heuristic/optimizer.rs 98.70% <ø> (ø)
src/orm/ddl.rs 90.37% <100.00%> (ø)
src/orm/dml.rs 100.00% <ø> (ø)
src/orm/dql.rs 96.68% <ø> (ø)
... and 4 more

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cursor
cursor Bot force-pushed the upstream/dialect-registry branch from 201ede3 to f9b242d Compare November 21, 2026 13:38
@cursor
cursor Bot force-pushed the upstream/dialect-registry branch from f9b242d to 690ea32 Compare November 21, 2026 16:37
@alexskinner alexskinner changed the title feat(dialect): add pluggable SqlDialect trait and DialectRegistry feat(dialect): add pluggable SqlDialect trait and DynDialect Nov 21, 2026
Comment thread src/dialect/mod.rs Resolved
Comment thread src/parser/mod.rs Resolved
Comment thread tests/custom_dialect_test.rs Resolved
@KKould

KKould commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

@alexskinner I think it’s nearly complete—just a few minor details left to address. It’s looking very good. Once this PR is merged, I’ll take a look at the subsequent PRs. Thanks for your patience in making these revisions.

@cursor
cursor Bot force-pushed the upstream/dialect-registry branch from 690ea32 to f51be4f Compare November 21, 2026 17:41
Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
@cursor
cursor Bot force-pushed the upstream/dialect-registry branch from f51be4f to 7b10cf2 Compare November 21, 2026 19:08

@KKould KKould 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.

Our discussion was very pleasant and meaningful; thank you so much for your contributions.

@KKould
KKould merged commit bb18cee into KipData:main Nov 21, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants