Skip to main content

reifydb_function/text/
format_bytes.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::data::ColumnData;
5use reifydb_type::value::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type};
6
7use crate::{
8	ScalarFunction, ScalarFunctionContext,
9	error::{ScalarFunctionError, ScalarFunctionResult},
10	propagate_options,
11};
12
13const IEC_UNITS: [&str; 6] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
14
15pub(super) fn format_bytes_internal(bytes: i64, base: f64, units: &[&str]) -> String {
16	if bytes == 0 {
17		return "0 B".to_string();
18	}
19
20	let bytes_abs = bytes.unsigned_abs() as f64;
21	let sign = if bytes < 0 {
22		"-"
23	} else {
24		""
25	};
26
27	let mut unit_index = 0;
28	let mut value = bytes_abs;
29
30	while value >= base && unit_index < units.len() - 1 {
31		value /= base;
32		unit_index += 1;
33	}
34
35	if unit_index == 0 {
36		format!("{}{} {}", sign, bytes_abs as i64, units[0])
37	} else if value == value.floor() {
38		format!("{}{} {}", sign, value as i64, units[unit_index])
39	} else {
40		let formatted = format!("{:.2}", value);
41		let trimmed = formatted.trim_end_matches('0').trim_end_matches('.');
42		format!("{}{} {}", sign, trimmed, units[unit_index])
43	}
44}
45
46#[macro_export]
47macro_rules! process_int_column {
48	($container:expr, $row_count:expr, $base:expr, $units:expr) => {{
49		let mut result_data = Vec::with_capacity($row_count);
50
51		for i in 0..$row_count {
52			if let Some(&value) = $container.get(i) {
53				result_data.push(format_bytes_internal(value as i64, $base, $units));
54			} else {
55				result_data.push(String::new());
56			}
57		}
58
59		Ok(ColumnData::Utf8 {
60			container: Utf8Container::new(result_data),
61			max_bytes: MaxBytes::MAX,
62		})
63	}};
64}
65
66#[macro_export]
67macro_rules! process_float_column {
68	($container:expr, $row_count:expr, $base:expr, $units:expr) => {{
69		let mut result_data = Vec::with_capacity($row_count);
70
71		for i in 0..$row_count {
72			if let Some(&value) = $container.get(i) {
73				result_data.push(format_bytes_internal(value as i64, $base, $units));
74			} else {
75				result_data.push(String::new());
76			}
77		}
78
79		Ok(ColumnData::Utf8 {
80			container: Utf8Container::new(result_data),
81			max_bytes: MaxBytes::MAX,
82		})
83	}};
84}
85
86#[macro_export]
87macro_rules! process_decimal_column {
88	($container:expr, $row_count:expr, $base:expr, $units:expr) => {{
89		let mut result_data = Vec::with_capacity($row_count);
90
91		for i in 0..$row_count {
92			if let Some(value) = $container.get(i) {
93				// Truncate decimal to integer by parsing the integer part
94				let s = value.to_string();
95				let int_part = s.split('.').next().unwrap_or("0");
96				let bytes = int_part.parse::<i64>().unwrap_or(0);
97				result_data.push(format_bytes_internal(bytes, $base, $units));
98			} else {
99				result_data.push(String::new());
100			}
101		}
102
103		Ok(ColumnData::Utf8 {
104			container: Utf8Container::new(result_data),
105			max_bytes: MaxBytes::MAX,
106		})
107	}};
108}
109
110/// Formats bytes using binary units (1024-based: B, KiB, MiB, GiB, TiB, PiB)
111pub struct FormatBytes;
112
113impl FormatBytes {
114	pub fn new() -> Self {
115		Self
116	}
117}
118
119impl ScalarFunction for FormatBytes {
120	fn scalar(&self, ctx: ScalarFunctionContext) -> ScalarFunctionResult<ColumnData> {
121		if let Some(result) = propagate_options(self, &ctx) {
122			return result;
123		}
124
125		let columns = ctx.columns;
126		let row_count = ctx.row_count;
127
128		if columns.len() != 1 {
129			return Err(ScalarFunctionError::ArityMismatch {
130				function: ctx.fragment.clone(),
131				expected: 1,
132				actual: columns.len(),
133			});
134		}
135
136		let column = columns.get(0).unwrap();
137
138		match &column.data() {
139			ColumnData::Int1(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
140			ColumnData::Int2(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
141			ColumnData::Int4(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
142			ColumnData::Int8(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
143			ColumnData::Uint1(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
144			ColumnData::Uint2(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
145			ColumnData::Uint4(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
146			ColumnData::Uint8(container) => process_int_column!(container, row_count, 1024.0, &IEC_UNITS),
147			ColumnData::Float4(container) => {
148				process_float_column!(container, row_count, 1024.0, &IEC_UNITS)
149			}
150			ColumnData::Float8(container) => {
151				process_float_column!(container, row_count, 1024.0, &IEC_UNITS)
152			}
153			ColumnData::Decimal {
154				container,
155				..
156			} => {
157				process_decimal_column!(container, row_count, 1024.0, &IEC_UNITS)
158			}
159			other => Err(ScalarFunctionError::InvalidArgumentType {
160				function: ctx.fragment.clone(),
161				argument_index: 0,
162				expected: vec![
163					Type::Int1,
164					Type::Int2,
165					Type::Int4,
166					Type::Int8,
167					Type::Uint1,
168					Type::Uint2,
169					Type::Uint4,
170					Type::Uint8,
171					Type::Float4,
172					Type::Float8,
173					Type::Decimal,
174				],
175				actual: other.get_type(),
176			}),
177		}
178	}
179
180	fn return_type(&self, _input_types: &[Type]) -> Type {
181		Type::Utf8
182	}
183}
184
185pub(super) use process_decimal_column;
186pub(super) use process_float_column;
187pub(super) use process_int_column;