Merging upstream version 23.13.1.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
63a75c51ff
commit
64041d1d66
85 changed files with 53899 additions and 50390 deletions
|
@ -8,6 +8,7 @@ from dataclasses import dataclass, field
|
|||
from sqlglot import Schema, exp, maybe_parse
|
||||
from sqlglot.errors import SqlglotError
|
||||
from sqlglot.optimizer import Scope, build_scope, find_all_in_scope, normalize_identifiers, qualify
|
||||
from sqlglot.optimizer.scope import ScopeType
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
from sqlglot.dialects.dialect import DialectType
|
||||
|
@ -129,12 +130,6 @@ def to_node(
|
|||
reference_node_name: t.Optional[str] = None,
|
||||
trim_selects: bool = True,
|
||||
) -> Node:
|
||||
source_names = {
|
||||
dt.alias: dt.comments[0].split()[1]
|
||||
for dt in scope.derived_tables
|
||||
if dt.comments and dt.comments[0].startswith("source: ")
|
||||
}
|
||||
|
||||
# Find the specific select clause that is the source of the column we want.
|
||||
# This can either be a specific, named select or a generic `*` clause.
|
||||
select = (
|
||||
|
@ -242,13 +237,31 @@ def to_node(
|
|||
# If the source is a UDTF find columns used in the UTDF to generate the table
|
||||
if isinstance(source, exp.UDTF):
|
||||
source_columns |= set(source.find_all(exp.Column))
|
||||
derived_tables = [
|
||||
source.expression.parent
|
||||
for source in scope.sources.values()
|
||||
if isinstance(source, Scope) and source.is_derived_table
|
||||
]
|
||||
else:
|
||||
derived_tables = scope.derived_tables
|
||||
|
||||
source_names = {
|
||||
dt.alias: dt.comments[0].split()[1]
|
||||
for dt in derived_tables
|
||||
if dt.comments and dt.comments[0].startswith("source: ")
|
||||
}
|
||||
|
||||
for c in source_columns:
|
||||
table = c.table
|
||||
source = scope.sources.get(table)
|
||||
|
||||
if isinstance(source, Scope):
|
||||
selected_node, _ = scope.selected_sources.get(table, (None, None))
|
||||
reference_node_name = None
|
||||
if source.scope_type == ScopeType.DERIVED_TABLE and table not in source_names:
|
||||
reference_node_name = table
|
||||
elif source.scope_type == ScopeType.CTE:
|
||||
selected_node, _ = scope.selected_sources.get(table, (None, None))
|
||||
reference_node_name = selected_node.name if selected_node else None
|
||||
# The table itself came from a more specific scope. Recurse into that one using the unaliased column name.
|
||||
to_node(
|
||||
c.name,
|
||||
|
@ -257,7 +270,7 @@ def to_node(
|
|||
scope_name=table,
|
||||
upstream=node,
|
||||
source_name=source_names.get(table) or source_name,
|
||||
reference_node_name=selected_node.name if selected_node else None,
|
||||
reference_node_name=reference_node_name,
|
||||
trim_selects=trim_selects,
|
||||
)
|
||||
else:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue