sqlglot-go

sqlglot-go is a Go library for tokenizing SQL, parsing it into a mutable AST, generating SQL, and
transpiling between dialects. It ports those components, schema handling, and the SQL optimizer from
tobymao/sqlglot v30.17.0. It requires Go 1.23 or later and uses
only the Go standard library.
Getting started
go get github.com/maltzsama/sqlglot-go
Parse and render a PostgreSQL query, then translate a MySQL query to PostgreSQL.
package main
import (
"fmt"
"log"
sqlglot "github.com/maltzsama/sqlglot-go"
"github.com/maltzsama/sqlglot-go/generator"
)
func main() {
expr, err := sqlglot.ParseOne("select id from users", "postgres")
if err != nil {
log.Fatal(err)
}
query, err := sqlglot.Generate(expr, "postgres", generator.Options{})
if err != nil {
log.Fatal(err)
}
fmt.Println(query) // SELECT id FROM users
queries, err := sqlglot.Transpile(
"SELECT IFNULL(total, 0) FROM orders", "mysql", "postgres", generator.Options{},
)
if err != nil {
log.Fatal(err)
}
for _, query := range queries {
fmt.Println(query) // SELECT COALESCE(total, 0) FROM orders
}
}
API guide
Use nil or "" for the base dialect, a registered name such as "postgres", or a resolved
*dialects.Dialect. Some dialects accept settings in the name string, for example
"mysql, mysql_version=8.0.35" to enable eligible MySQL executable comments.
ParseOne returns a KindBlock when its input produces multiple statements. Use Parse to
process them separately. Some unsupported statements parse as a source-preserving KindCommand.
Check the node kind when your application needs a structured statement. Tokenize can also return
tokens for input that the parser rejects. Token offsets are inclusive rune indexes, and Token.Text
may contain a decoded value. See tokens.Token
for the source-span contract.
Qualify columns and inspect scopes
Provide a schema when column resolution depends on table metadata, such as when expanding
SELECT * or resolving an unqualified column across joins. Qualify rewrites the supplied AST and
can panic on invalid schema or qualification failures. Pass a copy when the original AST is still
needed. Generate preserves its input tree.
package main
import (
"fmt"
"log"
sqlglot "github.com/maltzsama/sqlglot-go"
"github.com/maltzsama/sqlglot-go/generator"
"github.com/maltzsama/sqlglot-go/optimizer"
"github.com/maltzsama/sqlglot-go/schema"
)
func main() {
expr, err := sqlglot.ParseOne("SELECT id FROM users", "postgres")
if err != nil {
log.Fatal(err)
}
opts := optimizer.DefaultQualifyOpts()
opts.Dialect = "postgres"
opts.Schema = schema.M("users", schema.M("id", "INT"))
qualified := optimizer.Qualify(expr.Copy(), opts)
query, err := sqlglot.Generate(qualified, "postgres", generator.Options{})
if err != nil {
log.Fatal(err)
}
fmt.Println(query) // SELECT "users"."id" AS "id" FROM "users" AS "users"
for _, scope := range optimizer.TraverseScope(qualified) {
fmt.Println(scope.SelectedSourceNames()) // [users]
}
}
The scope API exposes sources, columns, CTEs, subqueries, and set operations for consumers that
build lineage analysis. The library also provides opt-in search-path qualification, source
resolution reports, and analysis scopes for top-level UPDATE, DELETE, and MERGE.
See DEVIATIONS.md for these extensions and engine-specific behavior.
Dialects and verified coverage
The identity corpus checks parse and generation under the same dialect. It passes 7,609 of
7,609 cases. The transpile corpus checks expected output from upstream validate_all read/write
pairs. These figures describe the committed test corpora. Coverage varies by construct and dialect
pair.
| Dialect |
Name |
Identity cases passing |
| Base |
"" |
980/980 |
| Athena |
athena |
51/51 |
| BigQuery |
bigquery |
689/689 |
| ClickHouse |
clickhouse |
468/468 |
| Databricks |
databricks |
176/176 |
| Doris |
doris |
56/56 |
| Dremio |
dremio |
46/46 |
| DuckDB |
duckdb |
565/565 |
| Hive |
hive |
141/141 |
| ksqlDB |
ksqldb (alias ksql) |
57/57 (project fixtures) |
| MariaDB |
mariadb |
Dedicated tests, no upstream identity corpus |
| Materialize |
materialize |
24/24 |
| MySQL |
mysql |
428/428 |
| Oracle |
oracle |
249/249 |
| PostgreSQL |
postgres |
468/468 |
| Presto |
presto |
170/170 |
| Redshift |
redshift |
171/171 |
| RisingWave |
risingwave |
7/7 |
| Snowflake |
snowflake |
1,543/1,543 |
| Spark |
spark |
227/227 |
| Spark 2 |
spark2 |
Covered by read/write pairs |
| SQLite |
sqlite |
136/136 |
| StarRocks |
starrocks |
145/145 |
| Teradata |
teradata |
84/84 |
| Trino |
trino |
115/115 |
| T-SQL |
tsql |
613/613 |
The transpile corpus passes 8,881 of 8,881 pairs, with 0 remaining gaps. Of the
recorded pairs, 6,760 cross dialects.
The identity corpus has no outstanding gaps
(parity_gaps.txt is empty); see
transpile gaps for the remaining transpile cases. The default
optimizer pipeline passes its full optimizer.sql,
TPC-H, and TPC-DS suites. The standalone simplification fixtures now pass 521/521 cases;
standalone type annotation gaps remain in
the annotation ledger.
Contributing
See CONTRIBUTING.md for checks and PR conventions,
AGENTS.md for the porting rules, and ROADMAP.md for current priorities.
The root package ties together tokenization, parsing,
AST expressions, and generation. Dialect configuration lives in
dialects, with parser and generator overrides in their respective packages.
optimizer contains qualification, scope analysis, and rewrite rules.
go test ./...
go vet ./...
The checked-in fixtures cover normal development. Fetch the pinned Python source when porting
behavior, running the upstream extension tripwire, or regenerating a corpus.
scripts/fetch-reference.sh
go test -count=1 -run '^Test(Corpus|TranspileCorpus|Fidelity|UpstreamExtensionsTripwire)$' -v .
For a live comparison, set PYTHONPATH=.reference/sqlglot-v30.17.0 and invoke Python's
sqlglot.parse_one(...). To re-extract the transpile corpus and refresh its gap ledger, run
PYTHONPATH=.reference/sqlglot-v30.17.0 python3 scripts/extract_dialect_corpus.py --transpile-only
and then SQLGLOT_TRANSPILE_UPDATE=1 go test . -run '^TestTranspileCorpus$' -v.
License and attribution
MIT, see LICENSE. sqlglot-go is an independent continuation of
ridi-oss/sqlglot-go, based on
tobymao/sqlglot (© Toby Mao, MIT). See
CHANGELOG.md for release history.