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::{f64, 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 std::f64::consts::{E, PI};
44
45	use reifydb_type::value::r#type::Type;
46
47	use crate::encoded::schema::Schema;
48
49	#[test]
50	fn test_set_get_f64() {
51		let schema = Schema::testing(&[Type::Float8]);
52		let mut row = schema.allocate();
53		schema.set_f64(&mut row, 0, 2.5f64);
54		assert_eq!(schema.get_f64(&row, 0), 2.5f64);
55	}
56
57	#[test]
58	fn test_try_get_f64() {
59		let schema = Schema::testing(&[Type::Float8]);
60		let mut row = schema.allocate();
61
62		assert_eq!(schema.try_get_f64(&row, 0), None);
63
64		schema.set_f64(&mut row, 0, 2.5f64);
65		assert_eq!(schema.try_get_f64(&row, 0), Some(2.5f64));
66	}
67
68	#[test]
69	fn test_special_values() {
70		let schema = Schema::testing(&[Type::Float8]);
71		let mut row = schema.allocate();
72
73		// Test zero
74		schema.set_f64(&mut row, 0, 0.0f64);
75		assert_eq!(schema.get_f64(&row, 0), 0.0f64);
76
77		// Test negative zero
78		let mut row2 = schema.allocate();
79		schema.set_f64(&mut row2, 0, -0.0f64);
80		assert_eq!(schema.get_f64(&row2, 0), -0.0f64);
81
82		// Test infinity
83		let mut row3 = schema.allocate();
84		schema.set_f64(&mut row3, 0, f64::INFINITY);
85		assert_eq!(schema.get_f64(&row3, 0), f64::INFINITY);
86
87		// Test negative infinity
88		let mut row4 = schema.allocate();
89		schema.set_f64(&mut row4, 0, f64::NEG_INFINITY);
90		assert_eq!(schema.get_f64(&row4, 0), f64::NEG_INFINITY);
91
92		// Test NaN
93		let mut row5 = schema.allocate();
94		schema.set_f64(&mut row5, 0, f64::NAN);
95		assert!(schema.get_f64(&row5, 0).is_nan());
96	}
97
98	#[test]
99	fn test_extreme_values() {
100		let schema = Schema::testing(&[Type::Float8]);
101		let mut row = schema.allocate();
102
103		schema.set_f64(&mut row, 0, f64::MAX);
104		assert_eq!(schema.get_f64(&row, 0), f64::MAX);
105
106		let mut row2 = schema.allocate();
107		schema.set_f64(&mut row2, 0, f64::MIN);
108		assert_eq!(schema.get_f64(&row2, 0), f64::MIN);
109
110		let mut row3 = schema.allocate();
111		schema.set_f64(&mut row3, 0, f64::MIN_POSITIVE);
112		assert_eq!(schema.get_f64(&row3, 0), f64::MIN_POSITIVE);
113	}
114
115	#[test]
116	fn test_high_precision() {
117		let schema = Schema::testing(&[Type::Float8]);
118		let mut row = schema.allocate();
119
120		let pi = PI;
121		schema.set_f64(&mut row, 0, pi);
122		assert_eq!(schema.get_f64(&row, 0), pi);
123
124		let mut row2 = schema.allocate();
125		let e = E;
126		schema.set_f64(&mut row2, 0, e);
127		assert_eq!(schema.get_f64(&row2, 0), e);
128	}
129
130	#[test]
131	fn test_mixed_with_other_types() {
132		let schema = Schema::testing(&[Type::Float8, Type::Int8, Type::Float8]);
133		let mut row = schema.allocate();
134
135		schema.set_f64(&mut row, 0, 3.14159265359);
136		schema.set_i64(&mut row, 1, 9223372036854775807i64);
137		schema.set_f64(&mut row, 2, -2.718281828459045);
138
139		assert_eq!(schema.get_f64(&row, 0), 3.14159265359);
140		assert_eq!(schema.get_i64(&row, 1), 9223372036854775807);
141		assert_eq!(schema.get_f64(&row, 2), -2.718281828459045);
142	}
143
144	#[test]
145	fn test_undefined_handling() {
146		let schema = Schema::testing(&[Type::Float8, Type::Float8]);
147		let mut row = schema.allocate();
148
149		schema.set_f64(&mut row, 0, 2.718281828459045);
150
151		assert_eq!(schema.try_get_f64(&row, 0), Some(2.718281828459045));
152		assert_eq!(schema.try_get_f64(&row, 1), None);
153
154		schema.set_none(&mut row, 0);
155		assert_eq!(schema.try_get_f64(&row, 0), None);
156	}
157
158	#[test]
159	fn test_try_get_f64_wrong_type() {
160		let schema = Schema::testing(&[Type::Boolean]);
161		let mut row = schema.allocate();
162
163		schema.set_bool(&mut row, 0, true);
164
165		assert_eq!(schema.try_get_f64(&row, 0), None);
166	}
167}