@@ -295,44 +295,17 @@ pub async fn optimize(mut opt_ctx: OptimizerContext, plan: Plan) -> Result<Plan>
295
295
overwrite,
296
296
forbid_occ_retry,
297
297
} => {
298
- let support_distributed_insert = {
299
- let append: Append = s_expr. plan ( ) . clone ( ) . try_into ( ) ?;
300
- let metadata = metadata. read ( ) ;
301
- metadata
302
- . table ( append. table_index )
303
- . table ( )
304
- . support_distributed_insert ( )
305
- } ;
306
- let enable_distributed = opt_ctx. enable_distributed_optimization
307
- && opt_ctx
308
- . table_ctx
309
- . get_settings ( )
310
- . get_enable_distributed_copy ( ) ?
311
- && support_distributed_insert;
312
- info ! (
313
- "after optimization enable_distributed_copy? : {}" ,
314
- enable_distributed
315
- ) ;
316
- let mut optimized_source =
317
- optimize_query ( & mut opt_ctx, s_expr. child ( 0 ) ?. clone ( ) ) . await ?;
318
- let optimized = match enable_distributed {
319
- true => {
320
- if let RelOperator :: Exchange ( Exchange :: Merge ) = optimized_source. plan . as_ref ( ) {
321
- optimized_source = optimized_source. child ( 0 ) . unwrap ( ) . clone ( ) ;
322
- }
323
- let copy_into = SExpr :: create_unary (
324
- Arc :: new ( s_expr. plan ( ) . clone ( ) ) ,
325
- Arc :: new ( optimized_source) ,
326
- ) ;
327
- let exchange = Arc :: new ( RelOperator :: Exchange ( Exchange :: Merge ) ) ;
328
- SExpr :: create_unary ( exchange, Arc :: new ( copy_into) )
329
- }
330
- false => {
331
- SExpr :: create_unary ( Arc :: new ( s_expr. plan ( ) . clone ( ) ) , Arc :: new ( optimized_source) )
332
- }
333
- } ;
298
+ let append: Append = s_expr. plan ( ) . clone ( ) . try_into ( ) ?;
299
+ let source = s_expr. child ( 0 ) ?. clone ( ) ;
300
+ let optimized_source = optimize_query ( & mut opt_ctx, source) . await ?;
301
+ let optimized_append = optimize_append (
302
+ append,
303
+ optimized_source,
304
+ metadata. clone ( ) ,
305
+ opt_ctx. table_ctx . as_ref ( ) ,
306
+ ) ?;
334
307
Ok ( Plan :: Append {
335
- s_expr : Box :: new ( optimized ) ,
308
+ s_expr : Box :: new ( optimized_append ) ,
336
309
metadata,
337
310
stage_table_info,
338
311
overwrite,
@@ -610,3 +583,34 @@ async fn optimize_mutation(mut opt_ctx: OptimizerContext, s_expr: SExpr) -> Resu
610
583
metadata : opt_ctx. metadata . clone ( ) ,
611
584
} )
612
585
}
586
+
587
+ pub fn optimize_append (
588
+ append : Append ,
589
+ source : SExpr ,
590
+ metadata : MetadataRef ,
591
+ table_ctx : & dyn TableContext ,
592
+ ) -> Result < SExpr > {
593
+ let support_distributed_insert = {
594
+ let metadata = metadata. read ( ) ;
595
+ metadata
596
+ . table ( append. table_index )
597
+ . table ( )
598
+ . support_distributed_insert ( )
599
+ } ;
600
+ let enable_distributed = table_ctx. get_settings ( ) . get_enable_distributed_copy ( ) ?
601
+ && support_distributed_insert
602
+ && matches ! ( source. plan( ) , RelOperator :: Exchange ( Exchange :: Merge ) ) ;
603
+ info ! (
604
+ "after optimization enable_distributed_copy? : {}" ,
605
+ enable_distributed
606
+ ) ;
607
+ match enable_distributed {
608
+ true => {
609
+ let source = source. child ( 0 ) . unwrap ( ) . clone ( ) ;
610
+ let copy_into = SExpr :: create_unary ( Arc :: new ( append. into ( ) ) , Arc :: new ( source) ) ;
611
+ let exchange = Arc :: new ( RelOperator :: Exchange ( Exchange :: Merge ) ) ;
612
+ Ok ( SExpr :: create_unary ( exchange, Arc :: new ( copy_into) ) )
613
+ }
614
+ false => Ok ( SExpr :: create_unary ( Arc :: new ( append. into ( ) ) , Arc :: new ( source) ) ) ,
615
+ }
616
+ }
0 commit comments