Expand description
SQL AST Transforms
This module provides functions to transform SQL ASTs for dialect compatibility. These transforms are used during transpilation to convert dialect-specific features to forms that are supported by the target dialect.
Based on the Python implementation in sqlglot/transforms.py.
Functions§
- add_
auto_ table_ alias - Add auto-generated table aliases (like
_t0) for POSEXPLODE/EXPLODE in FROM clause when the alias has column_aliases but no alias name. This is needed for Spark target:FROM POSEXPLODE(x) AS (a, b)->FROM POSEXPLODE(x) AS _t0(a, b) - add_
recursive_ cte_ column_ names - Add column names to recursive CTE definitions.
- eliminate_
distinct_ on - Eliminate DISTINCT ON clause by converting to a subquery with ROW_NUMBER
- eliminate_
distinct_ on_ for_ dialect - Eliminate DISTINCT ON with dialect-specific NULL ordering behavior.
- eliminate_
full_ outer_ join - Convert FULL OUTER JOIN to a UNION of LEFT and RIGHT OUTER joins.
- eliminate_
join_ marks - Eliminate Oracle-style (+) join marks by converting to standard JOINs.
- eliminate_
qualify - Eliminate QUALIFY clause by converting to a subquery with WHERE filter
- eliminate_
semi_ and_ anti_ joins - Convert SEMI and ANTI joins into equivalent forms that use EXISTS instead.
- eliminate_
window_ clause - Inline window definitions from WINDOW clause.
- ensure_
bools - Ensure boolean values in conditions.
- epoch_
cast_ to_ ts - Convert epoch string in CAST to timestamp literal.
- expand_
between_ in_ delete - Expand BETWEEN expressions in DELETE statements to >= AND <=
- expand_
posexplode_ duckdb - Expand POSEXPLODE in SELECT expressions for DuckDB.
- explode_
projection_ to_ unnest - Convert EXPLODE/POSEXPLODE in SELECT projections into CROSS JOIN UNNEST patterns.
- explode_
to_ unnest - Convert EXPLODE to UNNEST (for standard SQL compatibility)
- move_
ctes_ to_ top_ level - Move CTEs to the top level of the query.
- no_
comment_ column_ constraint - Convert COMMENT ON statements to inline comments.
- no_
ilike_ sql - Convert ILIKE to LOWER(x) LIKE LOWER(y).
- no_
safe_ cast_ sql - Convert SafeCast to Cast.
- no_
trycast_ sql - Convert TryCast to Cast.
- preprocess
- Apply a chain of transforms to an expression
- propagate_
struct_ field_ names - Convert BigQuery-style UNNEST aliases to column-alias format for DuckDB/Presto/Spark.
- pushdown_
cte_ column_ names - Push down CTE column names into SELECT expressions.
- remove_
precision_ parameterized_ types - Remove precision from parameterized types
- replace_
bool_ with_ int - Replace boolean literals for dialects that don’t support them
- replace_
int_ with_ bool - Replace integer literals for dialects that prefer boolean
- simplify_
nested_ paren_ values - Simplify nested parentheses around VALUES in FROM clause.
Converts
FROM ((VALUES (1)))toFROM (VALUES (1))by stripping redundant wrapping. Handles various nesting patterns: Subquery(Paren(Values)), Paren(Paren(Values)), etc. - snowflake_
flatten_ projection_ to_ unnest - Snowflake-specific rewrite to mirror Python sqlglot’s explode_projection_to_unnest behavior when FLATTEN appears in a nested LATERAL within a SELECT projection.
- strip_
unnest_ column_ refs - Strip UNNEST wrapping from column reference arguments for Redshift target. BigQuery UNNEST(column_ref) -> Redshift: just column_ref
- unnest_
alias_ to_ column_ alias - This walks the entire expression tree to find SELECT statements and converts UNNEST aliases in their FROM clauses and JOINs.
- unnest_
from_ to_ cross_ join - Move UNNEST items from FROM clause to CROSS JOINs without changing alias format. Used for BigQuery -> BigQuery/Redshift where we want CROSS JOIN but not _t0(col) aliases.
- unnest_
generate_ date_ array_ using_ recursive_ cte - Convert UNNEST(GENERATE_DATE_ARRAY(…)) to recursive CTE.
- unnest_
generate_ series - Convert TABLE GENERATE_SERIES to UNNEST(GENERATE_SERIES(…)).
- unnest_
to_ explode - Convert UNNEST to EXPLODE (for Spark/Hive compatibility)
- unnest_
to_ explode_ select - Convert CROSS JOIN UNNEST to LATERAL VIEW EXPLODE/INLINE for Spark/Hive/Databricks.
- unqualify_
columns - Remove table qualifiers from column references.
- wrap_
duckdb_ unnest_ struct - Wrap DuckDB UNNEST of struct arrays in (SELECT UNNEST(…, max_depth => 2)) subquery. BigQuery UNNEST of struct arrays needs this wrapping for DuckDB to properly expand struct fields.
- wrap_
unnest_ join_ aliases - Wrap UNNEST function aliases in JOIN items from
AS nametoAS _u(name)Used for PostgreSQL → Presto/Trino transpilation where GENERATE_SERIES is converted to UNNEST(SEQUENCE) and the alias needs the column-alias format.