-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
EXPLAIN ANALYZE
to profile queries (#849)
Signed-off-by: Runji Wang <[email protected]> Inspired by duckdb's [EXPLAIN ANALYZE](https://duckdb.org/docs/guides/meta/explain_analyze.html). <details> <summary>Example of TPCH Q6</summary> ```sql explain analyze select sum(l_extendedprice * l_discount) as revenue from lineitem where l_shipdate >= date '1994-01-01' and l_shipdate < date '1994-01-01' + interval '1' year and l_discount between 0.08 - 0.01 and 0.08 + 0.01 and l_quantity < 24; Projection ├── exprs:ref │ └── sum │ └── * { lhs: l_discount, rhs: l_extendedprice } ├── rows: 1 ├── time: 91.042µs └── Agg ├── aggs:sum │ └── * { lhs: l_discount, rhs: l_extendedprice } ├── rows: 1 ├── time: 4.525204ms └── Filter ├── cond: and { lhs: >= { lhs: 0.09, rhs: l_discount }, rhs: >= { lhs: l_discount, rhs: 0.07 } } ├── rows: 113920 ├── time: 14.407315ms └── Projection { exprs: [ l_extendedprice, l_discount ], rows: 417809, time: 2.277959ms } └── Filter ├── cond:and │ ├── lhs: > { lhs: 24, rhs: l_quantity } │ └── rhs:and │ ├── lhs: > { lhs: 1995-01-01, rhs: l_shipdate } │ └── rhs: >= { lhs: l_shipdate, rhs: 1994-01-01 } ├── rows: 417809 ├── time: 69.414093ms └── Scan ├── table: lineitem ├── list: [ l_quantity, l_extendedprice, l_discount, l_shipdate ] ├── filter: true ├── rows: 6001215 └── time: 289.72746ms ``` </details> --------- Signed-off-by: Runji Wang <[email protected]>
- Loading branch information
1 parent
50b2556
commit 8b19a09
Showing
10 changed files
with
318 additions
and
105 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2024 RisingLight Project Authors. Licensed under Apache-2.0. | ||
|
||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
||
use pretty_xmlish::PrettyConfig; | ||
|
||
use super::*; | ||
use crate::array::{ArrayImpl, StringArray}; | ||
use crate::planner::Explain; | ||
|
||
/// Run the query and return the query plan with profiling information. | ||
pub struct AnalyzeExecutor { | ||
pub plan: RecExpr, | ||
pub catalog: RootCatalogRef, | ||
pub metrics: Metrics, | ||
} | ||
|
||
impl AnalyzeExecutor { | ||
#[try_stream(boxed, ok = DataChunk, error = ExecutorError)] | ||
pub async fn execute(self, child: BoxedExecutor) { | ||
// consume the child stream | ||
#[for_await] | ||
for chunk in child { | ||
_ = chunk?; | ||
} | ||
|
||
// explain the plan | ||
let get_metadata = |id| { | ||
vec![ | ||
("rows", self.metrics.get_rows(id).to_string()), | ||
("time", format!("{:?}", self.metrics.get_time(id))), | ||
] | ||
}; | ||
let explain_obj = Explain::of(&self.plan) | ||
.with_catalog(&self.catalog) | ||
.with_metadata(&get_metadata); | ||
let explainer = explain_obj.pretty(); | ||
let mut explain = String::with_capacity(4096); | ||
let mut config = PrettyConfig { | ||
need_boundaries: false, | ||
..PrettyConfig::default() | ||
}; | ||
config.unicode(&mut explain, &explainer); | ||
let chunk = DataChunk::from_iter([ArrayImpl::new_string(StringArray::from_iter([Some( | ||
explain, | ||
)]))]); | ||
|
||
yield chunk; | ||
} | ||
} | ||
|
||
/// A collection of profiling information for a query. | ||
#[derive(Default)] | ||
pub struct Metrics { | ||
spans: HashMap<Id, TimeSpan>, | ||
rows: HashMap<Id, Counter>, | ||
} | ||
|
||
impl Metrics { | ||
/// Register metrics for a node. | ||
pub fn register(&mut self, id: Id, span: TimeSpan, rows: Counter) { | ||
self.spans.insert(id, span); | ||
self.rows.insert(id, rows); | ||
} | ||
|
||
/// Get the running time for a node. | ||
pub fn get_time(&self, id: Id) -> Duration { | ||
self.spans.get(&id).map(|span| span.busy_time()).unwrap() | ||
} | ||
|
||
/// Get the number of rows produced by a node. | ||
pub fn get_rows(&self, id: Id) -> u64 { | ||
self.rows.get(&id).map(|rows| rows.get()).unwrap() | ||
} | ||
} | ||
|
||
/// A counter. | ||
#[derive(Default, Clone)] | ||
pub struct Counter { | ||
count: Arc<AtomicU64>, | ||
} | ||
|
||
impl Counter { | ||
/// Increments the counter. | ||
pub fn inc(&self, value: u64) { | ||
self.count.fetch_add(value, Ordering::Relaxed); | ||
} | ||
|
||
/// Gets the current value of the counter. | ||
pub fn get(&self) -> u64 { | ||
self.count.load(Ordering::Relaxed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.