Skip to content
Closed
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
18 changes: 17 additions & 1 deletion crates/sqlquack-semantics/src/bigquery/profile/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::bigquery::profile::_helpers::typing::{
use crate::bigquery::profile::constants::{BIGQUERY_DATE_PART_FUNCTIONS, BIGQUERY_WEEKDAY_PARTS};
use crate::dialects::types::DialectProfile;
use crate::lineage::constants::SNOWFLAKE_VALUES_COLUMN;
use crate::lineage::models::Engine;
use crate::lineage::models::{Engine, JoinConditionReach};
use crate::lineage::models::{Lineage, LineageContext, TransformKind};
use crate::lineage::types::DialectRules;
use crate::lineage::types::ValueType;
Expand Down Expand Up @@ -58,6 +58,22 @@ impl DialectRules for BigQueryRules {
false
}

fn window_keys_read_outputs(&self) -> bool {
false
}

fn select_list_reads_outputs(&self) -> bool {
true
}

fn merged_keys_hide_other_items(&self) -> bool {
true
}

fn join_conditions_read(&self) -> JoinConditionReach {
JoinConditionReach::Every
}

fn checks_call_signatures(&self) -> bool {
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use crate::lineage::tests::test_types::QueryFindingsTestCase;
#[test]
fn given_a_bigquery_query_when_checking_it_then_findings_follow_googlesql() {
let test_cases = [
QueryFindingsTestCase {
description: "an inline window ordered by another output's alias",
sql: "SELECT quantity AS q, sum(quantity) OVER (ORDER BY q) AS x FROM orders",
expected_findings: &[("E201", "Unknown column 'q' in table 'orders'", 51)],
},
QueryFindingsTestCase {
description: "a TIMESTAMP_TRUNC date part is not a column (analyzer: accepted)",
sql: "SELECT TIMESTAMP_TRUNC(ordered_at, DAY) AS v FROM orders",
Expand Down
18 changes: 17 additions & 1 deletion crates/sqlquack-semantics/src/clickhouse/profile/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::lineage::constants::{
DATE_PART_FUNCTIONS, SNOWFLAKE_VALUES_COLUMN, WINDOW_ONLY_FUNCTIONS,
};
use crate::lineage::main::rendered_type::rendered_type;
use crate::lineage::models::Engine;
use crate::lineage::models::{Engine, JoinConditionReach};
use crate::lineage::models::{Lineage, LineageContext};
use crate::lineage::types::DialectRules;
use crate::lineage::types::ValueType;
Expand Down Expand Up @@ -73,6 +73,22 @@ impl DialectRules for ClickHouseRules {
false
}

fn window_keys_read_outputs(&self) -> bool {
true
}

fn select_list_reads_outputs(&self) -> bool {
true
}

fn merged_keys_hide_other_items(&self) -> bool {
true
}

fn join_conditions_read(&self) -> JoinConditionReach {
JoinConditionReach::Every
}

fn checks_call_signatures(&self) -> bool {
true
}
Expand Down
18 changes: 17 additions & 1 deletion crates/sqlquack-semantics/src/databricks/profile/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::databricks::profile::_helpers::typing::{
use crate::databricks::profile::constants::DATABRICKS_VALUES_COLUMN;
use crate::dialects::types::DialectProfile;
use crate::lineage::constants::DATE_PART_FUNCTIONS;
use crate::lineage::models::Engine;
use crate::lineage::models::{Engine, JoinConditionReach};
use crate::lineage::models::{Lineage, LineageContext};
use crate::lineage::types::DialectRules;
use crate::lineage::types::ValueType;
Expand Down Expand Up @@ -63,6 +63,22 @@ impl DialectRules for DatabricksRules {
false
}

fn window_keys_read_outputs(&self) -> bool {
true
}

fn select_list_reads_outputs(&self) -> bool {
true
}

fn merged_keys_hide_other_items(&self) -> bool {
true
}

fn join_conditions_read(&self) -> JoinConditionReach {
JoinConditionReach::Every
}

fn checks_call_signatures(&self) -> bool {
true
}
Expand Down
18 changes: 17 additions & 1 deletion crates/sqlquack-semantics/src/duckdb/profile/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::duckdb::profile::constants::{
DUCKDB_VALUES_COLUMN,
};
use crate::lineage::constants::{DATE_PART_FUNCTIONS, WINDOW_ONLY_FUNCTIONS};
use crate::lineage::models::Engine;
use crate::lineage::models::{Engine, JoinConditionReach};
use crate::lineage::models::{Lineage, LineageContext};
use crate::lineage::types::DialectRules;
use crate::lineage::types::ValueType;
Expand Down Expand Up @@ -64,6 +64,22 @@ impl DialectRules for DuckDbRules {
false
}

fn window_keys_read_outputs(&self) -> bool {
true
}

fn select_list_reads_outputs(&self) -> bool {
true
}

fn merged_keys_hide_other_items(&self) -> bool {
true
}

fn join_conditions_read(&self) -> JoinConditionReach {
JoinConditionReach::Earlier
}

fn checks_call_signatures(&self) -> bool {
true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,60 @@ fn given_named_windows_when_resolving_names_then_only_the_windows_an_over_reads_
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY nope)",
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 65)],
},
QueryFindingsTestCase {
description: "a window named in another case, which DuckDB resolves to it",
sql: "SELECT sum(amount) OVER W AS x FROM orders WINDOW w AS (PARTITION BY nope)",
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 69)],
},
QueryFindingsTestCase {
description: "a used window extending an unknown window",
sql: "SELECT sum(amount) OVER w2 AS x FROM orders WINDOW w AS (ORDER BY id), w2 AS (zz ORDER BY id)",
expected_findings: &[("E232", "Unknown named window 'zz'", 78)],
},
QueryFindingsTestCase {
description: "an unknown window two steps along the used window's chain",
sql: "SELECT sum(amount) OVER w3 AS x FROM orders WINDOW w2 AS (zz ORDER BY id), w3 AS (w2)",
expected_findings: &[("E232", "Unknown named window 'zz'", 58)],
},
QueryFindingsTestCase {
description: "an unused window extending an unknown window",
sql: "SELECT amount FROM orders WINDOW w2 AS (zz ORDER BY id)",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "an unknown column in a used window's frame offset",
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id ROWS BETWEEN nope PRECEDING AND CURRENT ROW)",
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 81)],
},
QueryFindingsTestCase {
description: "the same offset written inline",
sql: "SELECT sum(amount) OVER (ORDER BY id ROWS BETWEEN nope PRECEDING AND CURRENT ROW) AS x FROM orders",
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 50)],
},
QueryFindingsTestCase {
description: "an unknown qualifier in a used window's end offset",
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY id ROWS BETWEEN 1 PRECEDING AND zz.id FOLLOWING)",
expected_findings: &[(
"E222",
"Unknown table or alias 'zz' referenced by column 'id'",
97,
)],
},
QueryFindingsTestCase {
description: "a used window ordered by its own projection's alias, as inline (2.0: Binder Error)",
sql: "SELECT sum(amount) OVER w AS x FROM orders WINDOW w AS (ORDER BY x)",
expected_findings: &[("E201", "Unknown column 'x' in table 'orders'", 65)],
},
QueryFindingsTestCase {
description: "a window shared by two projections, ordered by one's alias",
sql: "SELECT sum(amount) OVER w AS x, avg(amount) OVER w AS y FROM orders WINDOW w AS (ORDER BY y)",
expected_findings: &[("E201", "Unknown column 'y' in table 'orders'", 90)],
},
QueryFindingsTestCase {
description: "an unknown column in a window shared by two projections is reported once",
sql: "SELECT sum(amount) OVER w AS x, avg(amount) OVER w AS y FROM orders WINDOW w AS (ORDER BY nope)",
expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 90)],
},
QueryFindingsTestCase {
description: "a window that the used window extends",
sql: "SELECT sum(amount) OVER w2 AS x FROM orders WINDOW w AS (PARTITION BY nope), w2 AS (w ORDER BY id)",
Expand Down Expand Up @@ -716,6 +770,58 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on
sql: "SELECT * FROM orders a JOIN orders b USING (status), customers c",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "an ON condition reading a USING key only a later join merges (2.0: column not found)",
sql: "SELECT 1 FROM exports a JOIN exports b ON id IS NOT NULL JOIN orders c ON true JOIN customers d USING (id)",
expected_findings: &[(
"E201",
"Unknown column 'id' (not found in any referenced table)",
42,
)],
},
QueryFindingsTestCase {
description: "an ON condition reading a key a later join widens, beside an item offering the name (2.0: accepted)",
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)",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "an ON condition reading a USING key a later join merges again (2.0: accepted)",
sql: "SELECT 1 FROM orders a JOIN customers b USING (id) JOIN exports c ON id IS NOT NULL JOIN customers d USING (id)",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "an ON condition reading a later comma group's item (2.0: table not found)",
sql: "SELECT 1 FROM orders a JOIN customers c ON b.id = c.id, customers b",
expected_findings: &[(
"E222",
"Unknown table or alias 'b' referenced by column 'id'",
43,
)],
},
QueryFindingsTestCase {
description: "an ON condition reading a later item of its own join chain (2.0: table not found)",
sql: "SELECT 1 FROM orders a JOIN customers b ON c.id = a.id JOIN customers c ON true",
expected_findings: &[(
"E222",
"Unknown table or alias 'c' referenced by column 'id'",
43,
)],
},
QueryFindingsTestCase {
description: "an ON condition reading an earlier comma group's item (2.0: accepted)",
sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON a.id = c.id",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "an ON condition reading an earlier comma group's column (2.0: accepted)",
sql: "SELECT 1 FROM orders a, customers b JOIN customers c ON customer_id = c.id",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "a parenthesised join's ON condition reading an earlier comma item (2.0: accepted)",
sql: "SELECT 1 FROM orders a, (customers b JOIN customers c ON a.id = c.id)",
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",
Expand All @@ -732,6 +838,55 @@ fn given_a_comma_after_a_joined_item_when_resolving_names_then_the_join_reads_on
}
}

#[test]
fn given_a_natural_join_when_resolving_names_then_sides_sharing_no_column_are_refused() {
let test_cases = [
QueryFindingsTestCase {
description: "a NATURAL join of tables sharing no column (2.0: no columns found to join on)",
sql: "SELECT * FROM orders NATURAL JOIN shop.products",
expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 39)],
},
QueryFindingsTestCase {
description: "a NATURAL LEFT join sharing no column",
sql: "SELECT * FROM customers NATURAL LEFT JOIN exports",
expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 42)],
},
QueryFindingsTestCase {
description: "a NATURAL join whose comma neighbour, not its left item, shares a column",
sql: "SELECT * FROM customers c, shop.products p NATURAL JOIN orders o",
expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 56)],
},
QueryFindingsTestCase {
description: "a NATURAL join of a derived table sharing no column",
sql: "SELECT * FROM orders NATURAL JOIN (SELECT 1 AS z) s",
expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 34)],
},
QueryFindingsTestCase {
description: "a second NATURAL join whose right shares no column with the joined left",
sql: "SELECT * FROM orders o NATURAL JOIN customers c NATURAL JOIN exports e",
expected_findings: &[("E201", "NATURAL JOIN has no column both sides offer", 61)],
},
QueryFindingsTestCase {
description: "a NATURAL join of tables sharing a column",
sql: "SELECT * FROM orders NATURAL JOIN customers",
expected_findings: &[],
},
QueryFindingsTestCase {
description: "a NATURAL join sharing a column spelled in another case",
sql: "SELECT * FROM orders NATURAL JOIN (SELECT 1 AS ID) s",
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_joined_and_comma_items_when_expanding_a_star_then_the_columns_follow_the_written_order() {
let test_cases = [
Expand Down
Loading
Loading