ScriptsApr 16, 2026·3 min read

SQLGlot — SQL Parser, Transpiler & Optimizer in Pure Python

SQLGlot is a no-dependency Python library that parses, transpiles, and optimizes SQL across 20+ dialects. Convert queries between Snowflake, BigQuery, DuckDB, Spark, Postgres, and more without touching the database.

TL;DR
SQLGlot parses, transpiles, and optimizes SQL across 20+ dialects with zero dependencies in Python.
§01

What it is

SQLGlot is a pure Python library with no external dependencies that parses SQL into an AST, transpiles queries between 20+ SQL dialects, and optimizes query plans. It supports Snowflake, BigQuery, DuckDB, Spark, Postgres, MySQL, and many more dialects without requiring a database connection.

Data engineers migrating between warehouses, building SQL linters, or creating multi-dialect tools will find SQLGlot essential. It handles the parsing and dialect translation that would otherwise require writing custom regex or maintaining separate query files per database.

§02

How it saves time or tokens

Manually rewriting SQL queries from one dialect to another is error-prone and time-consuming. SQLGlot automates this conversion with a single function call. It also optimizes queries by pushing down predicates and simplifying expressions, which can reduce token counts when SQL is part of LLM prompts.

§03

How to use

  1. Install SQLGlot with pip (no extra dependencies needed).
  2. Parse SQL strings into AST objects for inspection or transformation.
  3. Transpile between dialects by specifying the source and target.
§04

Example

import sqlglot

# Transpile from Spark SQL to BigQuery
spark_sql = "SELECT EPOCH(ts) FROM events"
bigquery_sql = sqlglot.transpile(
    spark_sql,
    read='spark',
    write='bigquery'
)[0]
print(bigquery_sql)
# Output: SELECT UNIX_SECONDS(ts) FROM events

# Parse and inspect the AST
ast = sqlglot.parse_one('SELECT a, b FROM t WHERE a > 1')
for col in ast.find_all(sqlglot.exp.Column):
    print(col.name)
§05

Related on TokRepo

§06

Common pitfalls

  • Assuming 100% dialect coverage. SQLGlot handles the most common syntax differences, but edge cases in vendor-specific extensions may need manual review.
  • Forgetting to specify the source dialect. Without read='dialect', SQLGlot uses its default parser which may misinterpret dialect-specific syntax.
  • Using SQLGlot as a query executor. It only parses and transforms SQL text; it does not connect to or run queries against a database.

Frequently Asked Questions

How many SQL dialects does SQLGlot support?+

SQLGlot supports 20+ SQL dialects including Snowflake, BigQuery, DuckDB, Spark, Postgres, MySQL, SQLite, Presto, Trino, ClickHouse, Redshift, and more. The list grows with each release.

Does SQLGlot require a database connection?+

No. SQLGlot is a pure parser and transpiler. It works entirely on SQL text without connecting to any database. This makes it safe to use in CI/CD pipelines and linting tools.

Can SQLGlot optimize SQL queries?+

Yes. SQLGlot includes a query optimizer that can push down predicates, eliminate subqueries, and simplify expressions. The optimizer works on the AST level and does not need database statistics.

Is SQLGlot suitable for production use?+

Yes. SQLGlot is used in production by data teams for SQL migration, linting, and multi-dialect tool development. It has no external dependencies, which simplifies deployment.

How does SQLGlot compare to sqlparse?+

sqlparse is a SQL formatter and tokenizer. SQLGlot goes further by building a full AST, supporting dialect-aware transpilation, and providing query optimization. For parsing and transformation tasks, SQLGlot is more capable.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets