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