sqlglot.optimizer.normalize_identifiers API documentation

Edit on GitHub

# sqlglot.optimizer.normalize_identifiers

View Source

     1from sqlglot import exp
     2from sqlglot._typing import E
     3from sqlglot.dialects.dialect import RESOLVES_IDENTIFIERS_AS_UPPERCASE, DialectType
     4
     5
     6def normalize_identifiers(expression: E, dialect: DialectType = None) -> E:
     7    """
     8    Normalize all unquoted identifiers to either lower or upper case, depending on
     9    the dialect. This essentially makes those identifiers case-insensitive.
    10
    11    Example:
    12        >>> import sqlglot
    13        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    14        >>> normalize_identifiers(expression).sql()
    15        'SELECT bar.a AS a FROM "Foo".bar'
    16
    17    Args:
    18        expression: The expression to transform.
    19        dialect: The dialect to use in order to decide how to normalize identifiers.
    20
    21    Returns:
    22        The transformed expression.
    23    """
    24    return expression.transform(_normalize, dialect, copy=False)
    25
    26
    27def _normalize(node: exp.Expression, dialect: DialectType = None) -> exp.Expression:
    28    if isinstance(node, exp.Identifier) and not node.quoted:
    29        node.set(
    30            "this",
    31            node.this.upper()
    32            if dialect in RESOLVES_IDENTIFIERS_AS_UPPERCASE
    33            else node.this.lower(),
    34        )
    35
    36    return node

def normalize_identifiers( expression: ~E, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None) -> ~E: View Source

     7def normalize_identifiers(expression: E, dialect: DialectType = None) -> E:
     8    """
     9    Normalize all unquoted identifiers to either lower or upper case, depending on
    10    the dialect. This essentially makes those identifiers case-insensitive.
    11
    12    Example:
    13        >>> import sqlglot
    14        >>> expression = sqlglot.parse_one('SELECT Bar.A AS A FROM "Foo".Bar')
    15        >>> normalize_identifiers(expression).sql()
    16        'SELECT bar.a AS a FROM "Foo".bar'
    17
    18    Args:
    19        expression: The expression to transform.
    20        dialect: The dialect to use in order to decide how to normalize identifiers.
    21
    22    Returns:
    23        The transformed expression.
    24    """
    25    return expression.transform(_normalize, dialect, copy=False)

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

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

###### Arguments:

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

###### Returns:

> The transformed expression.