Skip to main content

reifydb_core/encoded/
decimal.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::ptr;
5
6use bigdecimal::BigDecimal as StdBigDecimal;
7use num_bigint::BigInt as StdBigInt;
8use reifydb_type::value::{decimal::Decimal, r#type::Type};
9
10use crate::encoded::{encoded::EncodedValues, schema::Schema};
11
12/// Decimal storage using dynamic section
13/// All decimals are stored in dynamic section with MSB=1 to store both mantissa
14/// and scale
15const MODE_DYNAMIC: u128 = 0x80000000000000000000000000000000;
16const MODE_MASK: u128 = 0x80000000000000000000000000000000;
17
18/// Bit masks for dynamic mode (lower 127 bits contain offset+length)
19const DYNAMIC_OFFSET_MASK: u128 = 0x0000000000000000FFFFFFFFFFFFFFFF; // 64 bits for offset
20const DYNAMIC_LENGTH_MASK: u128 = 0x7FFFFFFFFFFFFFFF0000000000000000; // 63 bits for length
21
22impl Schema {
23	/// Set a Decimal value with 2-tier storage optimization
24	/// - Values that fit in i128: stored inline with MSB=0
25	/// - Large values: stored in dynamic section with MSB=1
26	pub fn set_decimal(&self, row: &mut EncodedValues, index: usize, value: &Decimal) {
27		let field = &self.fields()[index];
28		debug_assert!(matches!(field.constraint.get_type().inner_type(), Type::Decimal { .. }));
29
30		// Get the mantissa and original scale from the BigDecimal
31		let (mantissa, original_scale) = value.inner().as_bigint_and_exponent();
32
33		// Always use dynamic storage to store both mantissa and scale
34		debug_assert!(!row.is_defined(index), "Decimal field {} already set", index);
35
36		// Serialize as scale (i64) + mantissa (variable bytes)
37		let scale_bytes = original_scale.to_le_bytes();
38		let digits_bytes = mantissa.to_signed_bytes_le();
39
40		let dynamic_offset = self.dynamic_section_size(row);
41		let total_size = 8 + digits_bytes.len(); // 8 bytes for scale + variable for mantissa
42
43		// Append to dynamic section: scale first, then mantissa
44		row.0.extend_from_slice(&scale_bytes);
45		row.0.extend_from_slice(&digits_bytes);
46
47		// Pack offset and length in lower 127 bits, set MSB=1
48		let offset_part = (dynamic_offset as u128) & DYNAMIC_OFFSET_MASK;
49		let length_part = ((total_size as u128) << 64) & DYNAMIC_LENGTH_MASK;
50		let packed = MODE_DYNAMIC | offset_part | length_part;
51
52		unsafe {
53			ptr::write_unaligned(
54				row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut u128,
55				packed.to_le(),
56			);
57		}
58		row.set_valid(index, true);
59	}
60
61	/// Get a Decimal value, detecting storage mode from MSB
62	pub fn get_decimal(&self, row: &EncodedValues, index: usize) -> Decimal {
63		let field = &self.fields()[index];
64		debug_assert!(matches!(field.constraint.get_type().inner_type(), Type::Decimal { .. }));
65
66		let packed = unsafe { (row.as_ptr().add(field.offset as usize) as *const u128).read_unaligned() };
67		let packed = u128::from_le(packed);
68
69		// Always expect dynamic storage (MSB=1)
70		debug_assert!(packed & MODE_MASK == MODE_DYNAMIC, "Expected dynamic storage");
71
72		// Extract offset and length
73		let offset = (packed & DYNAMIC_OFFSET_MASK) as usize;
74		let length = ((packed & DYNAMIC_LENGTH_MASK) >> 64) as usize;
75
76		let dynamic_start = self.dynamic_section_start();
77		let data_bytes = &row.as_slice()[dynamic_start + offset..dynamic_start + offset + length];
78
79		// Parse scale (first 8 bytes) and mantissa (remaining bytes)
80		let original_scale = i64::from_le_bytes(data_bytes[0..8].try_into().unwrap());
81		let mantissa = StdBigInt::from_signed_bytes_le(&data_bytes[8..]);
82
83		// Reconstruct the BigDecimal with original scale
84		let big_decimal = StdBigDecimal::new(mantissa, original_scale);
85
86		// Create our Decimal from the reconstructed BigDecimal
87		Decimal::from(big_decimal)
88	}
89
90	/// Try to get a Decimal value, returning None if undefined
91	pub fn try_get_decimal(&self, row: &EncodedValues, index: usize) -> Option<Decimal> {
92		if row.is_defined(index)
93			&& matches!(self.fields()[index].constraint.get_type().inner_type(), Type::Decimal { .. })
94		{
95			Some(self.get_decimal(row, index))
96		} else {
97			None
98		}
99	}
100}
101
102#[cfg(test)]
103pub mod tests {
104	use std::str::FromStr;
105
106	use num_traits::Zero;
107	use reifydb_type::value::{decimal::Decimal, r#type::Type};
108
109	use crate::encoded::schema::Schema;
110
111	#[test]
112	fn test_compact_inline() {
113		let schema = Schema::testing(&[Type::Decimal]);
114		let mut row = schema.allocate();
115
116		// Test simple decimal
117		let decimal = Decimal::from_str("123.45").unwrap();
118		schema.set_decimal(&mut row, 0, &decimal);
119		assert!(row.is_defined(0));
120
121		let retrieved = schema.get_decimal(&row, 0);
122		assert_eq!(retrieved.to_string(), "123.45");
123
124		// Test negative decimal
125		let mut row2 = schema.allocate();
126		let negative = Decimal::from_str("-999.99").unwrap();
127		schema.set_decimal(&mut row2, 0, &negative);
128		assert_eq!(schema.get_decimal(&row2, 0).to_string(), "-999.99");
129	}
130
131	#[test]
132	fn test_compact_boundaries() {
133		// Test high precision decimal
134		let schema1 = Schema::testing(&[Type::Decimal]);
135		let mut row1 = schema1.allocate();
136		let high_precision = Decimal::from_str("1.0000000000000000000000000000001").unwrap();
137		schema1.set_decimal(&mut row1, 0, &high_precision);
138		let retrieved = schema1.get_decimal(&row1, 0);
139		assert_eq!(retrieved.to_string(), "1.0000000000000000000000000000001");
140
141		// Test large integer (scale 0)
142		let schema2 = Schema::testing(&[Type::Decimal]);
143		let mut row2 = schema2.allocate();
144		let large_int = Decimal::from_str("100000000000000000000000000000000").unwrap();
145		schema2.set_decimal(&mut row2, 0, &large_int);
146		assert_eq!(schema2.get_decimal(&row2, 0).to_string(), "100000000000000000000000000000000");
147	}
148
149	#[test]
150	fn test_extended_i128() {
151		let schema = Schema::testing(&[Type::Decimal]);
152		let mut row = schema.allocate();
153
154		// Value that needs i128 mantissa
155		let large = Decimal::from_str("999999999999999999999.123456789").unwrap();
156		schema.set_decimal(&mut row, 0, &large);
157		assert!(row.is_defined(0));
158
159		let retrieved = schema.get_decimal(&row, 0);
160		assert_eq!(retrieved.to_string(), "999999999999999999999.123456789");
161	}
162
163	#[test]
164	fn test_dynamic_storage() {
165		// Use a smaller test that will still trigger dynamic storage
166		// due to large mantissa
167		let schema = Schema::testing(&[Type::Decimal]);
168		let mut row = schema.allocate();
169
170		// Create a value with large precision that will exceed i128
171		// when scaled
172		let huge = Decimal::from_str("99999999999999999999999999999.123456789").unwrap();
173
174		schema.set_decimal(&mut row, 0, &huge);
175		assert!(row.is_defined(0));
176
177		let retrieved = schema.get_decimal(&row, 0);
178		assert_eq!(retrieved.to_string(), "99999999999999999999999999999.123456789");
179	}
180
181	#[test]
182	fn test_zero() {
183		let schema = Schema::testing(&[Type::Decimal]);
184		let mut row = schema.allocate();
185
186		let zero = Decimal::from_str("0.0").unwrap();
187		schema.set_decimal(&mut row, 0, &zero);
188		assert!(row.is_defined(0));
189
190		let retrieved = schema.get_decimal(&row, 0);
191		assert!(retrieved.inner().is_zero());
192	}
193
194	#[test]
195	fn test_currency_values() {
196		let schema = Schema::testing(&[Type::Decimal]);
197
198		// Test typical currency value (2 decimal places)
199		let mut row1 = schema.allocate();
200		let price = Decimal::from_str("19.99").unwrap();
201		schema.set_decimal(&mut row1, 0, &price);
202		assert_eq!(schema.get_decimal(&row1, 0).to_string(), "19.99");
203
204		// Test large currency value
205		let mut row2 = schema.allocate();
206		let large_price = Decimal::from_str("999999999.99").unwrap();
207		schema.set_decimal(&mut row2, 0, &large_price);
208		assert_eq!(schema.get_decimal(&row2, 0).to_string(), "999999999.99");
209
210		// Test small fraction
211		let mut row3 = schema.allocate();
212		let fraction = Decimal::from_str("0.00000001").unwrap();
213		schema.set_decimal(&mut row3, 0, &fraction);
214		assert_eq!(schema.get_decimal(&row3, 0), fraction);
215	}
216
217	#[test]
218	fn test_scientific_notation() {
219		let schema = Schema::testing(&[Type::Decimal]);
220		let mut row = schema.allocate();
221
222		let scientific = Decimal::from_str("1.23456e10").unwrap();
223		schema.set_decimal(&mut row, 0, &scientific);
224
225		let retrieved = schema.get_decimal(&row, 0);
226		assert_eq!(retrieved.to_string(), "12345600000");
227	}
228
229	#[test]
230	fn test_try_get() {
231		let schema = Schema::testing(&[Type::Decimal]);
232		let mut row = schema.allocate();
233
234		// Undefined initially
235		assert_eq!(schema.try_get_decimal(&row, 0), None);
236
237		// Set value
238		let value = Decimal::from_str("42.42").unwrap();
239		schema.set_decimal(&mut row, 0, &value);
240
241		let retrieved = schema.try_get_decimal(&row, 0);
242		assert!(retrieved.is_some());
243		assert_eq!(retrieved.unwrap().to_string(), "42.42");
244	}
245
246	#[test]
247	fn test_clone_on_write() {
248		let schema = Schema::testing(&[Type::Decimal]);
249		let row1 = schema.allocate();
250		let mut row2 = row1.clone();
251
252		let value = Decimal::from_str("3.14159").unwrap();
253		schema.set_decimal(&mut row2, 0, &value);
254
255		assert!(!row1.is_defined(0));
256		assert!(row2.is_defined(0));
257		assert_ne!(row1.as_ptr(), row2.as_ptr());
258		assert_eq!(schema.get_decimal(&row2, 0).to_string(), "3.14159");
259	}
260
261	#[test]
262	fn test_mixed_with_other_types() {
263		let schema = Schema::testing(&[Type::Boolean, Type::Decimal, Type::Utf8, Type::Decimal, Type::Int4]);
264		let mut row = schema.allocate();
265
266		schema.set_bool(&mut row, 0, true);
267
268		let small_decimal = Decimal::from_str("99.99").unwrap();
269		schema.set_decimal(&mut row, 1, &small_decimal);
270
271		schema.set_utf8(&mut row, 2, "test");
272
273		let large_decimal = Decimal::from_str("123456789.987654321").unwrap();
274		schema.set_decimal(&mut row, 3, &large_decimal);
275
276		schema.set_i32(&mut row, 4, -42);
277
278		assert_eq!(schema.get_bool(&row, 0), true);
279		assert_eq!(schema.get_decimal(&row, 1).to_string(), "99.99");
280		assert_eq!(schema.get_utf8(&row, 2), "test");
281		assert_eq!(schema.get_decimal(&row, 3).to_string(), "123456789.987654321");
282		assert_eq!(schema.get_i32(&row, 4), -42);
283	}
284
285	#[test]
286	fn test_negative_values() {
287		// Small negative (compact inline) - needs scale 2
288		let schema1 = Schema::testing(&[Type::Decimal]);
289
290		let mut row1 = schema1.allocate();
291		let small_neg = Decimal::from_str("-0.01").unwrap();
292		schema1.set_decimal(&mut row1, 0, &small_neg);
293		assert_eq!(schema1.get_decimal(&row1, 0).to_string(), "-0.01");
294
295		// Large negative (extended i128) - needs scale 3
296		let schema2 = Schema::testing(&[Type::Decimal]);
297		let mut row2 = schema2.allocate();
298		let large_neg = Decimal::from_str("-999999999999999999.999").unwrap();
299		schema2.set_decimal(&mut row2, 0, &large_neg);
300		assert_eq!(schema2.get_decimal(&row2, 0).to_string(), "-999999999999999999.999");
301
302		// Huge negative (dynamic) - needs scale 9
303		let schema3 = Schema::testing(&[Type::Decimal]);
304		let mut row3 = schema3.allocate();
305		let huge_neg = Decimal::from_str("-99999999999999999999999999999.999999999").unwrap();
306		schema3.set_decimal(&mut row3, 0, &huge_neg);
307		assert_eq!(schema3.get_decimal(&row3, 0).to_string(), "-99999999999999999999999999999.999999999");
308	}
309
310	#[test]
311	fn test_try_get_decimal_wrong_type() {
312		let schema = Schema::testing(&[Type::Boolean]);
313		let mut row = schema.allocate();
314
315		schema.set_bool(&mut row, 0, true);
316
317		assert_eq!(schema.try_get_decimal(&row, 0), None);
318	}
319}