Skip to content

feat(dialect): add comparison coercion and boolean text hooks - #406

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

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

Conversation

@alexskinner

@alexskinner alexskinner commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Part of #403.

Depends on #404 (please review only the last commit; I'll rebase once #404 lands).

What

Two new SqlDialect hooks. Both default to current behaviour, so GenericSqlDialect is still a no-op.

  • comparison_type(left, right) -> Option<LogicalType> picks the type both operands of a comparison are cast to. The binder asks for it when it binds =, <>, <, <=, >, >=, IN lists and BETWEEN. If the dialect returns None for every operand pair, nothing is cast and the built-in coercion applies exactly as before.
  • boolean_text(value) -> Option<&'static str> picks the text of a boolean in CAST(... AS CHAR) or CAST(... AS VARCHAR). The default keeps 'true'/'false'.
  • DynDialect forwards both hooks to its current dialect.
  • Rustdoc on both hooks and two lines in the dialect section of docs/features.md.

Why

Dialects differ on how mixed-type comparisons coerce. For example, some compare a number with a string numerically, and some render booleans as '1'/'0'. These hooks let a dialect crate set those rules without branches in core. See #403.

Testing

  • One unit test in src/dialect/mod.rs runs the same query against the generic dialect and a test-only dialect that compares numbers with strings as DOUBLE and renders booleans as '1'/'0': the generic results are unchanged, the test dialect's follow both hooks.
  • make test (650 passed, 2 ignored), make test-async (595 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 top of feat(dialect): add pluggable SqlDialect trait and DynDialect #404, no new dependencies.
  • comparison_type is applied in the binder, so it needs no new state: the casts it adds are ordinary TypeCast expressions, and constant folding and execution see them as usual.
  • boolean_text is also resolved in the binder, from the database's dialect, with no thread-local. When the dialect overrides it, the binder binds CAST(b AS VARCHAR) as CAST(CASE b WHEN TRUE THEN '1' WHEN FALSE THEN '0' END AS VARCHAR), so NULL stays NULL and the cast evaluators stay dialect-free. I didn't store the text in the cast evaluator itself because evaluators are looked up by their (from, to) type codes, which are serialized. With the generic dialect the cast binds exactly as before.
  • The hook covers explicit CASTs. Implicit boolean-to-text conversions keep 'true'/'false'. If a dialect needs those too, the binder is where to extend it.

@codecov

codecov Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.31298% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.98%. Comparing base (bb18cee) to head (56805b0).

Files with missing lines Patch % Lines
src/dialect/mod.rs 74.07% 14 Missing ⚠️

❌ Your patch check has failed because the patch coverage (89.31%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #406      +/-   ##
==========================================
- Coverage   92.99%   92.98%   -0.02%     
==========================================
  Files         275      275              
  Lines       39243    39360     +117     
==========================================
+ Hits        36495    36598     +103     
- Misses       2748     2762      +14     
Flag Coverage Δ
rust 92.98% <89.31%> (-0.02%) ⬇️

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

Files with missing lines Coverage Δ
src/binder/expr.rs 93.97% <100.00%> (+0.69%) ⬆️
src/binder/parser.rs 90.35% <100.00%> (+0.01%) ⬆️
src/dialect/mod.rs 85.18% <74.07%> (-3.71%) ⬇️
🚀 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-coercion branch from f479bf2 to 2e6fd91 Compare November 21, 2026 13:38
@cursor
cursor Bot force-pushed the upstream/dialect-coercion branch 3 times, most recently from 0c24da3 to 9bcfbd1 Compare November 21, 2026 19:08
@KKould
KKould force-pushed the upstream/dialect-coercion branch 2 times, most recently from 2f62018 to 79b264e Compare November 21, 2026 20:27

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

I’ve simplified some of the implementation; the PR looks good

@KKould KKould added the enhancement New feature or request label Nov 21, 2026
@KKould
KKould merged commit 2610d5a into KipData:main Nov 21, 2026
14 checks passed
cursoragent and others added 2 commits November 22, 2026 12:24
Add two SqlDialect hooks, both defaulting to the current behaviour:

- comparison_type(left, right) picks the type both operands of a
  comparison (=, <>, <, <=, >, >=, IN lists, BETWEEN) are cast to. The
  binder applies it while binding the comparison; when the dialect
  returns None for every operand pair, nothing is cast and the built-in
  coercion applies as before.
- boolean_text(value) picks the text of a boolean in CAST(.. AS CHAR)
  or CAST(.. AS VARCHAR). The binder reads it from the database's
  dialect when it binds the cast, so the text is fixed in the plan and
  cast evaluators stay dialect-free.

GenericSqlDialect implements neither, so it is still a no-op, and
DynDialect forwards both to its current dialect. A unit test in
src/dialect/mod.rs checks both hooks against the generic results.

Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
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