summaryrefslogtreecommitdiffstats
path: root/sqlglot/optimizer/scope.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-10-15 13:53:00 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-10-15 13:53:00 +0000
commit684905e3de7854a3806ffa55e0d1a09431ba5a19 (patch)
tree127ebd7d051f15fb8f8cf36cfd04a8a65a4d9680 /sqlglot/optimizer/scope.py
parentReleasing debian version 6.3.1-1. (diff)
downloadsqlglot-684905e3de7854a3806ffa55e0d1a09431ba5a19.tar.xz
sqlglot-684905e3de7854a3806ffa55e0d1a09431ba5a19.zip
Merging upstream version 7.1.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sqlglot/optimizer/scope.py')
-rw-r--r--sqlglot/optimizer/scope.py26
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):
"""