Skip to content

protegrity/sqlglot-net

SqlGlot.Net

A standalone .NET port of sql-glot-rust — a SQL parser, optimizer, and transpiler library.

This project is a pure-managed C# implementation with no native dependencies, suitable for ADO.NET drivers, EF Core providers, AOT-compiled apps, Blazor WebAssembly, Azure Functions, and other .NET hosts.

Status

This is a foundational port that mirrors the Rust crate layout 1:1, so further porting is mechanical. The initial drop covers the end-to-end happy path:

Module Rust LOC .NET status
tokens/ ~2,000 ✅ Lexer ported (literals, operators, identifiers, comments, keywords)
errors/ ~30 ✅ Exception hierarchy
dialects/ ~1,000 ✅ Dialect enum + quoting rules (30 dialects)
ast/types.rs ~2,300 ✅ Statement + Expr subset (SELECT path, full operator set)
parser/sql_parser.rs ~4,000 ✅ Recursive-descent SELECT/FROM/JOIN/WHERE/GROUP BY/HAVING/ORDER BY/LIMIT
generator/sql_generator.rs ~3,300 ✅ Visitor-based emitter with dialect quoting
builder/ ~1,500 ⏳ Fluent builder API (TODO)
optimizer/ ~3,000 ⏳ TODO
executor/, planner/, schema/, diff/ ~4,000 ⏳ TODO

Roundtrips today: SELECT … FROM … [JOIN …] [WHERE …] [GROUP BY …] [HAVING …] [ORDER BY …] [LIMIT n [OFFSET n]] with full expression grammar (arithmetic, comparison, logical, IS NULL, IN, BETWEEN, LIKE, CASE, CAST, function calls, parenthesised sub-expressions, qualified columns).

Layout

sqlglot-net/
├── SqlGlot.Net.sln
├── src/
│   └── SqlGlot.Net/
│       ├── SqlGlot.Net.csproj
│       ├── SqlGlot.cs                  # public Parse / Generate / Transpile API
│       ├── Errors/
│       │   └── SqlGlotException.cs
│       ├── Dialects/
│       │   └── Dialect.cs              # 30-dialect enum + QuoteStyle helpers
│       ├── Tokens/
│       │   ├── TokenType.cs
│       │   ├── Token.cs
│       │   └── Tokenizer.cs
│       ├── Ast/
│       │   ├── Statement.cs            # Statement abstract + sub-records
│       │   ├── Expr.cs                 # Expr abstract + sub-records
│       │   └── Common.cs               # QuoteStyle, BinaryOperator, etc.
│       ├── Parser/
│       │   └── SqlParser.cs
│       └── Generator/
│           └── SqlGenerator.cs
└── tests/
    └── SqlGlot.Net.Tests/
        ├── SqlGlot.Net.Tests.csproj
        ├── TokenizerTests.cs
        ├── ParserTests.cs
        ├── GeneratorTests.cs
        └── RoundtripTests.cs

Quick start

using SqlGlot.Net;
using SqlGlot.Net.Dialects;

// Parse
var ast = SqlGlot.Parse("SELECT a, b FROM t WHERE a > 1", Dialect.Ansi);

// Generate
var sql = SqlGlot.Generate(ast, Dialect.Postgres);
// => SELECT a, b FROM t WHERE a > 1

// Transpile in one call
var tsql = SqlGlot.Transpile(
    "SELECT \"id\" FROM users",
    from: Dialect.Postgres,
    to: Dialect.Tsql);
// => SELECT [id] FROM users

Build & test

dotnet build
dotnet test
dotnet pack src/SqlGlot.Net/SqlGlot.Net.csproj -c Release

Targets net10.0. No third-party runtime dependencies.

Project health

  • CI runs on GitHub Actions for every push and pull request.
  • Community support and contribution guidance live in CONTRIBUTING.md, SECURITY.md, and SUPPORT.md.
  • The package metadata is set up for public NuGet publishing.

Porting strategy

The Rust source is the source of truth. Each .NET type carries an // Rust: <path>:<symbol> comment that points to the originating Rust item so syncing future Rust changes is a line-by-line exercise.

Roadmap order (recommended): expression grammar gaps (window functions, EXTRACT, INTERVAL) → INSERT/UPDATE/DELETE/MERGE → CREATE/ALTER/DROP → builder fluent API → optimizer passes → schema & planner → diff/executor.

About

Standalone .NET port of sql-glot-rust, a SQL parser, optimizer, and transpiler library.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages