@@ -80,16 +80,7 @@ pub fn validate(def: RawModuleDefV9) -> Result<ModuleDef> {
8080 . combine_errors ( )
8181 . and_then ( |( mut tables, types, reducers) | {
8282 let sched_exists = check_scheduled_reducers_exist ( & tables, & reducers) ;
83- let default_values_work = misc_exports
84- . into_iter ( )
85- . map ( |export| match export {
86- RawMiscModuleExportV9 :: ColumnDefaultValue ( fdv) => {
87- validator. validate_column_default_value ( & mut tables, fdv)
88- }
89- _ => unimplemented ! ( "unknown misc export" ) ,
90- } )
91- . collect_all_errors :: < ( ) > ( ) ;
92-
83+ let default_values_work = default_values_work ( misc_exports, & mut validator, & mut tables) ;
9384 ( sched_exists, default_values_work) . combine_errors ( ) ?;
9485
9586 Ok ( ( tables, types, reducers) )
@@ -363,44 +354,44 @@ impl ModuleValidator<'_> {
363354 ) -> Result < ( ) > {
364355 let table_name = identifier ( cdv. table . clone ( ) ) ?;
365356
357+ // Extract the table. We cannot make progress otherwise.
366358 let table = tables
367359 . get_mut ( & table_name)
368360 . ok_or_else ( || ValidationError :: TableNotFound {
369361 table : cdv. table . clone ( ) ,
370362 } ) ?;
371363
372- if table. columns . len ( ) <= cdv. col_id . idx ( ) {
364+ // Get the column that a default is being added to.
365+ let Some ( col) = table. columns . get_mut ( cdv. col_id . idx ( ) ) else {
373366 return Err ( ValidationError :: ColumnNotFound {
374367 table : cdv. table . clone ( ) ,
375368 def : cdv. table . clone ( ) ,
376369 column : cdv. col_id ,
377370 }
378371 . into ( ) ) ;
379- }
372+ } ;
380373
381- let col = & mut table. columns [ cdv. col_id . idx ( ) ] ;
382- // First time we have a type for it, so decode it.
374+ // First time the type of the default value is known, so decode it.
383375 let mut reader = & cdv. value [ ..] ;
384376 let ty = WithTypespace :: new ( self . typespace , & col. ty ) ;
385- let field_value = match ty. deserialize ( Deserializer :: new ( & mut reader) ) {
386- Ok ( field_value) => Some ( field_value) ,
387- Err ( decode_error) => {
388- return Err ( ValidationError :: ColumnDefaultValueMalformed {
377+ let field_value: Result < AlgebraicValue > =
378+ ty. deserialize ( Deserializer :: new ( & mut reader) ) . map_err ( |decode_error| {
379+ ValidationError :: ColumnDefaultValueMalformed {
389380 table : cdv. table . clone ( ) ,
390381 col_id : cdv. col_id ,
391382 err : decode_error,
392383 }
393- . into ( ) )
394- }
395- } ;
384+ . into ( )
385+ } ) ;
386+ // Ensure there's only one default value.
396387 if col. default_value . is_some ( ) {
397388 return Err ( ValidationError :: MultipleColumnDefaultValues {
398389 table : cdv. table . clone ( ) ,
399390 col_id : cdv. col_id ,
400391 }
401392 . into ( ) ) ;
402393 }
403- col. default_value = field_value. clone ( ) ;
394+ col. default_value = field_value. ok ( ) ;
404395
405396 Ok ( ( ) )
406397 }
@@ -953,6 +944,20 @@ fn check_scheduled_reducers_exist(
953944 . collect_all_errors ( )
954945}
955946
947+ fn default_values_work (
948+ misc_exports : Vec < RawMiscModuleExportV9 > ,
949+ validator : & mut ModuleValidator ,
950+ tables : & mut IdentifierMap < TableDef > ,
951+ ) -> Result < ( ) > {
952+ misc_exports
953+ . into_iter ( )
954+ . map ( |export| match export {
955+ RawMiscModuleExportV9 :: ColumnDefaultValue ( fdv) => validator. validate_column_default_value ( tables, fdv) ,
956+ _ => unimplemented ! ( "unknown misc export" ) ,
957+ } )
958+ . collect_all_errors :: < ( ) > ( )
959+ }
960+
956961#[ cfg( test) ]
957962mod tests {
958963 use crate :: def:: validate:: tests:: {
0 commit comments