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    Note:
    26        Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even
    27        when they're quoted, so in these cases all identifiers are normalized.
    28
    29    Example:
    30        >>> import sqlglot
    31        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    32        >>> normalize_identifiers(expression).sql()
    33        'SELECT bar.a AS a FROM "Foo".bar'
    34        >>> normalize_identifiers("foo", dialect="snowflake").sql(dialect="snowflake")
    35        'FOO'
    36
    37    Args:
    38        expression: The expression to transform.
    39        dialect: The dialect to use in order to decide how to normalize identifiers.
    40
    41    Returns:
    42        The transformed expression.
    43    """
    44    if isinstance(expression, str):
    45        expression = exp.to_identifier(expression)
    46    return expression.transform(Dialect.get_or_raise(dialect).normalize_identifier, copy=False)

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    Note:
    27        Some dialects (e.g. BigQuery) treat identifiers as case-insensitive even
    28        when they're quoted, so in these cases all identifiers are normalized.
    29
    30    Example:
    31        >>> import sqlglot
    32        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    33        >>> normalize_identifiers(expression).sql()
    34        'SELECT bar.a AS a FROM "Foo".bar'
    35        >>> normalize_identifiers("foo", dialect="snowflake").sql(dialect="snowflake")
    36        'FOO'
    37
    38    Args:
    39        expression: The expression to transform.
    40        dialect: The dialect to use in order to decide how to normalize identifiers.
    41
    42    Returns:
    43        The transformed expression.
    44    """
    45    if isinstance(expression, str):
    46        expression = exp.to_identifier(expression)
    47    return expression.transform(Dialect.get_or_raise(dialect).normalize_identifier, copy=False)

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

###### 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.