@@ -32,6 +32,7 @@ use datafusion_catalog::TableProvider;
3232use datafusion_common:: { config_err, DataFusionError , Result } ;
3333use datafusion_datasource:: file_scan_config:: { FileScanConfig , FileScanConfigBuilder } ;
3434use datafusion_datasource:: schema_adapter:: DefaultSchemaAdapterFactory ;
35+ use datafusion_execution:: config:: SessionConfig ;
3536use datafusion_expr:: dml:: InsertOp ;
3637use datafusion_expr:: { Expr , TableProviderFilterPushDown } ;
3738use datafusion_expr:: { SortExpr , TableType } ;
@@ -195,7 +196,8 @@ impl ListingTableConfig {
195196
196197 let listing_options = ListingOptions :: new ( file_format)
197198 . with_file_extension ( listing_file_extension)
198- . with_target_partitions ( state. config ( ) . target_partitions ( ) ) ;
199+ . with_target_partitions ( state. config ( ) . target_partitions ( ) )
200+ . with_collect_stat ( state. config ( ) . collect_statistics ( ) ) ;
199201
200202 Ok ( Self {
201203 table_paths : self . table_paths ,
@@ -313,18 +315,29 @@ impl ListingOptions {
313315 /// - use default file extension filter
314316 /// - no input partition to discover
315317 /// - one target partition
316- /// - stat collection
318+ /// - do not collect statistics
317319 pub fn new ( format : Arc < dyn FileFormat > ) -> Self {
318320 Self {
319321 file_extension : format. get_ext ( ) ,
320322 format,
321323 table_partition_cols : vec ! [ ] ,
322- collect_stat : true ,
324+ collect_stat : false ,
323325 target_partitions : 1 ,
324326 file_sort_order : vec ! [ ] ,
325327 }
326328 }
327329
330+ /// Set options from [`SessionConfig`] and returns self.
331+ ///
332+ /// Currently this sets `target_partitions` and `collect_stat`
333+ /// but if more options are added in the future that need to be coordinated
334+ /// they will be synchronized thorugh this method.
335+ pub fn with_session_config_options ( mut self , config : & SessionConfig ) -> Self {
336+ self = self . with_target_partitions ( config. target_partitions ( ) ) ;
337+ self = self . with_collect_stat ( config. collect_statistics ( ) ) ;
338+ self
339+ }
340+
328341 /// Set file extension on [`ListingOptions`] and returns self.
329342 ///
330343 /// # Example
@@ -1282,7 +1295,9 @@ mod tests {
12821295
12831296 #[ tokio:: test]
12841297 async fn read_single_file ( ) -> Result < ( ) > {
1285- let ctx = SessionContext :: new ( ) ;
1298+ let ctx = SessionContext :: new_with_config (
1299+ SessionConfig :: new ( ) . with_collect_statistics ( true ) ,
1300+ ) ;
12861301
12871302 let table = load_table ( & ctx, "alltypes_plain.parquet" ) . await ?;
12881303 let projection = None ;
@@ -1309,7 +1324,7 @@ mod tests {
13091324
13101325 #[ cfg( feature = "parquet" ) ]
13111326 #[ tokio:: test]
1312- async fn load_table_stats_by_default ( ) -> Result < ( ) > {
1327+ async fn do_not_load_table_stats_by_default ( ) -> Result < ( ) > {
13131328 use crate :: datasource:: file_format:: parquet:: ParquetFormat ;
13141329
13151330 let testdata = crate :: test_util:: parquet_test_data ( ) ;
@@ -1321,6 +1336,22 @@ mod tests {
13211336
13221337 let opt = ListingOptions :: new ( Arc :: new ( ParquetFormat :: default ( ) ) ) ;
13231338 let schema = opt. infer_schema ( & state, & table_path) . await ?;
1339+ let config = ListingTableConfig :: new ( table_path. clone ( ) )
1340+ . with_listing_options ( opt)
1341+ . with_schema ( schema) ;
1342+ let table = ListingTable :: try_new ( config) ?;
1343+
1344+ let exec = table. scan ( & state, None , & [ ] , None ) . await ?;
1345+ assert_eq ! ( exec. partition_statistics( None ) ?. num_rows, Precision :: Absent ) ;
1346+ // TODO correct byte size: https://github.com/apache/datafusion/issues/14936
1347+ assert_eq ! (
1348+ exec. partition_statistics( None ) ?. total_byte_size,
1349+ Precision :: Absent
1350+ ) ;
1351+
1352+ let opt = ListingOptions :: new ( Arc :: new ( ParquetFormat :: default ( ) ) )
1353+ . with_collect_stat ( true ) ;
1354+ let schema = opt. infer_schema ( & state, & table_path) . await ?;
13241355 let config = ListingTableConfig :: new ( table_path)
13251356 . with_listing_options ( opt)
13261357 . with_schema ( schema) ;
0 commit comments