@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55use std:: time:: Instant ;
66
77use pyo3:: prelude:: { Bound , PyAny , PyAnyMethods , PyModule , PyModuleMethods , PyResult , Python } ;
8- use pyo3:: types:: { PyDict , PyDictMethods , PyTuple } ;
8+ use pyo3:: types:: { PyBytes , PyTuple } ;
99use pyo3:: { IntoPyObjectExt , pyfunction, wrap_pyfunction} ;
1010use sqlbuild_cache:: digest:: main:: sha256_hex:: sha256_hex;
1111use sqlbuild_core:: json:: main:: dumps:: dumps;
@@ -18,17 +18,17 @@ use sqlbuild_rules::custom_rule_cache::types::ReadSet;
1818use crate :: bindings:: _helpers:: cache:: native_store:: current_code_identity;
1919use crate :: bindings:: _helpers:: compile_driver:: python_calls:: imported;
2020use crate :: bindings:: _helpers:: compile_driver:: python_calls:: keywords;
21- use crate :: bindings:: _helpers:: compiled_project:: records:: compiled_project_record;
22- use crate :: bindings:: _helpers:: records:: construct:: { new_record, new_record_with} ;
21+ use crate :: bindings:: _helpers:: records:: construct:: new_record_with;
2322use crate :: bindings:: _helpers:: records:: fields:: field as native_field;
24- use crate :: bindings:: _helpers:: records:: fields:: replaced_keywords;
2523use crate :: bindings:: _helpers:: records:: ids:: RecordId ;
2624use crate :: bindings:: _helpers:: rules:: constants:: {
2725 CUSTOM_HOST_RUNTIME_VERSION , CUSTOM_RULES_CACHE_FILE ,
2826} ;
29- use crate :: bindings:: _helpers:: rules:: custom_hosts:: {
30- HostCancel , HostRequest , host_payload_project, run_custom_hosts,
27+ use crate :: bindings:: _helpers:: rules:: custom_host_columns:: custom_host_declared_columns;
28+ use crate :: bindings:: _helpers:: rules:: custom_host_payload:: {
29+ CUSTOM_HOST_PAYLOAD_MAGIC , custom_host_payload,
3130} ;
31+ use crate :: bindings:: _helpers:: rules:: custom_hosts:: { HostCancel , HostRequest , run_custom_hosts} ;
3232use crate :: bindings:: _helpers:: rules:: custom_rule_cache:: CustomRuleCache ;
3333use crate :: bindings:: _helpers:: rules:: custom_rule_cache:: custom_rule_identity;
3434use crate :: bindings:: _helpers:: rules:: custom_rule_evidence:: custom_rule_implementation_fingerprint;
@@ -39,29 +39,6 @@ use crate::bindings::_helpers::rules::rule_run::rules_bulk_cache_path;
3939
4040const PROJECT_SUBJECT : & str = "project" ;
4141const MODEL_SUBJECT : & str = "model" ;
42- /// Project fields no rule fact reads, cleared before digests and host payloads use the project.
43- const HOST_PROJECT_CLEARED : [ ( & str , Cleared ) ; 11 ] = [
44- ( "binding_catalog" , Cleared :: None ) ,
45- ( "sql_expansions" , Cleared :: Dict ) ,
46- ( "loaded_macros" , Cleared :: Dict ) ,
47- ( "loader_functions" , Cleared :: Tuple ) ,
48- ( "hook_functions" , Cleared :: Tuple ) ,
49- ( "materialization_files" , Cleared :: Tuple ) ,
50- ( "external_sql_reference_resolver" , Cleared :: None ) ,
51- ( "sql_scenarios" , Cleared :: Tuple ) ,
52- ( "sql_hook_files" , Cleared :: Tuple ) ,
53- ( "diagnostics" , Cleared :: Tuple ) ,
54- ( "unmatched_literal_sql_relations" , Cleared :: Tuple ) ,
55- ] ;
56-
57- /// The empty value one cleared project field takes.
58- #[ derive( Clone , Copy ) ]
59- enum Cleared {
60- None ,
61- Dict ,
62- Tuple ,
63- }
64-
6542/// The run one custom-rule evaluation describes.
6643pub ( crate ) struct CustomRun < ' a , ' py > {
6744 pub ( crate ) project : & ' a Bound < ' py , PyAny > ,
@@ -140,11 +117,10 @@ pub(crate) fn evaluate_custom_rules_cached<'py>(
140117 . iter ( )
141118 . map ( |( code, rule) | Ok ( ( code. clone ( ) , host_subject ( rule) ?) ) )
142119 . collect :: < PyResult < _ > > ( ) ?;
143- let facts_project: Bound < ' py , PyAny > = host_project ( run. project ) ?;
144120 let host_run: HostRun = run_custom_hosts (
145121 py,
146122 & HostRequest {
147- project : & facts_project ,
123+ project : run . project ,
148124 config : run. config ,
149125 project_dir : & project_dir,
150126 dialect : run. dialect ,
@@ -301,25 +277,6 @@ fn orjson_key(key: &FactKey) -> String {
301277 dumps ( & value, & JsonDialect :: Orjson ( OrjsonOptions :: default ( ) ) ) . unwrap_or_default ( )
302278}
303279
304- /// The project without state no rule fact reads, before digests and host payloads use it.
305- fn host_project < ' py > ( project : & Bound < ' py , PyAny > ) -> PyResult < Bound < ' py , PyAny > > {
306- let py: Python < ' py > = project. py ( ) ;
307- let record: Bound < ' py , PyAny > = compiled_project_record ( project, false ) ?;
308- let changes: Bound < ' py , PyDict > = PyDict :: new ( py) ;
309- for ( name, cleared) in HOST_PROJECT_CLEARED {
310- match cleared {
311- Cleared :: None => changes. set_item ( name, py. None ( ) ) ?,
312- Cleared :: Dict => changes. set_item ( name, PyDict :: new ( py) ) ?,
313- Cleared :: Tuple => changes. set_item ( name, PyTuple :: empty ( py) ) ?,
314- }
315- }
316- changes. set_item (
317- "scope_index" ,
318- new_record ( py, RecordId :: ScopeIndex , ( ) , None ) ?,
319- ) ?;
320- replaced_keywords ( & record, & changes)
321- }
322-
323280fn subject_value ( rule : & Bound < ' _ , PyAny > ) -> PyResult < Option < String > > {
324281 let subject: Bound < ' _ , PyAny > = native_field ( rule, "subject" ) ?;
325282 if subject. is_none ( ) {
@@ -389,12 +346,6 @@ fn cpu_seconds(py: Python<'_>) -> PyResult<f64> {
389346 Ok ( thread + user + system)
390347}
391348
392- /// The project every custom-rule host decodes from its payload.
393- #[ pyfunction]
394- fn custom_host_payload < ' py > ( project : & Bound < ' py , PyAny > ) -> PyResult < Bound < ' py , PyAny > > {
395- host_payload_project ( & host_project ( project) ?)
396- }
397-
398349/// The digest the custom-rule cache keys one recorded fact on, or `None` when unreproducible.
399350#[ pyfunction]
400351#[ pyo3( signature = ( * , project, project_dir, key) ) ]
@@ -408,5 +359,10 @@ fn custom_rule_fact_digest(
408359
409360pub ( crate ) fn register ( module : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
410361 module. add_function ( wrap_pyfunction ! ( custom_host_payload, module) ?) ?;
362+ module. add_function ( wrap_pyfunction ! ( custom_host_declared_columns, module) ?) ?;
363+ module. add (
364+ "CUSTOM_HOST_PAYLOAD_MAGIC" ,
365+ PyBytes :: new ( module. py ( ) , CUSTOM_HOST_PAYLOAD_MAGIC ) ,
366+ ) ?;
411367 module. add_function ( wrap_pyfunction ! ( custom_rule_fact_digest, module) ?)
412368}
0 commit comments