sqlglot.optimizer.expand_laterals API documentation

Edit on GitHub

# sqlglot.optimizer.expand_laterals

View Source

     1from __future__ import annotations
     2
     3import typing as t
     4
     5from sqlglot import exp
     6
     7
     8def expand_laterals(expression: exp.Expression) -> exp.Expression:
     9    """
    10    Expand lateral column alias references.
    11
    12    This assumes `qualify_columns` as already run.
    13
    14    Example:
    15        >>> import sqlglot
    16        >>> sql = "SELECT x.a + 1 AS b, b + 1 AS c FROM x"
    17        >>> expression = sqlglot.parse_one(sql)
    18        >>> expand_laterals(expression).sql()
    19        'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'
    20
    21    Args:
    22        expression: expression to optimize
    23    Returns:
    24        optimized expression
    25    """
    26    for select in expression.find_all(exp.Select):
    27        alias_to_expression: t.Dict[str, exp.Expression] = {}
    28        for projection in select.expressions:
    29            for column in projection.find_all(exp.Column):
    30                if not column.table and column.name in alias_to_expression:
    31                    column.replace(alias_to_expression[column.name].copy())
    32            if isinstance(projection, exp.Alias):
    33                alias_to_expression[projection.alias] = projection.this
    34    return expression

def expand_laterals( expression: sqlglot.expressions.Expression) -> sqlglot.expressions.Expression: View Source

     9def expand_laterals(expression: exp.Expression) -> exp.Expression:
    10    """
    11    Expand lateral column alias references.
    12
    13    This assumes `qualify_columns` as already run.
    14
    15    Example:
    16        >>> import sqlglot
    17        >>> sql = "SELECT x.a + 1 AS b, b + 1 AS c FROM x"
    18        >>> expression = sqlglot.parse_one(sql)
    19        >>> expand_laterals(expression).sql()
    20        'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'
    21
    22    Args:
    23        expression: expression to optimize
    24    Returns:
    25        optimized expression
    26    """
    27    for select in expression.find_all(exp.Select):
    28        alias_to_expression: t.Dict[str, exp.Expression] = {}
    29        for projection in select.expressions:
    30            for column in projection.find_all(exp.Column):
    31                if not column.table and column.name in alias_to_expression:
    32                    column.replace(alias_to_expression[column.name].copy())
    33            if isinstance(projection, exp.Alias):
    34                alias_to_expression[projection.alias] = projection.this
    35    return expression

Expand lateral column alias references.

This assumes `qualify_columns` as already run.

###### Example:

>     >>> import sqlglot
>     >>> sql = "SELECT x.a + 1 AS b, b + 1 AS c FROM x"
>     >>> expression = sqlglot.parse_one(sql)
>     >>> expand_laterals(expression).sql()
>     'SELECT x.a + 1 AS b, x.a + 1 + 1 AS c FROM x'

###### Arguments:

- expression: expression to optimize

###### Returns:

> optimized expression