Skip to main content

reifydb_core/encoded/
decimal.rs

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