Skills2026年4月16日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
SQLGlot Overview
直接安装命令
npx -y tokrepo@latest install 16b7b083-39ec-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
SQLGlot parses, transpiles, and optimizes SQL across 20+ dialects with zero dependencies in Python.
§01

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.

§02

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.

§03

How to use

  1. Install SQLGlot with pip (no extra dependencies needed).
  2. Parse SQL strings into AST objects for inspection or transformation.
  3. Transpile between dialects by specifying the source and target.
§04

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)
§05

Related on TokRepo

§06

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.

常见问题

How many SQL dialects does SQLGlot support?+

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.

Does SQLGlot require a database connection?+

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.

Can SQLGlot optimize SQL queries?+

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.

Is SQLGlot suitable for production use?+

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.

How does SQLGlot compare to sqlparse?+

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.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产