sqlglot

package module
v0.43.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sqlglot-go

CI Go Reference Go Report Card Release

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

Task Entry point
Inspect raw SQL tokens sqlglot.Tokenize
Parse a query or scripts with multiple statements sqlglot.ParseOne for an AST, sqlglot.Parse for separate statement ASTs
Parse a supported fragment as a specific AST kind sqlglot.ParseInto
Inspect or build an AST expressions.Expression, Kind, and builders
Render a tree or translate SQL sqlglot.Generate or sqlglot.Transpile, with generator options
Resolve columns or inspect scopes for lineage optimizer.Qualify and optimizer.TraverseScope, with column metadata from schema
Apply the default rewrite pipeline optimizer.Optimize

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

Generate renders e to SQL under dialect (see dialects.DialectType for the accepted forms).

func Parse

func Parse(sql string, dialect dialects.DialectType) ([]expressions.Expression, error)

Parse tokenizes and parses sql under dialect (see dialects.DialectType for the accepted forms: nil | string | *dialects.Dialect), returning one Expression per statement.

func ParseInto

func ParseInto(sql string, dialect dialects.DialectType, into expressions.Kind) (expressions.Expression, error)

func ParseOne

func ParseOne(sql string, dialect dialects.DialectType) (expressions.Expression, error)

ParseOne parses a single statement from sql under dialect (see dialects.DialectType).

func Tokenize

func Tokenize(sql string, dialect dialects.DialectType) ([]tokens.Token, error)

Tokenize lexes sql under dialect into a token stream. It is a pure lexer, independent of the parser, so it tokenizes input the parser would reject — and it never silently truncates: an unterminated string, quoted identifier, block comment, or Postgres dollar-quote returns a non-nil error, which a consumer should treat as fail-closed. Ordinary comments are attached to a token's Comments (not emitted as stream tokens). For the byte-exact source lexeme of a token, slice the source by its Start/End offsets rather than reading Token.Text (see tokens.Token).

func Transpile

func Transpile(sql string, read dialects.DialectType, write dialects.DialectType, opts generator.Options) ([]string, error)

Transpile parses sql under the read dialect and re-renders each statement under the write dialect. read and write are each a dialects.DialectType (nil | string | *dialects.Dialect).

Types

This section is empty.

Directories

Path Synopsis
Package jsonpath ports sqlglot/jsonpath.py (pinned v30.17.0): the JSONPath tokenizer and parser that turn a JSON path string into an exp.JSONPath AST.
Package jsonpath ports sqlglot/jsonpath.py (pinned v30.17.0): the JSONPath tokenizer and parser that turn a JSON path string into an exp.JSONPath AST.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL