sqlglot.optimizer.normalize_identifiers API documentation

Edit on GitHub

# sqlglot.optimizer.normalize_identifiers

View Source

     1from __future__ import annotations
     2
     3import typing as t
     4
     5from sqlglot import exp
     6from sqlglot._typing import E
     7from sqlglot.dialects.dialect import Dialect, DialectType
     8
     9
    10@t.overload
    11def normalize_identifiers(expression: E, dialect: DialectType = None) -> E:
    12    ...
    13
    14
    15@t.overload
    16def normalize_identifiers(expression: str, dialect: DialectType = None) -> exp.Expression:
    17    ...
    18
    19
    20def normalize_identifiers(expression, dialect=None):
    21    """
    22    Normalize all unquoted identifiers to either lower or upper case, depending
    23    on the dialect. This essentially makes those identifiers case-insensitive.
    24
    25    It's possible to make this a no-op by adding a special comment next to the
    26    identifier of interest:
    27
    28        SELECT a /* sqlglot.meta case_sensitive */ FROM table
    29
    30    In this example, the identifier `a` will not be normalized.
    31
    32    Note:
    33        Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even
    34        when they're quoted, so in these cases all identifiers are normalized.
    35
    36    Example:
    37        >>> import sqlglot
    38        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    39        >>> normalize_identifiers(expression).sql()
    40        'SELECT bar.a AS a FROM "Foo".bar'
    41        >>> normalize_identifiers("foo", dialect="snowflake").sql(dialect="snowflake")
    42        'FOO'
    43
    44    Args:
    45        expression: The expression to transform.
    46        dialect: The dialect to use in order to decide how to normalize identifiers.
    47
    48    Returns:
    49        The transformed expression.
    50    """
    51    if isinstance(expression, str):
    52        expression = exp.parse_identifier(expression, dialect=dialect)
    53
    54    dialect = Dialect.get_or_raise(dialect)
    55
    56    def _normalize(node: E) -> E:
    57        if not node.meta.get("case_sensitive"):
    58            exp.replace_children(node, _normalize)
    59            node = dialect.normalize_identifier(node)
    60        return node
    61
    62    return _normalize(expression)

def normalize_identifiers(expression, dialect=None): View Source

    21def normalize_identifiers(expression, dialect=None):
    22    """
    23    Normalize all unquoted identifiers to either lower or upper case, depending
    24    on the dialect. This essentially makes those identifiers case-insensitive.
    25
    26    It's possible to make this a no-op by adding a special comment next to the
    27    identifier of interest:
    28
    29        SELECT a /* sqlglot.meta case_sensitive */ FROM table
    30
    31    In this example, the identifier `a` will not be normalized.
    32
    33    Note:
    34        Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even
    35        when they're quoted, so in these cases all identifiers are normalized.
    36
    37    Example:
    38        >>> import sqlglot
    39        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    40        >>> normalize_identifiers(expression).sql()
    41        'SELECT bar.a AS a FROM "Foo".bar'
    42        >>> normalize_identifiers("foo", dialect="snowflake").sql(dialect="snowflake")
    43        'FOO'
    44
    45    Args:
    46        expression: The expression to transform.
    47        dialect: The dialect to use in order to decide how to normalize identifiers.
    48
    49    Returns:
    50        The transformed expression.
    51    """
    52    if isinstance(expression, str):
    53        expression = exp.parse_identifier(expression, dialect=dialect)
    54
    55    dialect = Dialect.get_or_raise(dialect)
    56
    57    def _normalize(node: E) -> E:
    58        if not node.meta.get("case_sensitive"):
    59            exp.replace_children(node, _normalize)
    60            node = dialect.normalize_identifier(node)
    61        return node
    62
    63    return _normalize(expression)

Normalize all unquoted identifiers to either lower or upper case, depending on the dialect. This essentially makes those identifiers case-insensitive.

It's possible to make this a no-op by adding a special comment next to the identifier of interest:

    SELECT a /* sqlglot.meta case_sensitive */ FROM table

In this example, the identifier `a` will not be normalized.

###### Note:

> Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even when they're quoted, so in these cases all identifiers are normalized.

###### Example:

>     >>> import sqlglot
>     >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
>     >>> normalize_identifiers(expression).sql()
>     'SELECT bar.a AS a FROM "Foo".bar'
>     >>> normalize_identifiers("foo", dialect="snowflake").sql(dialect="snowflake")
>     'FOO'

###### Arguments:

- expression: The expression to transform.
- dialect: The dialect to use in order to decide how to normalize identifiers.

###### Returns:

> The transformed expression.