@@ -10,6 +10,7 @@ use sqlx::PgPool;
1010
1111use super :: db_tracking:: { self , read_source_tracking_info} ;
1212use super :: db_tracking_setup;
13+ use super :: memoization:: { EvaluationCache , MemoizationInfo } ;
1314use crate :: base:: schema;
1415use crate :: base:: spec:: FlowInstanceSpec ;
1516use crate :: base:: value:: { self , FieldValues , KeyValue } ;
@@ -115,6 +116,10 @@ struct TrackingInfoForTarget<'a> {
115116 mutation : ExportTargetMutation ,
116117}
117118
119+ struct PrecommitData < ' a > {
120+ scope_value : & ' a ScopeValueBuilder ,
121+ memoization_info : & ' a MemoizationInfo ,
122+ }
118123struct PrecommitMetadata {
119124 source_entry_exists : bool ,
120125 process_ordinal : i64 ,
@@ -130,10 +135,9 @@ async fn precommit_source_tracking_info(
130135 source_id : i32 ,
131136 source_key_json : & serde_json:: Value ,
132137 source_ordinal : Option < i64 > ,
133- memoization_info : serde_json :: Value ,
138+ data : Option < PrecommitData < ' _ > > ,
134139 process_timestamp : & chrono:: DateTime < chrono:: Utc > ,
135140 db_setup : & db_tracking_setup:: TrackingTableSetupState ,
136- scope_value : & Option < ScopeValueBuilder > ,
137141 export_ops : & [ AnalyzedExportOp ] ,
138142 pool : & PgPool ,
139143) -> Result < WithApplyStatus < PrecommitOutput > > {
@@ -199,9 +203,9 @@ async fn precommit_source_tracking_info(
199203 }
200204
201205 let mut new_target_keys_info = db_tracking:: TrackedTargetKeyForSource :: default ( ) ;
202- if let Some ( scope_value ) = scope_value {
206+ if let Some ( data ) = & data {
203207 for export_op in export_ops. iter ( ) {
204- let collected_values = scope_value. collected_values
208+ let collected_values = data . scope_value . collected_values
205209 [ export_op. input . collector_idx as usize ]
206210 . lock ( )
207211 . unwrap ( ) ;
@@ -304,7 +308,7 @@ async fn precommit_source_tracking_info(
304308 source_key_json,
305309 process_ordinal,
306310 new_staging_target_keys,
307- memoization_info,
311+ data . as_ref ( ) . map ( |data| data . memoization_info ) ,
308312 db_setup,
309313 & mut * txn,
310314 if tracking_info_exists {
@@ -319,7 +323,7 @@ async fn precommit_source_tracking_info(
319323
320324 Ok ( WithApplyStatus :: Normal ( PrecommitOutput {
321325 metadata : PrecommitMetadata {
322- source_entry_exists : scope_value . is_some ( ) ,
326+ source_entry_exists : data . is_some ( ) ,
323327 process_ordinal,
324328 existing_process_ordinal,
325329 new_target_keys : new_target_keys_info,
@@ -438,26 +442,51 @@ pub async fn update_source_entry<'a>(
438442 pool,
439443 )
440444 . await ?;
441- let scope_value = evaluate_source_entry ( plan, source_op_idx, schema, key) . await ?;
445+ let already_exists = existing_tracking_info. is_some ( ) ;
446+ let memoization_info = existing_tracking_info
447+ . map ( |info| info. memoization_info . map ( |info| info. 0 ) )
448+ . flatten ( ) ;
449+ let evaluation_cache = memoization_info
450+ . map ( |info| EvaluationCache :: from_stored ( info. cache ) )
451+ . unwrap_or_default ( ) ;
452+ let value_builder =
453+ evaluate_source_entry ( plan, source_op_idx, schema, key, Some ( & evaluation_cache) ) . await ?;
442454
443455 // Didn't exist and still doesn't exist. No need to apply any changes.
444- if existing_tracking_info . is_none ( ) && scope_value . is_none ( ) {
456+ if !already_exists && value_builder . is_none ( ) {
445457 return Ok ( ( ) ) ;
446458 }
447459
448- // TODO: Generate the actual source ordinal and memoization info.
449- let source_ordinal: Option < i64 > = if scope_value. is_some ( ) { Some ( 1 ) } else { None } ;
450- let memoization_info = serde_json:: Value :: Null ;
460+ let memoization_info = MemoizationInfo {
461+ cache : evaluation_cache. into_stored ( ) ?,
462+ } ;
463+ let ( source_ordinal, precommit_data) = match & value_builder {
464+ Some ( scope_value) => {
465+ (
466+ // TODO: Generate the actual source ordinal.
467+ Some ( 1 ) ,
468+ Some ( PrecommitData {
469+ scope_value,
470+ memoization_info : & memoization_info,
471+ } ) ,
472+ )
473+ }
474+ None => ( None , None ) ,
475+ } ;
476+ if value_builder. is_some ( ) {
477+ Some ( 1 )
478+ } else {
479+ None
480+ } ;
451481
452482 // Phase 2 (precommit): Update with the memoization info and stage target keys.
453483 let precommit_output = precommit_source_tracking_info (
454484 source_id,
455485 & source_key_json,
456486 source_ordinal,
457- memoization_info ,
487+ precommit_data ,
458488 & process_timestamp,
459489 & plan. tracking_table_setup ,
460- & scope_value,
461490 & plan. export_ops ,
462491 pool,
463492 )
0 commit comments