From 282d582ac4d9727c504f46ddb19ffea4bb9afa6a Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:06:22 +0000 Subject: [PATCH 1/5] fix!: keep each comma item's joins with it in Postgres, Databricks, DuckDB and SQL Server These dialects bind a comma looser than JOIN, so in `a JOIN b USING (k), c` the join reads only a and b. DuckDB and SQL Server kept the first item's joins in the select's joins while later items were collapsed, and semantics lists FROM items before those joins, so c counted on b's left side: a USING key c offered hid the missing-key E201, and a bare star listed a, c, b. Postgres and Databricks flattened every item's joins into the select's joins, with the same effect for every item. In a FROM list of several items, each item now keeps its own joins as one joined table, in all four parsers; a lone item keeps the select's joins as before. SQL Server's query, UPDATE and DELETE share one reader. BigQuery, SQLite, ClickHouse and Snowflake trees are unchanged. This is breaking: the tree JSON of a FROM list with joins after a comma changes. Semantics now records the sources each USING or NATURAL key covers, so a join's keys, its right-outer side and a bare star's placement of a key reach only the parts on its left in its own group. Reading an unqualified name a merged key covers is unchanged. DuckDB, Databricks and PostgreSQL cases cover a USING key only a later comma item offers, and DuckDB and SQL Server cases the star order. A DuckDB case keeps an unqualified USING key read beside a later item's column of its name, which DuckDB binds. --- .../databricks/profile/tests/test_lineage.rs | 28 ++++ .../profile/tests/test_statement_findings.rs | 92 +++++++++++- .../src/lineage/_helpers/findings/checks.rs | 4 +- .../src/lineage/_helpers/tracing/queries.rs | 133 +++++++++++------- .../lineage/_helpers/tracing/resolution.rs | 4 +- .../src/lineage/_helpers/tracing/sources.rs | 83 +++++++---- .../sqlquack-semantics/src/lineage/models.rs | 12 +- .../src/lineage/tests/test_types.rs | 7 + .../profile/tests/test_statement_findings.rs | 48 +++++++ .../sqlserver/profile/tests/test_lineage.rs | 32 +++++ .../src/parse/_helpers/commands/from.rs | 24 ++-- .../src/parse/_helpers/commands/from.rs | 18 ++- .../src/parse/_helpers/commands/from.rs | 23 ++- .../src/parse/_helpers/statements/dml.rs | 16 +-- .../src/parse/_helpers/statements/queries.rs | 44 +++--- 15 files changed, 428 insertions(+), 140 deletions(-) diff --git a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs index cba7b298b..24a456226 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -722,3 +722,31 @@ 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 + ); + } +} diff --git a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs index 1867ddff0..06f793283 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs @@ -6,11 +6,12 @@ use sqlquack_syntax::{CallMode, Dialect, ParseOptions}; use crate::dialects::constants::DUCKDB; use crate::duckdb::profile::tests::helpers::duckdb_parser_findings; use crate::lineage::_helpers::findings::statement::statement_findings; +use crate::lineage::main::statement_lineage::statement_lineage; use crate::lineage::models::LineageContext; use crate::lineage::tests::helpers::{ duckdb_shop_schema, found_findings, owned_findings, shop_schema, }; -use crate::lineage::tests::test_types::QueryFindingsTestCase; +use crate::lineage::tests::test_types::{QueryFindingsTestCase, StarOrderTestCase}; #[test] fn given_a_duckdb_name_not_definitely_unresolved_when_resolving_names_then_nothing_is_found() { @@ -697,3 +698,92 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_ ); } } + +#[test] +fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_only_its_own_items() { + let test_cases = [ + QueryFindingsTestCase { + description: "a USING key the left of its join lacks, a later comma item 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 item", + sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an unqualified USING key beside a later item's column of its name, which DuckDB binds", + sql: "SELECT id FROM orders a JOIN customers b USING (id), exports c CROSS JOIN customers d", + expected_findings: &[], + }, + ]; + for test_case in test_cases { + assert_eq!( + duckdb_parser_findings(test_case.sql), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} + +#[test] +fn given_a_comma_after_a_joined_item_when_expanding_a_star_then_the_columns_follow_the_written_order() + { + let test_cases = [ + StarOrderTestCase { + description: "the joined item's columns come before the comma item's", + sql: "SELECT * FROM orders a JOIN customers b ON a.id = b.id, exports c", + expected_names: &[ + "id", + "customer_id", + "amount", + "items", + "status", + "id", + "name", + "country", + "listingId", + "listing_status", + ], + }, + StarOrderTestCase { + description: "a USING key once, first in its own join, before the comma item", + sql: "SELECT * FROM orders a JOIN customers b USING (id), exports c", + expected_names: &[ + "id", + "customer_id", + "amount", + "items", + "status", + "name", + "country", + "listingId", + "listing_status", + ], + }, + ]; + let schema = duckdb_shop_schema(); + for test_case in test_cases { + let statement = sqlquack_syntax_duckdb::parse_strict(test_case.sql) + .unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description)) + .statements + .remove(0); + let context = LineageContext { + profile: &DUCKDB, + schema: &schema, + sql: Some(test_case.sql), + }; + let names: Vec = statement_lineage(&statement, context) + .outputs + .into_iter() + .map(|output| output.name) + .collect(); + assert_eq!(names, test_case.expected_names, "{}", test_case.description); + } +} diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs index 68aa6d30b..29ce9a360 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs @@ -105,7 +105,7 @@ pub(crate) fn column_finding( if current .merged .iter() - .any(|merged| context.profile.same_identifier(&merged.key, &wanted)) + .any(|merged| context.profile.same_identifier(&merged.column.key, &wanted)) { return None; } @@ -173,7 +173,7 @@ pub(crate) fn value_keyword( || level .merged .iter() - .any(|merged| context.profile.same_identifier(&merged.key, &wanted)) + .any(|merged| context.profile.same_identifier(&merged.column.key, &wanted)) { return false; } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs index 6b4a857e1..d16cb474d 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs @@ -29,8 +29,8 @@ use crate::lineage::constants::{ ORDINAL_BASE, RECURSION_LIMIT, SQL_STAR, SUBTOTAL_GROUPINGS, UNNAMED_CHARACTERS, }; use crate::lineage::models::{ - FromItems, Lineage, LineageContext, LineageScope, NamedQuery, Nullability, ResolvedQuery, - RowReads, ScopeSource, ScopedColumn, TransformKind, + FromItems, Lineage, LineageContext, LineageScope, MergedKey, NamedQuery, Nullability, + ResolvedQuery, RowReads, ScopeSource, ScopedColumn, TransformKind, }; /// The outputs of `query`, with `ctes` in reach and `outer` the enclosing level. @@ -860,7 +860,7 @@ pub(crate) fn after_windows( named_windows(windows, scope, context) } -/// The columns a star selects (a bare star's merged join keys once, first), EXCLUDE etc. applied. +/// The columns a star selects (a bare star's merged join keys once each), EXCLUDE etc. applied. pub(crate) fn expanded_star( star: &Star, scope: &LineageScope<'_>, @@ -882,10 +882,11 @@ pub(crate) fn expanded_star( owner = outer; } let scope: &LineageScope<'_> = owner; - let selected: Vec<&ScopeSource> = scope + let selected: Vec<(usize, &ScopeSource)> = scope .sources .iter() - .filter(|source| match (&qualifier, &source.key) { + .enumerate() + .filter(|(_, source)| match (&qualifier, &source.key) { (None, _) => true, (Some(qualifier), Some(key)) => context.profile.same_identifier(key, qualifier), (Some(_), None) => false, @@ -897,31 +898,19 @@ pub(crate) fn expanded_star( .flatten() .map(|name| key(name, context)) .collect(); - let keys: Vec<&ScopedColumn> = match qualifier { + let keys: Vec<&MergedKey> = match qualifier { None => scope.merged.iter().collect(), Some(_) => Vec::new(), }; - let merged_keys: Vec = keys.iter().map(|column| column.key.clone()).collect(); let open: Vec<&ScopeSource> = selected .iter() - .copied() + .map(|(_, source)| *source) .filter(|source| source.query.open) .collect(); let mut columns: Vec = Vec::new(); - let in_place: bool = dialect_rules(context.profile).star_keys_in_place(); - let offered: Vec = match in_place { + let offered: Vec = match dialect_rules(context.profile).star_keys_in_place() { true => keys_in_place(&keys, &selected, context), - false => keys - .into_iter() - .cloned() - .chain( - selected - .iter() - .flat_map(|source| source.query.columns.iter()) - .filter(|column| !listed(&merged_keys, &column.key, context)) - .cloned(), - ) - .collect(), + false => keys_first(&keys, &selected, context), }; for column in &offered { if !listed(&excluded, &column.key, context) { @@ -948,51 +937,89 @@ pub(crate) fn expanded_star( } } -/// DuckDB's and SQLite's star: each join key once, where and as its first source offers it. +/// The columns a bare star offers with each merged key first in its join, hiding its sources' own. +fn keys_first( + keys: &[&MergedKey], + selected: &[(usize, &ScopeSource)], + context: LineageContext<'_>, +) -> Vec { + let mut columns: Vec = Vec::new(); + for (index, source) in selected { + columns.extend( + keys.iter() + .filter(|key| key.sources.start == *index) + .map(|key| key.column.clone()), + ); + columns.extend( + source + .query + .columns + .iter() + .filter(|column| covering(keys, *index, column, context).is_none()) + .cloned(), + ); + } + columns +} + +/// The columns a bare star offers with each merged key where its join first offers it. fn keys_in_place( - keys: &[&ScopedColumn], - selected: &[&ScopeSource], + keys: &[&MergedKey], + selected: &[(usize, &ScopeSource)], context: LineageContext<'_>, ) -> Vec { - let offered: Vec<&ScopedColumn> = selected - .iter() - .flat_map(|source| source.query.columns.iter()) - .collect(); let mut columns: Vec = Vec::new(); - for merged in keys { - if same_key(&offered, merged, context).is_none() { - columns.push((*merged).clone()); + let mut placed: Vec<&MergedKey> = Vec::new(); + for (index, source) in selected { + for key in keys.iter().filter(|key| key.sources.start == *index) { + let offered: bool = selected + .iter() + .any(|(at, source)| key.sources.contains(at) && offers_key(source, key, context)); + if !offered { + columns.push(key.column.clone()); + } } - } - let mut placed: Vec<&ScopedColumn> = Vec::new(); - for column in offered { - match same_key(keys, column, context) { - Some(merged) if same_key(&placed, merged, context).is_some() => {} - Some(merged) => { - placed.push(merged); - columns.push(ScopedColumn { - name: column.name.clone(), - key: column.key.clone(), - lineage: merged.lineage.clone(), - expression: None, - }); + for column in source.query.columns.iter() { + match covering(keys, *index, column, context) { + Some(key) if placed.iter().any(|done| std::ptr::eq(*done, key)) => {} + Some(key) => { + placed.push(key); + columns.push(ScopedColumn { + name: column.name.clone(), + key: column.key.clone(), + lineage: key.column.lineage.clone(), + expression: None, + }); + } + None => columns.push(column.clone()), } - None => columns.push(column.clone()), } } columns } -/// The first of `columns` naming the same key as `column`. -fn same_key<'a>( - columns: &[&'a ScopedColumn], +/// Whether `source` offers a column named as merged `key`. +fn offers_key(source: &ScopeSource, key: &MergedKey, context: LineageContext<'_>) -> bool { + source.query.columns.iter().any(|column| { + context + .profile + .same_identifier(&column.key, &key.column.key) + }) +} + +/// The merged key that stands in for `column` of the source at `index`, if any. +fn covering<'k>( + keys: &[&'k MergedKey], + index: usize, column: &ScopedColumn, context: LineageContext<'_>, -) -> Option<&'a ScopedColumn> { - columns - .iter() - .find(|candidate| context.profile.same_identifier(&candidate.key, &column.key)) - .copied() +) -> Option<&'k MergedKey> { + keys.iter().copied().find(|key| { + key.sources.contains(&index) + && context + .profile + .same_identifier(&key.column.key, &column.key) + }) } fn star_column( diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/resolution.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/resolution.rs index 3cae3224e..260765a98 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/resolution.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/resolution.rs @@ -116,8 +116,8 @@ fn unqualified( if scope.lambda_names.iter().any(|name| same(name)) { return Some(constant()); } - if let Some(merged) = scope.merged.iter().find(|merged| same(&merged.key)) { - return Some(merged.lineage.clone()); + if let Some(merged) = scope.merged.iter().find(|merged| same(&merged.column.key)) { + return Some(merged.column.lineage.clone()); } let known = scope .sources diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs index a98021351..f5a15724e 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs @@ -1,5 +1,6 @@ //! The FROM items of a query level, in order, each with the columns it offers. +use std::ops::Range; use std::rc::Rc; use sqlquack_syntax::Expression; @@ -24,11 +25,11 @@ use crate::lineage::constants::{ STRUCT_FUNCTION, TABLE_FUNCTION_COLUMNS, TABLE_WRAPPER, UNNEST_FUNCTION, }; use crate::lineage::models::{ - FromItems, FromPart, Lineage, LineageContext, LineageScope, NamedQuery, Nullability, + FromItems, FromPart, Lineage, LineageContext, LineageScope, MergedKey, NamedQuery, Nullability, ResolvedQuery, RowReads, SchemaColumn, ScopeSource, ScopedColumn, TransformKind, }; -/// The FROM items of `select` and its joins; a later item may read earlier ones (LATERAL). +/// The FROM items of `select` and its joins; a join's keys and outer sides reach its own group. pub(crate) fn from_sources( select: &Select, ctes: &[NamedQuery], @@ -36,11 +37,17 @@ pub(crate) fn from_sources( context: LineageContext<'_>, ) -> FromItems { let mut from = FromItems::default(); - for (item, join) in from_parts(select) { + for part in sided_parts(select) { from.starts.push(from.sources.len()); - if join.is_some_and(|join| OUTER_RIGHT_JOINS.contains(&join.kind)) { + let left: Range = from.starts[part.left.start]..from.sources.len(); + if part + .join + .is_some_and(|join| OUTER_RIGHT_JOINS.contains(&join.kind)) + { for key in &mut from.merged { - key.lineage.nullability = Nullability::Nullable; + if overlaps(&key.sources, &left) { + key.column.lineage.nullability = Nullability::Nullable; + } } } let mut scope = LineageScope { @@ -48,9 +55,9 @@ pub(crate) fn from_sources( merged: std::mem::take(&mut from.merged), ..empty_scope(ctes, outer) }; - let mut added: Vec = item_sources(item, &scope, context); + let mut added: Vec = item_sources(part.item, &scope, context); let earlier: usize = scope.sources.len(); - let Some(join) = join else { + let Some(join) = part.join else { from.sources = scope.sources; from.merged = scope.merged; from.sources.extend(added); @@ -58,13 +65,21 @@ pub(crate) fn from_sources( }; scope.sources.append(&mut added); scope.traced.get_mut().clear(); - let (merged, rows) = joined(join, (&scope, earlier), context); + let (merged, rows) = joined(join, (&scope, left.clone()), context); added = scope.sources.split_off(earlier); from.sources = scope.sources; from.merged = merged; from.rows.absorb(&rows); if OUTER_RIGHT_JOINS.contains(&join.kind) { - from.sources = from.sources.into_iter().map(outer_nullable).collect(); + from.sources = from + .sources + .into_iter() + .enumerate() + .map(|(index, source)| match left.contains(&index) { + true => outer_nullable(source), + false => source, + }) + .collect(); } if OUTER_LEFT_JOINS.contains(&join.kind) || outer_as_of(join.kind, context) { added = added.into_iter().map(outer_nullable).collect(); @@ -211,25 +226,38 @@ fn holds_join(item: &Expression) -> bool { } } -/// The merged keys after a join, and the columns its conditions and keys decide rows by. +/// Whether two ranges of sources share one. +pub(crate) fn overlaps(first: &Range, second: &Range) -> bool { + first.start < second.end && second.start < first.end +} + +/// The merged keys after a join with `left` its left sources, and the columns it decides rows by. fn joined( join: &Join, - (both, earlier): (&LineageScope<'_>, usize), + (both, left): (&LineageScope<'_>, Range), context: LineageContext<'_>, -) -> (Vec, RowReads) { - let (left, right): (&[ScopeSource], &[ScopeSource]) = both.sources.split_at(earlier); - let mut merged_keys: Vec = both.merged.clone(); +) -> (Vec, RowReads) { + let right: &[ScopeSource] = &both.sources[left.end..]; + let reach: Range = left.start..both.sources.len(); + let left_keys: Vec = both + .merged + .iter() + .filter(|merged| overlaps(&merged.sources, &left)) + .map(|merged| merged.column.clone()) + .collect(); + let left: &[ScopeSource] = &both.sources[left]; + let mut merged_keys: Vec = both.merged.clone(); let mut rows: RowReads = RowReads::default(); for condition in join.on.iter().chain(join.match_condition.iter()) { let read: Lineage = expression_lineage(condition, both, context); rows.read(&read); } let names: Vec = match natural(join.kind) { - true => common_names((&both.merged, left), right, context), + true => common_names((&left_keys, left), right, context), false => join.using.iter().map(|name| key(name, context)).collect(), }; for name in names { - let left_side: Lineage = side_column(&both.merged, left, &name, context); + let left_side: Lineage = side_column(&left_keys, left, &name, context); let right_side: Lineage = side_column(&[], right, &name, context); rows.read(&left_side); rows.read(&right_side); @@ -249,16 +277,21 @@ fn joined( } _ => left_side, }; - let column = ScopedColumn { - name: name.clone(), - key: name, - lineage, - expression: None, + let column = MergedKey { + column: ScopedColumn { + name: name.clone(), + key: name, + lineage, + expression: None, + }, + sources: reach.clone(), }; - match merged_keys - .iter_mut() - .find(|merged| context.profile.same_identifier(&merged.key, &column.key)) - { + match merged_keys.iter_mut().find(|merged| { + overlaps(&merged.sources, &reach) + && context + .profile + .same_identifier(&merged.column.key, &column.column.key) + }) { Some(merged) => *merged = column, None => merged_keys.push(column), } diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index b9dc89b9b..702825e77 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -155,6 +155,14 @@ pub(crate) struct ScopedColumn { pub(crate) expression: Option, } +/// A key a USING or NATURAL join merged, and the sources that join reads. +#[derive(Clone, Debug)] +pub(crate) struct MergedKey { + pub(crate) column: ScopedColumn, + /// The sources of the join, whose columns of the key's name it stands in for. + pub(crate) sources: Range, +} + /// A FROM item: its key, its known columns, and whether it offers others of unknown name. #[derive(Clone, Debug)] pub(crate) struct ScopeSource { @@ -179,7 +187,7 @@ pub(crate) struct LineageScope<'a> { pub(crate) aliases: Vec, pub(crate) lambda_names: Vec, /// The keys USING and NATURAL joins merged, read unqualified and once by a bare star. - pub(crate) merged: Vec, + pub(crate) merged: Vec, /// The named windows of the level, with what their partitions and orders read. pub(crate) windows: Vec<(String, Lineage)>, /// Columns the level's WHERE requires to be NOT NULL, never NULL where the level reads them. @@ -223,7 +231,7 @@ pub(crate) struct ResolvedQuery { #[derive(Clone, Debug, Default)] pub(crate) struct FromItems { pub(crate) sources: Vec, - pub(crate) merged: Vec, + pub(crate) merged: Vec, pub(crate) rows: RowReads, /// How many sources come before each FROM part, in `from_parts` order, then the total. pub(crate) starts: Vec, diff --git a/crates/sqlquack-semantics/src/lineage/tests/test_types.rs b/crates/sqlquack-semantics/src/lineage/tests/test_types.rs index d968b58c8..ace227680 100644 --- a/crates/sqlquack-semantics/src/lineage/tests/test_types.rs +++ b/crates/sqlquack-semantics/src/lineage/tests/test_types.rs @@ -137,3 +137,10 @@ pub(crate) struct QueryFindingsTestCase { pub(crate) sql: &'static str, pub(crate) expected_findings: &'static [(&'static str, &'static str, usize)], } + +/// A query whose bare star's output names are known, in order. +pub(crate) struct StarOrderTestCase { + pub(crate) description: &'static str, + pub(crate) sql: &'static str, + pub(crate) expected_names: &'static [&'static str], +} diff --git a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs index ffa2b82d3..c819ac4bd 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs @@ -325,3 +325,51 @@ fn given_a_named_window_when_resolving_names_then_its_keys_read_the_input_column ); } } + +#[test] +fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_only_its_own_items() { + let test_cases = [ + QueryFindingsTestCase { + description: "a USING key the left of its join lacks, a later comma item 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 item", + sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c", + expected_findings: &[], + }, + ]; + let schema = duckdb_shop_schema(); + for test_case in test_cases { + let statement = sqlquack_syntax_postgres::parse_strict(test_case.sql) + .unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description)) + .statements + .remove(0); + let context = LineageContext { + profile: &POSTGRES, + schema: &schema, + sql: Some(test_case.sql), + }; + let native: Vec<(&'static str, String, usize)> = statement_findings(&statement, context) + .into_iter() + .map(|finding| { + ( + finding.code, + finding.message, + finding.span.map_or(0, |span| span.start), + ) + }) + .collect(); + assert_eq!( + native, + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-semantics/src/sqlserver/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/sqlserver/profile/tests/test_lineage.rs index bdeaa39f8..33e0ee04e 100644 --- a/crates/sqlquack-semantics/src/sqlserver/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/sqlserver/profile/tests/test_lineage.rs @@ -3,6 +3,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; use crate::sqlserver::profile::tests::helpers::sqlserver_findings; use crate::sqlserver::profile::tests::helpers::sqlserver_outputs; @@ -290,3 +291,34 @@ fn given_a_strict_parse_when_tracing_lineage_then_each_output_has_its_items_byte ); } } + +#[test] +fn given_a_comma_after_a_joined_source_when_expanding_a_star_then_the_columns_follow_the_written_order() + { + let test_cases = [StarOrderTestCase { + description: "the joined source's columns come before the comma source's", + sql: "SELECT * FROM customers a JOIN customers b ON a.id = b.id, orders c", + expected_names: &[ + "id", + "name", + "id", + "name", + "id", + "big", + "amount", + "price", + "status", + "created_at", + "logged_at", + "label", + ], + }]; + let schema = sqlserver_shop_schema(); + for test_case in test_cases { + let names: Vec = sqlserver_outputs(&schema, test_case.sql) + .into_iter() + .map(|(name, _)| name) + .collect(); + assert_eq!(names, test_case.expected_names, "{}", test_case.description); + } +} diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs index dc44dca49..94fef9b3d 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs @@ -67,21 +67,29 @@ fn percent_outside_range(text: &str, negated: bool) -> Option { } impl Parser<'_> { - /// `fromClause`: `FROM relation, ... lateralView*`, into `select`'s sources and joins. + /// `fromClause`: `FROM relation, ... lateralView*`; several relations each keep their joins. pub(crate) fn select_from_clause(&mut self, mut select: Select) -> Parsing { self.expect_kw("FROM")?; let mut refs: Vec<(Expression, Vec)> = self.comma_list(|parser| parser.table_ref())?; - let (first, joins) = refs.remove(0); - let mut items: Vec = vec![first]; - items.extend(refs.into_iter().map(|(item, more)| collapse(item, more))); + if refs.len() == 1 { + let (item, joins) = refs.remove(0); + select.from = Some(From { + expressions: vec![item], + }); + select.joins = joins; + return Ok(select); + } + let items: Vec = refs + .into_iter() + .map(|(item, joins)| collapse(item, joins)) + .collect(); select.from = Some(From { expressions: items }); - select.joins = joins; Ok(select) } diff --git a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/from.rs b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/from.rs index 776731e03..429a72ed1 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/from.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/from.rs @@ -47,17 +47,28 @@ fn joined_alias(left: Expression, joins: Vec, alias: Option) - } impl Parser<'_> { - /// `from_list`: the comma-separated items, and every join, in order. + /// `from_list`: the items, and a lone item's joins; several items each keep their own. pub(crate) fn parse_from_list(&mut self) -> Parsing<(Vec, Vec)> { - let mut items: Vec = Vec::new(); - let mut joins: Vec = Vec::new(); + let mut refs: Vec<(Expression, Vec)> = Vec::new(); loop { - items.push(self.table_ref()?); - joins.extend(self.joins()?); + let item: Expression = self.table_ref()?; + refs.push((item, self.joins()?)); if !self.eat_char(',') { - return Ok((items, joins)); + break; } } + if refs.len() == 1 { + let (item, joins) = refs.remove(0); + return Ok((vec![item], joins)); + } + let items: Vec = refs + .into_iter() + .map(|(item, joins)| match joins.is_empty() { + true => item, + false => joined_table(item, joins, None), + }) + .collect(); + Ok((items, Vec::new())) } /// Whether a join begins at the cursor. diff --git a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/dml.rs b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/dml.rs index 0dc3cc09c..02dcf2fe3 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/dml.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/dml.rs @@ -2,7 +2,7 @@ use sqlquack_syntax::Expression; use sqlquack_syntax::expressions::names::models::{BooleanLiteral, Identifier, TableRef}; -use sqlquack_syntax::expressions::queries::models::{From, Join, Where, With}; +use sqlquack_syntax::expressions::queries::models::{Where, With}; use sqlquack_syntax::expressions::statements::models::{Merge, OutputClause, When, Whens}; use sqlquack_syntax::expressions::types::models::Tuple; @@ -313,18 +313,4 @@ impl<'sql> Parser<'sql> { schema_evolution: false, }))) } - - /// The `FROM` sources of `UPDATE` and `DELETE`. - fn read_sources(&mut self) -> Parsing<(From, Vec)> { - let (first, joins) = self.single_source()?; - let mut expressions: Vec = vec![first]; - while self.eat_char(',') { - let (next, more) = self.single_source()?; - expressions.push(match more.is_empty() { - true => next, - false => crate::parse::_helpers::tree::nodes::joined_table(next, more, None), - }); - } - Ok((From { expressions }, joins)) - } } diff --git a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/queries.rs b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/queries.rs index a8f3d5676..eb37a4d5e 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/queries.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/queries.rs @@ -268,27 +268,8 @@ impl<'sql> Parser<'sql> { }); } if self.eat_kw("FROM") { - let mut sources: Vec = Vec::new(); - let mut joins: Vec = Vec::new(); - loop { - let source: Expression = self.table_source()?; - if sources.is_empty() { - sources.push(source); - joins = self.joins()?; - } else { - let more: Vec = self.joins()?; - sources.push(match more.is_empty() { - true => source, - false => joined_table(source, more, None), - }); - } - if !self.eat_char(',') { - break; - } - } - select.from = Some(From { - expressions: sources, - }); + let (from, joins) = self.read_sources()?; + select.from = Some(from); select.joins = joins; } if self.eat_kw("WHERE") { @@ -930,6 +911,27 @@ impl<'sql> Parser<'sql> { }) } + /// The `FROM` sources of a query, `UPDATE` or `DELETE`; several sources each keep their joins. + pub(crate) fn read_sources(&mut self) -> Parsing<(From, Vec)> { + let mut sources: Vec<(Expression, Vec)> = vec![self.single_source()?]; + while self.eat_char(',') { + sources.push(self.single_source()?); + } + if sources.len() == 1 { + let (source, joins) = sources.remove(0); + let expressions: Vec = vec![source]; + return Ok((From { expressions }, joins)); + } + let expressions: Vec = sources + .into_iter() + .map(|(source, joins)| match joins.is_empty() { + true => source, + false => joined_table(source, joins, None), + }) + .collect(); + Ok((From { expressions }, Vec::new())) + } + /// One table source and the joins after it. pub(crate) fn single_source(&mut self) -> Parsing<(Expression, Vec)> { let source: Expression = self.table_source()?; From 59c8af57d434f4266d284cb6447ac39085ed4290 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:06:23 +0000 Subject: [PATCH 2/5] fix(lint): read cross joins and join keys inside FROM items The lint rules read only a select's own joins, so a join kept inside a FROM item, in parentheses or after a comma, went unread: a cross join there was reported against an unnamed relation, or never proved one row, and its USING, NATURAL and ON conditions were missed by the CTE column checks. The cross-join, CTE-column and key checks now walk every join chain of a select, each with the relation it starts from, and the relations of an unaliased joined table count as the select's own sources. Cross-join cases cover a cross join after a comma in DuckDB and PostgreSQL, and a one-row seed on the left of its own comma group. --- .../src/lint/_helpers/join_predicates.rs | 17 ++++---- .../src/lint/tests/test_cross_joins.rs | 21 ++++++++++ .../src/quality/_helpers/cte_columns.rs | 17 ++++++-- .../src/quality/_helpers/keys.rs | 3 ++ .../src/quality/_helpers/syntax.rs | 22 +++++++++- .../src/tree/main/join_chains.rs | 41 +++++++++++++++++++ crates/sqlquack-lint/src/tree/main/mod.rs | 1 + 7 files changed, 111 insertions(+), 11 deletions(-) create mode 100644 crates/sqlquack-lint/src/tree/main/join_chains.rs diff --git a/crates/sqlquack-lint/src/lint/_helpers/join_predicates.rs b/crates/sqlquack-lint/src/lint/_helpers/join_predicates.rs index 8ebe08ee7..eb633a36f 100644 --- a/crates/sqlquack-lint/src/lint/_helpers/join_predicates.rs +++ b/crates/sqlquack-lint/src/lint/_helpers/join_predicates.rs @@ -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; @@ -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"; @@ -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), diff --git a/crates/sqlquack-lint/src/lint/tests/test_cross_joins.rs b/crates/sqlquack-lint/src/lint/tests/test_cross_joins.rs index daeca8479..d4038f2b7 100644 --- a/crates/sqlquack-lint/src/lint/tests/test_cross_joins.rs +++ b/crates/sqlquack-lint/src/lint/tests/test_cross_joins.rs @@ -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), \ diff --git a/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs b/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs index 1f96417a8..bb8f41371 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs @@ -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; @@ -220,7 +222,11 @@ fn reader_usage(target: &Target<'_>, reader: Option, 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 @@ -231,7 +237,7 @@ fn reader_usage(target: &Target<'_>, reader: Option, 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))); @@ -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 } diff --git a/crates/sqlquack-lint/src/quality/_helpers/keys.rs b/crates/sqlquack-lint/src/quality/_helpers/keys.rs index 74781f4df..70a74c25d 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/keys.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/keys.rs @@ -65,6 +65,9 @@ impl KeyEnvironment { return Vec::new(); } let found: Vec<(String, Option, &Expression)> = sources(select); + if found.len() != select.joins.len() + 1 { + return Vec::new(); + } let Some((first_alias, _, first)) = found.first() else { return Vec::new(); }; diff --git a/crates/sqlquack-lint/src/quality/_helpers/syntax.rs b/crates/sqlquack-lint/src/quality/_helpers/syntax.rs index dc4e3b0d9..ac39f7cdf 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/syntax.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/syntax.rs @@ -109,7 +109,10 @@ pub(super) fn qualifier(column: &Column) -> Option { pub(super) fn sources(select: &Select) -> Vec<(String, Option, &Expression)> { let mut found: Vec<(String, Option, &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); @@ -132,6 +135,23 @@ pub(super) fn sources(select: &Select) -> Vec<(String, Option, &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 { let significant: Vec = (0..tokens.len()) diff --git a/crates/sqlquack-lint/src/tree/main/join_chains.rs b/crates/sqlquack-lint/src/tree/main/join_chains.rs new file mode 100644 index 000000000..2d2d966e6 --- /dev/null +++ b/crates/sqlquack-lint/src/tree/main/join_chains.rs @@ -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(), + } +} diff --git a/crates/sqlquack-lint/src/tree/main/mod.rs b/crates/sqlquack-lint/src/tree/main/mod.rs index b267d4fc1..db39d96a7 100644 --- a/crates/sqlquack-lint/src/tree/main/mod.rs +++ b/crates/sqlquack-lint/src/tree/main/mod.rs @@ -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; From bd3520330fd4c978b762d1f0d1696b23391051ca Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:08:59 +0000 Subject: [PATCH 3/5] fix: list a nested USING or NATURAL key once in a star When a USING or NATURAL key merged again a key an inner join had merged, the new key took the outer join's range of sources only, so a bare star still placed the inner key's own sources' column: SELECT * FROM a JOIN (b JOIN c USING (k)) USING (k) listed k twice, as did its RIGHT JOIN form, a NATURAL JOIN (b NATURAL JOIN c), and a JOIN b JOIN c USING (k) USING (k). The merged key now covers the union of both ranges. DuckDB cases cover the nested USING, RIGHT and NATURAL forms, and the same key merged in two comma groups. --- .../profile/tests/test_statement_findings.rs | 38 ++++++++++++++++++- .../src/lineage/_helpers/tracing/sources.rs | 6 ++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs index 06f793283..2d8651296 100644 --- a/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/duckdb/profile/tests/test_statement_findings.rs @@ -733,8 +733,7 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on } #[test] -fn given_a_comma_after_a_joined_item_when_expanding_a_star_then_the_columns_follow_the_written_order() - { +fn given_joined_and_comma_items_when_expanding_a_star_then_the_columns_follow_the_written_order() { let test_cases = [ StarOrderTestCase { description: "the joined item's columns come before the comma item's", @@ -752,6 +751,41 @@ fn given_a_comma_after_a_joined_item_when_expanding_a_star_then_the_columns_foll "listing_status", ], }, + StarOrderTestCase { + description: "a nested USING key once, though the inner join merges it again", + sql: "SELECT * FROM customers a JOIN (customers b JOIN customers c USING (id)) USING (id)", + expected_names: &[ + "id", "name", "country", "name", "country", "name", "country", + ], + }, + StarOrderTestCase { + description: "a nested USING key once under a RIGHT JOIN", + sql: "SELECT * FROM customers a RIGHT JOIN (customers b JOIN customers c USING (id)) USING (id)", + expected_names: &[ + "id", "name", "country", "name", "country", "name", "country", + ], + }, + StarOrderTestCase { + description: "a nested NATURAL key once", + sql: "SELECT * FROM orders a NATURAL JOIN (customers b NATURAL JOIN customers c)", + expected_names: &[ + "id", + "customer_id", + "amount", + "items", + "status", + "name", + "country", + ], + }, + StarOrderTestCase { + description: "the same USING key in two comma groups, once in each", + sql: "SELECT * FROM customers a JOIN customers b USING (id), customers c JOIN customers d USING (id)", + expected_names: &[ + "id", "name", "country", "name", "country", "id", "name", "country", "name", + "country", + ], + }, StarOrderTestCase { description: "a USING key once, first in its own join, before the comma item", sql: "SELECT * FROM orders a JOIN customers b USING (id), exports c", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs index f5a15724e..9e9eee1e3 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs @@ -292,7 +292,11 @@ fn joined( .profile .same_identifier(&merged.column.key, &column.column.key) }) { - Some(merged) => *merged = column, + Some(merged) => { + let sources: Range = + merged.sources.start.min(reach.start)..merged.sources.end.max(reach.end); + *merged = MergedKey { sources, ..column }; + } None => merged_keys.push(column), } } From 4b5010222667b928486f83b3e3a4f1fd2635d216 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:09:00 +0000 Subject: [PATCH 4/5] test: cover comma groups' key order, outer joins and comma precedence PostgreSQL and Databricks cases place a USING key first in its own comma group, after the earlier item, and list a key merged twice by a JOIN b JOIN c USING (k) USING (k) once. A PostgreSQL case keeps a RIGHT JOIN after a comma from nulling the item before the comma, and the PostgreSQL shape table records that a join stays with the item after its comma. --- .../databricks/profile/tests/test_lineage.rs | 17 ++++ .../src/postgres/profile/tests/helpers.rs | 8 ++ .../profile/tests/test_query_lineage.rs | 89 ++++++++++++++++++- .../src/parse/tests/test_parse.rs | 6 ++ 4 files changed, 119 insertions(+), 1 deletion(-) diff --git a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs index 24a456226..41195f270 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -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] @@ -750,3 +751,19 @@ fn given_a_comma_after_a_joined_relation_when_checked_then_the_join_reads_only_i ); } } + +#[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 = databricks_outputs(test_case.sql) + .into_iter() + .map(|(name, _)| name) + .collect(); + assert_eq!(names, test_case.expected_names, "{}", test_case.description); + } +} diff --git a/crates/sqlquack-semantics/src/postgres/profile/tests/helpers.rs b/crates/sqlquack-semantics/src/postgres/profile/tests/helpers.rs index f83c5f7f6..ad2175d2d 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/tests/helpers.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/tests/helpers.rs @@ -182,3 +182,11 @@ pub(crate) fn window_only_answers(name: &str) -> Vec<(&'static str, [bool; 3])> }) .collect() } + +/// The one statement of `sql` as the native PostgreSQL parser reads it. +pub(crate) fn native_postgres_statement(sql: &str) -> Expression { + sqlquack_syntax_postgres::parse_strict(sql) + .unwrap_or_else(|failure| panic!("{sql}: {failure:?}")) + .statements + .remove(0) +} diff --git a/crates/sqlquack-semantics/src/postgres/profile/tests/test_query_lineage.rs b/crates/sqlquack-semantics/src/postgres/profile/tests/test_query_lineage.rs index 4e0da033f..2503d5984 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/tests/test_query_lineage.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/tests/test_query_lineage.rs @@ -3,13 +3,17 @@ use std::collections::HashMap; use sqlquack_syntax::Dialect; use crate::dialects::constants::POSTGRES; -use crate::lineage::models::TransformKind; +use crate::lineage::main::statement_lineage::statement_lineage; +use crate::lineage::models::{LineageContext, Nullability, SchemaColumn, TransformKind}; use crate::lineage::tests::helpers::output_ranges; use crate::lineage::tests::helpers::{duckdb_shop_schema, typed_column, typed_duckdb_shop_schema}; +use crate::lineage::tests::test_types::DialectTypeTestCase; use crate::lineage::tests::test_types::QueryLineageTestCase; +use crate::lineage::tests::test_types::StarOrderTestCase; use crate::lineage::tests::test_types::StrictRangeTestCase; use crate::lineage::tests::test_types::TypedShopSchema; use crate::postgres::profile::tests::helpers::lineage_outputs; +use crate::postgres::profile::tests::helpers::native_postgres_statement; #[test] fn given_unaliased_postgres_outputs_when_tracing_lineage_then_each_takes_figurecolnames_name() { @@ -276,3 +280,86 @@ fn given_a_strict_parse_when_tracing_lineage_then_each_output_has_its_items_byte ); } } + +#[test] +fn given_joined_and_comma_items_when_expanding_a_star_then_each_group_places_its_own_keys() { + let test_cases = [ + StarOrderTestCase { + description: "a USING key first in its own comma group, after the earlier item", + sql: "SELECT * FROM customers a, customers b JOIN customers c USING (name)", + expected_names: &[ + "id", "name", "country", "name", "id", "country", "id", "country", + ], + }, + StarOrderTestCase { + description: "a nested USING key once, first", + sql: "SELECT * FROM customers a JOIN customers b JOIN customers c USING (name) USING (name)", + expected_names: &["name", "id", "country", "id", "country", "id", "country"], + }, + ]; + let schema = duckdb_shop_schema(); + for test_case in test_cases { + let context = LineageContext { + profile: &POSTGRES, + schema: &schema, + sql: Some(test_case.sql), + }; + let names: Vec = + statement_lineage(&native_postgres_statement(test_case.sql), context) + .outputs + .into_iter() + .map(|output| output.name) + .collect(); + assert_eq!(names, test_case.expected_names, "{}", test_case.description); + } +} + +#[test] +fn given_an_outer_join_after_a_comma_when_tracing_nullability_then_only_its_group_is_nulled() { + let test_cases = [DialectTypeTestCase { + description: "a RIGHT JOIN nulls only its own group's left side", + sql: "SELECT a.name, b.status, c.name FROM customers a, orders b RIGHT JOIN customers c ON b.id = c.id", + dialect: Dialect::PostgreSQL, + profile: &POSTGRES, + expected_outputs: &[ + ("name", Some("TEXT"), Nullability::NonNull), + ("status", Some("TEXT"), Nullability::Nullable), + ("name", Some("TEXT"), Nullability::NonNull), + ], + }]; + let column = |name: &str| SchemaColumn { + nullability: Nullability::NonNull, + ..typed_column(name, "TEXT") + }; + let schema = TypedShopSchema { + closed: HashMap::from([ + ("orders".to_owned(), vec![column("id"), column("status")]), + ("customers".to_owned(), vec![column("id"), column("name")]), + ]), + ..TypedShopSchema::default() + }; + for test_case in test_cases { + let context = LineageContext { + profile: test_case.profile, + schema: &schema, + sql: Some(test_case.sql), + }; + let lineage = statement_lineage(&native_postgres_statement(test_case.sql), context); + let outputs: Vec<(&str, Option<&str>, Nullability)> = lineage + .outputs + .iter() + .map(|output| { + ( + output.name.as_str(), + output.data_type.as_deref(), + output.nullability, + ) + }) + .collect(); + assert_eq!( + outputs, test_case.expected_outputs, + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-syntax-postgres/src/parse/tests/test_parse.rs b/crates/sqlquack-syntax-postgres/src/parse/tests/test_parse.rs index 7a8f0fd91..28d506ff0 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/tests/test_parse.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/tests/test_parse.rs @@ -792,6 +792,12 @@ fn given_sql_when_parsing_then_the_tree_follows_postgresqls_grammar() { pointer: "/0/select/joins/0/this", expected_node: "joined_table", }, + ShapeTestCase { + description: "a comma binds looser than JOIN, so the join stays with the item after the comma", + sql: "SELECT * FROM a, b JOIN c ON true", + pointer: "/0/select/from/expressions/1", + expected_node: "joined_table", + }, ShapeTestCase { description: "INTERSECT binds tighter than UNION", sql: "SELECT 1 UNION SELECT 2 INTERSECT SELECT 3", From 5859236a2710e05ac307eddbd2adce3fab98288d Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:09:21 +0000 Subject: [PATCH 5/5] 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. --- .../src/quality/_helpers/safety.rs | 7 ++--- .../src/quality/tests/test_safety_rules.rs | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/crates/sqlquack-lint/src/quality/_helpers/safety.rs b/crates/sqlquack-lint/src/quality/_helpers/safety.rs index ecf3ae1d7..b78ac1b40 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/safety.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/safety.rs @@ -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 { diff --git a/crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs b/crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs index 1119f522c..6eee72e87 100644 --- a/crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs +++ b/crates/sqlquack-lint/src/quality/tests/test_safety_rules.rs @@ -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",