Skip to content

Commit e47ee46

Browse files
committed
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.
1 parent 2ddd78a commit e47ee46

4 files changed

Lines changed: 119 additions & 1 deletion

File tree

‎crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::lineage::tests::helpers::duckdb_shop_schema;
88
use crate::lineage::tests::helpers::output_ranges;
99
use crate::lineage::tests::helpers::owned_findings;
1010
use crate::lineage::tests::test_types::QueryFindingsTestCase;
11+
use crate::lineage::tests::test_types::StarOrderTestCase;
1112
use crate::lineage::tests::test_types::StrictRangeTestCase;
1213

1314
#[test]
@@ -750,3 +751,19 @@ fn given_a_comma_after_a_joined_relation_when_checked_then_the_join_reads_only_i
750751
);
751752
}
752753
}
754+
755+
#[test]
756+
fn given_a_using_join_after_a_comma_when_expanding_a_star_then_its_key_leads_its_own_group() {
757+
let test_cases = [StarOrderTestCase {
758+
description: "the USING key comes first in its group, after the earlier comma relation",
759+
sql: "SELECT * FROM customers a, customers b JOIN customers c USING (name)",
760+
expected_names: &["id", "name", "name", "id", "id"],
761+
}];
762+
for test_case in test_cases {
763+
let names: Vec<String> = databricks_outputs(test_case.sql)
764+
.into_iter()
765+
.map(|(name, _)| name)
766+
.collect();
767+
assert_eq!(names, test_case.expected_names, "{}", test_case.description);
768+
}
769+
}

‎crates/sqlquack-semantics/src/postgres/profile/tests/helpers.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,11 @@ pub(crate) fn window_only_answers(name: &str) -> Vec<(&'static str, [bool; 3])>
182182
})
183183
.collect()
184184
}
185+
186+
/// The one statement of `sql` as the native PostgreSQL parser reads it.
187+
pub(crate) fn native_postgres_statement(sql: &str) -> Expression {
188+
sqlquack_syntax_postgres::parse_strict(sql)
189+
.unwrap_or_else(|failure| panic!("{sql}: {failure:?}"))
190+
.statements
191+
.remove(0)
192+
}

‎crates/sqlquack-semantics/src/postgres/profile/tests/test_query_lineage.rs‎

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ use std::collections::HashMap;
33
use sqlquack_syntax::Dialect;
44

55
use crate::dialects::constants::POSTGRES;
6-
use crate::lineage::models::TransformKind;
6+
use crate::lineage::main::statement_lineage::statement_lineage;
7+
use crate::lineage::models::{LineageContext, Nullability, SchemaColumn, TransformKind};
78
use crate::lineage::tests::helpers::output_ranges;
89
use crate::lineage::tests::helpers::{duckdb_shop_schema, typed_column, typed_duckdb_shop_schema};
10+
use crate::lineage::tests::test_types::DialectTypeTestCase;
911
use crate::lineage::tests::test_types::QueryLineageTestCase;
12+
use crate::lineage::tests::test_types::StarOrderTestCase;
1013
use crate::lineage::tests::test_types::StrictRangeTestCase;
1114
use crate::lineage::tests::test_types::TypedShopSchema;
1215
use crate::postgres::profile::tests::helpers::lineage_outputs;
16+
use crate::postgres::profile::tests::helpers::native_postgres_statement;
1317

1418
#[test]
1519
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
276280
);
277281
}
278282
}
283+
284+
#[test]
285+
fn given_joined_and_comma_items_when_expanding_a_star_then_each_group_places_its_own_keys() {
286+
let test_cases = [
287+
StarOrderTestCase {
288+
description: "a USING key first in its own comma group, after the earlier item",
289+
sql: "SELECT * FROM customers a, customers b JOIN customers c USING (name)",
290+
expected_names: &[
291+
"id", "name", "country", "name", "id", "country", "id", "country",
292+
],
293+
},
294+
StarOrderTestCase {
295+
description: "a nested USING key once, first",
296+
sql: "SELECT * FROM customers a JOIN customers b JOIN customers c USING (name) USING (name)",
297+
expected_names: &["name", "id", "country", "id", "country", "id", "country"],
298+
},
299+
];
300+
let schema = duckdb_shop_schema();
301+
for test_case in test_cases {
302+
let context = LineageContext {
303+
profile: &POSTGRES,
304+
schema: &schema,
305+
sql: Some(test_case.sql),
306+
};
307+
let names: Vec<String> =
308+
statement_lineage(&native_postgres_statement(test_case.sql), context)
309+
.outputs
310+
.into_iter()
311+
.map(|output| output.name)
312+
.collect();
313+
assert_eq!(names, test_case.expected_names, "{}", test_case.description);
314+
}
315+
}
316+
317+
#[test]
318+
fn given_an_outer_join_after_a_comma_when_tracing_nullability_then_only_its_group_is_nulled() {
319+
let test_cases = [DialectTypeTestCase {
320+
description: "a RIGHT JOIN nulls only its own group's left side",
321+
sql: "SELECT a.name, b.status, c.name FROM customers a, orders b RIGHT JOIN customers c ON b.id = c.id",
322+
dialect: Dialect::PostgreSQL,
323+
profile: &POSTGRES,
324+
expected_outputs: &[
325+
("name", Some("TEXT"), Nullability::NonNull),
326+
("status", Some("TEXT"), Nullability::Nullable),
327+
("name", Some("TEXT"), Nullability::NonNull),
328+
],
329+
}];
330+
let column = |name: &str| SchemaColumn {
331+
nullability: Nullability::NonNull,
332+
..typed_column(name, "TEXT")
333+
};
334+
let schema = TypedShopSchema {
335+
closed: HashMap::from([
336+
("orders".to_owned(), vec![column("id"), column("status")]),
337+
("customers".to_owned(), vec![column("id"), column("name")]),
338+
]),
339+
..TypedShopSchema::default()
340+
};
341+
for test_case in test_cases {
342+
let context = LineageContext {
343+
profile: test_case.profile,
344+
schema: &schema,
345+
sql: Some(test_case.sql),
346+
};
347+
let lineage = statement_lineage(&native_postgres_statement(test_case.sql), context);
348+
let outputs: Vec<(&str, Option<&str>, Nullability)> = lineage
349+
.outputs
350+
.iter()
351+
.map(|output| {
352+
(
353+
output.name.as_str(),
354+
output.data_type.as_deref(),
355+
output.nullability,
356+
)
357+
})
358+
.collect();
359+
assert_eq!(
360+
outputs, test_case.expected_outputs,
361+
"{}",
362+
test_case.description
363+
);
364+
}
365+
}

‎crates/sqlquack-syntax-postgres/src/parse/tests/test_parse.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,12 @@ fn given_sql_when_parsing_then_the_tree_follows_postgresqls_grammar() {
792792
pointer: "/0/select/joins/0/this",
793793
expected_node: "joined_table",
794794
},
795+
ShapeTestCase {
796+
description: "a comma binds looser than JOIN, so the join stays with the item after the comma",
797+
sql: "SELECT * FROM a, b JOIN c ON true",
798+
pointer: "/0/select/from/expressions/1",
799+
expected_node: "joined_table",
800+
},
795801
ShapeTestCase {
796802
description: "INTERSECT binds tighter than UNION",
797803
sql: "SELECT 1 UNION SELECT 2 INTERSECT SELECT 3",

0 commit comments

Comments
 (0)