@@ -67,7 +67,6 @@ use crate::sessions::QueryContext;
6767use crate :: sessions:: TableContext ;
6868use crate :: sessions:: TableContextPartitionStats ;
6969use crate :: sessions:: TableContextQueryIdentity ;
70- use crate :: sessions:: TableContextQueryProfile ;
7170use crate :: sessions:: TableContextRuntimeFilter ;
7271use crate :: sessions:: TableContextSettings ;
7372use crate :: sql:: optimizer:: ir:: SExpr ;
@@ -460,10 +459,11 @@ impl ExplainInterpreter {
460459
461460 let mut builder = PhysicalPlanBuilder :: new ( metadata. clone ( ) , self . ctx . clone ( ) , true ) ;
462461 let plan = builder. build ( s_expr, required) . await ?;
462+ let main_group_id = plan. get_meta ( ) . profile_group_id ;
463463 let build_res = build_query_pipeline ( & self . ctx , & [ ] , & plan, ignore_result) . await ?;
464464
465465 // Drain the data
466- let query_profiles = self . execute_and_get_profiles ( build_res) . await ?;
466+ let query_profiles = self . execute_and_get_profiles ( build_res, main_group_id ) . await ?;
467467
468468 Ok ( GraphicalProfiles {
469469 query_id : query_ctx. get_id ( ) ,
@@ -485,10 +485,11 @@ impl ExplainInterpreter {
485485 builder. set_mutation_build_info ( build_info) ;
486486 }
487487 let mut plan = builder. build ( s_expr, required) . await ?;
488+ let main_group_id = plan. get_meta ( ) . profile_group_id ;
488489 let build_res = build_query_pipeline ( & self . ctx , & [ ] , & plan, ignore_result) . await ?;
489490
490491 // Drain the data
491- let query_profiles = self . execute_and_get_profiles ( build_res) . await ?;
492+ let query_profiles = self . execute_and_get_profiles ( build_res, main_group_id ) . await ?;
492493
493494 let mut pruned_partitions_stats = self . ctx . get_pruned_partitions_stats ( ) ;
494495 if !pruned_partitions_stats. is_empty ( ) {
@@ -541,6 +542,7 @@ impl ExplainInterpreter {
541542 async fn execute_and_get_profiles (
542543 & self ,
543544 mut build_res : PipelineBuildResult ,
545+ main_group_id : u64 ,
544546 ) -> Result < HashMap < u32 , PlanProfile > > {
545547 let settings = self . ctx . get_settings ( ) ;
546548 build_res. set_max_threads ( settings. get_max_threads ( ) ? as usize ) ;
@@ -563,9 +565,12 @@ impl ExplainInterpreter {
563565 while ( executor. pull_data ( ) . await ?) . is_some ( ) { }
564566 }
565567 }
568+ // EXPLAIN ANALYZE renders the main query plan tree only and looks up each node by its
569+ // original plan id. Restrict to the main plan's profile group so that profiles from other
570+ // execution units (e.g. materialized CTE fills) with overlapping plan ids are not matched.
566571 Ok ( self
567572 . ctx
568- . get_query_profiles ( )
573+ . get_query_profiles_by_group ( main_group_id )
569574 . into_iter ( )
570575 . filter ( |x| x. id . is_some ( ) )
571576 . map ( |x| ( x. id . unwrap ( ) , x) )
0 commit comments