summaryrefslogtreecommitdiffstats
path: root/sqlglot/optimizer
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-20 08:50:35 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-20 08:50:35 +0000
commit6a22086850fc960715b618e82f4c2e43a4529146 (patch)
tree2005800d79fbd3b3a72ed36d10857fadd61306b7 /sqlglot/optimizer
parentReleasing debian version 11.1.3-1. (diff)
downloadsqlglot-6a22086850fc960715b618e82f4c2e43a4529146.tar.xz
sqlglot-6a22086850fc960715b618e82f4c2e43a4529146.zip
Merging upstream version 11.2.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/optimizer')
-rw-r--r--sqlglot/optimizer/annotate_types.py2
-rw-r--r--sqlglot/optimizer/qualify_columns.py27
2 files changed, 20 insertions, 9 deletions
diff --git a/sqlglot/optimizer/annotate_types.py b/sqlglot/optimizer/annotate_types.py
index be65ab9..ca2131c 100644
--- a/sqlglot/optimizer/annotate_types.py
+++ b/sqlglot/optimizer/annotate_types.py
@@ -286,7 +286,7 @@ class TypeAnnotator:
source = scope.sources.get(col.table)
if isinstance(source, exp.Table):
col.type = self.schema.get_column_type(source, col)
- elif source and col.table in selects:
+ elif source and col.table in selects and col.name in selects[col.table]:
col.type = selects[col.table][col.name].type
# Then (possibly) annotate the remaining expressions in the scope
self._maybe_annotate(scope.expression)
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):