SQLGlot C Extensions
sqlglotc provides performance-enhancing extensions for the sqlglot library, compiled using mypyc. SQLGlot is a robust, no-dependency Python library for parsing, transpiling, optimizing, and executing SQL across numerous dialects like DuckDB, Presto, Spark, Snowflake, and BigQuery. It aims to accurately process diverse SQL inputs and generate syntactically and semantically correct SQL in target dialects. The library is actively maintained with a rapid release cadence, currently at version 30.4.2.
Warnings
- breaking Minor version updates of `sqlglot` (and thus `sqlglotc`) can introduce backwards-incompatible changes in SQL parsing or transpilation logic, especially concerning specific dialect behaviors.
- gotcha Failing to explicitly specify the source (`read`) and target (`write`) SQL dialects when using `sqlglot.transpile` or `sqlglot.parse_one` can lead to incorrect parsing or output. `sqlglot` defaults to its 'SQLGlot dialect' if not specified.
- gotcha If pre-built wheels for `sqlglotc` are not available for your specific platform or Python version, installation will attempt to build from source. This requires `mypyc` and a compatible C compiler (e.g., GCC or Clang) to be present on your system.
- gotcha `sqlglot` primarily acts as a parser, transpiler, and optimizer, not a full SQL validator. While it detects many syntax errors, it may not catch all semantic or dialect-specific validation issues.
Install
-
pip install sqlglotc -
pip install "sqlglot[c]"
Imports
- transpile
from sqlglot import transpile
- parse_one
from sqlglot import parse_one
Quickstart
import sqlglot
# Example: Transpile SQL from DuckDB to Hive dialect
duckdb_sql = "SELECT EPOCH_MS(1618088028295)"
hive_sql = sqlglot.transpile(duckdb_sql, read='duckdb', write='hive')[0]
print(f"Original DuckDB SQL: {duckdb_sql}")
print(f"Transpiled Hive SQL: {hive_sql}")
# Example: Parse a SQL query into an expression tree
expression = sqlglot.parse_one("SELECT id, name FROM users WHERE age > 18")
print(f"Parsed Expression: {expression}")