Edit on GitHub

sqlglot.optimizer.normalize_identifiers

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

Normalize identifiers by converting them to either lower or upper case, ensuring the semantics are preserved in each case (e.g. by respecting case-sensitivity).

This transformation reflects how identifiers would be resolved by the engine corresponding to each SQL dialect, and plays a very important role in the standardization of the AST.

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. DuckDB) treat all 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.