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.
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.
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.
How to use
- Install SQLGlot with pip (no extra dependencies needed).
- Parse SQL strings into AST objects for inspection or transformation.
- Transpile between dialects by specifying the source and target.
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)
Related on TokRepo
- AI tools for database — Other tools for database management, querying, and migration.
- AI tools for coding — Developer tools that assist with code transformation and analysis.
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
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.
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.
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.
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.
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)
- SQLGlot GitHub— SQLGlot parses and transpiles SQL across 20+ dialects
- Google BigQuery Docs— BigQuery SQL reference for function mapping
- Snowflake Docs— Snowflake SQL dialect documentation
Related on TokRepo
Discussion
Related Assets
Moodle — Open-Source Learning Management System
The most widely used open-source learning platform, providing course management, assessments, and collaboration tools for educators and organizations worldwide.
Sylius — Headless E-Commerce Framework on Symfony
An open-source headless e-commerce platform built on Symfony and API Platform, designed for developers who need a customizable and API-first commerce solution.
Akaunting — Free Self-Hosted Accounting Software
A free, open-source online accounting application built on Laravel for small businesses and freelancers to manage invoices, expenses, and financial reports.