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 expand_stars: 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 to expand references to aliases. 52 expand_stars: Whether to expand star queries. This is a necessary step 53 for most of the optimizer's rules to work; do not set to False unless you 54 know what you're doing! 55 infer_schema: Whether to infer the schema if missing. 56 isolate_tables: Whether to isolate table selects. 57 qualify_columns: Whether to qualify columns. 58 validate_qualify_columns: Whether to validate columns. 59 quote_identifiers: Whether to run the quote_identifiers step. 60 This step is necessary to ensure correctness for case sensitive queries. 61 But this flag is provided in case this step is performed at a later time. 62 identify: If True, quote all identifiers, else only necessary ones. 63 64 Returns: 65 The qualified expression. 66 """ 67 schema = ensure_schema(schema, dialect=dialect) 68 expression = normalize_identifiers(expression, dialect=dialect) 69 expression = qualify_tables(expression, db=db, catalog=catalog, schema=schema, dialect=dialect) 70 71 if isolate_tables: 72 expression = isolate_table_selects(expression, schema=schema) 73 74 if Dialect.get_or_raise(dialect).PREFER_CTE_ALIAS_COLUMN: 75 expression = pushdown_cte_alias_columns_func(expression) 76 77 if qualify_columns: 78 expression = qualify_columns_func( 79 expression, 80 schema, 81 expand_alias_refs=expand_alias_refs, 82 expand_stars=expand_stars, 83 infer_schema=infer_schema, 84 ) 85 86 if quote_identifiers: 87 expression = quote_identifiers_func(expression, dialect=dialect, identify=identify) 88 89 if validate_qualify_columns: 90 validate_qualify_columns_func(expression) 91 92 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, expand_stars: 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 expand_stars: bool = True, 28 infer_schema: t.Optional[bool] = None, 29 isolate_tables: bool = False, 30 qualify_columns: bool = True, 31 validate_qualify_columns: bool = True, 32 quote_identifiers: bool = True, 33 identify: bool = True, 34) -> exp.Expression: 35 """ 36 Rewrite sqlglot AST to have normalized and qualified tables and columns. 37 38 This step is necessary for all further SQLGlot optimizations. 39 40 Example: 41 >>> import sqlglot 42 >>> schema = {"tbl": {"col": "INT"}} 43 >>> expression = sqlglot.parse_one("SELECT col FROM tbl") 44 >>> qualify(expression, schema=schema).sql() 45 'SELECT "tbl"."col" AS "col" FROM "tbl" AS "tbl"' 46 47 Args: 48 expression: Expression to qualify. 49 db: Default database name for tables. 50 catalog: Default catalog name for tables. 51 schema: Schema to infer column names and types. 52 expand_alias_refs: Whether to expand references to aliases. 53 expand_stars: Whether to expand star queries. This is a necessary step 54 for most of the optimizer's rules to work; do not set to False unless you 55 know what you're doing! 56 infer_schema: Whether to infer the schema if missing. 57 isolate_tables: Whether to isolate table selects. 58 qualify_columns: Whether to qualify columns. 59 validate_qualify_columns: Whether to validate columns. 60 quote_identifiers: Whether to run the quote_identifiers step. 61 This step is necessary to ensure correctness for case sensitive queries. 62 But this flag is provided in case this step is performed at a later time. 63 identify: If True, quote all identifiers, else only necessary ones. 64 65 Returns: 66 The qualified expression. 67 """ 68 schema = ensure_schema(schema, dialect=dialect) 69 expression = normalize_identifiers(expression, dialect=dialect) 70 expression = qualify_tables(expression, db=db, catalog=catalog, schema=schema, dialect=dialect) 71 72 if isolate_tables: 73 expression = isolate_table_selects(expression, schema=schema) 74 75 if Dialect.get_or_raise(dialect).PREFER_CTE_ALIAS_COLUMN: 76 expression = pushdown_cte_alias_columns_func(expression) 77 78 if qualify_columns: 79 expression = qualify_columns_func( 80 expression, 81 schema, 82 expand_alias_refs=expand_alias_refs, 83 expand_stars=expand_stars, 84 infer_schema=infer_schema, 85 ) 86 87 if quote_identifiers: 88 expression = quote_identifiers_func(expression, dialect=dialect, identify=identify) 89 90 if validate_qualify_columns: 91 validate_qualify_columns_func(expression) 92 93 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 to expand references to aliases.
- expand_stars: Whether to expand star queries. This is a necessary step for most of the optimizer's rules to work; do not set to False unless you know what you're doing!
- infer_schema: Whether to infer the schema if missing.
- isolate_tables: Whether to isolate table selects.
- qualify_columns: Whether to qualify columns.
- validate_qualify_columns: Whether to validate columns.
- quote_identifiers: Whether 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.