Skip to content

Commit 2ddd78a

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

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -733,8 +733,7 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on
733733
}
734734

735735
#[test]
736-
fn given_a_comma_after_a_joined_item_when_expanding_a_star_then_the_columns_follow_the_written_order()
737-
{
736+
fn given_joined_and_comma_items_when_expanding_a_star_then_the_columns_follow_the_written_order() {
738737
let test_cases = [
739738
StarOrderTestCase {
740739
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
752751
"listing_status",
753752
],
754753
},
754+
StarOrderTestCase {
755+
description: "a nested USING key once, though the inner join merges it again",
756+
sql: "SELECT * FROM customers a JOIN (customers b JOIN customers c USING (id)) USING (id)",
757+
expected_names: &[
758+
"id", "name", "country", "name", "country", "name", "country",
759+
],
760+
},
761+
StarOrderTestCase {
762+
description: "a nested USING key once under a RIGHT JOIN",
763+
sql: "SELECT * FROM customers a RIGHT JOIN (customers b JOIN customers c USING (id)) USING (id)",
764+
expected_names: &[
765+
"id", "name", "country", "name", "country", "name", "country",
766+
],
767+
},
768+
StarOrderTestCase {
769+
description: "a nested NATURAL key once",
770+
sql: "SELECT * FROM orders a NATURAL JOIN (customers b NATURAL JOIN customers c)",
771+
expected_names: &[
772+
"id",
773+
"customer_id",
774+
"amount",
775+
"items",
776+
"status",
777+
"name",
778+
"country",
779+
],
780+
},
781+
StarOrderTestCase {
782+
description: "the same USING key in two comma groups, once in each",
783+
sql: "SELECT * FROM customers a JOIN customers b USING (id), customers c JOIN customers d USING (id)",
784+
expected_names: &[
785+
"id", "name", "country", "name", "country", "id", "name", "country", "name",
786+
"country",
787+
],
788+
},
755789
StarOrderTestCase {
756790
description: "a USING key once, first in its own join, before the comma item",
757791
sql: "SELECT * FROM orders a JOIN customers b USING (id), exports c",

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,11 @@ fn joined(
292292
.profile
293293
.same_identifier(&merged.column.key, &column.column.key)
294294
}) {
295-
Some(merged) => *merged = column,
295+
Some(merged) => {
296+
let sources: Range<usize> =
297+
merged.sources.start.min(reach.start)..merged.sources.end.max(reach.end);
298+
*merged = MergedKey { sources, ..column };
299+
}
296300
None => merged_keys.push(column),
297301
}
298302
}

0 commit comments

Comments
 (0)