Skip to main content

reifydb_core/encoded/
f64.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::ptr;
5
6use reifydb_type::value::r#type::Type;
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11	pub fn set_f64(&self, row: &mut EncodedValues, index: usize, value: impl Into<f64>) {
12		let field = &self.fields()[index];
13		debug_assert!(row.len() >= self.total_static_size());
14		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Float8);
15		row.set_valid(index, true);
16		unsafe {
17			ptr::write_unaligned(
18				row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut f64,
19				value.into(),
20			)
21		}
22	}
23
24	pub fn get_f64(&self, row: &EncodedValues, index: usize) -> f64 {
25		let field = &self.fields()[index];
26		debug_assert!(row.len() >= self.total_static_size());
27		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Float8);
28		unsafe { (row.as_ptr().add(field.offset as usize) as *const f64).read_unaligned() }
29	}
30
31	pub fn try_get_f64(&self, row: &EncodedValues, index: usize) -> Option<f64> {
32		if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Float8 {
33			Some(self.get_f64(row, index))
34		} else {
35			None
36		}
37	}
38}
39
40#[cfg(test)]
41#[allow(clippy::approx_constant)]
42pub mod tests {
43	use reifydb_type::value::r#type::Type;
44
45	use crate::encoded::schema::Schema;
46
47	#[test]
48	fn test_set_get_f64() {
49		let schema = Schema::testing(&[Type::Float8]);
50		let mut row = schema.allocate();
51		schema.set_f64(&mut row, 0, 2.5f64);
52		assert_eq!(schema.get_f64(&row, 0), 2.5f64);
53	}
54
55	#[test]
56	fn test_try_get_f64() {
57		let schema = Schema::testing(&[Type::Float8]);
58		let mut row = schema.allocate();
59
60		assert_eq!(schema.try_get_f64(&row, 0), None);
61
62		schema.set_f64(&mut row, 0, 2.5f64);
63		assert_eq!(schema.try_get_f64(&row, 0), Some(2.5f64));
64	}
65
66	#[test]
67	fn test_special_values() {
68		let schema = Schema::testing(&[Type::Float8]);
69		let mut row = schema.allocate();
70
71		// Test zero
72		schema.set_f64(&mut row, 0, 0.0f64);
73		assert_eq!(schema.get_f64(&row, 0), 0.0f64);
74
75		// Test negative zero
76		let mut row2 = schema.allocate();
77		schema.set_f64(&mut row2, 0, -0.0f64);
78		assert_eq!(schema.get_f64(&row2, 0), -0.0f64);
79
80		// Test infinity
81		let mut row3 = schema.allocate();
82		schema.set_f64(&mut row3, 0, f64::INFINITY);
83		assert_eq!(schema.get_f64(&row3, 0), f64::INFINITY);
84
85		// Test negative infinity
86		let mut row4 = schema.allocate();
87		schema.set_f64(&mut row4, 0, f64::NEG_INFINITY);
88		assert_eq!(schema.get_f64(&row4, 0), f64::NEG_INFINITY);
89
90		// Test NaN
91		let mut row5 = schema.allocate();
92		schema.set_f64(&mut row5, 0, f64::NAN);
93		assert!(schema.get_f64(&row5, 0).is_nan());
94	}
95
96	#[test]
97	fn test_extreme_values() {
98		let schema = Schema::testing(&[Type::Float8]);
99		let mut row = schema.allocate();
100
101		schema.set_f64(&mut row, 0, f64::MAX);
102		assert_eq!(schema.get_f64(&row, 0), f64::MAX);
103
104		let mut row2 = schema.allocate();
105		schema.set_f64(&mut row2, 0, f64::MIN);
106		assert_eq!(schema.get_f64(&row2, 0), f64::MIN);
107
108		let mut row3 = schema.allocate();
109		schema.set_f64(&mut row3, 0, f64::MIN_POSITIVE);
110		assert_eq!(schema.get_f64(&row3, 0), f64::MIN_POSITIVE);
111	}
112
113	#[test]
114	fn test_high_precision() {
115		let schema = Schema::testing(&[Type::Float8]);
116		let mut row = schema.allocate();
117
118		let pi = std::f64::consts::PI;
119		schema.set_f64(&mut row, 0, pi);
120		assert_eq!(schema.get_f64(&row, 0), pi);
121
122		let mut row2 = schema.allocate();
123		let e = std::f64::consts::E;
124		schema.set_f64(&mut row2, 0, e);
125		assert_eq!(schema.get_f64(&row2, 0), e);
126	}
127
128	#[test]
129	fn test_mixed_with_other_types() {
130		let schema = Schema::testing(&[Type::Float8, Type::Int8, Type::Float8]);
131		let mut row = schema.allocate();
132
133		schema.set_f64(&mut row, 0, 3.14159265359);
134		schema.set_i64(&mut row, 1, 9223372036854775807i64);
135		schema.set_f64(&mut row, 2, -2.718281828459045);
136
137		assert_eq!(schema.get_f64(&row, 0), 3.14159265359);
138		assert_eq!(schema.get_i64(&row, 1), 9223372036854775807);
139		assert_eq!(schema.get_f64(&row, 2), -2.718281828459045);
140	}
141
142	#[test]
143	fn test_undefined_handling() {
144		let schema = Schema::testing(&[Type::Float8, Type::Float8]);
145		let mut row = schema.allocate();
146
147		schema.set_f64(&mut row, 0, 2.718281828459045);
148
149		assert_eq!(schema.try_get_f64(&row, 0), Some(2.718281828459045));
150		assert_eq!(schema.try_get_f64(&row, 1), None);
151
152		schema.set_undefined(&mut row, 0);
153		assert_eq!(schema.try_get_f64(&row, 0), None);
154	}
155
156	#[test]
157	fn test_try_get_f64_wrong_type() {
158		let schema = Schema::testing(&[Type::Boolean]);
159		let mut row = schema.allocate();
160
161		schema.set_bool(&mut row, 0, true);
162
163		assert_eq!(schema.try_get_f64(&row, 0), None);
164	}
165}