SuperAPI AI Directory

sqlglot

SQLGlot is a SQL parser, transpiler, optimizer, and execution engine written in pure Python with no third-party dependencies. It can format SQL and convert between more than 30 dialects, including DuckDB, Presto/Trino, Spark/Databricks, Snowflake, and BigQuery, with the goal of reading various SQL inputs and outputting SQL that is syntactically and semantically correct in the target dialect. It provides a robust test suite, has good performance, supports custom parsers, analyzing queries, traversing expression trees, and building SQL programmatically, and can also detect syntax errors such as mismatched parentheses and misuse of reserved words, and issue warnings or raise exceptions for dialect incompatibilities according to configuration.

Open website

About this tool

SQLGlot is a dependency-free Python SQL parser, transpiler, optimizer, and execution engine, hosted on GitHub (https://github.com/tobymao/sqlglot)。其核心用途是格式化 SQL, and translating between more than 30 SQL dialects, such as DuckDB, Presto/Trino, Spark/Databricks, Snowflake, and BigQuery. The project goal is to read a variety of SQL inputs and output SQL that is syntactically and semantically correct in the target dialect.

Main capabilities:

  • Dialect transpilation: supports cross-dialect conversion of date/time functions, custom time formats, identifier delimiters, and data types; the target engine version can be specified, e.g. write="spark, version=3.5".
  • Syntax error detection: can find issues such as mismatched parentheses and misuse of reserved words, and issue warnings or raise exceptions for dialect incompatibilities according to configuration.
  • Expression and AST manipulation: supports incrementally building SQL programmatically, modifying parse trees, recursively transforming nodes, viewing AST representations, and computing semantic differences between two expressions (sequences of Insert, Remove, Move, Update, and Keep actions).
  • Query optimization: can rewrite queries into a canonical AST, for standardizing queries or as a basis for implementing an engine.
  • SQL execution: can interpret and execute SQL by representing tables as Python dictionaries, suitable for unit testing or running SQL natively on Python objects, and can integrate with computation kernels such as Arrow and Pandas.
  • Custom dialects: dialects can be added by subclassing Dialect, or published as dialect plugins in a standalone package (since v28.6.0, registration via entry points is supported).

Usage: install via pip; to install the compiled version, run pip3 install "sqlglot[c]", which on Python 3.9 installs the pure Python version; it can also be installed from a local code checkout. When parsing, the source dialect should be passed explicitly, e.g. parse_one(sql, dialect="spark"); when transpiling, the target dialect must be specified, e.g. parse_one(sql, dialect="spark").sql(dialect="duckdb") or transpile(sql, read="spark", write="duckdb"). Output formatting can use pretty=True, and quoting all identifiers can use identify=True.

Use cases: SQL dialect migration and compatibility, queryformatting, SQL static analysis and structure extraction, programmatically generating or rewriting SQL, and SQL unit testing and lightweight execution in Python.

Limitations and notes: the parser is intentionally lenient and can accept queries that real engines would reject; it is a transpiler rather than a validator; output preserves query semantics rather than the original formatting, casing, or quoting, and comments are preserved only on a best-effort basis; transpilation between some dialect pairs may be incomplete, and some conversions depend on table schema information; the qualify and annotate_types optimization rules are not enabled by default; after installing sqlglot[c], dialect classes may not be subclassable properly, and custom dialects may require the pure Python version; the project currently only accepts pull requests submitted by maintainers, and contributors can report bugs or request features via issues.