Repository navigation
Description
Problem
effect/sql treats SQL as one language that every database speaks with minor variations. A SqlClient carries one dialect from a closed list, and code that has to work across databases branches on that list. Each new database stretches this assumption a little further.
Two things share the name "SQL" here, and only one of them is universal:
- The session: connections and pooling, transactions, streaming, row transforms, errors, tracing, reactivity, and the mechanics of the
sqltemplate (fragments, parameters, identifiers, helpers such assql.insert). These work the same way regardless of database. - The language: the SQL text itself. In practice it is written for one dialect. Application code targets one database. The only code that needs to run on several dialects is the set of multi-dialect library modules, mostly in
effectitself.
The current design models the language as if it were as universal as the session. Where that assumption shows:
1. Dialects are a closed list in effect
Statement.Dialect is "sqlite" | "pg" | "mysql" | "mssql" | "clickhouse", and a compiler must declare one of them. So:
- A driver cannot live outside this repository unless its database behaves exactly like one of the five.
- Every new dialect is a breaking change.
onDialectrequires a callback for every member, so each addition breaks every exhaustive caller (Fix sql.insert and sql.updateValues crashing on an empty list #9453, and feat(effect): add the trino SQL dialect #10622 for Trino). - Databases that share a wire protocol but not a language cannot be told apart from it. For example, databases that speak Postgres's wire protocol, such as CockroachDB or Redshift, don't accept all of Postgres's SQL.
2. Multi-dialect modules silently run SQL written for another dialect
These modules in effect choose SQL with onDialectOrElse:
Persistence,KeyValueStore.layerSql,PersistedQueue;- the event journal and event log servers;
- cluster message and runner storage;
Migrator,SqlModel.
For a dialect they don't list, they run the orElse branch, which is SQLite or Postgres flavoured. ClickHouse falls through this way today, and Trino would too. Nothing tells the user which modules support which databases. A module that doesn't support a dialect fails at runtime, against the database, with a syntax error.
3. The compiler checks dialect names
The statement compiler hardcodes behaviour by name:
A dialect that differs in one of these ways has to be added to core, even when its driver supplies its own compiler.
4. SqlClient promises capabilities that not every engine has
withTransaction, nested transactions through savepoints, and RETURNING (which SqlModel relies on) are part of the generic client, but not every engine supports them:
- ClickHouse's
BEGINis experimental, soMigratorspecial-cases it. - Trino has transactions but no savepoints (SQL driver for Trino (@effect/sql-trino), and a trino SQL dialect #10621).
- Open-source Spark SQL has neither.
The client's type says these work; the database decides at runtime.
What prompted this
Analytics and lakehouse engines in particular don't fit the current model:
- Trino has a proposal in SQL driver for Trino (@effect/sql-trino), and a trino SQL dialect #10621, with PRs feat(effect): add the trino SQL dialect #10622 and Add a Trino SQL client and dialect #10632.
- Apache Spark SQL / Databricks has none yet; it led to this issue.
- Others, such as DuckDB, Snowflake and BigQuery, would have the same problem.
Each needs a change to the closed list, falls through the multi-dialect modules, and lacks some of what SqlClient promises. Adding them one at a time keeps the assumption and makes each addition a breaking change in effect.
Not proposing a fix yet
This issue is about agreeing on the problem. Some questions a fix would have to answer:
- Should dialect identity be open, so that a driver package can define a dialect without changing
effect? - Should multi-dialect modules state which dialects they support, and fail clearly (or not exist) for the rest, instead of running another dialect's SQL?
- Should capabilities that depend on the engine (transactions, savepoints,
RETURNING) be visible in the client's type rather than discovered at runtime? - Is "SQL" the right level for one shared abstraction, or should the shared part be the session, with the language handled per dialect?
Related: #10621, #10622, #10632 (Trino), #9453 (earlier type-level break to Statement), #2693 (made @effect/sql dialect-agnostic).
Activity
[-]effect/sql: let drivers define their own dialect, and a Spark SQL driver[/-][+]effect/sql treats SQL as one language across dialects[/+]