SQLGlot: Write Once, Run Anywhere SQL

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:

  1. 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')"
  1. 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`
  1. Supports 21 different SQL dialects, including DuckDB, Presto/Trino, Spark/Databricks, Snowflake, and BigQuery.
  2. Offers additional features like SQL formatting, AST manipulation, and query optimization.

Link to SQLGlot.

Scroll to Top

Work with Khuyen Tran

Work with Khuyen Tran