← Back to Glossary

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

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.