polyglot-sql
Core SQL parsing and dialect translation library for Rust. Parses, generates, transpiles, and formats SQL across 32 database dialects.
Part of the Polyglot project.
Features
- Parse SQL into a fully-typed AST with 200+ expression types
- Generate SQL from AST nodes for any target dialect
- Transpile between any pair of 32 dialects in one call
- Format / pretty-print SQL
- Fluent builder API for constructing queries programmatically
- AST traversal utilities (DFS/BFS iterators, transform, walk)
- Validation with syntax checking and error location reporting
- Schema module for column resolution and type annotation
Usage
Transpile
use ;
let result = transpile.unwrap;
assert_eq!;
Parse + Generate
use ;
let ast = parse.unwrap;
let sql = generate.unwrap;
assert_eq!;
Fluent Builder
use *;
// SELECT id, name FROM users WHERE age > 18 ORDER BY name LIMIT 10
let expr = select
.from
.where_
.order_by
.limit
.build;
Expression Helpers
use *;
// Column references (supports dotted names)
let c = col;
// Literals
let s = lit; // 'hello'
let n = lit; // 42
let f = lit; // 3.14
let b = lit; // TRUE
// Operators
let cond = col.gte.and;
// Functions
let f = func;
CASE Expressions
use *;
let expr = case
.when
.when
.else_
.build;
Set Operations
use *;
let expr = union_all
.order_by
.limit
.build;
INSERT, UPDATE, DELETE
use *;
// INSERT INTO users (id, name) VALUES (1, 'Alice')
let ins = insert_into
.columns
.values
.build;
// UPDATE users SET name = 'Bob' WHERE id = 1
let upd = update
.set
.where_
.build;
// DELETE FROM users WHERE id = 1
let del = delete
.where_
.build;
AST Traversal
use ;
let ast = parse.unwrap;
let columns = get_columns;
let tables = get_tables;
Validation
use ;
let result = validate;
// result contains error with line/column location
Supported Dialects
Athena, BigQuery, ClickHouse, CockroachDB, Databricks, Doris, Dremio, Drill, Druid, DuckDB, Dune, Exasol, Fabric, Hive, Materialize, MySQL, Oracle, PostgreSQL, Presto, Redshift, RisingWave, SingleStore, Snowflake, Solr, Spark, SQLite, StarRocks, Tableau, Teradata, TiDB, Trino, TSQL
Feature Flags
| Flag | Description |
|---|---|
bindings |
Enable ts-rs TypeScript type generation |