Skip to content

Commit

Permalink
Fix: Make exp.Update a DML node (#4223)
Browse files Browse the repository at this point in the history
* feat: Make exp.Update be a DML node

* Add more tests

* Add qualify test
  • Loading branch information
VaggelisD authored Oct 8, 2024
1 parent 0882f03 commit dcdec95
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
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
3 changes: 3 additions & 0 deletions tests/fixtures/optimizer/qualify_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,6 @@ COPY INTO (SELECT * FROM c.db.x AS x) TO 'data' WITH (FORMAT 'CSV');
# title: tablesample
SELECT 1 FROM x TABLESAMPLE SYSTEM (10 PERCENT) CROSS JOIN y TABLESAMPLE SYSTEM (10 PERCENT);
SELECT 1 FROM c.db.x AS x TABLESAMPLE SYSTEM (10 PERCENT) CROSS JOIN c.db.y AS y TABLESAMPLE SYSTEM (10 PERCENT);

WITH cte_tbl AS (SELECT 1 AS col2) UPDATE y SET col1 = (SELECT * FROM x) WHERE EXISTS(SELECT 1 FROM cte_tbl);
WITH cte_tbl AS (SELECT 1 AS col2) UPDATE c.db.y SET col1 = (SELECT * FROM c.db.x AS x) WHERE EXISTS(SELECT 1 FROM cte_tbl AS cte_tbl);
12 changes: 12 additions & 0 deletions tests/test_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,18 @@ def test_scope(self):
self.assertEqual(scopes[2].expression.sql(), f"SELECT a FROM foo CROSS JOIN {udtf}")
self.assertEqual(set(scopes[2].sources), {"", "foo"})

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

sql = "UPDATE tbl1 SET col = 1 WHERE EXISTS (SELECT 1 FROM tbl2 WHERE tbl1.id = tbl2.id)"
self.assertEqual(len(traverse_scope(parse_one(sql))), 1)

sql = "UPDATE tbl1 SET col = 0"
self.assertEqual(len(traverse_scope(parse_one(sql))), 0)

@patch("sqlglot.optimizer.scope.logger")
def test_scope_warning(self, logger):
self.assertEqual(len(traverse_scope(parse_one("WITH q AS (@y) SELECT * FROM q"))), 1)
Expand Down

0 comments on commit dcdec95

Please sign in to comment.