diff options
Diffstat (limited to 'sqlglot/optimizer/scope.py')
-rw-r--r-- | sqlglot/optimizer/scope.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py index 89de517..68298a0 100644 --- a/sqlglot/optimizer/scope.py +++ b/sqlglot/optimizer/scope.py @@ -1,4 +1,5 @@ import itertools +from collections import defaultdict from enum import Enum, auto from sqlglot import exp @@ -315,6 +316,16 @@ class Scope: return self._external_columns @property + def unqualified_columns(self): + """ + Unqualified columns in the current scope. + + Returns: + list[exp.Column]: Unqualified columns + """ + return [c for c in self.columns if not c.table] + + @property def join_hints(self): """ Hints that exist in the scope that reference tables @@ -403,6 +414,21 @@ class Scope: yield from child_scope.traverse() yield self + def ref_count(self): + """ + Count the number of times each scope in this tree is referenced. + + Returns: + dict[int, int]: Mapping of Scope instance ID to reference count + """ + scope_ref_count = defaultdict(lambda: 0) + + for scope in self.traverse(): + for _, source in scope.selected_sources.values(): + scope_ref_count[id(source)] += 1 + + return scope_ref_count + def traverse_scope(expression): """ |