tobymao /sqlglot

Python SQL Parser and Transpiler

★ 9,790⑂ 1,413👁 49⑃ 5,738● 3,146▤ 0
PythonMITCreated 2021-03-13sqlglot.com/
+2
日增长
9,790
stars
1,413
forks
49
watchers
5,738
prs
3,146
issues
0
releases

README

!SQLGlot logo

SQLGlot is a no-dependency SQL parser, transpiler, optimizer, and engine. It can be used to format SQL or translate between over 30 dialects like DuckDB, Presto / Trino, Spark / Databricks, Snowflake, and BigQuery. It aims to read a wide variety of SQL inputs and output syntactically and semantically correct SQL in the targeted dialects.

It is a very comprehensive generic SQL parser with a robust test suite. It is also quite performant, while being written purely in Python.

You can easily customize the parser, analyze queries, traverse expression trees, and programmatically build SQL.

SQLGlot can detect a variety of syntax errors, such as unbalanced parentheses, incorrect usage of reserved keywords, and so on. These errors are highlighted and dialect incompatibilities can warn or raise depending on configurations.

Learn more about SQLGlot in the API documentation and the expression tree primer.

Contributions are very welcome in SQLGlot; read the contribution guide and the onboarding document to get started!

 Table of Contents

 Install
 Versioning
 Get in Touch
 FAQ
 Examples
    Formatting and Transpiling
    Metadata
    Parser Errors
    Unsupported Errors
    Build and Modify SQL
    SQL Optimizer
    AST Introspection
    AST Diff
    Custom Dialects
    SQL Execution
 Used By
 Documentation
 Run Tests and Lint
 Benchmarks
 Optional Dependencies
 Supported Dialects

 Install

From PyPI:

bash
 Pure python version
pip3 install sqlglot

 C extensions compiled with mypyc (Python 3.10+)
 prebuilt wheel if available for your platform, otherwise builds from source
pip3 install "sqlglot[c]"


On Python 3.9, sqlglot[c] installs the pure Python version.

Or with a local checkout:


 Optionally prefix with UV=1 to use uv for the installation
make install


Requirements for development (optional):


 Optionally prefix with UV=1 to use uv for the installation
make install-dev


 Versioning

Given a version number MAJOR.MINOR.PATCH:

- PATCH is incremented for backwards-compatible changes.
- MINOR is incremented for backwards-incompatible changes.
- MAJOR is incremented for significant backwards-incompatible changes.

 Get in Touch

We'd love to hear from you. Join our community Slack channel!

 FAQ

I tried to parse SQL that should be valid, but it failed. Why?

You probably didn't specify the source dialect. Without it, parse_one assumes the "SQLGlot dialect", which is designed to be a superset of all supported dialects. Always pass the dialect when you know it, e.g. parse_one(sql, dialect="spark"). If parsing still fails, please file an issue.

The generated SQL is not in the correct dialect!

The target dialect must also be specified explicitly. For example, to transpile a query from Spark SQL to DuckDB, do parse_one(sql, dialect="spark").sql(dialect="duckdb"), or transpile(sql, read="spark", write="duckdb").

Why does SQLGlot parse my invalid SQL without complaining?

The parser is intentionally lenient, so it can accept queries

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON