@@ -472,4 +472,131 @@ mod tests {
472472
473473        assert ! ( parsed. is_err( ) ) ; 
474474    } 
475+ 
476+     #[ test]  
477+     fn  updates_field_type_to_timestamp_for_time_related_fields ( )  { 
478+         let  fields = Fields :: from ( vec ! [ Field :: new( "created_time" ,  DataType :: Utf8 ,  true ) ] ) ; 
479+         let  mut  schema = Schema :: new ( fields) ; 
480+ 
481+         // Create a JSON log record with a time field in RFC3339 format 
482+         let  log_record = serde_json:: json!( { 
483+             "created_time" :  "2023-01-01T12:00:00Z" 
484+         } ) ; 
485+ 
486+         // Call the function to override the inferred data type 
487+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V1 ) ; 
488+ 
489+         // Check that the field type was updated to Timestamp 
490+         let  updated_field = schema. field ( 0 ) ; 
491+         assert_eq ! ( 
492+             updated_field. data_type( ) , 
493+             & DataType :: Timestamp ( TimeUnit :: Millisecond ,  None ) 
494+         ) ; 
495+         assert_eq ! ( updated_field. name( ) ,  "created_time" ) ; 
496+     } 
497+ 
498+     #[ test]  
499+     fn  update_field_type_to_timestamp_for_rfc2822_format ( )  { 
500+         let  fields = Fields :: from ( vec ! [ Field :: new( "event_time" ,  DataType :: Utf8 ,  true ) ] ) ; 
501+         let  mut  schema = Schema :: new ( fields) ; 
502+ 
503+         // Create a JSON log record with a time field in RFC2822 format 
504+         let  log_record = serde_json:: json!( { 
505+             "event_time" :  "Wed, 02 Oct 2002 15:00:00 +0200" 
506+         } ) ; 
507+ 
508+         // Call the function to override the inferred data type 
509+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V1 ) ; 
510+ 
511+         // Check that the field type was updated to Timestamp 
512+         let  updated_field = schema. field ( 0 ) ; 
513+         assert_eq ! ( 
514+             updated_field. data_type( ) , 
515+             & DataType :: Timestamp ( TimeUnit :: Millisecond ,  None ) 
516+         ) ; 
517+         assert_eq ! ( updated_field. name( ) ,  "event_time" ) ; 
518+     } 
519+ 
520+     #[ test]  
521+     fn  update_numeric_fields_to_float64 ( )  { 
522+         let  fields = Fields :: from ( vec ! [ Field :: new( "numeric_field" ,  DataType :: Int32 ,  true ) ] ) ; 
523+         let  mut  schema = Schema :: new ( fields) ; 
524+ 
525+         // Create a JSON log record with a numeric field 
526+         let  log_record = serde_json:: json!( { 
527+             "numeric_field" :  42 
528+         } ) ; 
529+ 
530+         // Call the function to override the inferred data type 
531+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V1 ) ; 
532+ 
533+         // Check that the field type was updated to Float64 
534+         let  updated_field = schema. field ( 0 ) ; 
535+         assert_eq ! ( updated_field. data_type( ) ,  & DataType :: Float64 ) ; 
536+         assert_eq ! ( updated_field. name( ) ,  "numeric_field" ) ; 
537+     } 
538+ 
539+     #[ test]  
540+     fn  handle_non_standard_time_strings ( )  { 
541+         let  fields = Fields :: from ( vec ! [ Field :: new( "event_time" ,  DataType :: Utf8 ,  true ) ] ) ; 
542+         let  mut  schema = Schema :: new ( fields) ; 
543+ 
544+         // Create a JSON log record with a non-standard time format 
545+         let  log_record = serde_json:: json!( { 
546+             "event_time" :  "01-01-2023 12:00:00" 
547+         } ) ; 
548+ 
549+         // Call the function to override the inferred data type 
550+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V1 ) ; 
551+ 
552+         // Check that the field type was not updated to Timestamp 
553+         let  updated_field = schema. field ( 0 ) ; 
554+         assert_eq ! ( updated_field. data_type( ) ,  & DataType :: Utf8 ) ; 
555+         assert_eq ! ( updated_field. name( ) ,  "event_time" ) ; 
556+     } 
557+ 
558+     #[ test]  
559+     fn  handles_numeric_fields_already_float64 ( )  { 
560+         let  fields = Fields :: from ( vec ! [ Field :: new( "numeric_value" ,  DataType :: Float64 ,  true ) ] ) ; 
561+         let  mut  schema = Schema :: new ( fields) ; 
562+ 
563+         // Create a JSON log record with a numeric field 
564+         let  log_record = serde_json:: json!( { 
565+             "numeric_value" :  42.0 
566+         } ) ; 
567+ 
568+         // Call the function to override the inferred data type 
569+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V1 ) ; 
570+ 
571+         // Check that the field type remains Float64 
572+         let  updated_field = schema. field ( 0 ) ; 
573+         assert_eq ! ( updated_field. data_type( ) ,  & DataType :: Float64 ) ; 
574+         assert_eq ! ( updated_field. name( ) ,  "numeric_value" ) ; 
575+     } 
576+ 
577+     #[ test]  
578+     fn  does_not_update_field_type_for_v0_schema_version ( )  { 
579+         let  fields = Fields :: from ( vec ! [ 
580+             Field :: new( "event_time" ,  DataType :: Utf8 ,  true ) , 
581+             Field :: new( "numeric_field" ,  DataType :: Int32 ,  true ) , 
582+         ] ) ; 
583+         let  mut  schema = Schema :: new ( fields) ; 
584+ 
585+         // Create a JSON log record with a string field 
586+         let  log_record = serde_json:: json!( { 
587+             "event_time" :  "01-01-2023 12:00:00" , 
588+             "numeric_field" :  42 
589+         } ) ; 
590+ 
591+         // Call the function to override the inferred data type with a non-V1 schema version 
592+         override_inferred_data_type ( & mut  schema,  & log_record,  SchemaVersion :: V0 ) ; 
593+ 
594+         // Check that the field type was not updated 
595+         let  updated_field = schema. field ( 0 ) ; 
596+         assert_eq ! ( updated_field. data_type( ) ,  & DataType :: Utf8 ) ; 
597+         assert_eq ! ( updated_field. name( ) ,  "event_time" ) ; 
598+         let  updated_field = schema. field ( 1 ) ; 
599+         assert_eq ! ( updated_field. data_type( ) ,  & DataType :: Int32 ) ; 
600+         assert_eq ! ( updated_field. name( ) ,  "numeric_field" ) ; 
601+     } 
475602} 
0 commit comments