Skip to content

Commit 11f7ff1

Browse files
committed
refactor(native): read resolution and strategy models through native views
Planner resolution, strategy, scope and change detection read a model's schema entry, config, references, inferred columns, relative path, name and dependency rows, and iterate the project's models, sources, seeds, functions and audits, through the native view helpers instead of attribute reads. Reads that serve every resource kind keep the shared attribute dispatch.
1 parent 19087ad commit 11f7ff1

14 files changed

Lines changed: 154 additions & 26 deletions

File tree

‎crates/sqlbuild-python/src/bindings/_helpers/compiled_views/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub(crate) mod inputs_view;
88
pub(crate) mod model_input_view;
99
pub(crate) mod model_reads;
1010
pub(crate) mod project_host;
11+
pub(crate) mod project_reads;
1112
pub(crate) mod project_view;
1213
pub(crate) mod records;
1314
pub(crate) mod registration;

‎crates/sqlbuild-python/src/bindings/_helpers/compiled_views/model_reads.rs‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A compiled model's analysis outcomes as Rust data: views read their owner, records their fields.
22
3-
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult};
3+
use pyo3::prelude::{Bound, PyAny, PyAnyMethods, PyResult, Python};
44

55
use crate::bindings::_helpers::compile_driver::stage_work::{COMPILED_VIEW_FALLBACKS, count_stage};
66
use crate::bindings::_helpers::compiled_views::compiled_model_view::{
@@ -252,3 +252,56 @@ pub(crate) fn name_object<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py,
252252
}
253253
native_field(model, "name")
254254
}
255+
256+
/// The model's attached `SchemaModelEntry`, or `None`.
257+
pub(crate) fn schema_entry<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
258+
let py: Python<'py> = model.py();
259+
if let Some(view) = native_model(model)? {
260+
return Ok(view.schema_entry(py)?.into_bound(py));
261+
}
262+
native_field(model, "schema_entry")
263+
}
264+
265+
/// The model's `CompileModelConfig`.
266+
pub(crate) fn config<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
267+
let py: Python<'py> = model.py();
268+
if let Some(view) = native_model(model)? {
269+
return Ok(view.config(py)?.into_bound(py));
270+
}
271+
native_field(model, "config")
272+
}
273+
274+
/// The model's `CompileSqlReference` tuple.
275+
pub(crate) fn references<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
276+
let py: Python<'py> = model.py();
277+
if let Some(view) = native_model(model)? {
278+
return Ok(view.references(py)?.into_bound(py).into_any());
279+
}
280+
native_field(model, "references")
281+
}
282+
283+
/// The model file's relative path.
284+
pub(crate) fn relative_path<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
285+
let py: Python<'py> = model.py();
286+
if let Some(view) = native_model(model)? {
287+
return Ok(view.relative_path(py)?.into_bound(py));
288+
}
289+
native_field(model, "relative_path")
290+
}
291+
292+
/// The model's `InferredColumn` tuple, or `None`.
293+
pub(crate) fn inferred_columns<'py>(model: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
294+
let py: Python<'py> = model.py();
295+
if let Some(view) = native_model(model)? {
296+
return Ok(view.inferred_columns(py)?.into_bound(py));
297+
}
298+
native_field(model, "inferred_columns")
299+
}
300+
301+
/// `(resource type, name)` of each dependency.
302+
pub(crate) fn dep_rows(model: &Bound<'_, PyAny>) -> PyResult<Vec<(String, String)>> {
303+
if let Some(view) = native_model(model)? {
304+
return view.dep_rows(model.py());
305+
}
306+
native_field(model, "dep_rows")?.extract()
307+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//! A compiled project's resource collections, read from its native view.
2+
3+
use pyo3::prelude::{Bound, PyAny, PyResult, Python};
4+
5+
use crate::bindings::_helpers::compile_driver::stage_work::{COMPILED_VIEW_FALLBACKS, count_stage};
6+
use crate::bindings::_helpers::compiled_views::project_view::NativeProjectView;
7+
use crate::bindings::_helpers::records::fields::field as native_field;
8+
9+
/// The native view behind a project object; any other object is a Python-built record, counted.
10+
#[inline]
11+
fn native_project<'a>(project: &'a Bound<'_, PyAny>) -> PyResult<Option<&'a NativeProjectView>> {
12+
if let Ok(view) = project.cast::<NativeProjectView>() {
13+
return Ok(Some(view.get()));
14+
}
15+
count_stage(project.py(), COMPILED_VIEW_FALLBACKS, 1)?;
16+
Ok(None)
17+
}
18+
19+
/// The project's compiled models, in compile order.
20+
pub(crate) fn models<'py>(project: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
21+
let py: Python<'py> = project.py();
22+
if let Some(view) = native_project(project)? {
23+
return Ok(view.models(py).into_bound(py).into_any());
24+
}
25+
native_field(project, "models")
26+
}
27+
28+
/// The project's compiled sources.
29+
pub(crate) fn sources<'py>(project: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
30+
let py: Python<'py> = project.py();
31+
if let Some(view) = native_project(project)? {
32+
return Ok(view.sources(py).into_bound(py));
33+
}
34+
native_field(project, "sources")
35+
}
36+
37+
/// The project's compiled functions.
38+
pub(crate) fn functions<'py>(project: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
39+
let py: Python<'py> = project.py();
40+
if let Some(view) = native_project(project)? {
41+
return Ok(view.functions(py).into_bound(py));
42+
}
43+
native_field(project, "functions")
44+
}
45+
46+
/// The project's compiled seeds.
47+
pub(crate) fn seeds<'py>(project: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
48+
let py: Python<'py> = project.py();
49+
if let Some(view) = native_project(project)? {
50+
return Ok(view.seeds(py).into_bound(py));
51+
}
52+
native_field(project, "seeds")
53+
}
54+
55+
/// The project's compiled audits.
56+
pub(crate) fn audits<'py>(project: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
57+
let py: Python<'py> = project.py();
58+
if let Some(view) = native_project(project)? {
59+
return Ok(view.audits(py).into_bound(py));
60+
}
61+
native_field(project, "audits")
62+
}

‎crates/sqlbuild-python/src/bindings/_helpers/planner_changes/detect.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use sqlbuild_host::entries::models::{ExecutorHost, UserCodeHost};
44

55
use crate::bindings::_helpers::compiled_views::model_reads;
6+
use crate::bindings::_helpers::compiled_views::project_reads;
67
use crate::bindings::_helpers::contracts::spec_resolution::matching_dynamic_families;
78
use crate::bindings::_helpers::model_config::typed_config::{config_text, model_values};
89
use crate::bindings::_helpers::planner_changes::results::native_enum;
@@ -107,7 +108,7 @@ pub(crate) fn detect_changes<'py>(
107108
let query_change_tracking: bool =
108109
native_field(&native_field(project, "settings")?, "query_change_tracking")?.is_truthy()?;
109110
let dialect: Option<String> = native_field(snapshot, "column_dialect")?.extract()?;
110-
for function in native_field(project, "functions")?.try_iter()? {
111+
for function in project_reads::functions(project)?.try_iter()? {
111112
let function: Bound<'py, PyAny> = function?;
112113
let name: Bound<'py, PyAny> = native_field(&function, "name")?;
113114
let fingerprint_sql: String = compiled_function_fingerprint_sql(&function)?;
@@ -248,7 +249,7 @@ pub(crate) fn scope_model_changes<'py>(
248249
}
249250
};
250251
let function_local_hashes: Bound<'py, PyAny> = function_local_hashes(
251-
&native_field(project, "functions")?,
252+
&project_reads::functions(project)?,
252253
native_field(project, "sql_analysis_dialect")?
253254
.extract::<Option<String>>()?
254255
.as_deref(),
@@ -530,7 +531,7 @@ fn model_schema_findings<'py>(
530531
for column in &validity {
531532
warehouse.retain(|(name, _)| !column.eq_ignore_ascii_case(name));
532533
}
533-
let schema_entry: Bound<'py, PyAny> = native_field(model, "schema_entry")?;
534+
let schema_entry: Bound<'py, PyAny> = model_reads::schema_entry(model)?;
534535
let mut yml: Vec<(String, String)> = Vec::new();
535536
let mut type_enforcement: bool = false;
536537
let mut dynamic: Option<Bound<'py, PyAny>> = None;

‎crates/sqlbuild-python/src/bindings/_helpers/planner_changes/renames.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ fn changed_referencing_query(
240240
continue;
241241
}
242242
let mut references_models: bool = false;
243-
for reference in native_field(&model, "references")?.try_iter()? {
243+
for reference in model_reads::references(&model)?.try_iter()? {
244244
if native_field(&reference?, "ref_kind")?.eq(&ref_kind)? {
245245
references_models = true;
246246
break;

‎crates/sqlbuild-python/src/bindings/_helpers/planner_scope/scope.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Planner scope assembly using native graph and selector operations.
22
33
use crate::bindings::_helpers::boundary::python_calls::KeywordArguments;
4+
use crate::bindings::_helpers::compiled_views::model_reads;
5+
use crate::bindings::_helpers::compiled_views::project_reads;
46
use crate::bindings::_helpers::graph::keys::KeyCache;
57
use crate::bindings::_helpers::graph::project_graph::NativeProjectGraph;
68
use crate::bindings::_helpers::graph::views::lineage_graph_names;
@@ -33,7 +35,7 @@ pub(crate) fn managed_source_upstream_keys<'py>(
3335
project: &Bound<'py, PyAny>,
3436
) -> PyResult<Bound<'py, PyFrozenSet>> {
3537
let mut managed: HashSet<String> = HashSet::new();
36-
for source in native_field(project, "sources")?.try_iter()? {
38+
for source in project_reads::sources(project)?.try_iter()? {
3739
let source = source?;
3840
if !native_field(&native_field(&source, "source_entry")?, "loader")?.is_none() {
3941
managed.insert(native_field(&source, "name")?.extract()?);
@@ -119,9 +121,9 @@ pub(crate) fn build_planner_scope<'py>(
119121
let origins = build_execution_edge_origins(&project)?.into_any();
120122
let order = topologically_order_keys(&upstream, Some(origins))?.into_any();
121123
let models = PyDict::new(py);
122-
for model in native_field(&project, "models")?.try_iter()? {
124+
for model in project_reads::models(&project)?.try_iter()? {
123125
let model = model?;
124-
models.set_item(native_field(&model, "name")?, model)?;
126+
models.set_item(model_reads::name_object(&model)?, model)?;
125127
}
126128
let mut reads = args.value("python_read_source_names")?;
127129
if reads.is_none() {

‎crates/sqlbuild-python/src/bindings/_helpers/planner_scope/sources.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use sqlbuild_host::entries::models::ExecutorHost;
44

5+
use crate::bindings::_helpers::compiled_views::project_reads;
56
use crate::bindings::_helpers::graph::algorithms::closure;
67
use crate::bindings::_helpers::native_host::host;
78
use crate::bindings::_helpers::planner_scope::loaders::intermediate_map;
@@ -30,7 +31,7 @@ pub(crate) fn build_source_load_map<'py>(
3031
})
3132
.transpose()?;
3233
let result = PyDict::new(project.py());
33-
for source in native_field(project, "sources")?.try_iter()? {
34+
for source in project_reads::sources(project)?.try_iter()? {
3435
let source = source?;
3536
if let Some(keys) = &relevant
3637
&& !keys.contains(native_field(&source, "key")?)?

‎crates/sqlbuild-python/src/bindings/_helpers/planner_scope/values.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Python function identity and compiled graph conversion.
22
3+
use crate::bindings::_helpers::compiled_views::project_reads;
34
use crate::bindings::_helpers::graph::keys::native_key;
45
use crate::bindings::_helpers::records::fields::field as native_field;
56
use crate::bindings::_helpers::records::fields::mapping_items;
@@ -67,7 +68,7 @@ pub(crate) fn loader_graph(loaders: &Bound<'_, PyAny>) -> PyResult<LoaderGraph>
6768

6869
pub(crate) fn project_graph(project: &Bound<'_, PyAny>) -> PyResult<LoaderGraph> {
6970
let mut graph = loader_graph(&native_field(project, "loader_functions")?)?;
70-
for source in native_field(project, "sources")?.try_iter()? {
71+
for source in project_reads::sources(project)?.try_iter()? {
7172
let entry = native_field(&source?, "source_entry")?;
7273
graph.sources.push((
7374
native_field(&entry, "name")?.extract()?,

‎crates/sqlbuild-python/src/bindings/_helpers/planner_strategy/audit_entries.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::bindings::_helpers::attachments::reference_markers::assert_no_unresolved_sql_markers;
44
use crate::bindings::_helpers::auditing::render::render_audit_sql;
55
use crate::bindings::_helpers::boundary::python_calls::keywords;
6+
use crate::bindings::_helpers::compiled_views::project_reads;
67
use crate::bindings::_helpers::graph::views::project_lineage_views;
78
use crate::bindings::_helpers::planner_phases::objects::object;
89
use crate::bindings::_helpers::planner_strategy::audits::{
@@ -229,7 +230,7 @@ pub(crate) fn build_selected_audit_entries<'py>(
229230
let project = request.get("project")?;
230231
let selected_keys = native_field(&request.get("scope")?, "selected_keys")?;
231232
let mut selected: Vec<Bound<'py, PyAny>> = Vec::new();
232-
for audit in native_field(&project, "audits")?.try_iter()? {
233+
for audit in project_reads::audits(&project)?.try_iter()? {
233234
let audit = audit?;
234235
if audit_is_selected(&audit, &selected_keys)? {
235236
selected.push(audit);

‎crates/sqlbuild-python/src/bindings/_helpers/planner_strategy/fixture_context.rs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use sqlbuild_core::text::main::python_casefold::python_casefold;
1313

1414
use crate::bindings::_helpers::analysis_session::fixture_inferences::FixtureColumnInferences;
1515
use crate::bindings::_helpers::compiled_views::model_reads;
16+
use crate::bindings::_helpers::compiled_views::project_reads;
1617
use crate::bindings::_helpers::planner_strategy::constants::{
1718
MODEL_REFERENCE_KIND, SEED_REFERENCE_KIND, SOURCE_REFERENCE_KIND,
1819
};
@@ -53,7 +54,7 @@ pub(crate) fn planning_context(
5354
Some(models) => models,
5455
None => {
5556
let models = PyDict::new(py);
56-
for model in native_field(project, "models")?.try_iter()? {
57+
for model in project_reads::models(project)?.try_iter()? {
5758
let model = model?;
5859
models.set_item(model_reads::name_object(&model)?, &model)?;
5960
}
@@ -67,11 +68,16 @@ pub(crate) fn planning_context(
6768
model_fixture_relation(&model)?,
6869
);
6970
}
70-
for (collection, entry, kind, source) in [
71-
("sources", "source_entry", SOURCE_REFERENCE_KIND, true),
72-
("seeds", "schema_entry", SEED_REFERENCE_KIND, false),
71+
for (entry, kind, source) in [
72+
("source_entry", SOURCE_REFERENCE_KIND, true),
73+
("schema_entry", SEED_REFERENCE_KIND, false),
7374
] {
74-
for resource in native_field(project, collection)?.try_iter()? {
75+
let resources: Bound<'_, PyAny> = if source {
76+
project_reads::sources(project)?
77+
} else {
78+
project_reads::seeds(project)?
79+
};
80+
for resource in resources.try_iter()? {
7581
let resource = resource?;
7682
let entry = native_field(&resource, entry)?;
7783
let columns: Vec<FixtureColumn> = columns(&native_field(&entry, "columns")?, false)?;

0 commit comments

Comments
 (0)