Skip to content

Commit 2786df7

Browse files
committed
fix(lint): a join inside UPDATE FROM never limits the write
SQLCLEAN049 counted a join in an UPDATE's FROM list as limiting the rows written, so UPDATE t SET x = 1 FROM a JOIN b ON a.k = b.k was not reported, though it changes every row of t. A join inside FROM, or inside a DELETE's USING list, cannot read the target: PostgreSQL, DuckDB and SQLite all refuse an ON that names it. So only WHERE, LIMIT and a join on the target itself (UPDATE's table joins and the MySQL multi-table DELETE's joins) limit which rows change. PostgreSQL and DuckDB cases cover the one-item, comma and parenthesised forms and DELETE USING, each changing every row on the engines, and the same statements with a WHERE reading the target, which change one.
1 parent e47ee46 commit 2786df7

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

‎crates/sqlquack-lint/src/quality/_helpers/safety.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,14 @@ fn statement_spans(
478478
(writes, inserts)
479479
}
480480

481+
/// Whether a WHERE, LIMIT or join on the target limits an UPDATE; a join inside FROM cannot read it.
481482
fn update_is_limited(update: &Update) -> bool {
482483
update
483484
.where_clause
484485
.as_ref()
485486
.is_some_and(|clause| reads_rows(&clause.this))
486487
|| update.limit.is_some()
487-
|| update
488-
.table_joins
489-
.iter()
490-
.chain(&update.from_joins)
491-
.any(join_is_limiting)
488+
|| update.table_joins.iter().any(join_is_limiting)
492489
}
493490

494491
fn delete_is_limited(delete: &Delete) -> bool {

‎crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ fn given_safety_rule_inputs_when_linting_then_exactly_the_mistakes_are_reported(
164164
rule: "SQLCLEAN049",
165165
expected_anchors: &["UPDATE", "DELETE"],
166166
},
167+
SafetyRuleTestCase {
168+
description: "a join inside UPDATE FROM or DELETE USING does not limit the write (PostgreSQL 18: 3 of 3 rows)",
169+
dialect: "postgres",
170+
sql: "UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id; \
171+
UPDATE orders SET status = 'closed' FROM stores s, customers c JOIN refunds r ON r.id = c.id; \
172+
UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id, stores s; \
173+
UPDATE orders SET status = 'closed' FROM (customers c JOIN refunds r ON r.id = c.id); \
174+
DELETE FROM orders USING customers c JOIN refunds r ON r.id = c.id",
175+
rule: "SQLCLEAN049",
176+
expected_anchors: &["UPDATE", "UPDATE", "UPDATE", "UPDATE", "DELETE"],
177+
},
178+
SafetyRuleTestCase {
179+
description: "a WHERE reading the target limits an UPDATE FROM or DELETE USING with joins",
180+
dialect: "postgres",
181+
sql: "UPDATE orders SET status = 'closed' FROM stores s, customers c JOIN refunds r ON r.id = c.id WHERE r.id = orders.id; \
182+
DELETE FROM orders USING customers c JOIN refunds r ON r.id = c.id WHERE r.id = orders.id",
183+
rule: "SQLCLEAN049",
184+
expected_anchors: &[],
185+
},
186+
SafetyRuleTestCase {
187+
description: "a join inside UPDATE FROM does not limit the write (DuckDB 2.0: 3 of 3 rows)",
188+
dialect: "duckdb",
189+
sql: "UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id; \
190+
UPDATE orders SET status = 'closed' FROM (customers c JOIN refunds r ON r.id = c.id)",
191+
rule: "SQLCLEAN049",
192+
expected_anchors: &["UPDATE", "UPDATE"],
193+
},
167194
SafetyRuleTestCase {
168195
description: "DEFAULT VALUES needs no column list",
169196
dialect: "postgres",

0 commit comments

Comments
 (0)