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.
Installation agent prête
Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.
npx -y tokrepo@latest install 16b7b083-39ec-11f1-9bc6-00163e2b0d79 --target codexÀ exécuter après confirmation du plan en dry-run.
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.
Questions fréquentes
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.
Sources citées (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
En lien sur TokRepo
Fil de discussion
Actifs similaires
SQLModel — SQL Databases in Python with Type Safety and Pydantic
SQLModel combines SQLAlchemy and Pydantic into a single library, letting you define database models as Python classes with type annotations that serve as both ORM models and data validation schemas.
SQLx — Compile-Time Checked SQL for Rust
SQLx is an async, pure-Rust SQL crate that checks queries against your database at compile time. It supports PostgreSQL, MySQL, MariaDB, and SQLite without a DSL or ORM layer.
BeeWare Toga — Native Cross-Platform GUI Apps in Python
Write native desktop and mobile applications in pure Python that render with platform-native widgets on macOS, Windows, Linux, iOS, Android, and the web.
Clap — Command Line Argument Parser for Rust
Clap is the most popular CLI argument parser for Rust. It provides derive macros for declarative argument definitions, rich help generation, shell completions, subcommands, and comprehensive validation — the Rust equivalent of Python argparse on steroids.