SQLGlot is a powerful SQL parser and transpiler that supports 21 different SQL dialects. It allows automatic translation of SQL queries between various database systems, eliminating the need for manual query rewrites when porting between dialects.
Key features:
- Translates SQL syntax differences, like date/time functions:
import sqlglot
sqlglot.transpile("SELECT STRFTIME(x, '%y-%-m-%S')", read="duckdb", write="hive")[0]
# Output: "SELECT DATE_FORMAT(x, 'yy-M-ss')"
- Handles dialect-specific identifier delimiters and data types:
sql = "SELECT id, name, CAST(price AS REAL) AS converted_price FROM products"
print(sqlglot.transpile(sql, write="spark", identify=True, pretty=True)[0])
# Output:
# SELECT
# `id`,
# `name`,
# CAST(`price` AS FLOAT) AS `converted_price`
# FROM `products`
- Supports 21 different SQL dialects, including DuckDB, Presto/Trino, Spark/Databricks, Snowflake, and BigQuery.
- Offers additional features like SQL formatting, AST manipulation, and query optimization.