Skip to content

Commit b728e19

Browse files
committed
fix(semantics): a join condition reads only the items its engine lets it see
An ON condition was checked against every FROM item of its level. In PostgreSQL it sees only its own comma group, up to the join: `a, b JOIN c ON a.id = c.id` is an "invalid reference to FROM-clause entry", also from a subquery or a parenthesised join, and an ON condition reading a later comma group or a later item of its own join chain is a "missing FROM-clause entry". DuckDB lets it read every earlier item and rejects only later ones. A new dialect rule, join_conditions_read, gives each join condition the items it reaches: its own group on PostgreSQL, every earlier item on DuckDB, and every item elsewhere, as before. The existing name checks then report E222 or E201.
1 parent 027ee63 commit b728e19

15 files changed

Lines changed: 298 additions & 13 deletions

File tree

‎crates/sqlquack-semantics/src/bigquery/profile/models.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::bigquery::profile::_helpers::typing::{
1010
use crate::bigquery::profile::constants::{BIGQUERY_DATE_PART_FUNCTIONS, BIGQUERY_WEEKDAY_PARTS};
1111
use crate::dialects::types::DialectProfile;
1212
use crate::lineage::constants::SNOWFLAKE_VALUES_COLUMN;
13-
use crate::lineage::models::Engine;
13+
use crate::lineage::models::{Engine, JoinConditionReach};
1414
use crate::lineage::models::{Lineage, LineageContext, TransformKind};
1515
use crate::lineage::types::DialectRules;
1616
use crate::lineage::types::ValueType;
@@ -70,6 +70,10 @@ impl DialectRules for BigQueryRules {
7070
true
7171
}
7272

73+
fn join_conditions_read(&self) -> JoinConditionReach {
74+
JoinConditionReach::Every
75+
}
76+
7377
fn checks_call_signatures(&self) -> bool {
7478
true
7579
}

‎crates/sqlquack-semantics/src/clickhouse/profile/models.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::lineage::constants::{
1414
DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS,
1515
};
1616
use crate::lineage::main::rendered_type::rendered_type;
17-
use crate::lineage::models::Engine;
17+
use crate::lineage::models::{Engine, JoinConditionReach};
1818
use crate::lineage::models::{Lineage, LineageContext};
1919
use crate::lineage::types::DialectRules;
2020
use crate::lineage::types::ValueType;
@@ -85,6 +85,10 @@ impl DialectRules for ClickHouseRules {
8585
true
8686
}
8787

88+
fn join_conditions_read(&self) -> JoinConditionReach {
89+
JoinConditionReach::Every
90+
}
91+
8892
fn checks_call_signatures(&self) -> bool {
8993
true
9094
}

‎crates/sqlquack-semantics/src/databricks/profile/models.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::databricks::profile::_helpers::typing::{
1111
use crate::databricks::profile::constants::DATABRICKS_VALUES_COLUMN;
1212
use crate::dialects::types::DialectProfile;
1313
use crate::lineage::constants::DATE_PART_FUNCTIONS;
14-
use crate::lineage::models::Engine;
14+
use crate::lineage::models::{Engine, JoinConditionReach};
1515
use crate::lineage::models::{Lineage, LineageContext};
1616
use crate::lineage::types::DialectRules;
1717
use crate::lineage::types::ValueType;
@@ -75,6 +75,10 @@ impl DialectRules for DatabricksRules {
7575
true
7676
}
7777

78+
fn join_conditions_read(&self) -> JoinConditionReach {
79+
JoinConditionReach::Every
80+
}
81+
7882
fn checks_call_signatures(&self) -> bool {
7983
true
8084
}

‎crates/sqlquack-semantics/src/duckdb/profile/models.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::duckdb::profile::constants::{
1616
DUCKDB_VALUES_COLUMN,
1717
};
1818
use crate::lineage::constants::{DATE_PART_FUNCTIONS, WINDOW_ONLY_FUNCTIONS};
19-
use crate::lineage::models::Engine;
19+
use crate::lineage::models::{Engine, JoinConditionReach};
2020
use crate::lineage::models::{Lineage, LineageContext};
2121
use crate::lineage::types::DialectRules;
2222
use crate::lineage::types::ValueType;
@@ -76,6 +76,10 @@ impl DialectRules for DuckDbRules {
7676
true
7777
}
7878

79+
fn join_conditions_read(&self) -> JoinConditionReach {
80+
JoinConditionReach::Earlier
81+
}
82+
7983
fn checks_call_signatures(&self) -> bool {
8084
true
8185
}

‎crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,58 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on
770770
sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c",
771771
expected_findings: &[],
772772
},
773+
QueryFindingsTestCase {
774+
description: "an ON condition reading a USING key only a later join merges (2.0: column not found)",
775+
sql: "SELECT 1 FROM exports a JOIN exports b ON id IS NOT NULL JOIN orders c ON true JOIN customers d USING (id)",
776+
expected_findings: &[(
777+
"E201",
778+
"Unknown column 'id' (not found in any referenced table)",
779+
42,
780+
)],
781+
},
782+
QueryFindingsTestCase {
783+
description: "an ON condition reading a key a later join widens, beside an item offering the name (2.0: accepted)",
784+
sql: "SELECT 1 FROM orders x JOIN (orders a JOIN customers b USING (id)) ON true JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)",
785+
expected_findings: &[],
786+
},
787+
QueryFindingsTestCase {
788+
description: "an ON condition reading a USING key a later join merges again (2.0: accepted)",
789+
sql: "SELECT 1 FROM orders a JOIN customers b USING (id) JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)",
790+
expected_findings: &[],
791+
},
792+
QueryFindingsTestCase {
793+
description: "an ON condition reading a later comma group's item (2.0: table not found)",
794+
sql: "SELECT 1 FROM orders a JOIN customers c ON b.id = c.id, customers b",
795+
expected_findings: &[(
796+
"E222",
797+
"Unknown table or alias 'b' referenced by column 'id'",
798+
43,
799+
)],
800+
},
801+
QueryFindingsTestCase {
802+
description: "an ON condition reading a later item of its own join chain (2.0: table not found)",
803+
sql: "SELECT 1 FROM orders a JOIN customers b ON c.id = a.id JOIN customers c ON true",
804+
expected_findings: &[(
805+
"E222",
806+
"Unknown table or alias 'c' referenced by column 'id'",
807+
43,
808+
)],
809+
},
810+
QueryFindingsTestCase {
811+
description: "an ON condition reading an earlier comma group's item (2.0: accepted)",
812+
sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON a.id = c.id",
813+
expected_findings: &[],
814+
},
815+
QueryFindingsTestCase {
816+
description: "an ON condition reading an earlier comma group's column (2.0: accepted)",
817+
sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON customer_id = c.id",
818+
expected_findings: &[],
819+
},
820+
QueryFindingsTestCase {
821+
description: "a parenthesised join's ON condition reading an earlier comma item (2.0: accepted)",
822+
sql: "SELECT 1 FROM orders a, (customers b JOIN customers c ON a.id = c.id)",
823+
expected_findings: &[],
824+
},
773825
QueryFindingsTestCase {
774826
description: "an unqualified USING key beside a later item's column of its name, which DuckDB binds",
775827
sql: "SELECT id FROM orders a JOIN customers b USING (id), exports c CROSS JOIN customers d",

‎crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ use crate::lineage::_helpers::tracing::queries::{
2626
};
2727
use crate::lineage::_helpers::tracing::resolution::key;
2828
use crate::lineage::_helpers::tracing::reuse::{Reach, Reuse, reusing};
29-
use crate::lineage::_helpers::tracing::sources::{every_join, from_parts};
29+
use crate::lineage::_helpers::tracing::sources::{every_join, from_parts, sided_parts};
3030
use crate::lineage::constants::{COLUMNS_FUNCTION, OUTER_RIGHT_JOINS};
31-
use crate::lineage::models::Engine;
3231
use crate::lineage::models::NameFinding;
33-
use crate::lineage::models::{Lineage, LineageContext, LineageScope, NamedQuery};
32+
use crate::lineage::models::{Engine, JoinConditionReach};
33+
use crate::lineage::models::{Lineage, LineageContext, LineageScope, MergedKey, NamedQuery};
3434
use crate::lineage::types::DialectRules;
3535

3636
/// The findings of `query` and its inner queries; `aliases` are enclosing levels' output aliases.
@@ -205,6 +205,67 @@ fn left_scope<'a>(
205205
})
206206
}
207207

208+
/// Each join condition of `select`, with the FROM items `reach` lets it read.
209+
fn condition_scopes<'s, 'a>(
210+
select: &'s Select,
211+
scope: &LineageScope<'a>,
212+
reach: JoinConditionReach,
213+
) -> Vec<(&'s Expression, LineageScope<'a>)> {
214+
let at = |part: usize| {
215+
scope.starts.get(part).map_or(scope.sources.len(), |start| {
216+
(*start).min(scope.sources.len())
217+
})
218+
};
219+
let mut scopes: Vec<(&'s Expression, LineageScope<'a>)> = Vec::new();
220+
for part in sided_parts(select) {
221+
let Some(join) = part.join else {
222+
continue;
223+
};
224+
let first: usize = match reach {
225+
JoinConditionReach::OwnGroup => at(part.left.start),
226+
_ => 0,
227+
};
228+
let reached: std::ops::Range<usize> = first..at(part.right.end);
229+
for condition in join.on.iter().chain(join.match_condition.iter()) {
230+
scopes.push((condition, reached_scope(scope, reached.clone())));
231+
}
232+
}
233+
scopes
234+
}
235+
236+
/// `scope` narrowed to the sources in `reached`, with each merged key as it stood within them.
237+
fn reached_scope<'a>(
238+
scope: &LineageScope<'a>,
239+
reached: std::ops::Range<usize>,
240+
) -> LineageScope<'a> {
241+
LineageScope {
242+
sources: scope.sources[reached.clone()].to_vec(),
243+
merged: scope
244+
.merged
245+
.iter()
246+
.filter_map(|merged| {
247+
let range = reached_range(merged, &reached)?;
248+
Some(MergedKey {
249+
sources: range.start - reached.start..range.end - reached.start,
250+
earlier: Vec::new(),
251+
..merged.clone()
252+
})
253+
})
254+
.collect(),
255+
..empty_scope(scope.ctes, scope.outer)
256+
}
257+
}
258+
259+
/// The latest sources a merged key stood for wholly inside `reached`, if it was merged by then.
260+
fn reached_range<'k>(
261+
merged: &'k MergedKey,
262+
reached: &std::ops::Range<usize>,
263+
) -> Option<&'k std::ops::Range<usize>> {
264+
std::iter::once(&merged.sources)
265+
.chain(merged.earlier.iter().rev())
266+
.find(|range| reached.start <= range.start && range.end <= reached.end)
267+
}
268+
208269
/// The key of a derived table in FROM, by its alias; none when `item` is not a derived table.
209270
fn derived_key(item: &Expression, context: LineageContext<'_>) -> Option<Option<String>> {
210271
match item {
@@ -355,6 +416,10 @@ fn select_findings(
355416
}
356417
let mut read: Vec<&Expression> = clauses(select);
357418
read.extend(orders.iter().flat_map(|order| order_keys(order)));
419+
let conditions: Vec<(&Expression, LineageScope<'_>)> = match rules.join_conditions_read() {
420+
JoinConditionReach::Every => Vec::new(),
421+
reach => condition_scopes(select, &scope, reach),
422+
};
358423
for clause in read {
359424
let named_place: bool = ordered_name_place(select, &orders, clause)
360425
|| (rules.group_keys_read_outputs() && group_key_place(select, clause));
@@ -366,6 +431,13 @@ fn select_findings(
366431
},
367432
(false, false) => unnamed,
368433
};
434+
let read = match conditions.iter().find(|(on, _)| std::ptr::eq(*on, clause)) {
435+
Some((_, reached)) => Level {
436+
scope: reached,
437+
..read
438+
},
439+
None => read,
440+
};
369441
let (found, inner) = clause_findings(clause, &read, context);
370442
own.extend(found);
371443
nested.extend(inner);

‎crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ fn joined(
285285
expression: None,
286286
},
287287
sources: reach.clone(),
288+
earlier: Vec::new(),
288289
};
289290
match merged_keys.iter_mut().find(|merged| {
290291
overlaps(&merged.sources, &reach)
@@ -295,7 +296,13 @@ fn joined(
295296
Some(merged) => {
296297
let sources: Range<usize> =
297298
merged.sources.start.min(reach.start)..merged.sources.end.max(reach.end);
298-
*merged = MergedKey { sources, ..column };
299+
let mut earlier: Vec<Range<usize>> = std::mem::take(&mut merged.earlier);
300+
earlier.push(merged.sources.clone());
301+
*merged = MergedKey {
302+
sources,
303+
earlier,
304+
..column
305+
};
299306
}
300307
None => merged_keys.push(column),
301308
}

‎crates/sqlquack-semantics/src/lineage/models.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ pub(crate) struct MergedKey {
161161
pub(crate) column: ScopedColumn,
162162
/// The sources of the join, whose columns of the key's name it stands in for.
163163
pub(crate) sources: Range<usize>,
164+
/// The sources it stood for before later joins merged it again, oldest first.
165+
pub(crate) earlier: Vec<Range<usize>>,
164166
}
165167

166168
/// A FROM item: its key, its known columns, and whether it offers others of unknown name.
@@ -294,6 +296,10 @@ impl DialectRules for SharedRules {
294296
true
295297
}
296298

299+
fn join_conditions_read(&self) -> JoinConditionReach {
300+
JoinConditionReach::Every
301+
}
302+
297303
fn checks_call_signatures(&self) -> bool {
298304
true
299305
}
@@ -427,6 +433,17 @@ impl DialectRules for SharedRules {
427433
}
428434
}
429435

436+
/// Which FROM items a join's ON condition may read.
437+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
438+
pub(crate) enum JoinConditionReach {
439+
/// Every FROM item of its query level.
440+
Every,
441+
/// Every item before the join, and the join's own item.
442+
Earlier,
443+
/// The items of its own comma group before the join, and the join's own item.
444+
OwnGroup,
445+
}
446+
430447
/// The engine whose binder the checks follow.
431448
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
432449
pub(crate) enum Engine {

‎crates/sqlquack-semantics/src/lineage/types.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use sqlquack_syntax::{DataType, Expression};
88
use crate::dialects::types::DialectProfile;
99
use crate::lineage::_helpers::checks::window_only::named_window_only;
1010
use crate::lineage::main::rendered_type::rendered_type;
11-
use crate::lineage::models::{Engine, Nullability};
11+
use crate::lineage::models::{Engine, JoinConditionReach, Nullability};
1212
use crate::lineage::models::{
1313
Lineage, LineageContext, NullabilityRule, SchemaColumn, SourceColumn,
1414
};
@@ -81,6 +81,9 @@ pub(crate) trait DialectRules: Sync {
8181
/// Whether an unqualified USING or NATURAL key reads its merged column though an item outside its join offers the name.
8282
fn merged_keys_hide_other_items(&self) -> bool;
8383

84+
/// Which FROM items a join's ON condition may read.
85+
fn join_conditions_read(&self) -> JoinConditionReach;
86+
8487
/// Whether calls are checked against the signatures of the functions they name.
8588
fn checks_call_signatures(&self) -> bool;
8689

‎crates/sqlquack-semantics/src/postgres/profile/models.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::dialects::types::DialectProfile;
88
use crate::lineage::constants::{
99
DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS,
1010
};
11-
use crate::lineage::models::Engine;
11+
use crate::lineage::models::{Engine, JoinConditionReach};
1212
use crate::lineage::models::{Lineage, LineageContext};
1313
use crate::lineage::types::DialectRules;
1414
use crate::lineage::types::ValueType;
@@ -77,6 +77,10 @@ impl DialectRules for PostgresRules {
7777
false
7878
}
7979

80+
fn join_conditions_read(&self) -> JoinConditionReach {
81+
JoinConditionReach::OwnGroup
82+
}
83+
8084
fn checks_call_signatures(&self) -> bool {
8185
true
8286
}

0 commit comments

Comments
 (0)