Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 10 additions & 7 deletions crates/sqlquack-lint/src/lint/_helpers/join_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use sqlquack_syntax::ExpressionWalk;
use sqlquack_syntax::expressions::names::models::Identifier;
use sqlquack_syntax::expressions::names::models::Literal;
use sqlquack_syntax::expressions::names::models::TableRef;
use sqlquack_syntax::expressions::queries::models::Join;
use sqlquack_syntax::expressions::queries::models::JoinKind;
use sqlquack_syntax::expressions::queries::models::Select;
use sqlquack_syntax::expressions::queries::models::Subquery;
Expand All @@ -24,6 +25,7 @@ use crate::lint::constants::{
CROSS_JOIN_REASON_PLACEHOLDER, CROSS_JOIN_SUFFIX, CROSS_JOIN_UNNAMED_MESSAGE,
};
use crate::lint::models::{CrossJoinRequest, LintDiagnostic, LintRuleMetadata};
use crate::tree::main::join_chains::join_chains;

const CROSS_JOIN_FIX_REASON: &str = "a keyed relationship requires user intent";

Expand Down Expand Up @@ -453,17 +455,18 @@ impl Analysis<'_> {
}

fn record_joins(&mut self, select: &Select, cte: Option<&str>) {
let single_from: Option<&Expression> = select
.from
.as_ref()
.filter(|from| from.expressions.len() == 1)
.and_then(|from| from.expressions.first());
for (index, join) in select.joins.iter().enumerate() {
for (start, joins) in join_chains(select) {
self.record_chain((start, joins), cte);
}
}

fn record_chain(&mut self, (start, joins): (Option<&Expression>, &[Join]), cte: Option<&str>) {
for (index, join) in joins.iter().enumerate() {
if join.kind != JoinKind::Cross {
continue;
}
let left_one_row: bool =
index == 0 && single_from.is_some_and(|from| self.relation_one_row(from));
index == 0 && start.is_some_and(|from| self.relation_one_row(from));
let multiplies: bool = !(left_one_row || self.relation_one_row(&join.this));
self.targets.push(CrossJoinTarget {
anchors: relation_anchors(&join.this),
Expand Down
21 changes: 21 additions & 0 deletions crates/sqlquack-lint/src/lint/tests/test_cross_joins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ fn given_cross_joins_when_one_side_is_provably_one_row_then_only_multiplying_joi
one_row_relations: &[],
expected_messages: &["CROSS JOIN with `newest` creates an explicit cartesian product"],
},
CrossJoinTestCase {
description: "a cross join after a comma item, in DuckDB",
sql: "SELECT o.order_id FROM orders AS o, customers AS c CROSS JOIN regions AS r",
dialect: "duckdb",
one_row_relations: &[],
expected_messages: &["CROSS JOIN with `regions` creates an explicit cartesian product"],
},
CrossJoinTestCase {
description: "a cross join after a comma item, in PostgreSQL",
sql: "SELECT o.order_id FROM orders AS o, customers AS c CROSS JOIN regions AS r",
dialect: "postgres",
one_row_relations: &[],
expected_messages: &["CROSS JOIN with `regions` creates an explicit cartesian product"],
},
CrossJoinTestCase {
description: "a cross join of a one-row seed after a comma item",
sql: "SELECT o.order_id FROM orders AS o, __sqlclean_interpolation_7__ AS p CROSS JOIN customers AS c",
dialect: "postgres",
one_row_relations: &[POLICY_SENTINEL],
expected_messages: &[],
},
CrossJoinTestCase {
description: "a join inside the carrying CTE stops the proof",
sql: "WITH totals AS (SELECT COUNT(*) AS n FROM orders), \
Expand Down
17 changes: 14 additions & 3 deletions crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use std::borrow::Cow;

use crate::tree::main::join_chains::join_chains;
use crate::tree::main::walk_in_scope::walk_in_scope;
use sqlquack_syntax::expressions::queries::models::Cte;
use sqlquack_syntax::expressions::queries::models::Join;
use sqlquack_syntax::expressions::queries::models::JoinKind;
use sqlquack_syntax::expressions::queries::models::Select;
use sqlquack_syntax::expressions::queries::models::With;
Expand Down Expand Up @@ -220,7 +222,11 @@ fn reader_usage(target: &Target<'_>, reader: Option<String>, body: &[&Expression
if local.is_empty() {
continue;
}
if select.joins.iter().any(|join| {
let joins: Vec<&Join> = join_chains(select)
.into_iter()
.flat_map(|(_, joins)| joins)
.collect();
if joins.iter().any(|join| {
matches!(
join.kind,
JoinKind::Natural
Expand All @@ -231,7 +237,7 @@ fn reader_usage(target: &Target<'_>, reader: Option<String>, body: &[&Expression
}) {
usage.everything = true;
}
for join in &select.joins {
for join in joins {
usage
.names
.extend(join.using.iter().map(|identifier| lower(&identifier.name)));
Expand Down Expand Up @@ -332,7 +338,12 @@ fn clause_roots(select: &Select) -> Vec<&Expression> {
if let Some(order) = &select.order_by {
roots.extend(order.expressions.iter().map(|ordered| &ordered.this));
}
roots.extend(select.joins.iter().filter_map(|join| join.on.as_ref()));
roots.extend(
join_chains(select)
.into_iter()
.flat_map(|(_, joins)| joins)
.filter_map(|join| join.on.as_ref()),
);
roots
}

Expand Down
3 changes: 3 additions & 0 deletions crates/sqlquack-lint/src/quality/_helpers/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ impl KeyEnvironment {
return Vec::new();
}
let found: Vec<(String, Option<String>, &Expression)> = sources(select);
if found.len() != select.joins.len() + 1 {
return Vec::new();
}
let Some((first_alias, _, first)) = found.first() else {
return Vec::new();
};
Expand Down
7 changes: 2 additions & 5 deletions crates/sqlquack-lint/src/quality/_helpers/safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,14 @@ fn statement_spans(
(writes, inserts)
}

/// Whether a WHERE, LIMIT or join on the target limits an UPDATE; a join inside FROM cannot read it.
fn update_is_limited(update: &Update) -> bool {
update
.where_clause
.as_ref()
.is_some_and(|clause| reads_rows(&clause.this))
|| update.limit.is_some()
|| update
.table_joins
.iter()
.chain(&update.from_joins)
.any(join_is_limiting)
|| update.table_joins.iter().any(join_is_limiting)
}

fn delete_is_limited(delete: &Delete) -> bool {
Expand Down
22 changes: 21 additions & 1 deletion crates/sqlquack-lint/src/quality/_helpers/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ pub(super) fn qualifier(column: &Column) -> Option<String> {
pub(super) fn sources(select: &Select) -> Vec<(String, Option<String>, &Expression)> {
let mut found: Vec<(String, Option<String>, &Expression)> = Vec::new();
let from = select.from.iter().flat_map(|from| from.expressions.iter());
for expression in from.chain(select.joins.iter().map(|join| &join.this)) {
for expression in from
.chain(select.joins.iter().map(|join| &join.this))
.flat_map(joined_relations)
{
match expression {
Expression::Table(table) => {
let name: String = lower(&table.name.name);
Expand All @@ -132,6 +135,23 @@ pub(super) fn sources(select: &Select) -> Vec<(String, Option<String>, &Expressi
found
}

/// The relations of an unaliased joined table, in parentheses or not, in order; else `item`.
fn joined_relations(item: &Expression) -> Vec<&Expression> {
match item {
Expression::JoinedTable(joined) if joined.alias.is_none() => {
let mut relations: Vec<&Expression> = joined_relations(&joined.left);
for join in &joined.joins {
relations.extend(joined_relations(&join.this));
}
relations
}
Expression::Paren(paren) if matches!(paren.this, Expression::JoinedTable(_)) => {
joined_relations(&paren.this)
}
_ => vec![item],
}
}

/// Token index of the SELECT opening each `name AS ( [( ...] SELECT` CTE body, by lower-cased name.
pub(super) fn cte_select_tokens(tokens: &[Token]) -> FxHashMap<String, usize> {
let significant: Vec<usize> = (0..tokens.len())
Expand Down
27 changes: 27 additions & 0 deletions crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,33 @@ fn given_safety_rule_inputs_when_linting_then_exactly_the_mistakes_are_reported(
rule: "SQLCLEAN049",
expected_anchors: &["UPDATE", "DELETE"],
},
SafetyRuleTestCase {
description: "a join inside UPDATE FROM or DELETE USING does not limit the write (PostgreSQL 18: 3 of 3 rows)",
dialect: "postgres",
sql: "UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id; \
UPDATE orders SET status = 'closed' FROM stores s, customers c JOIN refunds r ON r.id = c.id; \
UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id, stores s; \
UPDATE orders SET status = 'closed' FROM (customers c JOIN refunds r ON r.id = c.id); \
DELETE FROM orders USING customers c JOIN refunds r ON r.id = c.id",
rule: "SQLCLEAN049",
expected_anchors: &["UPDATE", "UPDATE", "UPDATE", "UPDATE", "DELETE"],
},
SafetyRuleTestCase {
description: "a WHERE reading the target limits an UPDATE FROM or DELETE USING with joins",
dialect: "postgres",
sql: "UPDATE orders SET status = 'closed' FROM stores s, customers c JOIN refunds r ON r.id = c.id WHERE r.id = orders.id; \
DELETE FROM orders USING customers c JOIN refunds r ON r.id = c.id WHERE r.id = orders.id",
rule: "SQLCLEAN049",
expected_anchors: &[],
},
SafetyRuleTestCase {
description: "a join inside UPDATE FROM does not limit the write (DuckDB 2.0: 3 of 3 rows)",
dialect: "duckdb",
sql: "UPDATE orders SET status = 'closed' FROM customers c JOIN refunds r ON r.id = c.id; \
UPDATE orders SET status = 'closed' FROM (customers c JOIN refunds r ON r.id = c.id)",
rule: "SQLCLEAN049",
expected_anchors: &["UPDATE", "UPDATE"],
},
SafetyRuleTestCase {
description: "DEFAULT VALUES needs no column list",
dialect: "postgres",
Expand Down
41 changes: 41 additions & 0 deletions crates/sqlquack-lint/src/tree/main/join_chains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! `join_chains`: a SELECT's joins, each chain with the relation it starts from.

use sqlquack_syntax::Expression;
use sqlquack_syntax::expressions::queries::models::{Join, Select};

/// Every join chain of `select` with the relation it starts from, its own first, then nested ones.
pub(crate) fn join_chains(select: &Select) -> Vec<(Option<&Expression>, &[Join])> {
let items: &[Expression] = select
.from
.as_ref()
.map_or(&[], |from| from.expressions.as_slice());
let lone: Option<&Expression> = match items {
[item] => Some(item),
_ => None,
};
let mut chains: Vec<(Option<&Expression>, &[Join])> = vec![(lone, select.joins.as_slice())];
for item in items
.iter()
.chain(select.joins.iter().map(|join| &join.this))
{
chains.extend(nested_chains(item));
}
chains
}

/// The join chains written inside `item`, outermost first, each with its joined table's left side.
fn nested_chains(item: &Expression) -> Vec<(Option<&Expression>, &[Join])> {
match item {
Expression::JoinedTable(joined) => {
let mut chains: Vec<(Option<&Expression>, &[Join])> =
vec![(Some(&joined.left), joined.joins.as_slice())];
chains.extend(nested_chains(&joined.left));
for join in &joined.joins {
chains.extend(nested_chains(&join.this));
}
chains
}
Expression::Paren(paren) => nested_chains(&paren.this),
_ => Vec::new(),
}
}
1 change: 1 addition & 0 deletions crates/sqlquack-lint/src/tree/main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub(crate) mod contains_aggregate;
pub(crate) mod is_aggregate;
pub(crate) mod is_foreign_aggregate;
pub(crate) mod is_row_number;
pub(crate) mod join_chains;
pub(crate) mod walk_in_scope;
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::lineage::tests::helpers::duckdb_shop_schema;
use crate::lineage::tests::helpers::output_ranges;
use crate::lineage::tests::helpers::owned_findings;
use crate::lineage::tests::test_types::QueryFindingsTestCase;
use crate::lineage::tests::test_types::StarOrderTestCase;
use crate::lineage::tests::test_types::StrictRangeTestCase;

#[test]
Expand Down Expand Up @@ -722,3 +723,47 @@ fn given_one_string_spelled_two_ways_when_grouping_then_the_expressions_match()
);
}
}

#[test]
fn given_a_comma_after_a_joined_relation_when_checked_then_the_join_reads_only_its_own_relations() {
let test_cases = [
QueryFindingsTestCase {
description: "a USING key the left of its join lacks, a later comma relation offering it",
sql: "SELECT * FROM customers a JOIN orders b USING (status), orders c",
expected_findings: &[(
"E201",
"JOIN USING column 'status' must exist on both sides",
7,
)],
},
QueryFindingsTestCase {
description: "a USING key both sides of its join offer, before a comma relation",
sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c",
expected_findings: &[],
},
];
for test_case in test_cases {
assert_eq!(
databricks_findings(test_case.sql),
owned_findings(test_case.expected_findings),
"{}",
test_case.description
);
}
}

#[test]
fn given_a_using_join_after_a_comma_when_expanding_a_star_then_its_key_leads_its_own_group() {
let test_cases = [StarOrderTestCase {
description: "the USING key comes first in its group, after the earlier comma relation",
sql: "SELECT * FROM customers a, customers b JOIN customers c USING (name)",
expected_names: &["id", "name", "name", "id", "id"],
}];
for test_case in test_cases {
let names: Vec<String> = databricks_outputs(test_case.sql)
.into_iter()
.map(|(name, _)| name)
.collect();
assert_eq!(names, test_case.expected_names, "{}", test_case.description);
}
}
Loading
Loading