@@ -33,6 +33,7 @@ use tokio::task::JoinHandle;
3333
3434use  super :: creator:: { TableDefinition ,  TableManager ,  ViewCreator } ; 
3535use  super :: { to_datafusion_error,  RelationName } ; 
36+ use  super :: write_settings:: DuckDBWriteSettings ; 
3637
3738/// A callback handler that is invoked after data has been successfully written to a DuckDB table 
3839/// but before the transaction is committed. 
@@ -61,6 +62,7 @@ pub struct DuckDBTableWriterBuilder {
6162    on_conflict :  Option < OnConflict > , 
6263    table_definition :  Option < Arc < TableDefinition > > , 
6364    on_data_written :  Option < WriteCompletionHandler > , 
65+     write_settings :  Option < DuckDBWriteSettings > , 
6466} 
6567
6668impl  DuckDBTableWriterBuilder  { 
@@ -99,6 +101,12 @@ impl DuckDBTableWriterBuilder {
99101        self 
100102    } 
101103
104+     #[ must_use]  
105+     pub  fn  with_write_settings ( mut  self ,  write_settings :  DuckDBWriteSettings )  -> Self  { 
106+         self . write_settings  = Some ( write_settings) ; 
107+         self 
108+     } 
109+ 
102110    /// Builds a `DuckDBTableWriter` from the provided configuration. 
103111/// 
104112/// # Errors 
@@ -126,6 +134,7 @@ impl DuckDBTableWriterBuilder {
126134            table_definition, 
127135            pool, 
128136            on_data_written :  self . on_data_written , 
137+             write_settings :  self . write_settings . unwrap_or_default ( ) , 
129138        } ) 
130139    } 
131140} 
@@ -137,6 +146,7 @@ pub struct DuckDBTableWriter {
137146    table_definition :  Arc < TableDefinition > , 
138147    on_conflict :  Option < OnConflict > , 
139148    on_data_written :  Option < WriteCompletionHandler > , 
149+     write_settings :  DuckDBWriteSettings , 
140150} 
141151
142152impl  std:: fmt:: Debug  for  DuckDBTableWriter  { 
@@ -153,6 +163,7 @@ impl std::fmt::Debug for DuckDBTableWriter {
153163                    . as_ref ( ) 
154164                    . map_or ( "None" ,  |_| "Some(callback)" ) , 
155165            ) 
166+             . field ( "write_settings" ,  & self . write_settings ) 
156167            . finish ( ) 
157168    } 
158169} 
@@ -173,6 +184,11 @@ impl DuckDBTableWriter {
173184        self . on_conflict . as_ref ( ) 
174185    } 
175186
187+     #[ must_use]  
188+     pub  fn  write_settings ( & self )  -> & DuckDBWriteSettings  { 
189+         & self . write_settings 
190+     } 
191+ 
176192    #[ must_use]  
177193    pub  fn  with_on_data_written_handler ( mut  self ,  on_data_written :  WriteCompletionHandler )  -> Self  { 
178194        self . on_data_written  = Some ( on_data_written) ; 
@@ -222,7 +238,8 @@ impl TableProvider for DuckDBTableWriter {
222238            overwrite, 
223239            self . on_conflict . clone ( ) , 
224240            self . schema ( ) , 
225-         ) ; 
241+         ) 
242+         . with_write_settings ( self . write_settings . clone ( ) ) ; 
226243
227244        if  let  Some ( handler)  = & self . on_data_written  { 
228245            sink = sink. with_on_data_written_handler ( Arc :: clone ( handler) ) ; 
@@ -239,6 +256,7 @@ pub(crate) struct DuckDBDataSink {
239256    on_conflict :  Option < OnConflict > , 
240257    schema :  SchemaRef , 
241258    on_data_written :  Option < WriteCompletionHandler > , 
259+     write_settings :  DuckDBWriteSettings , 
242260} 
243261
244262#[ async_trait]  
@@ -265,6 +283,7 @@ impl DataSink for DuckDBDataSink {
265283        let  overwrite = self . overwrite ; 
266284        let  on_conflict = self . on_conflict . clone ( ) ; 
267285        let  on_data_written = self . on_data_written . clone ( ) ; 
286+         let  write_settings = self . write_settings . clone ( ) ; 
268287
269288        // Limit channel size to a maximum of 100 RecordBatches queued for cases when DuckDB is slower than the writer stream, 
270289        // so that we don't significantly increase memory usage. After the maximum RecordBatches are queued, the writer stream will wait 
@@ -287,6 +306,7 @@ impl DataSink for DuckDBDataSink {
287306                        on_data_written. as_ref ( ) , 
288307                        on_commit_transaction, 
289308                        schema, 
309+                         & write_settings, 
290310                    ) ?, 
291311                    InsertOp :: Append  | InsertOp :: Replace  => insert_append ( 
292312                        pool, 
@@ -296,6 +316,7 @@ impl DataSink for DuckDBDataSink {
296316                        on_data_written. as_ref ( ) , 
297317                        on_commit_transaction, 
298318                        schema, 
319+                         & write_settings, 
299320                    ) ?, 
300321                } ; 
301322
@@ -379,6 +400,7 @@ impl DuckDBDataSink {
379400            on_conflict, 
380401            schema, 
381402            on_data_written :  None , 
403+             write_settings :  DuckDBWriteSettings :: default ( ) , 
382404        } 
383405    } 
384406
@@ -387,6 +409,12 @@ impl DuckDBDataSink {
387409        self . on_data_written  = Some ( handler) ; 
388410        self 
389411    } 
412+ 
413+     #[ must_use]  
414+     pub  fn  with_write_settings ( mut  self ,  write_settings :  DuckDBWriteSettings )  -> Self  { 
415+         self . write_settings  = write_settings; 
416+         self 
417+     } 
390418} 
391419
392420impl  std:: fmt:: Debug  for  DuckDBDataSink  { 
@@ -401,6 +429,7 @@ impl DisplayAs for DuckDBDataSink {
401429    } 
402430} 
403431
432+ #[ allow( clippy:: too_many_arguments) ]  
404433fn  insert_append ( 
405434    pool :  Arc < DuckDbConnectionPool > , 
406435    table_definition :  & Arc < TableDefinition > , 
@@ -409,6 +438,7 @@ fn insert_append(
409438    on_data_written :  Option < & WriteCompletionHandler > , 
410439    mut  on_commit_transaction :  tokio:: sync:: oneshot:: Receiver < ( ) > , 
411440    schema :  SchemaRef , 
441+     write_settings :  & DuckDBWriteSettings , 
412442)  -> datafusion:: common:: Result < u64 >  { 
413443    let  mut  db_conn = pool
414444        . connect_sync ( ) 
@@ -466,7 +496,9 @@ fn insert_append(
466496        callback ( & tx,  & append_table,  & schema,  num_rows) ?; 
467497    } 
468498
469-     execute_analyze_sql ( & tx,  & append_table. table_name ( ) . to_string ( ) ) ; 
499+     if  write_settings. recompute_statistics_on_refresh  { 
500+         execute_analyze_sql ( & tx,  & append_table. table_name ( ) . to_string ( ) ) ; 
501+     } 
470502
471503    on_commit_transaction
472504        . try_recv ( ) 
@@ -521,6 +553,7 @@ fn insert_append(
521553} 
522554
523555#[ allow( clippy:: too_many_lines) ]  
556+ #[ allow( clippy:: too_many_arguments) ]  
524557fn  insert_overwrite ( 
525558    pool :  Arc < DuckDbConnectionPool > , 
526559    table_definition :  & Arc < TableDefinition > , 
@@ -529,6 +562,7 @@ fn insert_overwrite(
529562    on_data_written :  Option < & WriteCompletionHandler > , 
530563    mut  on_commit_transaction :  tokio:: sync:: oneshot:: Receiver < ( ) > , 
531564    schema :  SchemaRef , 
565+     write_settings :  & DuckDBWriteSettings , 
532566)  -> datafusion:: common:: Result < u64 >  { 
533567    let  cloned_pool = Arc :: clone ( & pool) ; 
534568    let  mut  db_conn = pool
@@ -642,7 +676,9 @@ fn insert_overwrite(
642676        callback ( & tx,  & new_table,  & schema,  num_rows) ?; 
643677    } 
644678
645-     execute_analyze_sql ( & tx,  & new_table. table_name ( ) . to_string ( ) ) ; 
679+     if  write_settings. recompute_statistics_on_refresh  { 
680+         execute_analyze_sql ( & tx,  & new_table. table_name ( ) . to_string ( ) ) ; 
681+     } 
646682
647683    tx. commit ( ) 
648684        . context ( super :: UnableToCommitTransactionSnafu ) 
0 commit comments