Skip to content

Commit

Permalink
move to new scope depth
Browse files Browse the repository at this point in the history
  • Loading branch information
hsheth2 committed Oct 22, 2024
1 parent 6c80d1f commit e0fd167
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
7 changes: 1 addition & 6 deletions sqlglot/lineage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions sqlglot/optimizer/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

TRAVERSABLES = (exp.Query, exp.DDL, exp.DML)

MAX_SCOPE_DEPTH = 500


class ScopeType(Enum):
ROOT = auto()
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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)

Expand Down

0 comments on commit e0fd167

Please sign in to comment.