Skip to main content

datafusion_common/display/
human_readable.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Helpers for rendering sizes, counts, and durations in human readable form.
6
7/// Common data size units
8pub mod units {
9    pub const TB: u64 = 1 << 40;
10    pub const GB: u64 = 1 << 30;
11    pub const MB: u64 = 1 << 20;
12    pub const KB: u64 = 1 << 10;
13}
14
15/// Present size in human-readable form
16pub fn human_readable_size(size: usize) -> String {
17    use units::*;
18
19    let size = size as u64;
20    let (value, unit) = {
21        if size >= 2 * TB {
22            (size as f64 / TB as f64, "TB")
23        } else if size >= 2 * GB {
24            (size as f64 / GB as f64, "GB")
25        } else if size >= 2 * MB {
26            (size as f64 / MB as f64, "MB")
27        } else if size >= 2 * KB {
28            (size as f64 / KB as f64, "KB")
29        } else {
30            (size as f64, "B")
31        }
32    };
33    format!("{value:.1} {unit}")
34}
35
36/// Present count in human-readable form with K, M, B, T suffixes
37pub fn human_readable_count(count: usize) -> String {
38    let count = count as u64;
39    let (value, unit) = {
40        if count >= 1_000_000_000_000 {
41            (count as f64 / 1_000_000_000_000.0, " T")
42        } else if count >= 1_000_000_000 {
43            (count as f64 / 1_000_000_000.0, " B")
44        } else if count >= 1_000_000 {
45            (count as f64 / 1_000_000.0, " M")
46        } else if count >= 1_000 {
47            (count as f64 / 1_000.0, " K")
48        } else {
49            return count.to_string();
50        }
51    };
52
53    // Format with appropriate precision
54    // For values >= 100, show 1 decimal place (e.g., 123.4 K)
55    // For values < 100, show 2 decimal places (e.g., 10.12 K)
56    if value >= 100.0 {
57        format!("{value:.1}{unit}")
58    } else {
59        format!("{value:.2}{unit}")
60    }
61}
62
63/// Present duration in human-readable form with 2 decimal places
64pub fn human_readable_duration(nanos: u64) -> String {
65    const NANOS_PER_SEC: f64 = 1_000_000_000.0;
66    const NANOS_PER_MILLI: f64 = 1_000_000.0;
67    const NANOS_PER_MICRO: f64 = 1_000.0;
68
69    let nanos_f64 = nanos as f64;
70
71    if nanos >= 1_000_000_000 {
72        // >= 1 second: show in seconds
73        format!("{:.2}s", nanos_f64 / NANOS_PER_SEC)
74    } else if nanos >= 1_000_000 {
75        // >= 1 millisecond: show in milliseconds
76        format!("{:.2}ms", nanos_f64 / NANOS_PER_MILLI)
77    } else if nanos >= 1_000 {
78        // >= 1 microsecond: show in microseconds
79        format!("{:.2}µs", nanos_f64 / NANOS_PER_MICRO)
80    } else {
81        // < 1 microsecond: show in nanoseconds
82        format!("{nanos}ns")
83    }
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn test_human_readable_count() {
92        assert_eq!(human_readable_count(0), "0");
93        assert_eq!(human_readable_count(1), "1");
94        assert_eq!(human_readable_count(999), "999");
95        assert_eq!(human_readable_count(1_000), "1.00 K");
96        assert_eq!(human_readable_count(10_100), "10.10 K");
97        assert_eq!(human_readable_count(1_532), "1.53 K");
98        assert_eq!(human_readable_count(99_999), "100.00 K");
99        assert_eq!(human_readable_count(1_000_000), "1.00 M");
100        assert_eq!(human_readable_count(1_532_000), "1.53 M");
101        assert_eq!(human_readable_count(99_000_000), "99.00 M");
102        assert_eq!(human_readable_count(123_456_789), "123.5 M");
103        assert_eq!(human_readable_count(1_000_000_000), "1.00 B");
104        assert_eq!(human_readable_count(1_532_000_000), "1.53 B");
105        assert_eq!(human_readable_count(999_999_999_999), "1000.0 B");
106        assert_eq!(human_readable_count(1_000_000_000_000), "1.00 T");
107        assert_eq!(human_readable_count(42_000_000_000_000), "42.00 T");
108    }
109
110    #[test]
111    fn test_human_readable_duration() {
112        assert_eq!(human_readable_duration(0), "0ns");
113        assert_eq!(human_readable_duration(1), "1ns");
114        assert_eq!(human_readable_duration(999), "999ns");
115        assert_eq!(human_readable_duration(1_000), "1.00µs");
116        assert_eq!(human_readable_duration(1_234), "1.23µs");
117        assert_eq!(human_readable_duration(999_999), "1000.00µs");
118        assert_eq!(human_readable_duration(1_000_000), "1.00ms");
119        assert_eq!(human_readable_duration(11_295_377), "11.30ms");
120        assert_eq!(human_readable_duration(1_234_567), "1.23ms");
121        assert_eq!(human_readable_duration(999_999_999), "1000.00ms");
122        assert_eq!(human_readable_duration(1_000_000_000), "1.00s");
123        assert_eq!(human_readable_duration(1_234_567_890), "1.23s");
124        assert_eq!(human_readable_duration(42_000_000_000), "42.00s");
125    }
126}