Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix!: traverse union scopes iteratively #3112

Merged
merged 5 commits into from
Mar 11, 2024

Conversation

georgesittas
Copy link
Collaborator

No description provided.

sqlglot/optimizer/scope.py Outdated Show resolved Hide resolved
@tobymao
Copy link
Owner

tobymao commented Mar 9, 2024

does this work now? should we add a test like

from sqlglot import parse_one
from sqlglot.optimizer.scope import build_scope

sql = "SELECT 1 UNION ALL " * 10000
sql += "SELECT 1"

q = parse_one(sql)

build_scope(q)

@georgesittas
Copy link
Collaborator Author

yep it works

@georgesittas
Copy link
Collaborator Author

Hmm, the traversal though is still problematic...

>>> from sqlglot import exp, parse_one
>>> from sqlglot.optimizer.scope import build_scope
>>>
>>> large_union = parse_one(" UNION ALL ".join(["SELECT x FROM t"] * 1000))
>>> len(list(large_union.find_all(exp.Select)))
1000
>>>
>>> scope = build_scope(large_union) # ok
>>> for s in scope.traverse():
...     print(s)
...
    for child_scope in itertools.chain(
RecursionError: maximum recursion depth exceeded while calling a Python object

sqlglot/optimizer/scope.py Outdated Show resolved Hide resolved
sqlglot/optimizer/scope.py Outdated Show resolved Hide resolved
sqlglot/optimizer/scope.py Outdated Show resolved Hide resolved
@georgesittas georgesittas merged commit b1c8cac into main Mar 11, 2024
5 checks passed
@georgesittas georgesittas deleted the jo/iterative_union_scope_traversal branch March 11, 2024 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants