An open API service indexing awesome lists of open source software.

https://github.com/tobilg/polyglot

Rust/Wasm-powered SQL transpiler
https://github.com/tobilg/polyglot

rust sql sqlglot transpiler wasm

Last synced: about 1 month ago
JSON representation

Rust/Wasm-powered SQL transpiler

Awesome Lists containing this project

README

          

# Polyglot

Rust/Wasm-powered SQL transpiler for 32 dialects, inspired by [sqlglot](https://github.com/tobymao/sqlglot).

Polyglot parses, generates, transpiles, and formats SQL across 32 database dialects. It ships as a Rust crate ([`polyglot-sql`](https://crates.io/crates/polyglot-sql/)) and a TypeScript/WASM SDK ([`@polyglot-sql/sdk`](https://www.npmjs.com/package/@polyglot-sql/sdk) on npm).

There's also a [playground](https://polyglot-playground.gh.tobilg.com/) where you can try it out in the browser, as well as the [Rust API Docs](https://docs.rs/polyglot-sql/latest/polyglot_sql/) and [TypeScript API Docs](https://polyglot.gh.tobilg.com/).

## Features

- **Transpile** SQL between any pair of 32 dialects
- **Parse** SQL into a fully-typed AST
- **Generate** SQL back from AST nodes
- **Format** / pretty-print SQL
- **Fluent builder API** for constructing queries programmatically
- **Validation** with syntax, semantic, and schema-aware checks
- **AST visitor** utilities for walking, transforming, and analyzing queries

## Supported Dialects (32)

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

## Quick Start

### Rust

```rust
use polyglot_sql::{transpile, DialectType};

// Transpile MySQL to PostgreSQL
let result = transpile(
"SELECT IFNULL(a, b) FROM t",
DialectType::MySQL,
DialectType::Postgres,
).unwrap();
assert_eq!(result[0], "SELECT COALESCE(a, b) FROM t");
```

```rust
use polyglot_sql::builder::*;

// Fluent query builder
let query = select(["id", "name"])
.from("users")
.where_(col("age").gt(lit(18)))
.order_by(["name"])
.limit(10)
.build();
```

See the full [Rust crate README](crates/polyglot-sql/README.md) for more examples.

### TypeScript

```bash
npm install @polyglot-sql/sdk
```

```typescript
import { transpile, Dialect } from '@polyglot-sql/sdk';

// Transpile MySQL to PostgreSQL
const result = transpile(
'SELECT IFNULL(a, b) FROM t',
Dialect.MySQL,
Dialect.PostgreSQL,
);
console.log(result.sql[0]); // SELECT COALESCE(a, b) FROM t
```

```typescript
import { select, col, lit } from '@polyglot-sql/sdk';

// Fluent query builder
const sql = select('id', 'name')
.from('users')
.where(col('age').gt(lit(18)))
.orderBy(col('name').asc())
.limit(10)
.toSql('postgresql');
```

See the full [TypeScript SDK README](packages/sdk/README.md) for more examples.

## Project Structure

```
polyglot/
├── crates/
│ ├── polyglot-sql/ # Core Rust library (parser, generator, builder)
│ └── polyglot-sql-wasm/ # WASM bindings
├── packages/
│ ├── sdk/ # TypeScript SDK (@polyglot-sql/sdk on npm)
│ └── playgroud/ # Playground for testing the SDK (React 19, Tailwind v4, Vite)
└── tools/
├── sqlglot-compare/ # Test extraction & comparison tool
└── bench-compare/ # Performance benchmarks
```

## Examples

Standalone example projects are available in the [`examples/`](examples/) directory. Each one pulls the latest published package and can be run independently.

### Rust

```bash
cargo run --manifest-path examples/rust/Cargo.toml
```

### TypeScript

```bash
cd examples/typescript
pnpm install --ignore-workspace && pnpm start
```

## Building from Source

```bash
# Build Rust core
cargo build -p polyglot-sql

# Build WASM + TypeScript SDK
make build-all

# Or step by step:
cd crates/polyglot-sql-wasm && wasm-pack build --target bundler --release
cd packages/sdk && npm run build
```

## Testing

Polyglot currently runs **10,220 SQLGlot fixture cases** plus additional project-specific suites. All strict pass/fail suites are at **100%** in the latest verification run.

| Category | Count | Pass Rate |
|----------|------:|:---------:|
| SQLGlot generic identity | 956 | 100% |
| SQLGlot dialect identity | 3,554 | 100% |
| SQLGlot transpilation | 5,513 | 100% |
| SQLGlot transpile (generic) | 145 | 100% |
| SQLGlot parser | 29 | 100% |
| SQLGlot pretty-print | 23 | 100% |
| Lib unit tests | 739 | 100% |
| Custom dialect identity | 276 | 100% |
| Custom dialect transpilation | 347 | 100% |
| ClickHouse parser corpus (non-skipped) | 7,047 | 100% |
| **Total (strict pass/fail case count)** | **18,629** | **100%** |

```bash
# Setup fixtures (required once)
make setup-fixtures

# Run all tests
make test-rust-all # All SQLGlot fixture suites
make test-rust-lib # Lib unit tests
make test-rust-verify # Full strict verification suite

# Individual test suites
make test-rust-identity # 956 generic identity cases
make test-rust-dialect # 3,554 dialect identity cases
make test-rust-transpile # 5,513 transpilation cases
make test-rust-pretty # 23 pretty-print cases

# Additional tests
make test-rust-roundtrip # Organized roundtrip unit tests
make test-rust-matrix # Dialect matrix transpilation tests
make test-rust-compat # SQLGlot compatibility tests
make test-rust-errors # Error handling tests
make test-rust-functions # Function normalization tests

# TypeScript SDK tests
cd packages/sdk && npm test

# Full comparison against Python SQLGlot
make test-compare
```

### Benchmarks

```bash
make bench-compare # Compare polyglot-sql vs sqlglot performance
make bench-rust # Rust benchmarks (JSON output)
make bench-python # Python sqlglot benchmarks (JSON output)
cargo bench -p polyglot-sql # Criterion benchmarks
```

### Fuzzing

```bash
cargo +nightly fuzz run fuzz_parser
cargo +nightly fuzz run fuzz_roundtrip
cargo +nightly fuzz run fuzz_transpile
```

## Makefile Targets

| Target | Description |
|--------|-------------|
| `make help` | Show all available commands |
| `make build-all` | Build WASM + Rust (release) |
| `make build-wasm` | Build WASM package + TypeScript SDK |
| `make test-rust` | Run all sqlglot compatibility tests |
| `make test-rust-all` | Run all 10,220 SQLGlot fixture cases |
| `make test-rust-lib` | Run 739 lib unit tests |
| `make test-rust-verify` | Full verification suite |
| `make test-compare` | Compare against Python sqlglot |
| `make bench-compare` | Performance comparison |
| `make extract-fixtures` | Regenerate JSON fixtures from Python |
| `make setup-fixtures` | Create fixture symlink for Rust tests |
| `make generate-bindings` | Generate TypeScript type bindings |
| `make clean` | Remove all build artifacts |

## Licenses

[MIT](LICENSE)
[sqlglot MIT](licenses/SQLGLOT_LICENSE.md)