Skip to main content

Module transforms

Module transforms 

Source
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))) to FROM (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 name to AS _u(name) Used for PostgreSQL → Presto/Trino transpilation where GENERATE_SERIES is converted to UNNEST(SEQUENCE) and the alias needs the column-alias format.