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