Skip to content
Merged
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
16 changes: 16 additions & 0 deletions crates/sqlbuild-rules-native/src/engine/tests/test_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,22 @@ fn given_enforced_contract_outputs_when_evaluating_explicit_type_rule_then_retur
expected_fault_count: 1,
expected_message_fragment: "wildcard output",
},
test_types::ExplicitOutputTypeTestCase {
description: "proven dependency import wildcard",
query_sql: "WITH final AS (SELECT * FROM __ref(\"orders\")) SELECT order_id FROM final",
columns: json!([{"name": "order_id", "type": "INTEGER", "type_proven": true}]),
contract: "enforced",
expected_fault_count: 0,
expected_message_fragment: "",
},
test_types::ExplicitOutputTypeTestCase {
description: "unproven dependency import wildcard",
query_sql: "WITH final AS (SELECT * FROM __ref(\"orders\")) SELECT order_id FROM final",
columns: json!([{"name": "order_id", "type": "INTEGER", "type_proven": false}]),
contract: "enforced",
expected_fault_count: 1,
expected_message_fragment: "wildcard output",
},
test_types::ExplicitOutputTypeTestCase {
description: "positional set branches require casts",
query_sql: "SELECT order_id FROM current_orders UNION ALL SELECT order_id FROM archived_orders",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::constants::ENFORCED_CONTRACT;
use crate::models::{Column, Fault, Model, RuleMetadata};
use crate::rules::_helpers::evaluation::{plain_select, root_select, sole_table_name, top_ctes};
use crate::rules::models::FaultCollector;
use sqlparser::ast::{Expr, Query, Select, SelectItem, SetExpr, SetQuantifier, Spanned};
use sqlparser::ast::{
Expr, Query, Select, SelectItem, SetExpr, SetQuantifier, Spanned, TableFactor,
};

#[derive(Clone, Copy)]
struct OutputScope<'a> {
Expand Down Expand Up @@ -95,6 +97,9 @@ impl OutputRule<'_> {
item,
SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _)
) {
if proven_dependency_wildcard(select, scope.columns) {
continue;
}
self.push(
Some(source_position(item)),
"wildcard output does not establish enforced contract types explicitly".into(),
Expand Down Expand Up @@ -168,6 +173,29 @@ impl OutputRule<'_> {
}
}

fn proven_dependency_wildcard(select: &Select, columns: &[Column]) -> bool {
let Some(source) = select.from.first().filter(|_| select.from.len() == 1) else {
return false;
};
if !plain_select(select, true) || !source.joins.is_empty() {
return false;
}
let TableFactor::Table {
name,
args: Some(_),
..
} = &source.relation
else {
return false;
};
let dependency = name.to_string().to_ascii_lowercase();
matches!(dependency.as_str(), "__ref" | "__source")
&& columns
.iter()
.filter(|column| !column.data_type.is_empty())
.all(|column| column.type_proven)
}

fn final_cte_output_boundary<'a>(
query: &'a Query,
columns: &[Column],
Expand Down Expand Up @@ -234,6 +262,9 @@ fn columns_in_output_order_with_missing(
let Some(output_names) = set_expression_output_names_with_missing(set_expr) else {
return columns.to_vec();
};
if output_names.len() == 1 && output_names[0].is_none() {
return columns.to_vec();
}
output_names
.into_iter()
.enumerate()
Expand Down
Loading