-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat: add human-readable formatting to EXPLAIN ANALYZE metrics #18689 #18734
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
Merged
2010YOUY01
merged 2 commits into
apache:main
from
T2MIX:feat/explain-analyze-readable-formatting
Nov 18, 2025
+263
−42
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -503,6 +503,56 @@ pub fn human_readable_size(size: usize) -> String { | |
| format!("{value:.1} {unit}") | ||
| } | ||
|
|
||
| /// Present count in human-readable form with K, M, B, T suffixes | ||
| pub fn human_readable_count(count: usize) -> String { | ||
| let count = count as u64; | ||
| let (value, unit) = { | ||
| if count >= 1_000_000_000_000 { | ||
| (count as f64 / 1_000_000_000_000.0, " T") | ||
| } else if count >= 1_000_000_000 { | ||
| (count as f64 / 1_000_000_000.0, " B") | ||
| } else if count >= 1_000_000 { | ||
| (count as f64 / 1_000_000.0, " M") | ||
| } else if count >= 1_000 { | ||
| (count as f64 / 1_000.0, " K") | ||
| } else { | ||
| return count.to_string(); | ||
| } | ||
| }; | ||
|
|
||
| // Format with appropriate precision | ||
| // For values >= 100, show 1 decimal place (e.g., 123.4 K) | ||
| // For values < 100, show 2 decimal places (e.g., 10.12 K) | ||
| if value >= 100.0 { | ||
| format!("{value:.1}{unit}") | ||
| } else { | ||
| format!("{value:.2}{unit}") | ||
| } | ||
| } | ||
|
|
||
| /// Present duration in human-readable form with 2 decimal places | ||
| pub fn human_readable_duration(nanos: u64) -> String { | ||
| const NANOS_PER_SEC: f64 = 1_000_000_000.0; | ||
| const NANOS_PER_MILLI: f64 = 1_000_000.0; | ||
| const NANOS_PER_MICRO: f64 = 1_000.0; | ||
|
|
||
| let nanos_f64 = nanos as f64; | ||
|
|
||
| if nanos >= 1_000_000_000 { | ||
| // >= 1 second: show in seconds | ||
| format!("{:.2}s", nanos_f64 / NANOS_PER_SEC) | ||
| } else if nanos >= 1_000_000 { | ||
| // >= 1 millisecond: show in milliseconds | ||
| format!("{:.2}ms", nanos_f64 / NANOS_PER_MILLI) | ||
| } else if nanos >= 1_000 { | ||
| // >= 1 microsecond: show in microseconds | ||
| format!("{:.2}µs", nanos_f64 / NANOS_PER_MICRO) | ||
| } else { | ||
| // < 1 microsecond: show in nanoseconds | ||
| format!("{nanos}ns") | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
@@ -599,4 +649,57 @@ mod tests { | |
| assert_eq!(r2.size(), 25); | ||
| assert_eq!(pool.reserved(), 28); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_human_readable_count() { | ||
| // Test small numbers (< 1000) - should display as-is | ||
| assert_eq!(human_readable_count(0), "0"); | ||
| assert_eq!(human_readable_count(1), "1"); | ||
| assert_eq!(human_readable_count(999), "999"); | ||
|
|
||
| // Test thousands (K) | ||
| assert_eq!(human_readable_count(1_000), "1.00 K"); | ||
| assert_eq!(human_readable_count(10_100), "10.10 K"); | ||
| assert_eq!(human_readable_count(1_532), "1.53 K"); | ||
| assert_eq!(human_readable_count(99_999), "100.00 K"); | ||
|
Member
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. Shouldn't this be displayed as |
||
|
|
||
| // Test millions (M) | ||
| assert_eq!(human_readable_count(1_000_000), "1.00 M"); | ||
| assert_eq!(human_readable_count(1_532_000), "1.53 M"); | ||
| assert_eq!(human_readable_count(99_000_000), "99.00 M"); | ||
| assert_eq!(human_readable_count(123_456_789), "123.5 M"); | ||
|
|
||
| // Test billions (B) | ||
| assert_eq!(human_readable_count(1_000_000_000), "1.00 B"); | ||
| assert_eq!(human_readable_count(1_532_000_000), "1.53 B"); | ||
| assert_eq!(human_readable_count(999_999_999_999), "1000.0 B"); | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Test trillions (T) | ||
| assert_eq!(human_readable_count(1_000_000_000_000), "1.00 T"); | ||
| assert_eq!(human_readable_count(42_000_000_000_000), "42.00 T"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_human_readable_duration() { | ||
| // Test nanoseconds (< 1µs) | ||
| assert_eq!(human_readable_duration(0), "0ns"); | ||
| assert_eq!(human_readable_duration(1), "1ns"); | ||
| assert_eq!(human_readable_duration(999), "999ns"); | ||
|
|
||
| // Test microseconds (1µs to < 1ms) | ||
| assert_eq!(human_readable_duration(1_000), "1.00µs"); | ||
| assert_eq!(human_readable_duration(1_234), "1.23µs"); | ||
| assert_eq!(human_readable_duration(999_999), "1000.00µs"); | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Test milliseconds (1ms to < 1s) | ||
| assert_eq!(human_readable_duration(1_000_000), "1.00ms"); | ||
| assert_eq!(human_readable_duration(11_295_377), "11.30ms"); | ||
| assert_eq!(human_readable_duration(1_234_567), "1.23ms"); | ||
| assert_eq!(human_readable_duration(999_999_999), "1000.00ms"); | ||
2010YOUY01 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Test seconds (>= 1s) | ||
| assert_eq!(human_readable_duration(1_000_000_000), "1.00s"); | ||
| assert_eq!(human_readable_duration(1_234_567_890), "1.23s"); | ||
| assert_eq!(human_readable_duration(42_000_000_000), "42.00s"); | ||
| } | ||
| } | ||
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.
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.
You need to round it first to make it consistent (i.e.
99999->100.0 K):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.
Good point, let's leave it to an optional follow-up.