Skip to main content

reifydb_routine/function/text/
format_bytes_si.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{constraint::bytes::MaxBytes, container::utf8::Utf8Container, r#type::Type};
6
7use crate::function::{
8	Function, FunctionCapability, FunctionContext, FunctionInfo,
9	error::FunctionError,
10	text::format_bytes::{format_bytes_internal, process_decimal_column, process_float_column, process_int_column},
11};
12
13const SI_UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];
14
15/// Formats bytes using SI/decimal units (1000-based: B, KB, MB, GB, TB, PB)
16pub struct FormatBytesSi {
17	info: FunctionInfo,
18}
19
20impl Default for FormatBytesSi {
21	fn default() -> Self {
22		Self::new()
23	}
24}
25
26impl FormatBytesSi {
27	pub fn new() -> Self {
28		Self {
29			info: FunctionInfo::new("text::format_bytes_si"),
30		}
31	}
32}
33
34impl Function for FormatBytesSi {
35	fn info(&self) -> &FunctionInfo {
36		&self.info
37	}
38
39	fn capabilities(&self) -> &[FunctionCapability] {
40		&[FunctionCapability::Scalar]
41	}
42
43	fn return_type(&self, _input_types: &[Type]) -> Type {
44		Type::Utf8
45	}
46
47	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
48		if args.len() != 1 {
49			return Err(FunctionError::ArityMismatch {
50				function: ctx.fragment.clone(),
51				expected: 1,
52				actual: args.len(),
53			});
54		}
55
56		let column = &args[0];
57		let (data, bitvec) = column.data().unwrap_option();
58		let row_count = data.len();
59
60		let result_data = match data {
61			ColumnData::Int1(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
62			ColumnData::Int2(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
63			ColumnData::Int4(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
64			ColumnData::Int8(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
65			ColumnData::Uint1(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
66			ColumnData::Uint2(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
67			ColumnData::Uint4(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
68			ColumnData::Uint8(container) => process_int_column!(container, row_count, 1000.0, &SI_UNITS),
69			ColumnData::Float4(container) => process_float_column!(container, row_count, 1000.0, &SI_UNITS),
70			ColumnData::Float8(container) => process_float_column!(container, row_count, 1000.0, &SI_UNITS),
71			ColumnData::Decimal {
72				container,
73				..
74			} => {
75				process_decimal_column!(container, row_count, 1000.0, &SI_UNITS)
76			}
77			other => {
78				return Err(FunctionError::InvalidArgumentType {
79					function: ctx.fragment.clone(),
80					argument_index: 0,
81					expected: vec![
82						Type::Int1,
83						Type::Int2,
84						Type::Int4,
85						Type::Int8,
86						Type::Uint1,
87						Type::Uint2,
88						Type::Uint4,
89						Type::Uint8,
90						Type::Float4,
91						Type::Float8,
92						Type::Decimal,
93					],
94					actual: other.get_type(),
95				});
96			}
97		};
98
99		let final_data = match bitvec {
100			Some(bv) => ColumnData::Option {
101				inner: Box::new(result_data),
102				bitvec: bv.clone(),
103			},
104			None => result_data,
105		};
106		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
107	}
108}