diff --git a/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_analysis.rs b/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_analysis.rs index 07c95d22ac..77333ed2d0 100644 --- a/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_analysis.rs +++ b/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_analysis.rs @@ -5,7 +5,7 @@ use sqlquack_semantics::lineage::models::{ LineageContext, NameFinding, Nullability, OutputLineage, QueryLineage, SourceColumn, TransformKind, }; -use sqlquack_semantics::{statement_lineage, statement_name_findings}; +use sqlquack_semantics::statement_lineage_and_findings; use sqlquack_syntax::Expression; use crate::analysis_route::constants::{ @@ -37,7 +37,8 @@ pub(crate) fn native_analysis( schema: input.schema, sql: Some(input.sql), }; - let lineage: QueryLineage = statement_lineage(statement, context); + let (lineage, findings): (QueryLineage, Vec) = + statement_lineage_and_findings(statement, context); NativeAnalysis { columns: lineage .outputs @@ -56,11 +57,7 @@ pub(crate) fn native_analysis( .collect(), has_star: lineage.projects_star, star_resolved: !lineage.open_star, - diagnostics: diagnostics( - dialect.name(), - input, - statement_name_findings(statement, context), - ), + diagnostics: diagnostics(dialect.name(), input, findings), } } diff --git a/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_sharing.rs b/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_sharing.rs index c8ce6429b5..19e7f91b1c 100644 --- a/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_sharing.rs +++ b/crates/sqlbuild-analysis/src/analysis_route/_helpers/analysis/native_sharing.rs @@ -1,6 +1,7 @@ //! One native analysis shared by the models of a wave whose analyses differ only in names. use std::collections::{HashMap, HashSet}; +use std::hash::{Hash, Hasher}; use rayon::ThreadPool; use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator}; @@ -436,11 +437,23 @@ fn share_key( name_order(&names, |name| name.to_owned()), name_order(&names, str::to_ascii_uppercase), ]; - let mut hasher: Sha256 = Sha256::new(); - hasher.update(sql.len().to_le_bytes()); - hasher.update(sql.as_bytes()); - hasher.update(format!("{:?}", (shaped, names.len(), name_orders)).as_bytes()); - Some(hasher.finalize().into()) + let mut hasher: DigestHasher = DigestHasher(Sha256::new()); + (sql, shaped, names.len(), name_orders).hash(&mut hasher); + Some(hasher.0.finalize().into()) +} + +/// Feeds `Hash` output into a SHA-256 digest, so a key hashes its fields without formatting them. +struct DigestHasher(Sha256); + +impl Hasher for DigestHasher { + fn write(&mut self, bytes: &[u8]) { + self.0.update(bytes); + } + + /// Unused: the key is the digest, read with `finalize`. + fn finish(&self) -> u64 { + 0 + } } /// The positions of `names` sorted by each name as `spelled`. diff --git a/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/mappings.rs b/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/mappings.rs index 7d3d99dd2c..ab7f3b35a4 100644 --- a/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/mappings.rs +++ b/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/mappings.rs @@ -318,6 +318,10 @@ fn typed_columns( columns: &Pairs, known: Option<&Pairs>, ) -> Vec { + let mut spelled: HashMap<&str, &str> = HashMap::new(); + for (name, value) in known.into_iter().flatten() { + let _ = spelled.entry(name.as_str()).or_insert(value.as_str()); + } columns .iter() .map(|(name, data_type)| { @@ -325,19 +329,13 @@ fn typed_columns( name, (!data_type.is_empty() && data_type != UNKNOWN_TYPE) .then(|| dialect.session_type(data_type)), - column_nullability(known, name), + column_nullability(spelled.get(name.as_str()).copied()), ) }) .collect() } -fn column_nullability(known: Option<&Pairs>, column: &str) -> Nullability { - let spelled: Option<&str> = known.and_then(|pairs| { - pairs - .iter() - .find(|(name, _)| name == column) - .map(|(_, value)| value.as_str()) - }); +fn column_nullability(spelled: Option<&str>) -> Nullability { match spelled { Some("non_null") => Nullability::NonNull, Some("nullable") => Nullability::Nullable, diff --git a/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/session.rs b/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/session.rs index 56a868baea..9ee5a7848a 100644 --- a/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/session.rs +++ b/crates/sqlbuild-analysis/src/assembly/analysis_session/_helpers/session.rs @@ -394,8 +394,9 @@ impl AnalysisSession { ) }) .collect(); + let pool: Arc = self.catalog.native.analysis_pool()?; let findings: Vec> = - check_sql_macro_arguments(&self.request.dialect, &batch); + check_sql_macro_arguments(&self.request.dialect, &batch, &pool); for ((model, _, _), found) in inputs.iter().zip(findings) { if let Some(outcome) = self.outcomes[*model].as_mut() { outcome.sql_macro_findings = found; diff --git a/crates/sqlbuild-analysis/src/semantic_validation/_helpers/normalization.rs b/crates/sqlbuild-analysis/src/semantic_validation/_helpers/normalization.rs index efe0a2f7d4..814068823b 100644 --- a/crates/sqlbuild-analysis/src/semantic_validation/_helpers/normalization.rs +++ b/crates/sqlbuild-analysis/src/semantic_validation/_helpers/normalization.rs @@ -55,15 +55,21 @@ impl MappedSql { } } - fn slice(&self, start: usize, end: usize) -> Self { - let first = self.sql[..start].chars().count(); - let last = first + self.sql[start..end].chars().count(); + /// The text from byte `start` to `end`, with `cursor` counting characters along `self.sql`. + fn slice(&self, cursor: &mut CharCursor, start: usize, end: usize) -> Self { + let first: usize = cursor.at(&self.sql, start); + let last: usize = cursor.at(&self.sql, end); Self { sql: self.sql[start..end].to_owned(), offsets: self.offsets[first..=last].to_vec(), } } + /// The source offset of the character at byte `byte`. + fn anchor(&self, cursor: &mut CharCursor, byte: usize) -> usize { + self.offsets[cursor.at(&self.sql, byte)] + } + fn generated(text: String, anchor: usize) -> Self { Self { offsets: vec![anchor; text.chars().count() + 1], @@ -81,18 +87,35 @@ impl MappedSql { if edits.is_empty() { return self; } - let mut result = self.slice(0, 0); + let mut cursor: CharCursor = CharCursor::default(); + let mut result = self.slice(&mut cursor, 0, 0); let mut previous = 0; for (start, end, replacement) in edits { - result.append(self.slice(previous, start)); + result.append(self.slice(&mut cursor, previous, start)); result.append(replacement); previous = end; } - result.append(self.slice(previous, self.sql.len())); + result.append(self.slice(&mut cursor, previous, self.sql.len())); result } } +/// The character index of byte offsets read in increasing order, counted on from the last one read. +#[derive(Default)] +struct CharCursor { + byte: usize, + chars: usize, +} + +impl CharCursor { + /// The number of characters of `sql` before byte `byte`, which is at or after the last one read. + fn at(&mut self, sql: &str, byte: usize) -> usize { + self.chars += sql[self.byte..byte].chars().count(); + self.byte = byte; + self.chars + } +} + pub(super) fn normalize( sql: &str, dialect: &str, @@ -101,6 +124,7 @@ pub(super) fn normalize( ) -> Result { let mut result = MappedSql::new(sql); let mut edits: Vec = Vec::new(); + let mut cursor: CharCursor = CharCursor::default(); for captures in REFERENCES .as_ref() .map_err(Clone::clone)? @@ -111,15 +135,16 @@ pub(super) fn normalize( let replacement = if &captures[1] != DBT_REF && stubs.contains_key(name.as_str()) { MappedSql::generated( stubs[name.as_str()].clone(), - result.offsets[result.sql[..name.start()].chars().count()], + result.anchor(&mut cursor, name.start()), ) } else { - result.slice(name.start(), name.end()) + result.slice(&mut cursor, name.start(), name.end()) }; edits.push((whole.start(), whole.end(), replacement)); } result = result.edits(edits); let mut edits: Vec = Vec::new(); + let mut cursor: CharCursor = CharCursor::default(); let mut consumed_until = 0; let non_code: Vec<(usize, usize)> = dialect_non_code_ranges(&result.sql, &dialect_lexical_syntax(dialect)); @@ -157,7 +182,7 @@ pub(super) fn normalize( edits.push(( start, end, - MappedSql::generated(stub, result.offsets[result.sql[..start].chars().count()]), + MappedSql::generated(stub, result.anchor(&mut cursor, start)), )); consumed_until = end; } @@ -166,6 +191,7 @@ pub(super) fn normalize( result = normalize_variant(result)?; } let mut edits: Vec = Vec::new(); + let mut cursor: CharCursor = CharCursor::default(); for captures in PLACEHOLDERS .as_ref() .map_err(Clone::clone)? @@ -176,10 +202,7 @@ pub(super) fn normalize( edits.push(( whole.start(), whole.end(), - MappedSql::generated( - value.clone(), - result.offsets[result.sql[..whole.start()].chars().count()], - ), + MappedSql::generated(value.clone(), result.anchor(&mut cursor, whole.start())), )); } } @@ -213,10 +236,11 @@ pub(crate) fn normalize_native_sql_mapped( placeholders: &[(String, String)], ) -> Result<(String, Vec), String> { let native: MappedSql = native_mapped(sql, dialect, stub_prefix, &HashMap::new())?; + let mut cursor: CharCursor = CharCursor::default(); let edits: Vec = placeholder_default_edits(&native.sql, placeholders) .into_iter() .map(|(start, end, default)| { - let anchor: usize = native.offsets[native.sql[..start].chars().count()]; + let anchor: usize = native.anchor(&mut cursor, start); (start, end, MappedSql::generated(default.to_owned(), anchor)) }) .collect(); @@ -237,6 +261,7 @@ fn native_mapped( let non_code: Vec<(usize, usize)> = dialect_non_code_ranges(&normalized.sql, &dialect_lexical_syntax(dialect)); let mut edits: Vec = Vec::new(); + let mut cursor: CharCursor = CharCursor::default(); for captures in stubs.captures_iter(&normalized.sql) { let whole = captures.get(0).ok_or("missing stub match")?; let quoted: bool = non_code @@ -255,7 +280,7 @@ fn native_mapped( (None, Some(parameter)) => format!("'{HOOK_PARAMETER_PREFIX}{}'", parameter.as_str()), (None, None) => continue, }; - let anchor: usize = normalized.offsets[normalized.sql[..whole.start()].chars().count()]; + let anchor: usize = normalized.anchor(&mut cursor, whole.start()); edits.push(( whole.start(), whole.end(), @@ -337,6 +362,8 @@ fn matching_bracket(sql: &str, start: usize) -> Result, String> { fn normalize_variant(input: MappedSql) -> Result { let sql = &input.sql; let mut edits: Vec = Vec::new(); + let mut cursor: CharCursor = CharCursor::default(); + let mut key_cursor: CharCursor = CharCursor::default(); let mut index = 0; while index < sql.len() { if let Some(end) = skip_non_code(sql, index)? { @@ -359,7 +386,7 @@ fn normalize_variant(input: MappedSql) -> Result { index = close + 1; continue; } - let key = normalize_variant(input.slice(index + 1, close))?; + let key = normalize_variant(input.slice(&mut key_cursor, index + 1, close))?; let mut base_end = index; loop { base_end = sql[..base_end].trim_end().len(); @@ -376,17 +403,17 @@ fn normalize_variant(input: MappedSql) -> Result { .map_err(Clone::clone)? .find(&sql[..base_end]) { - let anchor = input.offsets[sql[..base.start()].chars().count()]; + let anchor = input.anchor(&mut cursor, base.start()); let mut replacement = MappedSql::generated("GET(".to_owned(), anchor); - replacement.append(input.slice(base.start(), index)); + replacement.append(input.slice(&mut cursor, base.start(), index)); replacement.append(MappedSql::generated( ", ".to_owned(), - input.offsets[sql[..index].chars().count()], + input.anchor(&mut cursor, index), )); replacement.append(key); replacement.append(MappedSql::generated( ")".to_owned(), - input.offsets[sql[..close].chars().count()], + input.anchor(&mut cursor, close), )); edits.push((base.start(), close + 1, replacement)); } else if key.sql != sql[index + 1..close] { diff --git a/crates/sqlbuild-analysis/src/semantic_validation/tests/test_positions.rs b/crates/sqlbuild-analysis/src/semantic_validation/tests/test_positions.rs index 4cfc66be4e..459c02ba6e 100644 --- a/crates/sqlbuild-analysis/src/semantic_validation/tests/test_positions.rs +++ b/crates/sqlbuild-analysis/src/semantic_validation/tests/test_positions.rs @@ -1,6 +1,7 @@ +use crate::semantic_validation::_helpers::normalization::normalize_native_sql_mapped; use crate::semantic_validation::models::BindingPositions; use crate::semantic_validation::tests::test_types::{ - UniqueWordOffsetTestCase, UniqueWordScalingTestCase, + MappedNormalizationTestCase, UniqueWordOffsetTestCase, UniqueWordScalingTestCase, }; use std::sync::OnceLock; use std::time::{Duration, Instant}; @@ -109,3 +110,52 @@ fn given_many_unique_identifiers_when_locating_each_then_work_stays_linear() { ); } } + +#[test] +fn given_non_ascii_text_around_several_edits_when_normalizing_then_copied_characters_map_to_their_source() +-> Result<(), String> { + let test_cases = [ + MappedNormalizationTestCase { + description: "references between strings in other scripts", + dialect: "duckdb", + sql: "SELECT '\u{e9}' AS a, '\u{20ac}' AS b FROM __ref(\"orders\") AS o, \ + __ref(\"customers\") AS c WHERE o.note = '\u{fc}' AND c.n = '\u{1f600}'", + expected_fragment: "FROM orders AS o", + }, + MappedNormalizationTestCase { + description: "Snowflake variant paths with computed keys holding other scripts", + dialect: "snowflake", + sql: "SELECT '\u{e9}' AS a, v:a['x' || '\u{20ac}'] AS b, w:b['\u{df}' || 'k'] AS c \ + FROM __ref(\"orders\") AS o WHERE o.n = '\u{fc}' || '\u{1f600}'", + expected_fragment: "GET(", + }, + ]; + for test_case in test_cases { + let source: Vec = test_case.sql.chars().collect(); + let (normalized, offsets) = + normalize_native_sql_mapped(test_case.sql, test_case.dialect, "__stub_", &[])?; + let output: Vec = normalized.chars().collect(); + assert!( + normalized.contains(test_case.expected_fragment), + "{}: {normalized}", + test_case.description + ); + + assert_eq!(offsets.len(), output.len() + 1, "{}", test_case.description); + let copied: Vec<(char, Option)> = output + .iter() + .zip(&offsets) + .filter(|(character, _)| !character.is_ascii()) + .map(|(character, offset)| (*character, source.get(*offset).copied())) + .collect(); + let authored: usize = source + .iter() + .filter(|character| !character.is_ascii()) + .count(); + assert_eq!(copied.len(), authored, "{}", test_case.description); + for (character, mapped) in copied { + assert_eq!(Some(character), mapped, "{}", test_case.description); + } + } + Ok(()) +} diff --git a/crates/sqlbuild-analysis/src/semantic_validation/tests/test_types.rs b/crates/sqlbuild-analysis/src/semantic_validation/tests/test_types.rs index efa00a1ef6..d3ddca12e3 100644 --- a/crates/sqlbuild-analysis/src/semantic_validation/tests/test_types.rs +++ b/crates/sqlbuild-analysis/src/semantic_validation/tests/test_types.rs @@ -92,3 +92,10 @@ pub(crate) struct TemporalCastDialectTestCase { pub(crate) dialect: polyglot_sql::DialectType, pub(crate) expected_warnings: usize, } + +pub(crate) struct MappedNormalizationTestCase { + pub(crate) description: &'static str, + pub(crate) dialect: &'static str, + pub(crate) sql: &'static str, + pub(crate) expected_fragment: &'static str, +} diff --git a/crates/sqlbuild-analysis/src/sql_macros/_helpers/typing/probes.rs b/crates/sqlbuild-analysis/src/sql_macros/_helpers/typing/probes.rs index 56a94506ff..dffc94d36b 100644 --- a/crates/sqlbuild-analysis/src/sql_macros/_helpers/typing/probes.rs +++ b/crates/sqlbuild-analysis/src/sql_macros/_helpers/typing/probes.rs @@ -1,5 +1,7 @@ //! Probe queries that read the caller's column types at each SQL macro call, and their verdicts. +use std::collections::HashMap; + use sqlquack_semantics::lineage::models::{Nullability, SchemaColumn}; use sqlquack_semantics::lineage::types::LineageSchema; @@ -521,20 +523,12 @@ fn shown_type(inferred: &str) -> &str { } } -/// Each model's probes' output columns, inferred against that model's bound relations. -pub(crate) fn analysed_probes( - dialect: &str, - models: &[(&[String], &RelationShapes)], -) -> Vec> { - let mut analysed: Vec> = Vec::with_capacity(models.len()); - for (probes, schema) in models { - analysed.push(model_probes(dialect, probes, schema)); - } - analysed -} - /// One model's probes' output columns, inferred against its bound relations by the native route. -fn model_probes(dialect: &str, probes: &[String], schema: &RelationShapes) -> Vec { +pub(crate) fn model_probes( + dialect: &str, + probes: &[String], + schema: &RelationShapes, +) -> Vec { let AnalysisRoute::Native(parser) = analysis_route(Some(dialect)) else { return vec![None; probes.len()]; }; @@ -549,29 +543,36 @@ fn model_probes(dialect: &str, probes: &[String], schema: &RelationShapes) -> Ve /// The relations a probe reads, by name; each also answers to the trailing parts of its name. #[derive(Debug)] pub(crate) struct ProbeSchema { - relations: Vec<(String, Vec)>, + relations: Vec>, + /// Each lower-cased name and dotted suffix of one, to the first relation it names. + by_name: HashMap, } impl ProbeSchema { /// Each relation with its `(column, type)` list; an unknown type leaves the column untyped. pub(crate) fn new(relations: &RelationShapes) -> Self { + let mut by_name: HashMap = HashMap::new(); + for (index, (name, _)) in relations.iter().enumerate() { + let name: String = unquoted_parts(name); + let suffixes = name.match_indices('.').map(|(dot, _)| &name[dot + 1..]); + for key in std::iter::once(name.as_str()).chain(suffixes) { + by_name.entry(key.to_owned()).or_insert(index); + } + } Self { relations: relations .iter() - .map(|(name, columns)| (unquoted_parts(name), schema_columns(columns))) + .map(|(_, columns)| schema_columns(columns)) .collect(), + by_name, } } } impl LineageSchema for ProbeSchema { fn columns(&self, relation: &str) -> Option<&[SchemaColumn]> { - let relation: String = relation.to_ascii_lowercase(); - let suffix: String = format!(".{relation}"); - self.relations - .iter() - .find(|(name, _)| *name == relation || name.ends_with(&suffix)) - .map(|(_, columns)| columns.as_slice()) + let index: usize = *self.by_name.get(&relation.to_ascii_lowercase())?; + self.relations.get(index).map(Vec::as_slice) } } diff --git a/crates/sqlbuild-analysis/src/sql_macros/main/check_sql_macro_arguments.rs b/crates/sqlbuild-analysis/src/sql_macros/main/check_sql_macro_arguments.rs index b6acd14352..42eb8c5e22 100644 --- a/crates/sqlbuild-analysis/src/sql_macros/main/check_sql_macro_arguments.rs +++ b/crates/sqlbuild-analysis/src/sql_macros/main/check_sql_macro_arguments.rs @@ -1,36 +1,34 @@ +use rayon::ThreadPool; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; + use crate::sql_macros::_helpers::typing::probes::{ - ProbePlan, analysed_probes, check_findings, probe_plan, + ProbePlan, check_findings, model_probes, probe_plan, }; -use crate::sql_macros::models::SqlMacroFinding; +use crate::sql_macros::models::{SqlMacroCheck, SqlMacroFinding}; use crate::sql_macros::types::{ArgumentCheckInput, ProbeColumns}; -/// Each model's call arguments its analysed SQL shows do not fit. +/// Each model's call arguments its analysed SQL shows do not fit, checked on `pool`. #[must_use] pub fn check_sql_macro_arguments( dialect: &str, models: &[ArgumentCheckInput<'_>], + pool: &ThreadPool, ) -> Vec> { - let plans: Vec = models - .iter() - .map(|(sql, _, checks)| probe_plan(sql, checks)) - .collect(); - let batch: Vec<(&[String], _)> = plans - .iter() - .zip(models) - .map(|(plan, (_, schema, _))| (plan.probes.as_slice(), *schema)) - .collect(); - let results: Vec> = analysed_probes(dialect, &batch); - plans - .iter() - .zip(models) - .zip(&results) - .map(|((plan, (_, _, checks)), columns)| model_findings(plan, checks, columns, dialect)) - .collect() + pool.install(|| { + models + .par_iter() + .map(|(sql, schema, checks)| { + let plan: ProbePlan = probe_plan(sql, checks); + let columns: Vec = model_probes(dialect, &plan.probes, schema); + model_findings(&plan, checks, &columns, dialect) + }) + .collect() + }) } fn model_findings( plan: &ProbePlan, - checks: &[crate::sql_macros::models::SqlMacroCheck], + checks: &[SqlMacroCheck], columns: &[ProbeColumns], dialect: &str, ) -> Vec { diff --git a/crates/sqlbuild-analysis/src/sql_macros/tests/test_typed_calls.rs b/crates/sqlbuild-analysis/src/sql_macros/tests/test_typed_calls.rs index c0d2f4ffa6..5f86bfd1f5 100644 --- a/crates/sqlbuild-analysis/src/sql_macros/tests/test_typed_calls.rs +++ b/crates/sqlbuild-analysis/src/sql_macros/tests/test_typed_calls.rs @@ -1,10 +1,13 @@ +use sqlquack_semantics::lineage::types::LineageSchema; + +use crate::sql_macros::_helpers::typing::probes::ProbeSchema; use crate::sql_macros::main::typed_analysis_sql::typed_analysis_sql; use crate::sql_macros::main::untyped_call_outputs::untyped_call_outputs; use crate::sql_macros::models::{ExpandedText, TypedCall}; use crate::sql_macros::tests::helpers::{expanded_text_for, shown_typed_calls, untyped_calls}; use crate::sql_macros::tests::test_types::{ - ExpandedAnalysisTestCase, ReservedNameTestCase, TypedAnalysisTestCase, TypedCallTestCase, - UntypedOutputsTestCase, + ExpandedAnalysisTestCase, ProbeSchemaLookupTestCase, ReservedNameTestCase, + TypedAnalysisTestCase, TypedCallTestCase, UntypedOutputsTestCase, }; const SHOUT: (&str, &str) = ( @@ -279,3 +282,63 @@ fn given_a_returns_type_of_call_when_building_the_analysis_text_then_it_asserts_ assert_eq!(typed, test_case.expected_sql, "{}", test_case.description); } } + +#[test] +fn given_bound_relations_when_a_probe_names_one_then_the_first_relation_ending_in_that_name_answers() + { + let relation = |name: &str, column: &str| { + ( + name.to_owned(), + vec![(column.to_owned(), "BIGINT".to_owned())], + ) + }; + let schema: ProbeSchema = ProbeSchema::new(&[ + relation("warehouse.sales.orders", "sales_order_id"), + relation("archive.orders", "archived_order_id"), + relation("\"Customers\"", "customer_id"), + ]); + let test_cases = [ + ProbeSchemaLookupTestCase { + description: "a bare name answers to the first relation it ends", + relation: "orders", + expected_column: Some("sales_order_id"), + }, + ProbeSchemaLookupTestCase { + description: "trailing parts match without regard to case", + relation: "SALES.Orders", + expected_column: Some("sales_order_id"), + }, + ProbeSchemaLookupTestCase { + description: "a full name picks its own relation", + relation: "archive.orders", + expected_column: Some("archived_order_id"), + }, + ProbeSchemaLookupTestCase { + description: "a quoted relation answers to its unquoted name", + relation: "customers", + expected_column: Some("customer_id"), + }, + ProbeSchemaLookupTestCase { + description: "the end of a name part is not a trailing part", + relation: "rders", + expected_column: None, + }, + ProbeSchemaLookupTestCase { + description: "a name with an extra leading part matches nothing", + relation: "other.customers", + expected_column: None, + }, + ]; + for test_case in test_cases { + let found: Option<&str> = schema + .columns(test_case.relation) + .and_then(|columns| columns.first()) + .map(|column| column.name.as_str()); + + assert_eq!( + found, test_case.expected_column, + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlbuild-analysis/src/sql_macros/tests/test_types.rs b/crates/sqlbuild-analysis/src/sql_macros/tests/test_types.rs index b7400f466c..81d73c007f 100644 --- a/crates/sqlbuild-analysis/src/sql_macros/tests/test_types.rs +++ b/crates/sqlbuild-analysis/src/sql_macros/tests/test_types.rs @@ -122,3 +122,9 @@ pub(super) struct LexicalRulesCallsTestCase { pub(super) sql: &'static str, pub(super) expected_calls: &'static [&'static str], } + +pub(crate) struct ProbeSchemaLookupTestCase { + pub(crate) description: &'static str, + pub(crate) relation: &'static str, + pub(crate) expected_column: Option<&'static str>, +} diff --git a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/_test_types.py b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/_test_types.py index 8facea8681..e151a930c0 100644 --- a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/_test_types.py +++ b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/_test_types.py @@ -136,3 +136,13 @@ class SqlMacroBodyRulesTestCase: expected_returncode: int expected_fragments: tuple[str, ...] expected_absent_fragments: tuple[str, ...] + + +@dataclass(frozen=True) +class SqlMacroWaveTestCase: + """A chain of models, one per analysis wave, each calling a macro once with a wrong type.""" + + description: str + files: dict[str, str] + wave_count: int + expected_finding: str diff --git a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/helpers.py b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/helpers.py index 77cc1c3143..4874af6993 100644 --- a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/helpers.py +++ b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/helpers.py @@ -157,6 +157,22 @@ def run_project( return project_dir, result +def wave_models(count: int) -> dict[str, str]: + """`count` chained models; each reads the previous one and calls the macro right and wrong.""" + + files: dict[str, str] = {} + upstreams: list[str] = ["stg_orders", *(f"level_{level}" for level in range(1, count))] + for level, upstream in enumerate(upstreams, start=1): + files[f"models/marts/level_{level}.sql"] = ( + f'MODEL (description "Level {level}");\n\n' + "SELECT o.order_id, o.status, o.amount_cents,\n" + f" @sql.cents_to_dollars(o.amount_cents) AS dollars_{level},\n" + f" @sql.cents_to_dollars(o.status) AS wrong_{level}\n" + f'FROM __ref("{upstream}") AS o\n' + ) + return files + + def output(result: subprocess.CompletedProcess[str]) -> str: """Everything one command printed.""" diff --git a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/test_sql_macro_errors.py b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/test_sql_macro_errors.py index 59dd804657..8884799245 100644 --- a/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/test_sql_macro_errors.py +++ b/tests/e2e/src/sqlbuild/cli/commands/main/compile/sql_macros/test_sql_macro_errors.py @@ -10,6 +10,7 @@ from tests.e2e.src.sqlbuild.cli.commands.main.compile.sql_macros._test_types import ( SqlMacroErrorTestCase, SqlMacroRecompileTestCase, + SqlMacroWaveTestCase, ) from tests.e2e.src.sqlbuild.cli.commands.main.compile.sql_macros.helpers import ( AUDIT_COLUMNS, @@ -23,6 +24,7 @@ orders_model, output, run_project, + wave_models, ) from tests.e2e.src.sqlbuild.cli.commands.shared.helpers import run_sqb @@ -839,3 +841,32 @@ def test_given_a_compiled_project_when_a_macro_signature_changes_then_the_next_c assert second.returncode == 1, printed assert f"error[{test_case.expected_code}]" in printed, printed assert test_case.expected_fragment in printed, printed + + +@pytest.mark.parametrize( + "test_case", + [ + SqlMacroWaveTestCase( + description="six_waves_each_with_one_wrong_typed_call", + files={**CENTS_TO_DOLLARS, **wave_models(6)}, + wave_count=6, + expected_finding=( + "argument amount_cents of macro cents_to_dollars() expects BIGINT; " + "o.status is VARCHAR" + ), + ) + ], + ids=lambda case: case.description, +) +def test_given_typed_calls_in_every_wave_when_compiling_then_each_model_reports_its_own( + test_case: SqlMacroWaveTestCase, tmp_path: Path +) -> None: + _, result = run_project( + tmp_path=tmp_path, files=test_case.files, command=("compile", "--no-cache") + ) + printed: str = output(result) + + assert result.returncode == 1, printed + assert printed.count(test_case.expected_finding) == test_case.wave_count, printed + for level in range(1, test_case.wave_count + 1): + assert f"models/marts/level_{level}.sql:5:3" in printed, printed