Repolode
tobymao

tobymao/sqlglot

A no-dependency Python SQL parser, transpiler, optimizer and engine that translates queries across 30+ SQL dialects.

Stars
9,764
Forks
1,387
Open issues
13
Last commit
14 hours ago

What is it?

SQLGlot parses SQL into an expression tree and generates it back in another dialect, so queries written for DuckDB, Presto or Trino, Spark or Databricks, Snowflake, BigQuery and others can be translated between them. It can also reformat SQL, preserve comments on a best-effort basis, and report syntax errors or constructs that cannot be expressed in the target dialect.

Beyond transpilation, the library offers a programmatic API for building and modifying queries, traversing and transforming the AST, and running an optimizer that rewrites queries into a canonical form when schema information is supplied. It also includes a small in-memory SQL executor that runs queries over Python dictionaries, useful for tests. Custom dialects can be added by subclassing or through separately packaged plugins.

The package has no required dependencies and is written in pure Python. An optional mypyc-compiled build, installed with the [c] extra, is offered for faster parsing.

Key features

  • 01Translates queries between more than 30 SQL dialects.
  • 02Formats SQL and can quote all identifiers.
  • 03Reports syntax errors with line and column positions.
  • 04Warns or raises when a construct is unsupported by the target dialect.
  • 05Provides an API for building, modifying and transforming SQL expression trees.
  • 06Includes an optimizer and a small in-memory SQL executor.

Use cases

  • Migrating analytics queries from one warehouse engine, such as Snowflake or BigQuery, to another.
  • Building data tools that must accept SQL from several database backends.
  • Linting or analyzing SQL to extract tables and columns referenced in a query.
  • Writing unit tests for SQL logic against in-memory Python data.

Quick start

  1. 1

    Install SQLGlot from PyPI

    pip3 install sqlglot
  2. 2

    Parse and transpile a query from Spark to DuckDB in Python

    python -c "import sqlglot; print(sqlglot.transpile('SELECT EPOCH_MS(1618088028295)', read='duckdb', write='hive')[0])"

Things to know

  • Parsing is lenient, so invalid SQL may be accepted and only fail at execution time.
  • Some dialect pairs have incomplete translation coverage, and unsupported features produce best-effort output with warnings.

In collections

Topics

FAQ

What is sqlglot?
A no-dependency Python SQL parser, transpiler, optimizer and engine that translates queries across 30+ SQL dialects. SQLGlot parses SQL into an expression tree and generates it back in another dialect, so queries written for DuckDB, Presto or Trino, Spark or Databricks, Snowflake, BigQuery and others can be translated between them. It can also reformat SQL, preserve comments on a best-effort basis, and report syntax errors or constructs that cannot be expressed in the target dialect.
Is sqlglot free to use? What license does it have?
sqlglot is open source under the MIT license. Check the license terms before using it in commercial products.
Is sqlglot still maintained?
Actively maintained. The last commit to sqlglot was on Nov 6, 2026.
What can I use sqlglot for?
Migrating analytics queries from one warehouse engine, such as Snowflake or BigQuery, to another. Building data tools that must accept SQL from several database backends. Linting or analyzing SQL to extract tables and columns referenced in a query. Writing unit tests for SQL logic against in-memory Python data.
How do I install sqlglot?
Install SQLGlot from PyPI: pip3 install sqlglot Parse and transpile a query from Spark to DuckDB in Python: python -c "import sqlglot; print(sqlglot.transpile('SELECT EPOCH_MS(1618088028295)', read='duckdb', write='hive')[0])"
Where can I get sqlglot?
The source code and installation instructions are on GitHub: https://github.com/tobymao/sqlglot

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects