Skip to content

Commit

Permalink
feat(function): Use the "IEC 80000-13" to replace the decimal based unit
Browse files Browse the repository at this point in the history
kB/KB/MB always leads to a lot of misunderstandings, we just use the KiB
  • Loading branch information
cadl committed May 3, 2022
1 parent 0fd3577 commit 293da8e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
20 changes: 10 additions & 10 deletions common/functions/tests/it/scalars/others/humanize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ use crate::scalars::scalar_function_test::ScalarFunctionTest;
fn test_humanize_size_function() -> Result<()> {
let tests = vec![
ScalarFunctionTest {
name: "humanize_size(1000)",
columns: vec![Series::from_data(vec![1000_u32])],
expect: Series::from_data(vec!["1 KB"]),
name: "humanize_size(1024)",
columns: vec![Series::from_data(vec![1024_u32])],
expect: Series::from_data(vec!["1.00 KiB"]),
error: "",
},
ScalarFunctionTest {
name: "humanize_size(-1000)",
columns: vec![Series::from_data(vec![-1000_i32])],
expect: Series::from_data(vec!["-1 KB"]),
name: "humanize_size(-1024)",
columns: vec![Series::from_data(vec![-1024_i32])],
expect: Series::from_data(vec!["-1.00 KiB"]),
error: "",
},
ScalarFunctionTest {
name: "humanize_size('abc')",
columns: vec![Series::from_data(vec!["abc"])],
expect: Series::from_data(vec!["-1 KB"]),
expect: Series::from_data(vec!["-1 KiB"]),
error: "Expected a numeric type, but got String",
},
ScalarFunctionTest {
name: "humanize_size(true)",
columns: vec![Series::from_data(vec![true])],
expect: Series::from_data(vec!["-1 KB"]),
expect: Series::from_data(vec!["-1 KiB"]),
error: "Expected a numeric type, but got Boolean",
},
];
Expand All @@ -54,8 +54,8 @@ fn test_humanize_size_function() -> Result<()> {
fn test_humanize_size_nullable() -> Result<()> {
let tests = vec![ScalarFunctionTest {
name: "humanize_size(null)",
columns: vec![Series::from_data(vec![Some(1_000_000_i32), None])],
expect: Series::from_data(vec![Some("1 MB"), None]),
columns: vec![Series::from_data(vec![Some(1_048_576_i32), None])],
expect: Series::from_data(vec![Some("1.00 MiB"), None]),
error: "",
}];

Expand Down
11 changes: 4 additions & 7 deletions common/io/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,16 @@ use common_exception::Result;
pub fn convert_byte_size(num: f64) -> String {
let negative = if num.is_sign_positive() { "" } else { "-" };
let num = num.abs();
let units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
if num < 1_f64 {
return format!("{}{} {}", negative, num, "B");
return format!("{}{:.02} {}", negative, num, "B");
}
let delimiter = 1000_f64;
let delimiter = 1024_f64;
let exponent = cmp::min(
(num.ln() / delimiter.ln()).floor() as i32,
(units.len() - 1) as i32,
);
let pretty_bytes = format!("{:.2}", num / delimiter.powi(exponent))
.parse::<f64>()
.unwrap()
* 1_f64;
let pretty_bytes = format!("{:.02}", num / delimiter.powi(exponent));
let unit = units[exponent as usize];
format!("{}{} {}", negative, pretty_bytes, unit)
}
Expand Down
13 changes: 10 additions & 3 deletions common/io/tests/it/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@ use common_io::prelude::*;

#[test]
fn convert_test() {
assert_eq!(convert_byte_size(1_f64), "1 B");
assert_eq!(convert_byte_size(1022_f64), "1.02 KB");
assert_eq!(convert_byte_size(1022_f64 * 10000000f64), "10.22 GB");
assert_eq!(convert_byte_size(0_f64), "0.00 B");
assert_eq!(convert_byte_size(0.1_f64), "0.10 B");
assert_eq!(convert_byte_size(1_f64), "1.00 B");
assert_eq!(convert_byte_size(1023_f64), "1023.00 B");
assert_eq!(convert_byte_size(1024_f64), "1.00 KiB");
assert_eq!(convert_byte_size(1229_f64), "1.20 KiB");
assert_eq!(
convert_byte_size(1024_f64 * 1024_f64 * 1024_f64),
"1.00 GiB"
);

assert_eq!(convert_number_size(1_f64), "1");
assert_eq!(convert_number_size(1022_f64), "1.02 thousand");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: HUMANIZE_SIZE
---

Returns the readable size with a suffix(KB, MB, etc).
Returns the readable size with a suffix(KiB, MiB, etc).

## Syntax

Expand All @@ -24,10 +24,10 @@ String.
## Examples

```sql
SELECT HUMANIZE_SIZE(1000 * 1000)
SELECT HUMANIZE_SIZE(1024 * 1024)
+-------------------------+
| HUMANIZE_SIZE((1000 * 1000)) |
| HUMANIZE_SIZE((1024 * 1024)) |
+-------------------------+
| 1 MB |
| 1 MiB |
+-------------------------+
```
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
1 KB
1 MB
1 GB
1 TB
1 PB
1 EB
1 ZB
1 YB
1000 YB
-1 KB
0 B
1.00 KiB
1.20 KiB
1.00 MiB
1.00 GiB
1.00 TiB
1.00 PiB
1.00 EiB
1.00 ZiB
1.00 YiB
1024.00 YiB
-1.00 KiB
0.00 B
NULL
1
1 thousand
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
SELECT HUMANIZE_SIZE(1000);
SELECT HUMANIZE_SIZE(1000000);
SELECT HUMANIZE_SIZE(1000000000);
SELECT HUMANIZE_SIZE(1000000000000);
SELECT HUMANIZE_SIZE(1000000000000000);
SELECT HUMANIZE_SIZE(1000000000000000000);
SELECT HUMANIZE_SIZE(1000000000000000000000);
SELECT HUMANIZE_SIZE(1000000000000000000000000);
SELECT HUMANIZE_SIZE(1000000000000000000000000000);
SELECT HUMANIZE_SIZE(-1000);
SELECT HUMANIZE_SIZE(1024);
SELECT HUMANIZE_SIZE(1229);
SELECT HUMANIZE_SIZE(POW(2, 20));
SELECT HUMANIZE_SIZE(POW(2, 30));
SELECT HUMANIZE_SIZE(POW(2, 40));
SELECT HUMANIZE_SIZE(POW(2, 50));
SELECT HUMANIZE_SIZE(POW(2, 60));
SELECT HUMANIZE_SIZE(POW(2, 70));
SELECT HUMANIZE_SIZE(POW(2, 80));
SELECT HUMANIZE_SIZE(POW(2, 90));
SELECT HUMANIZE_SIZE(-1024);
SELECT HUMANIZE_SIZE(0);
SELECT HUMANIZE_SIZE(NULL);
SELECT HUMANIZE_NUMBER(1);
Expand Down

0 comments on commit 293da8e

Please sign in to comment.