Edit on GitHub

sqlglot.optimizer.qualify

 1from __future__ import annotations
 2
 3import typing as t
 4
 5from sqlglot import exp
 6from sqlglot.dialects.dialect import Dialect, DialectType
 7from sqlglot.optimizer.isolate_table_selects import isolate_table_selects
 8from sqlglot.optimizer.normalize_identifiers import normalize_identifiers
 9from sqlglot.optimizer.qualify_columns import (
10    pushdown_cte_alias_columns as pushdown_cte_alias_columns_func,
11    qualify_columns as qualify_columns_func,
12    quote_identifiers as quote_identifiers_func,
13    validate_qualify_columns as validate_qualify_columns_func,
14)
15from sqlglot.optimizer.qualify_tables import qualify_tables
16from sqlglot.schema import Schema, ensure_schema
17
18
19def qualify(
20    expression: exp.Expression,
21    dialect: DialectType = None,
22    db: t.Optional[str] = None,
23    catalog: t.Optional[str] = None,
24    schema: t.Optional[dict | Schema] = None,
25    expand_alias_refs: bool = True,
26    infer_schema: t.Optional[bool] = None,
27    isolate_tables: bool = False,
28    qualify_columns: bool = True,
29    validate_qualify_columns: bool = True,
30    quote_identifiers: bool = True,
31    identify: bool = True,
32) -> exp.Expression:
33    """
34    Rewrite sqlglot AST to have normalized and qualified tables and columns.
35
36    This step is necessary for all further SQLGlot optimizations.
37
38    Example:
39        >>> import sqlglot
40        >>> schema = {"tbl": {"col": "INT"}}
41        >>> expression = sqlglot.parse_one("SELECT col FROM tbl")
42        >>> qualify(expression, schema=schema).sql()
43        'SELECT "tbl"."col" AS "col" FROM "tbl" AS "tbl"'
44
45    Args:
46        expression: Expression to qualify.
47        db: Default database name for tables.
48        catalog: Default catalog name for tables.
49        schema: Schema to infer column names and types.
50        expand_alias_refs: Whether or not to expand references to aliases.
51        infer_schema: Whether or not to infer the schema if missing.
52        isolate_tables: Whether or not to isolate table selects.
53        qualify_columns: Whether or not to qualify columns.
54        validate_qualify_columns: Whether or not to validate columns.
55        quote_identifiers: Whether or not to run the quote_identifiers step.
56            This step is necessary to ensure correctness for case sensitive queries.
57            But this flag is provided in case this step is performed at a later time.
58        identify: If True, quote all identifiers, else only necessary ones.
59
60    Returns:
61        The qualified expression.
62    """
63    schema = ensure_schema(schema, dialect=dialect)
64    expression = normalize_identifiers(expression, dialect=dialect)
65    expression = qualify_tables(expression, db=db, catalog=catalog, schema=schema)
66
67    if isolate_tables:
68        expression = isolate_table_selects(expression, schema=schema)
69
70    if Dialect.get_or_raise(dialect).PREFER_CTE_ALIAS_COLUMN:
71        expression = pushdown_cte_alias_columns_func(expression)
72
73    if qualify_columns:
74        expression = qualify_columns_func(
75            expression, schema, expand_alias_refs=expand_alias_refs, infer_schema=infer_schema
76        )
77
78    if quote_identifiers:
79        expression = quote_identifiers_func(expression, dialect=dialect, identify=identify)
80
81    if validate_qualify_columns:
82        validate_qualify_columns_func(expression)
83
84    return expression
def qualify( expression: sqlglot.expressions.Expression, dialect: Union[str, sqlglot.dialects.dialect.Dialect, Type[sqlglot.dialects.dialect.Dialect], NoneType] = None, db: Optional[str] = None, catalog: Optional[str] = None, schema: Union[dict, sqlglot.schema.Schema, NoneType] = None, expand_alias_refs: bool = True, infer_schema: Optional[bool] = None, isolate_tables: bool = False, qualify_columns: bool = True, validate_qualify_columns: bool = True, quote_identifiers: bool = True, identify: bool = True) -> sqlglot.expressions.Expression:
20def qualify(
21    expression: exp.Expression,
22    dialect: DialectType = None,
23    db: t.Optional[str] = None,
24    catalog: t.Optional[str] = None,
25    schema: t.Optional[dict | Schema] = None,
26    expand_alias_refs: bool = True,
27    infer_schema: t.Optional[bool] = None,
28    isolate_tables: bool = False,
29    qualify_columns: bool = True,
30    validate_qualify_columns: bool = True,
31    quote_identifiers: bool = True,
32    identify: bool = True,
33) -> exp.Expression:
34    """
35    Rewrite sqlglot AST to have normalized and qualified tables and columns.
36
37    This step is necessary for all further SQLGlot optimizations.
38
39    Example:
40        >>> import sqlglot
41        >>> schema = {"tbl": {"col": "INT"}}
42        >>> expression = sqlglot.parse_one("SELECT col FROM tbl")
43        >>> qualify(expression, schema=schema).sql()
44        'SELECT "tbl"."col" AS "col" FROM "tbl" AS "tbl"'
45
46    Args:
47        expression: Expression to qualify.
48        db: Default database name for tables.
49        catalog: Default catalog name for tables.
50        schema: Schema to infer column names and types.
51        expand_alias_refs: Whether or not to expand references to aliases.
52        infer_schema: Whether or not to infer the schema if missing.
53        isolate_tables: Whether or not to isolate table selects.
54        qualify_columns: Whether or not to qualify columns.
55        validate_qualify_columns: Whether or not to validate columns.
56        quote_identifiers: Whether or not to run the quote_identifiers step.
57            This step is necessary to ensure correctness for case sensitive queries.
58            But this flag is provided in case this step is performed at a later time.
59        identify: If True, quote all identifiers, else only necessary ones.
60
61    Returns:
62        The qualified expression.
63    """
64    schema = ensure_schema(schema, dialect=dialect)
65    expression = normalize_identifiers(expression, dialect=dialect)
66    expression = qualify_tables(expression, db=db, catalog=catalog, schema=schema)
67
68    if isolate_tables:
69        expression = isolate_table_selects(expression, schema=schema)
70
71    if Dialect.get_or_raise(dialect).PREFER_CTE_ALIAS_COLUMN:
72        expression = pushdown_cte_alias_columns_func(expression)
73
74    if qualify_columns:
75        expression = qualify_columns_func(
76            expression, schema, expand_alias_refs=expand_alias_refs, infer_schema=infer_schema
77        )
78
79    if quote_identifiers:
80        expression = quote_identifiers_func(expression, dialect=dialect, identify=identify)
81
82    if validate_qualify_columns:
83        validate_qualify_columns_func(expression)
84
85    return expression

Rewrite sqlglot AST to have normalized and qualified tables and columns.

This step is necessary for all further SQLGlot optimizations.

Example:
>>> import sqlglot
>>> schema = {"tbl": {"col": "INT"}}
>>> expression = sqlglot.parse_one("SELECT col FROM tbl")
>>> qualify(expression, schema=schema).sql()
'SELECT "tbl"."col" AS "col" FROM "tbl" AS "tbl"'
Arguments:
  • expression: Expression to qualify.
  • db: Default database name for tables.
  • catalog: Default catalog name for tables.
  • schema: Schema to infer column names and types.
  • expand_alias_refs: Whether or not to expand references to aliases.
  • infer_schema: Whether or not to infer the schema if missing.
  • isolate_tables: Whether or not to isolate table selects.
  • qualify_columns: Whether or not to qualify columns.
  • validate_qualify_columns: Whether or not to validate columns.
  • quote_identifiers: Whether or not to run the quote_identifiers step. This step is necessary to ensure correctness for case sensitive queries. But this flag is provided in case this step is performed at a later time.
  • identify: If True, quote all identifiers, else only necessary ones.
Returns:

The qualified expression.