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: Make exp.Update a DML node #4223

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3278,7 +3278,7 @@ class Intersect(SetOperation):
pass


class Update(Expression):
class Update(DML):
arg_types = {
"with": False,
"this": False,
Expand Down
4 changes: 2 additions & 2 deletions sqlglot/optimizer/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,8 @@ def _traverse_scope(scope):
elif isinstance(expression, exp.DML):
yield from _traverse_ctes(scope)
for query in find_all_in_scope(expression, exp.Query):
# This check ensures we don't yield the CTE queries twice
if not isinstance(query.parent, exp.CTE):
# This check ensures we don't yield the CTE/nested queries twice
if not isinstance(query.parent, (exp.CTE, exp.Subquery)):
yield from _traverse_scope(Scope(query, cte_sources=scope.cte_sources))
return
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,12 @@ def test_scope(self):
)
self.assertEqual(set(scopes[3].sources), {""})

sql = (
"UPDATE customers SET total_spent = (SELECT 1 FROM t1) WHERE EXISTS (SELECT 1 FROM t2)"
)
expression = parse_one(sql)
self.assertEqual(len(traverse_scope(expression)), 3)

inner_query = "SELECT bar FROM baz"
for udtf in (f"UNNEST(({inner_query}))", f"LATERAL ({inner_query})"):
sql = f"SELECT a FROM foo CROSS JOIN {udtf}"
Expand Down
Loading