From ccb96d1393ae2c16620ea8e8dc749d9642b94e9b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 20 Feb 2024 10:38:01 +0100 Subject: Merging upstream version 21.1.2. Signed-off-by: Daniel Baumann --- sqlglot/optimizer/normalize.py | 4 ++-- sqlglot/optimizer/qualify.py | 14 +++++++------- sqlglot/optimizer/qualify_columns.py | 20 +++++++------------- sqlglot/optimizer/scope.py | 2 +- sqlglot/optimizer/simplify.py | 2 +- sqlglot/optimizer/unnest_subqueries.py | 2 +- 6 files changed, 19 insertions(+), 25 deletions(-) (limited to 'sqlglot/optimizer') diff --git a/sqlglot/optimizer/normalize.py b/sqlglot/optimizer/normalize.py index 6df36af..6bf877b 100644 --- a/sqlglot/optimizer/normalize.py +++ b/sqlglot/optimizer/normalize.py @@ -76,7 +76,7 @@ def normalized(expression: exp.Expression, dnf: bool = False) -> bool: Args: expression: The expression to check if it's normalized. - dnf: Whether or not to check if the expression is in Disjunctive Normal Form (DNF). + dnf: Whether to check if the expression is in Disjunctive Normal Form (DNF). Default: False, i.e. we check if it's in Conjunctive Normal Form (CNF). """ ancestor, root = (exp.And, exp.Or) if dnf else (exp.Or, exp.And) @@ -99,7 +99,7 @@ def normalization_distance(expression: exp.Expression, dnf: bool = False) -> int Args: expression: The expression to compute the normalization distance for. - dnf: Whether or not to check if the expression is in Disjunctive Normal Form (DNF). + dnf: Whether to check if the expression is in Disjunctive Normal Form (DNF). Default: False, i.e. we check if it's in Conjunctive Normal Form (CNF). Returns: diff --git a/sqlglot/optimizer/qualify.py b/sqlglot/optimizer/qualify.py index 8d83b47..e4f8b57 100644 --- a/sqlglot/optimizer/qualify.py +++ b/sqlglot/optimizer/qualify.py @@ -48,15 +48,15 @@ def 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. - expand_stars: Whether or not to expand star queries. This is a necessary step + 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 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. + 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. diff --git a/sqlglot/optimizer/qualify_columns.py b/sqlglot/optimizer/qualify_columns.py index 5c27bc3..ef589c9 100644 --- a/sqlglot/optimizer/qualify_columns.py +++ b/sqlglot/optimizer/qualify_columns.py @@ -35,11 +35,11 @@ def qualify_columns( Args: expression: Expression to qualify. schema: Database schema. - expand_alias_refs: Whether or not to expand references to aliases. - expand_stars: Whether or not to expand star queries. This is a necessary step + 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 or not to infer the schema if missing. + infer_schema: Whether to infer the schema if missing. Returns: The qualified expression. @@ -164,12 +164,7 @@ def _expand_using(scope: Scope, resolver: Resolver) -> t.Dict[str, t.Any]: table = table or source_table conditions.append( - exp.condition( - exp.EQ( - this=exp.column(identifier, table=table), - expression=exp.column(identifier, table=join_table), - ) - ) + exp.column(identifier, table=table).eq(exp.column(identifier, table=join_table)) ) # Set all values in the dict to None, because we only care about the key ordering @@ -449,10 +444,9 @@ def _expand_stars( continue for name in columns: + if name in columns_to_exclude or name in coalesced_columns: + continue if name in using_column_tables and table in using_column_tables[name]: - if name in coalesced_columns: - continue - coalesced_columns.add(name) tables = using_column_tables[name] coalesce = [exp.column(name, table=table) for table in tables] @@ -464,7 +458,7 @@ def _expand_stars( copy=False, ) ) - elif name not in columns_to_exclude: + else: alias_ = replace_columns.get(table_id, {}).get(name, name) column = exp.column(name, table=table) new_selections.append( diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py index 16cd548..0eae979 100644 --- a/sqlglot/optimizer/scope.py +++ b/sqlglot/optimizer/scope.py @@ -254,7 +254,7 @@ class Scope: self._columns = [] for column in columns + external_columns: ancestor = column.find_ancestor( - exp.Select, exp.Qualify, exp.Order, exp.Having, exp.Hint, exp.Table + exp.Select, exp.Qualify, exp.Order, exp.Having, exp.Hint, exp.Table, exp.Star ) if ( not ancestor diff --git a/sqlglot/optimizer/simplify.py b/sqlglot/optimizer/simplify.py index 90357dd..9ffddb5 100644 --- a/sqlglot/optimizer/simplify.py +++ b/sqlglot/optimizer/simplify.py @@ -41,7 +41,7 @@ def simplify( Args: expression (sqlglot.Expression): expression to simplify - constant_propagation: whether or not the constant propagation rule should be used + constant_propagation: whether the constant propagation rule should be used Returns: sqlglot.Expression: simplified expression diff --git a/sqlglot/optimizer/unnest_subqueries.py b/sqlglot/optimizer/unnest_subqueries.py index 26f4159..b4c7475 100644 --- a/sqlglot/optimizer/unnest_subqueries.py +++ b/sqlglot/optimizer/unnest_subqueries.py @@ -248,7 +248,7 @@ def decorrelate(select, parent_select, external_columns, next_alias_name): key.replace(exp.to_identifier("_x")) parent_predicate = _replace( parent_predicate, - f'({parent_predicate} AND ARRAY_ANY({nested}, "_x" -> {predicate}))', + f"({parent_predicate} AND ARRAY_ANY({nested}, _x -> {predicate}))", ) parent_select.join( -- cgit v1.2.3