-
Notifications
You must be signed in to change notification settings - Fork 2k
Add metric category filtering for EXPLAIN ANALYZE #21160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adriangb
wants to merge
8
commits into
apache:main
Choose a base branch
from
pydantic:explain-analyze-metric-categories
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3299803
Add metric category filtering for EXPLAIN ANALYZE
adriangb b307bf2
Replace generate_series examples with parquet-based TopK tests
adriangb 2faa4dd
Categorize all built-in custom metrics
adriangb 701aab9
fix
adriangb c290b2a
fix configs
adriangb 010dc6c
fixes
adriangb b13a270
update json
adriangb 5cf7348
refactor
adriangb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -206,47 +206,227 @@ impl ConfigField for ExplainFormat { | |
| } | ||
| } | ||
|
|
||
| /// Verbosity levels controlling how `EXPLAIN ANALYZE` renders metrics | ||
| /// Categorizes metrics so the display layer can choose the desired verbosity. | ||
| /// | ||
| /// The `datafusion.explain.analyze_level` configuration controls which | ||
| /// type is shown: | ||
| /// - `"dev"` (the default): all metrics are shown. | ||
| /// - `"summary"`: only metrics tagged as `Summary` are shown. | ||
| /// | ||
| /// This is orthogonal to [`MetricCategory`], which filters by *what kind* | ||
| /// of value a metric represents (rows / bytes / timing). | ||
| /// | ||
| /// # Difference from `EXPLAIN ANALYZE VERBOSE` | ||
| /// | ||
| /// The `VERBOSE` keyword controls whether per-partition metrics are shown | ||
| /// (when specified) or aggregated metrics are displayed (when omitted). | ||
| /// In contrast, `MetricType` determines which *levels* of metrics are | ||
| /// displayed. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub enum ExplainAnalyzeLevel { | ||
| /// Show a compact view containing high-level metrics | ||
| pub enum MetricType { | ||
| /// Common metrics for high-level insights (answering which operator is slow) | ||
| Summary, | ||
| /// Show a developer-focused view with per-operator details | ||
| /// For deep operator-level introspection for developers | ||
| Dev, | ||
| // When adding new enum, update the error message in `from_str()` accordingly. | ||
| } | ||
|
|
||
| impl FromStr for ExplainAnalyzeLevel { | ||
| impl MetricType { | ||
| /// Returns the set of metric types that should be shown for this level. | ||
| /// | ||
| /// `Dev` is a superset of `Summary`: when the user selects | ||
| /// `analyze_level = 'dev'`, both `Summary` and `Dev` metrics are shown. | ||
| pub fn included_types(self) -> Vec<MetricType> { | ||
| match self { | ||
| MetricType::Summary => vec![MetricType::Summary], | ||
| MetricType::Dev => vec![MetricType::Summary, MetricType::Dev], | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for MetricType { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(level: &str) -> Result<Self, Self::Err> { | ||
| match level.to_lowercase().as_str() { | ||
| "summary" => Ok(ExplainAnalyzeLevel::Summary), | ||
| "dev" => Ok(ExplainAnalyzeLevel::Dev), | ||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| match s.trim().to_lowercase().as_str() { | ||
| "summary" => Ok(Self::Summary), | ||
| "dev" => Ok(Self::Dev), | ||
| other => Err(DataFusionError::Configuration(format!( | ||
| "Invalid explain analyze level. Expected 'summary' or 'dev'. Got '{other}'" | ||
| ))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for ExplainAnalyzeLevel { | ||
| impl Display for MetricType { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let s = match self { | ||
| ExplainAnalyzeLevel::Summary => "summary", | ||
| ExplainAnalyzeLevel::Dev => "dev", | ||
| }; | ||
| write!(f, "{s}") | ||
| match self { | ||
| Self::Summary => write!(f, "summary"), | ||
| Self::Dev => write!(f, "dev"), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ConfigField for MetricType { | ||
| fn visit<V: Visit>(&self, v: &mut V, key: &str, description: &'static str) { | ||
| v.some(key, self, description) | ||
| } | ||
|
|
||
| fn set(&mut self, _: &str, value: &str) -> Result<()> { | ||
| *self = MetricType::from_str(value)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Classifies a metric by what it measures. | ||
| /// | ||
| /// This is orthogonal to [`MetricType`] (Summary / Dev), which controls | ||
| /// *verbosity*. `MetricCategory` controls *what kind of value* is shown, | ||
| /// so that `EXPLAIN ANALYZE` output can be narrowed to only the categories | ||
| /// that are useful in a given context. | ||
| /// | ||
| /// In particular this is useful for testing since metrics differ in their stability across runs: | ||
| /// - [`Rows`](Self::Rows) and [`Bytes`](Self::Bytes) depend only on the plan | ||
| /// and the data, so they are mostly deterministic across runs (given the same | ||
| /// input). Variations can existing e.g. because of non-deterministic ordering | ||
| /// of evaluation between threads. | ||
| /// Running with a single target partition often makes these metrics stable enough to assert on in tests. | ||
| /// - [`Timing`](Self::Timing) depends on hardware, system load, scheduling, | ||
| /// etc., so it varies from run to run even on the same machine. | ||
| /// | ||
| /// [`MetricCategory`] is especially useful in sqllogictest (`.slt`) files: | ||
| /// setting `datafusion.explain.analyze_categories = 'rows'` lets a test | ||
| /// assert on row-count metrics without sprinkling `<slt:ignore>` over every | ||
| /// timing value. | ||
| /// | ||
| /// Metrics that do not declare a category (the default for custom | ||
| /// `Count` / `Gauge` metrics) are treated as | ||
| /// [`Uncategorized`](Self::Uncategorized) for filtering purposes. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub enum MetricCategory { | ||
| /// Row counts and related dimensionless counters: `output_rows`, | ||
| /// `spilled_rows`, `output_batches`, pruning metrics, ratios, etc. | ||
| /// | ||
| /// Mostly deterministic given the same plan and data. | ||
| Rows, | ||
| /// Byte measurements: `output_bytes`, `spilled_bytes`, | ||
| /// `current_memory_usage`, `bytes_scanned`, etc. | ||
| /// | ||
| /// Mostly deterministic given the same plan and data. | ||
| Bytes, | ||
| /// Wall-clock durations and timestamps: `elapsed_compute`, | ||
| /// operator-defined `Time` metrics, `start_timestamp` / | ||
| /// `end_timestamp`, etc. | ||
| /// | ||
| /// **Non-deterministic** — varies across runs even on the same hardware. | ||
| Timing, | ||
| /// Catch-all for metrics that do not fit into [`Rows`](Self::Rows), | ||
| /// [`Bytes`](Self::Bytes), or [`Timing`](Self::Timing). | ||
| /// | ||
| /// Custom `Count` / `Gauge` metrics that are not explicitly assigned | ||
| /// a category are treated as `Uncategorized` for filtering purposes. | ||
| /// | ||
| /// This variant lets users explicitly include or exclude these | ||
| /// metrics, e.g.: | ||
| /// ```sql | ||
| /// SET datafusion.explain.analyze_categories = 'rows, bytes, uncategorized'; | ||
| /// ``` | ||
| Uncategorized, | ||
| } | ||
|
|
||
| impl FromStr for MetricCategory { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| match s.trim().to_lowercase().as_str() { | ||
| "rows" => Ok(Self::Rows), | ||
| "bytes" => Ok(Self::Bytes), | ||
| "timing" => Ok(Self::Timing), | ||
| "uncategorized" => Ok(Self::Uncategorized), | ||
| other => Err(DataFusionError::Configuration(format!( | ||
| "Invalid metric category '{other}'. \ | ||
| Expected 'rows', 'bytes', 'timing', or 'uncategorized'." | ||
| ))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for MetricCategory { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Self::Rows => write!(f, "rows"), | ||
| Self::Bytes => write!(f, "bytes"), | ||
| Self::Timing => write!(f, "timing"), | ||
| Self::Uncategorized => write!(f, "uncategorized"), | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we can add another variant for all uncategorized metrics, so we can do set datafusion.explain.analyze_category = 'rows, bytes, uncategorized' -- Only exclude `Timing` metrics category
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
|
|
||
| /// Controls which [`MetricCategory`] values are shown in `EXPLAIN ANALYZE`. | ||
| /// | ||
| /// Set via `SET datafusion.explain.analyze_categories = '...'`. | ||
| /// | ||
| /// See [`MetricCategory`] for the determinism properties that motivate | ||
| /// this filter. | ||
| #[derive(Debug, Clone, PartialEq, Eq, Hash, Default)] | ||
| pub enum ExplainAnalyzeCategories { | ||
| /// Show all metrics regardless of category (the default). | ||
| #[default] | ||
| All, | ||
| /// Show only metrics whose category is in the list. | ||
| /// Metrics with no declared category are treated as | ||
| /// [`Uncategorized`](MetricCategory::Uncategorized) for filtering. | ||
| /// | ||
| /// An **empty** vec means "plan only" — suppress all metrics. | ||
| Only(Vec<MetricCategory>), | ||
| } | ||
|
|
||
| impl FromStr for ExplainAnalyzeCategories { | ||
| type Err = DataFusionError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| let s = s.trim().to_lowercase(); | ||
| match s.as_str() { | ||
| "all" => Ok(Self::All), | ||
| "none" => Ok(Self::Only(vec![])), | ||
| other => { | ||
| let mut cats = Vec::new(); | ||
| for part in other.split(',') { | ||
| cats.push(part.trim().parse::<MetricCategory>()?); | ||
| } | ||
| cats.dedup(); | ||
| Ok(Self::Only(cats)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for ExplainAnalyzeCategories { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| match self { | ||
| Self::All => write!(f, "all"), | ||
| Self::Only(cats) if cats.is_empty() => write!(f, "none"), | ||
| Self::Only(cats) => { | ||
| let mut first = true; | ||
| for cat in cats { | ||
| if !first { | ||
| write!(f, ",")?; | ||
| } | ||
| first = false; | ||
| write!(f, "{cat}")?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ConfigField for ExplainAnalyzeLevel { | ||
| impl ConfigField for ExplainAnalyzeCategories { | ||
| fn visit<V: Visit>(&self, v: &mut V, key: &str, description: &'static str) { | ||
| v.some(key, self, description) | ||
| } | ||
|
|
||
| fn set(&mut self, _: &str, value: &str) -> Result<()> { | ||
| *self = ExplainAnalyzeLevel::from_str(value)?; | ||
| *self = ExplainAnalyzeCategories::from_str(value)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@2010YOUY01 this is now a bigger diff because I refactored
MetricTypeinto here. It removes the redundancy withExplainAnalyzeLevel(they were the same enum). I really wanted to haveMetricCategoryin this module (or insidedatafusion-commonso that it's available to this module) so that we can parse the strings directly into the enum variants and avoid carrying aroundVec<String>s. I think this also applies toMetricType. Now both code paths are parallel, there is less duplicate code and validation happens earlier.One thing to confirm is that we are okay with having
MetricTypeandMetricCategory. They sound somewhat redundant but I think are actually orthogonal, unless we can say something like "all timing metrics areDev" in which case one is a superset of the other. I'd like your input here since you addedMetricTypeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the new category tags are necessary