Skip to content

Commit 24af834

Browse files
committed
fix(query): align profiles with physical plan scope
1 parent 6f74e46 commit 24af834

20 files changed

Lines changed: 293 additions & 19 deletions

src/common/base/src/runtime/profile/profile.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub struct Profile {
4747
pub plan_id: Option<u32>,
4848
pub plan_name: Option<String>,
4949
pub plan_parent_id: Option<u32>,
50+
pub plan_group_id: u64,
5051
pub labels: Arc<Vec<ProfileLabel>>,
5152
pub title: Arc<String>,
5253

@@ -65,6 +66,7 @@ impl Clone for Profile {
6566
plan_id: self.plan_id,
6667
plan_name: self.plan_name.clone(),
6768
plan_parent_id: self.plan_parent_id,
69+
plan_group_id: self.plan_group_id,
6870
labels: self.labels.clone(),
6971
title: self.title.clone(),
7072
metrics_registry: self.metrics_registry.clone(),
@@ -87,6 +89,7 @@ impl Profile {
8789
plan_id: Option<u32>,
8890
plan_name: Option<String>,
8991
plan_parent_id: Option<u32>,
92+
plan_group_id: u64,
9093
title: Arc<String>,
9194
labels: Arc<Vec<ProfileLabel>>,
9295
metrics_registry: Option<Arc<ScopedRegistry>>,
@@ -97,6 +100,7 @@ impl Profile {
97100
plan_id,
98101
plan_name,
99102
plan_parent_id,
103+
plan_group_id,
100104
title,
101105
labels,
102106
statistics: Self::create_items(),

src/query/pipeline/src/core/profile.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ pub struct PlanProfile {
9494
pub id: Option<u32>,
9595
pub name: Option<String>,
9696
pub parent_id: Option<u32>,
97+
#[serde(default)]
98+
pub group_id: u64,
9799
pub title: Arc<String>,
98100
pub labels: Arc<Vec<ProfileLabel>>,
99101

@@ -136,6 +138,7 @@ impl PlanProfile {
136138
id: profile.plan_id,
137139
name: profile.plan_name.clone(),
138140
parent_id: profile.plan_parent_id,
141+
group_id: profile.plan_group_id,
139142
title: profile.title.clone(),
140143
labels: profile.labels.clone(),
141144
metrics: BTreeMap::new(),
@@ -198,6 +201,8 @@ pub struct PlanScope {
198201
pub id: u32,
199202
pub name: String,
200203
pub parent_id: Option<u32>,
204+
/// The execution unit (physical plan tree) this scope belongs to. See `Profile::plan_group_id`.
205+
pub group_id: u64,
201206
pub title: Arc<String>,
202207
pub labels: Arc<Vec<ProfileLabel>>,
203208
pub metrics_registry: Arc<ScopedRegistry>,
@@ -211,6 +216,7 @@ impl PlanScope {
211216
pub fn create(
212217
id: u32,
213218
name: String,
219+
group_id: u64,
214220
title: Arc<String>,
215221
labels: Arc<Vec<ProfileLabel>>,
216222
) -> Arc<PlanScope> {
@@ -220,6 +226,7 @@ impl PlanScope {
220226
labels,
221227
title,
222228
parent_id,
229+
group_id,
223230
name,
224231
metrics_registry: ScopedRegistry::create(None),
225232
})

src/query/service/src/interpreters/interpreter_copy_into_location.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl CopyIntoLocationInterpreter {
105105

106106
let mut next_plan_id = 0;
107107
physical_plan.adjust_plan_id(&mut next_plan_id);
108+
physical_plan.assign_profile_group_id();
108109
Ok((
109110
build_query_pipeline_without_render_result_set(&self.ctx, &physical_plan).await?,
110111
update_stream_meta_req,

src/query/service/src/interpreters/interpreter_copy_into_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ impl CopyIntoTableInterpreter {
333333

334334
let mut next_plan_id = 0;
335335
root.adjust_plan_id(&mut next_plan_id);
336+
root.assign_profile_group_id();
336337

337338
Ok((root, update_stream_meta_reqs, new_schema))
338339
}

src/query/service/src/interpreters/interpreter_explain.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ use crate::sessions::QueryContext;
6767
use crate::sessions::TableContext;
6868
use crate::sessions::TableContextPartitionStats;
6969
use crate::sessions::TableContextQueryIdentity;
70-
use crate::sessions::TableContextQueryProfile;
7170
use crate::sessions::TableContextRuntimeFilter;
7271
use crate::sessions::TableContextSettings;
7372
use 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))

src/query/service/src/interpreters/interpreter_insert.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ impl Interpreter for InsertInterpreter {
259259
};
260260

261261
insert_select_plan.adjust_plan_id(&mut 0);
262+
insert_select_plan.assign_profile_group_id();
262263
let mut build_res =
263264
build_query_pipeline_without_render_result_set(&self.ctx, &insert_select_plan)
264265
.await?;

src/query/service/src/interpreters/interpreter_insert_multi_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ impl InsertMultiTableInterpreter {
243243

244244
let mut next_plan_id = 0;
245245
root.adjust_plan_id(&mut next_plan_id);
246+
root.assign_profile_group_id();
246247
Ok(root)
247248
}
248249

src/query/service/src/interpreters/interpreter_replace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl ReplaceInterpreter {
378378
});
379379

380380
root.adjust_plan_id(&mut 0);
381+
root.assign_profile_group_id();
381382
Ok((root, purge_info))
382383
}
383384

src/query/service/src/interpreters/interpreter_table_modify_column.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,7 @@ pub(crate) async fn build_select_insert_plan(
912912

913913
let mut index = 0;
914914
insert_plan.adjust_plan_id(&mut index);
915+
insert_plan.assign_profile_group_id();
915916
let mut build_res = build_query_pipeline_without_render_result_set(&ctx, &insert_plan).await?;
916917

917918
// 5. commit new meta schema and snapshots

src/query/service/src/interpreters/interpreter_table_recluster.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ impl ReclusterTableInterpreter {
259259
return Ok(true);
260260
};
261261
physical_plan.adjust_plan_id(&mut 0);
262+
physical_plan.assign_profile_group_id();
262263
let mut build_res =
263264
build_query_pipeline_without_render_result_set(&self.ctx, &physical_plan).await?;
264265
{

0 commit comments

Comments
 (0)