SQLGlot
SQLGlot is a Python SQL parser, transpiler, and optimizer that can parse and translate SQL between more than 30 dialects, including DuckDB, Snowflake, BigQuery, and Spark.
Overview
SQLGlot is a pure-Python library for parsing SQL into an abstract syntax tree (AST), analyzing or rewriting that tree, and generating SQL back out in a specified dialect. Its most common use is transpilation: converting a query written for one database's SQL dialect into the equivalent syntax for another, which is useful when migrating between databases, building cross-dialect tooling, or writing dialect-agnostic query generators. Beyond transpilation, SQLGlot also includes a query optimizer and a lightweight, in-memory SQL execution engine.
Basic usage
Copy code
import sqlglot
sql = "SELECT EPOCH_MS(1700000000000) AS ts"
transpiled = sqlglot.transpile(sql, read="duckdb", write="hive")[0]
# 'SELECT FROM_UNIXTIME(1700000000000 / POW(10, 3)) AS ts'
You can also parse without converting dialects, walk the resulting expression tree, and programmatically build SQL from Python objects rather than string concatenation.
SQLGlot and DuckDB
SQLGlot has first-class support for the DuckDB SQL dialect, including many of DuckDB's distinctive extensions such as QUALIFY, list and struct literals, and DuckDB-specific function names, which means it can transpile queries written in other dialects into idiomatic DuckDB SQL, and vice versa. This makes it a common building block for tools that need to run the same logical query across multiple engines — for example, a BI tool or semantic layer that wants to support both a cloud warehouse and a local DuckDB instance without maintaining separate handwritten SQL for each. SQLGlot's DuckDB dialect support does not require an actual DuckDB installation; it operates purely on SQL text and is a separate, independently maintained open-source project rather than being part of DuckDB itself.
Related terms
SQL (Structured Query Language) is the standard language for working with relational databases.
SQL query →A SQL query is a structured request written in Structured Query Language (SQL) that allows you to retrieve, analyze, or manipulate data stored in a database.
query engine →A query engine parses, optimizes, and executes SQL queries against data sources. Learn its core architecture, how it differs from a database, and see examples like DuckDB and Presto.
Google BigQuery →Google BigQuery is a serverless, fully managed cloud data warehouse that runs SQL queries at scale without provisioning or managing infrastructure.
SQL analytics →SQL analytics refers to using SQL queries to analyze data and derive insights, typically working with large datasets stored in databases or data warehouses.
DuckDB →DuckDB is an embeddable SQL database management system designed for analytical workloads.
FAQS
It has an optional, lightweight execution engine for running SQL against Python objects, but SQLGlot's core purpose is parsing, transpiling, and optimizing SQL text, not serving as a general-purpose database engine.
No. SQLGlot is an independent open-source project, but it maintains dedicated support for the DuckDB SQL dialect so it can correctly translate DuckDB-specific syntax to and from other databases.
