From e0fd167849b8531afc99c7bb250b89f4251782c1 Mon Sep 17 00:00:00 2001 From: Harshal Sheth Date: Mon, 21 Oct 2024 23:20:23 -0700 Subject: [PATCH] move to new scope depth --- sqlglot/lineage.py | 7 +------ sqlglot/optimizer/scope.py | 15 +++++++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/sqlglot/lineage.py b/sqlglot/lineage.py index dc76a07d3d..2c9c1f5e10 100644 --- a/sqlglot/lineage.py +++ b/sqlglot/lineage.py @@ -294,12 +294,7 @@ def to_node( subfield = ".".join(subfields) node.downstream.append( - Node( - name=c.sql(comments=False), - source=source, - expression=source, - subfield=subfield, - ) + Node(name=c.sql(comments=False), source=source, expression=source, subfield=subfield) ) return node diff --git a/sqlglot/optimizer/scope.py b/sqlglot/optimizer/scope.py index 0a98d57b73..17009cdb86 100644 --- a/sqlglot/optimizer/scope.py +++ b/sqlglot/optimizer/scope.py @@ -14,8 +14,6 @@ TRAVERSABLES = (exp.Query, exp.DDL, exp.DML) -MAX_SCOPE_DEPTH = 500 - class ScopeType(Enum): ROOT = auto() @@ -454,7 +452,6 @@ def remove_source(self, name): def __repr__(self): return f"Scope<{self.expression.sql()}>" - def traverse(self): """ Traverse the scope tree from this node. @@ -463,9 +460,16 @@ def traverse(self): Scope: scope instances in depth-first-search post-order """ stack = [self] + seen_scopes = set() result = [] while stack: scope = stack.pop() + + # Scopes aren't hashable, so we use id(scope) instead. + if id(scope) in seen_scopes: + raise OptimizeError(f"Scope {scope} has a circular scope dependency") + seen_scopes.add(id(scope)) + result.append(scope) stack.extend( itertools.chain( @@ -475,11 +479,6 @@ def traverse(self): scope.subquery_scopes, ) ) - actual_depth = len(result) - if actual_depth > MAX_SCOPE_DEPTH: - raise OptimizeError( - f"Scope depth limit of {MAX_SCOPE_DEPTH} exceeded" - ) yield from reversed(result)