1 parent 6fbe985 commit 3ec5e35Copy full SHA for 3ec5e35
1 file changed
sqlglot/optimizer/qualify_columns.py
@@ -669,7 +669,9 @@ def _qualify_columns(
669
and isinstance(position := column.this.this, exp.Literal)
670
and position.is_int
671
):
672
- # Preserve references whose source shape cannot be resolved losslessly.
+ # Pivots from unrelated sources may share this scope, so prefer an exact
673
+ # output-alias match. For an aliasless chain, fall back to the last operator
674
+ # on the referenced source.
675
scope_pivot = next(
676
(pivot for pivot in scope.pivots if pivot.alias == column_table), None
677
)
0 commit comments