@@ -2548,4 +2548,114 @@ mod tests {
25482548
25492549 Ok ( ( ) )
25502550 }
2551+
2552+ #[ tokio:: test]
2553+ async fn test_schema_source_tracking ( ) -> Result < ( ) > {
2554+ let ctx = SessionContext :: new ( ) ;
2555+
2556+ let testdata = crate :: test_util:: datafusion_test_data ( ) ;
2557+ let filename = format ! ( "{testdata}/aggregate_simple.csv" ) ;
2558+ let table_path = ListingTableUrl :: parse ( filename) . unwrap ( ) ;
2559+
2560+ // Test default schema source
2561+ let config = ListingTableConfig :: new ( table_path. clone ( ) ) ;
2562+ assert_eq ! ( * config. schema_source( ) , SchemaSource :: None ) ;
2563+
2564+ // Test schema source after setting a schema explicitly
2565+ let provided_schema = Arc :: new ( Schema :: new ( vec ! [
2566+ Field :: new( "c1" , DataType :: Float32 , true ) ,
2567+ Field :: new( "c2" , DataType :: Float64 , true ) ,
2568+ Field :: new( "c3" , DataType :: Boolean , true ) ,
2569+ Field :: new( "c4" , DataType :: Utf8 , true ) ,
2570+ ] ) ) ;
2571+
2572+ let config = config. with_schema ( provided_schema. clone ( ) ) ;
2573+ assert_eq ! ( * config. schema_source( ) , SchemaSource :: Specified ) ;
2574+
2575+ // Test schema source after inferring schema
2576+ let format = csv:: CsvFormat :: default ( ) . with_schema ( None ) ;
2577+ let options = ListingOptions :: new ( Arc :: new ( format) ) ;
2578+ let config_without_schema =
2579+ ListingTableConfig :: new ( table_path) . with_listing_options ( options) ;
2580+ assert_eq ! ( * config_without_schema. schema_source( ) , SchemaSource :: None ) ;
2581+
2582+ let config_with_inferred =
2583+ config_without_schema. infer_schema ( & ctx. state ( ) ) . await ?;
2584+ assert_eq ! (
2585+ * config_with_inferred. schema_source( ) ,
2586+ SchemaSource :: Inferred
2587+ ) ;
2588+
2589+ Ok ( ( ) )
2590+ }
2591+
2592+ #[ tokio:: test]
2593+ async fn test_schema_source_in_listing_table ( ) -> Result < ( ) > {
2594+ let ctx = SessionContext :: new ( ) ;
2595+ let testdata = crate :: test_util:: datafusion_test_data ( ) ;
2596+ let filename = format ! ( "{testdata}/aggregate_simple.csv" ) ;
2597+ let table_path = ListingTableUrl :: parse ( filename) . unwrap ( ) ;
2598+
2599+ // Create a table with specified schema
2600+ let specified_schema = Arc :: new ( Schema :: new ( vec ! [
2601+ Field :: new( "c1" , DataType :: Float32 , true ) ,
2602+ Field :: new( "c2" , DataType :: Float64 , true ) ,
2603+ Field :: new( "c3" , DataType :: Boolean , true ) ,
2604+ Field :: new( "c4" , DataType :: Utf8 , true ) ,
2605+ ] ) ) ;
2606+
2607+ let format = csv:: CsvFormat :: default ( ) . with_schema ( None ) ;
2608+ let options = ListingOptions :: new ( Arc :: new ( format) ) ;
2609+
2610+ let config_specified = ListingTableConfig :: new ( table_path. clone ( ) )
2611+ . with_listing_options ( options. clone ( ) )
2612+ . with_schema ( specified_schema) ;
2613+
2614+ let table_specified = ListingTable :: try_new ( config_specified) ?;
2615+ assert_eq ! ( * table_specified. schema_source( ) , SchemaSource :: Specified ) ;
2616+
2617+ // Create a table with inferred schema
2618+ let config_to_infer =
2619+ ListingTableConfig :: new ( table_path) . with_listing_options ( options) ;
2620+
2621+ let config_inferred = config_to_infer. infer_schema ( & ctx. state ( ) ) . await ?;
2622+ let table_inferred = ListingTable :: try_new ( config_inferred) ?;
2623+ assert_eq ! ( * table_inferred. schema_source( ) , SchemaSource :: Inferred ) ;
2624+
2625+ Ok ( ( ) )
2626+ }
2627+
2628+ #[ tokio:: test]
2629+ async fn test_schema_source_preserved_through_config_operations ( ) -> Result < ( ) > {
2630+ let ctx = SessionContext :: new ( ) ;
2631+ let testdata = crate :: test_util:: datafusion_test_data ( ) ;
2632+ let filename = format ! ( "{testdata}/aggregate_simple.csv" ) ;
2633+ let table_path = ListingTableUrl :: parse ( filename) . unwrap ( ) ;
2634+
2635+ // Start with a specified schema
2636+ let specified_schema = Arc :: new ( Schema :: new ( vec ! [
2637+ Field :: new( "c1" , DataType :: Float32 , true ) ,
2638+ Field :: new( "c2" , DataType :: Float64 , true ) ,
2639+ Field :: new( "c3" , DataType :: Boolean , true ) ,
2640+ Field :: new( "c4" , DataType :: Utf8 , true ) ,
2641+ ] ) ) ;
2642+
2643+ let config =
2644+ ListingTableConfig :: new ( table_path. clone ( ) ) . with_schema ( specified_schema) ;
2645+
2646+ assert_eq ! ( * config. schema_source( ) , SchemaSource :: Specified ) ;
2647+
2648+ // Make sure source is preserved after adding options
2649+ let format = csv:: CsvFormat :: default ( ) . with_schema ( None ) ;
2650+ let options = ListingOptions :: new ( Arc :: new ( format) ) ;
2651+ let config = config. with_listing_options ( options) ;
2652+
2653+ assert_eq ! ( * config. schema_source( ) , SchemaSource :: Specified ) ;
2654+
2655+ // Make sure inferred schema doesn't override specified schema
2656+ let config = config. infer ( & ctx. state ( ) ) . await ?;
2657+ assert_eq ! ( * config. schema_source( ) , SchemaSource :: Specified ) ;
2658+
2659+ Ok ( ( ) )
2660+ }
25512661}
0 commit comments