From 6a22086850fc960715b618e82f4c2e43a4529146 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 20 Feb 2023 09:50:35 +0100 Subject: Merging upstream version 11.2.0. Signed-off-by: Daniel Baumann --- sqlglot/optimizer/qualify_columns.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'sqlglot/optimizer/qualify_columns.py') diff --git a/sqlglot/optimizer/qualify_columns.py b/sqlglot/optimizer/qualify_columns.py index a7bd9b5..e793e31 100644 --- a/sqlglot/optimizer/qualify_columns.py +++ b/sqlglot/optimizer/qualify_columns.py @@ -146,7 +146,7 @@ def _expand_group_by(scope, resolver): # Source columns get priority over select aliases if table: - node.set("table", exp.to_identifier(table)) + node.set("table", table) return node selects = {s.alias_or_name: s for s in scope.selects} @@ -212,7 +212,7 @@ def _qualify_columns(scope, resolver): # column_table can be a '' because bigquery unnest has no table alias if column_table: - column.set("table", exp.to_identifier(column_table)) + column.set("table", column_table) columns_missing_from_scope = [] # Determine whether each reference in the order by clause is to a column or an alias. @@ -239,7 +239,7 @@ def _qualify_columns(scope, resolver): column_table = resolver.get_table(column.name) if column_table: - column.set("table", exp.to_identifier(column_table)) + column.set("table", column_table) def _expand_stars(scope, resolver): @@ -340,7 +340,7 @@ class Resolver: self._unambiguous_columns = None self._all_columns = None - def get_table(self, column_name: str) -> t.Optional[str]: + def get_table(self, column_name: str) -> t.Optional[exp.Identifier]: """ Get the table for a column name. @@ -354,18 +354,29 @@ class Resolver: self._get_all_source_columns() ) - table = self._unambiguous_columns.get(column_name) + table_name = self._unambiguous_columns.get(column_name) - if not table: + if not table_name: sources_without_schema = tuple( source for source, columns in self._get_all_source_columns().items() if not columns or "*" in columns ) if len(sources_without_schema) == 1: - return sources_without_schema[0] + table_name = sources_without_schema[0] - return table + if table_name not in self.scope.selected_sources: + return exp.to_identifier(table_name) + + node, _ = self.scope.selected_sources.get(table_name) + + if isinstance(node, exp.Subqueryable): + while node and node.alias != table_name: + node = node.parent + node_alias = node.args.get("alias") + if node_alias: + return node_alias.this + return exp.to_identifier(table_name) @property def all_columns(self): -- cgit v1.2.3