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