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
1 change: 1 addition & 0 deletions crates/sqlbuild-analysis/src/analysis_route/main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub(crate) mod native_outcomes;
pub mod native_revalidation;
pub mod query_expression_ranges;
pub mod same_column_type;
pub mod same_unquoted_name;
pub mod schema_column;
pub mod skipped_analysis;
pub mod unapplied_nullability_rules;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Whether two unquoted names written in one dialect resolve to the same warehouse object.

use crate::analysis_route::main::analysis_route::analysis_route;
use crate::analysis_route::models::AnalysisRoute;

/// Whether unquoted `left` and `right` name one object in `dialect`; exact without its native module.
#[must_use]
pub fn same_unquoted_name(dialect: Option<&str>, left: &str, right: &str) -> bool {
let AnalysisRoute::Native(native) = analysis_route(dialect) else {
return left == right;
};
let profile = native.profile();
profile.same_identifier(
&profile.resolved_identifier(left, false),
&profile.resolved_identifier(right, false),
)
}
2 changes: 2 additions & 0 deletions crates/sqlbuild-analysis/src/analysis_route/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ mod test_native_revalidation;
mod test_postgres_dialect;
#[path = "tests/test_same_column_type.rs"]
mod test_same_column_type;
#[path = "tests/test_same_unquoted_name.rs"]
mod test_same_unquoted_name;
#[path = "tests/test_type_spelling.rs"]
mod test_type_spelling;
#[path = "tests/test_types.rs"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::analysis_route::main::same_unquoted_name::same_unquoted_name;
use crate::analysis_route::tests::test_types::SameUnquotedNameTestCase;

#[test]
fn given_two_unquoted_names_when_comparing_then_each_dialect_folds_them_as_it_resolves_names() {
let test_cases = [
SameUnquotedNameTestCase {
description: "Snowflake folds unquoted names to upper case",
dialect: Some("snowflake"),
left: "marts",
right: "MARTS",
expected_same_name: true,
},
SameUnquotedNameTestCase {
description: "PostgreSQL folds unquoted names to lower case",
dialect: Some("postgres"),
left: "Marts",
right: "marts",
expected_same_name: true,
},
SameUnquotedNameTestCase {
description: "DuckDB compares names without case",
dialect: Some("duckdb"),
left: "marts",
right: "MARTS",
expected_same_name: true,
},
SameUnquotedNameTestCase {
description: "ClickHouse names keep their case",
dialect: Some("clickhouse"),
left: "marts",
right: "MARTS",
expected_same_name: false,
},
SameUnquotedNameTestCase {
description: "different names never match",
dialect: Some("snowflake"),
left: "marts",
right: "staging",
expected_same_name: false,
},
SameUnquotedNameTestCase {
description: "a dialect without a native module compares exactly",
dialect: None,
left: "marts",
right: "MARTS",
expected_same_name: false,
},
];

for test_case in test_cases {
assert_eq!(
same_unquoted_name(test_case.dialect, test_case.left, test_case.right),
test_case.expected_same_name,
"{}",
test_case.description
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,11 @@ pub(super) struct RebaseRangesTestCase {
/// The text of `member` each lineage row's rebased range covers.
pub(super) expected_texts: &'static [Option<&'static str>],
}

pub(super) struct SameUnquotedNameTestCase {
pub(super) description: &'static str,
pub(super) dialect: Option<&'static str>,
pub(super) left: &'static str,
pub(super) right: &'static str,
pub(super) expected_same_name: bool,
}
5 changes: 5 additions & 0 deletions crates/sqlbuild-host/src/entries/_helpers/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,11 @@ pub(crate) const ENTRIES: &[HostEntry] = &[
kind: HostKind::Function,
edge: HostEdge::Executor,
},
HostEntry {
name: "execution_deadlines_suspended",
kind: HostKind::Function,
edge: HostEdge::Executor,
},
HostEntry {
name: "audit_evidence_scope",
kind: HostKind::Function,
Expand Down
1 change: 1 addition & 0 deletions crates/sqlbuild-host/src/entries/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ pub enum ExecutorHost {
RunStatus,
ExecutionDurationLimitError,
ExecutionDeadlineScope,
ExecutionDeadlinesSuspended,
AuditEvidenceScope,
CancelRunningStatements,
EventDispatcher,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub(crate) mod orchestration_triggers;
pub(crate) mod run_locations;
pub(crate) mod run_writes;
pub(crate) mod runs_state;
pub(crate) mod synced_catalog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
//! The schemas a build or seed writes, where it records its rows in the runs table, each once.

use crate::native_host::constants::SKIP_ACTION;
use crate::native_host::main::target_location::target_location;
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
use sqlbuild_analysis::analysis_route::main::same_unquoted_name::same_unquoted_name;
use sqlbuild_python_base::records::main::fields::field::field as native_field;
use sqlbuild_python_front::native_host::models::Location;
use sqlbuild_state::runs::main::distinct_run_schemas::distinct_run_schemas;
use sqlbuild_state::runs::models::RunSchema;
use std::collections::BTreeSet;

/// The schema of a relation the run writes; None for a relation without one.
fn written_schema(destination: &Bound<'_, PyAny>) -> PyResult<Option<Location>> {
let schema: Option<String> = native_field(destination, "schema")?.extract()?;
schema
.map(|schema| {
Ok(Location {
database: native_field(destination, "database")?.extract()?,
schema,
})
})
.transpose()
}

/// Where a run writing no relation records, as audits do: the target's schema, else the first node's.
fn default_location(
plan: &Bound<'_, PyAny>,
project: &Bound<'_, PyAny>,
) -> PyResult<Option<Location>> {
let database: Option<String> = target_location(project, "effective_target_database")?;
if let Some(schema) = target_location(project, "effective_target_schema")? {
return Ok(Some(Location { database, schema }));
}
for locations in ["model_locations", "seed_locations"] {
for location in native_field(plan, locations)?
.call_method0("values")?
.try_iter()?
{
if let Some(found) = written_schema(&location?)? {
return Ok(Some(Location {
database: database.or(found.database),
schema: found.schema,
}));
}
}
}
Ok(None)
}

/// Each physical schema of `schemas` once, as the dialect resolves names; else the default location.
fn run_locations(
schemas: BTreeSet<Location>,
(plan, project): (&Bound<'_, PyAny>, &Bound<'_, PyAny>),
) -> PyResult<Vec<Location>> {
if schemas.is_empty() {
return Ok(default_location(plan, project)?.into_iter().collect());
}
let dialect: Option<String> = native_field(project, "sql_analysis_dialect")?.extract()?;
let target_database: Option<String> = target_location(project, "effective_target_database")?;
let same_name = |left: &str, right: &str| same_unquoted_name(dialect.as_deref(), left, right);
Ok(distinct_run_schemas(
schemas
.into_iter()
.map(|location| RunSchema {
database: location.database,
schema: location.schema,
})
.collect(),
target_database.as_deref(),
&same_name,
)
.into_iter()
.map(|schema| Location {
database: schema.database,
schema: schema.schema,
})
.collect())
}

/// Where a build records: its models' and seeds' schemas that run, its functions', its sources'.
pub(crate) fn build_write_locations(pipeline: &Bound<'_, PyAny>) -> PyResult<Vec<Location>> {
let plan: &Bound<'_, PyAny> = &native_field(pipeline, "plan_output")?;
let project: &Bound<'_, PyAny> = &native_field(pipeline, "project")?;
let mut schemas: BTreeSet<Location> = BTreeSet::new();
for entries in ["model_entries", "seed_entries"] {
for entry in native_field(plan, entries)?.try_iter()? {
let entry: Bound<'_, PyAny> = entry?;
if !native_field(&entry, "action")?.eq(SKIP_ACTION)? {
schemas.extend(written_schema(&native_field(&entry, "destination")?)?);
}
}
}
for entry in native_field(plan, "function_entries")?.try_iter()? {
schemas.extend(written_schema(&native_field(&entry?, "destination")?)?);
}
let sources: Bound<'_, PyAny> = native_field(plan, "source_map")?;
for entry in native_field(plan, "source_load_entries")?.try_iter()? {
if let Some(source) = sources
.call_method1("get", (native_field(&entry?, "name")?,))?
.extract::<Option<Bound<'_, PyAny>>>()?
{
schemas.extend(written_schema(&source)?);
}
}
run_locations(schemas, (plan, project))
}

/// Where `sqb seed` records: the schemas of every seed it loads.
pub(crate) fn seed_write_locations(pipeline: &Bound<'_, PyAny>) -> PyResult<Vec<Location>> {
let plan: &Bound<'_, PyAny> = &native_field(pipeline, "plan_output")?;
let project: &Bound<'_, PyAny> = &native_field(pipeline, "project")?;
let mut schemas: BTreeSet<Location> = BTreeSet::new();
for entry in native_field(plan, "seed_entries")?.try_iter()? {
schemas.extend(written_schema(&native_field(&entry?, "destination")?)?);
}
run_locations(schemas, (plan, project))
}
Loading
Loading