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
- Used By
- Documentation
- Run Tests and Lint
- Benchmarks
- Optional Dependencies
- Supported Dialects
Install
From PyPI:
# Pure python version
pip3 install sqlglot
# C extensions compiled with mypyc
# prebuilt wheel if available for your platform, otherwise builds from source
pip3 install "sqlglot[c]"
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:
PATCHis incremented for backwards-compatible changes.MINORis incremented for backwards-incompatible changes.MAJORis 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 that a real engine would reject. SQLGlot is a transpiler, not a validator. A query that parses successfully may still fail at execution time.
Why doesn't the output preserve my formatting, casing or quoting?
SQLGlot parses queries into an AST and generates SQL back from it, so it preserves the meaning of a query rather than its exact text. Cosmetic details can change in the process. Use pretty=True for formatted output and identify=True to quote all identifiers. Comments are preserved on a best-effort basis.
How can I make parsing faster?
Install the version compiled with mypyc: pip3 install "sqlglot[c]". It is roughly 3-5x faster than the pure Python version (see Benchmarks).
My dialect isn't supported. What are my options?
You can subclass an existing dialect (Custom Dialects), or ship one as a separate package (Creating a Dialect Plugin). Keep in mind that subclassing may not work properly with sqlglot[c] installed, so custom dialects may require the pure Python version.
Examples
Formatting and Transpiling
Easily translate from one dialect to another. For example, date/time functions vary between dialects and can be hard to deal with:
import sqlglot
sqlglot.transpile("SELECT EPOCH_MS(1618088028295)", read="duckdb", write="hive")[0]
'SELECT FROM_UNIXTIME(1618088028295 / POW(10, 3))'
SQLGlot can even translate custom time formats:
import sqlglot
sqlglot.transpile("SELECT STRFTIME(x, '%y-%-m-%S')", read="duckdb", write="hive")[0]
"SELECT DATE_FORMAT(x, 'yy-M-ss')"
Identifier delimiters and data types can be translated as well:
import sqlglot
# Spark SQL requires backticks (`) for delimited identifiers and uses `FLOAT` over `REAL`
sql = """WITH baz AS (SELECT a, c FROM foo WHERE a = 1) SELECT f.a, b.b, baz.c, CAST("b"."a" AS REAL) d FROM foo f JOIN bar b ON f.a = b.a LEFT JOIN baz ON f.a = baz.a"""
# Translates the query into Spark SQL, formats it, and delimits all of its identifiers
print(sqlglot.transpile(sql, write="spark", identify=True, pretty=True)[0])
WITH `baz` AS (
SELECT
`a`,
`c`
FROM `foo`
WHERE
`a` = 1
)
SELECT
`f`.`a`,
`b`.`b`,
`baz`.`c`,
CAST(`b`.`a` AS FLOAT) AS `d`
FROM `foo` AS `f`
JOIN `bar` AS `b`
ON `f`.`a` = `b`.`a`
LEFT JOIN `baz`
ON `f`.`a` = `baz`.`a`
Comments are also preserved on a best-effort basis:
sql = """
/* multi
line
comment
*/
SELECT
tbl.cola /* comment 1 */ + tbl.colb /* comment 2 */,
CAST(x AS SIGNED), # comment 3
y -- comment 4
FROM
bar /* comment 5 */,
tbl # comment 6
"""
# Note: MySQL-specific comments (`#`) are converted into standard syntax
print(sqlglot.transpile(sql, read='mysql', pretty=True)[0])
/* multi
line
comment
*/
SELECT
tbl.cola /* comment 1 */ + tbl.colb /* comment 2 */,
CAST(x AS SIGNED), /* comment 3 */
y /* comment 4 */
FROM bar /* comment 5 */, tbl /* comment 6 */
Metadata
You can explore SQL with expression helpers to do things like find columns and tables in a query:
from sqlglot import parse_one, exp
# print all column references (a and b)
for column in parse_one("SELECT a, b + 1 AS c FROM d").find_all(exp.Column):
print