Skip to main content

reifydb_core/encoded/
any.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::str;
5
6use reifydb_type::value::{
7	Value,
8	blob::Blob,
9	date::Date,
10	datetime::DateTime,
11	duration::Duration,
12	identity::IdentityId,
13	ordered_f32::OrderedF32,
14	ordered_f64::OrderedF64,
15	time::Time,
16	r#type::Type,
17	uuid::{Uuid4, Uuid7},
18};
19use uuid::Uuid;
20
21use crate::encoded::{encoded::EncodedValues, schema::Schema};
22
23/// Encodes an inner value to a `[type_byte][payload]` byte vector.
24///
25/// Panics for unsupported types (Int, Uint, Decimal, DictionaryId, Any, None, List, Type).
26pub fn encode_value(value: &Value) -> Vec<u8> {
27	match value {
28		Value::Boolean(v) => vec![Type::Boolean.to_u8(), *v as u8],
29
30		Value::Uint1(v) => vec![Type::Uint1.to_u8(), *v],
31		Value::Uint2(v) => {
32			let mut b = vec![Type::Uint2.to_u8()];
33			b.extend_from_slice(&v.to_le_bytes());
34			b
35		}
36		Value::Uint4(v) => {
37			let mut b = vec![Type::Uint4.to_u8()];
38			b.extend_from_slice(&v.to_le_bytes());
39			b
40		}
41		Value::Uint8(v) => {
42			let mut b = vec![Type::Uint8.to_u8()];
43			b.extend_from_slice(&v.to_le_bytes());
44			b
45		}
46		Value::Uint16(v) => {
47			let mut b = vec![Type::Uint16.to_u8()];
48			b.extend_from_slice(&v.to_le_bytes());
49			b
50		}
51
52		Value::Int1(v) => vec![Type::Int1.to_u8(), *v as u8],
53		Value::Int2(v) => {
54			let mut b = vec![Type::Int2.to_u8()];
55			b.extend_from_slice(&v.to_le_bytes());
56			b
57		}
58		Value::Int4(v) => {
59			let mut b = vec![Type::Int4.to_u8()];
60			b.extend_from_slice(&v.to_le_bytes());
61			b
62		}
63		Value::Int8(v) => {
64			let mut b = vec![Type::Int8.to_u8()];
65			b.extend_from_slice(&v.to_le_bytes());
66			b
67		}
68		Value::Int16(v) => {
69			let mut b = vec![Type::Int16.to_u8()];
70			b.extend_from_slice(&v.to_le_bytes());
71			b
72		}
73
74		Value::Float4(v) => {
75			let mut b = vec![Type::Float4.to_u8()];
76			b.extend_from_slice(&v.value().to_bits().to_le_bytes());
77			b
78		}
79		Value::Float8(v) => {
80			let mut b = vec![Type::Float8.to_u8()];
81			b.extend_from_slice(&v.value().to_bits().to_le_bytes());
82			b
83		}
84
85		Value::Date(v) => {
86			let mut b = vec![Type::Date.to_u8()];
87			b.extend_from_slice(&v.to_days_since_epoch().to_le_bytes());
88			b
89		}
90		Value::DateTime(v) => {
91			let mut b = vec![Type::DateTime.to_u8()];
92			let (seconds, nanos) = v.to_parts();
93			b.extend_from_slice(&seconds.to_le_bytes());
94			b.extend_from_slice(&nanos.to_le_bytes());
95			b
96		}
97		Value::Time(v) => {
98			let mut b = vec![Type::Time.to_u8()];
99			b.extend_from_slice(&v.to_nanos_since_midnight().to_le_bytes());
100			b
101		}
102		Value::Duration(v) => {
103			let mut b = vec![Type::Duration.to_u8()];
104			b.extend_from_slice(&v.get_months().to_le_bytes());
105			b.extend_from_slice(&v.get_days().to_le_bytes());
106			b.extend_from_slice(&v.get_nanos().to_le_bytes());
107			b
108		}
109
110		Value::Uuid4(v) => {
111			let mut b = vec![Type::Uuid4.to_u8()];
112			b.extend_from_slice(v.as_bytes());
113			b
114		}
115		Value::Uuid7(v) => {
116			let mut b = vec![Type::Uuid7.to_u8()];
117			b.extend_from_slice(v.as_bytes());
118			b
119		}
120		Value::IdentityId(v) => {
121			let mut b = vec![Type::IdentityId.to_u8()];
122			b.extend_from_slice(v.as_bytes());
123			b
124		}
125
126		Value::Utf8(v) => {
127			let s = v.as_bytes();
128			let mut b = vec![Type::Utf8.to_u8()];
129			b.extend_from_slice(&(s.len() as u32).to_le_bytes());
130			b.extend_from_slice(s);
131			b
132		}
133		Value::Blob(v) => {
134			let s = v.as_bytes();
135			let mut b = vec![Type::Blob.to_u8()];
136			b.extend_from_slice(&(s.len() as u32).to_le_bytes());
137			b.extend_from_slice(s);
138			b
139		}
140
141		Value::Int(_)
142		| Value::Uint(_)
143		| Value::Decimal(_)
144		| Value::DictionaryId(_)
145		| Value::Any(_)
146		| Value::None {
147			..
148		}
149		| Value::Type(_)
150		| Value::List(_) => unreachable!("unsupported value type in Any encoding: {:?}", value),
151	}
152}
153
154/// Decodes bytes produced by `encode_value` back into a `Value`.
155pub fn decode_value(bytes: &[u8]) -> Value {
156	let type_byte = bytes[0];
157	let p = &bytes[1..];
158	let ty = Type::from_u8(type_byte);
159
160	match ty {
161		Type::Boolean => Value::Boolean(p[0] != 0),
162
163		Type::Uint1 => Value::Uint1(p[0]),
164		Type::Uint2 => Value::Uint2(u16::from_le_bytes([p[0], p[1]])),
165		Type::Uint4 => Value::Uint4(u32::from_le_bytes([p[0], p[1], p[2], p[3]])),
166		Type::Uint8 => Value::Uint8(u64::from_le_bytes(p[..8].try_into().unwrap())),
167		Type::Uint16 => Value::Uint16(u128::from_le_bytes(p[..16].try_into().unwrap())),
168
169		Type::Int1 => Value::Int1(p[0] as i8),
170		Type::Int2 => Value::Int2(i16::from_le_bytes([p[0], p[1]])),
171		Type::Int4 => Value::Int4(i32::from_le_bytes([p[0], p[1], p[2], p[3]])),
172		Type::Int8 => Value::Int8(i64::from_le_bytes(p[..8].try_into().unwrap())),
173		Type::Int16 => Value::Int16(i128::from_le_bytes(p[..16].try_into().unwrap())),
174
175		Type::Float4 => {
176			let bits = u32::from_le_bytes([p[0], p[1], p[2], p[3]]);
177			Value::Float4(OrderedF32::try_from(f32::from_bits(bits)).unwrap())
178		}
179		Type::Float8 => {
180			let bits = u64::from_le_bytes(p[..8].try_into().unwrap());
181			Value::Float8(OrderedF64::try_from(f64::from_bits(bits)).unwrap())
182		}
183
184		Type::Date => {
185			let days = i32::from_le_bytes([p[0], p[1], p[2], p[3]]);
186			Value::Date(Date::from_days_since_epoch(days).unwrap())
187		}
188		Type::DateTime => {
189			let seconds = i64::from_le_bytes(p[..8].try_into().unwrap());
190			let nanos = u32::from_le_bytes([p[8], p[9], p[10], p[11]]);
191			Value::DateTime(DateTime::from_parts(seconds, nanos).unwrap())
192		}
193		Type::Time => {
194			let nanos = u64::from_le_bytes(p[..8].try_into().unwrap());
195			Value::Time(Time::from_nanos_since_midnight(nanos).unwrap())
196		}
197		Type::Duration => {
198			let months = i32::from_le_bytes([p[0], p[1], p[2], p[3]]);
199			let days = i32::from_le_bytes([p[4], p[5], p[6], p[7]]);
200			let nanos = i64::from_le_bytes(p[8..16].try_into().unwrap());
201			Value::Duration(Duration::new(months, days, nanos))
202		}
203
204		Type::Uuid4 => {
205			let b: [u8; 16] = p[..16].try_into().unwrap();
206			Value::Uuid4(Uuid4::from(Uuid::from_bytes(b)))
207		}
208		Type::Uuid7 => {
209			let b: [u8; 16] = p[..16].try_into().unwrap();
210			Value::Uuid7(Uuid7::from(Uuid::from_bytes(b)))
211		}
212		Type::IdentityId => {
213			let b: [u8; 16] = p[..16].try_into().unwrap();
214			Value::IdentityId(IdentityId::from(Uuid7::from(Uuid::from_bytes(b))))
215		}
216
217		Type::Utf8 => {
218			let len = u32::from_le_bytes([p[0], p[1], p[2], p[3]]) as usize;
219			let s = str::from_utf8(&p[4..4 + len]).unwrap();
220			Value::Utf8(s.to_string())
221		}
222		Type::Blob => {
223			let len = u32::from_le_bytes([p[0], p[1], p[2], p[3]]) as usize;
224			Value::Blob(Blob::from_slice(&p[4..4 + len]))
225		}
226
227		_ => unreachable!("unsupported type byte {} in Any decoding", type_byte),
228	}
229}
230
231impl Schema {
232	pub fn set_any(&self, row: &mut EncodedValues, index: usize, value: &Value) {
233		let field = &self.fields()[index];
234		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Any);
235		debug_assert!(!row.is_defined(index), "Any field {} already set", index);
236
237		let encoded = encode_value(value);
238		let encoded_len = encoded.len();
239
240		let dynamic_offset = self.dynamic_section_size(row);
241
242		row.0.extend_from_slice(&encoded);
243
244		let ref_slice = &mut row.0.make_mut()[field.offset as usize..field.offset as usize + 8];
245		ref_slice[0..4].copy_from_slice(&(dynamic_offset as u32).to_le_bytes());
246		ref_slice[4..8].copy_from_slice(&(encoded_len as u32).to_le_bytes());
247
248		row.set_valid(index, true);
249	}
250
251	pub fn get_any(&self, row: &EncodedValues, index: usize) -> Value {
252		let field = &self.fields()[index];
253		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Any);
254
255		let ref_slice = &row.as_slice()[field.offset as usize..field.offset as usize + 8];
256		let offset = u32::from_le_bytes([ref_slice[0], ref_slice[1], ref_slice[2], ref_slice[3]]) as usize;
257		let length = u32::from_le_bytes([ref_slice[4], ref_slice[5], ref_slice[6], ref_slice[7]]) as usize;
258
259		let dynamic_start = self.dynamic_section_start();
260		let data_start = dynamic_start + offset;
261		let data_slice = &row.as_slice()[data_start..data_start + length];
262
263		decode_value(data_slice)
264	}
265}
266
267#[cfg(test)]
268pub mod tests {
269	use std::f64::consts::E;
270
271	use reifydb_type::value::{
272		Value,
273		blob::Blob,
274		date::Date,
275		datetime::DateTime,
276		duration::Duration,
277		ordered_f32::OrderedF32,
278		ordered_f64::OrderedF64,
279		time::Time,
280		r#type::Type,
281		uuid::{Uuid4, Uuid7},
282	};
283
284	use crate::encoded::schema::Schema;
285
286	#[test]
287	fn test_any_boolean() {
288		let schema = Schema::testing(&[Type::Any]);
289		let mut row = schema.allocate();
290		schema.set_any(&mut row, 0, &Value::Boolean(true));
291		assert_eq!(schema.get_any(&row, 0), Value::Boolean(true));
292	}
293
294	#[test]
295	fn test_any_integers() {
296		let schema = Schema::testing(&[Type::Any]);
297
298		let cases: &[Value] = &[
299			Value::Int1(-42),
300			Value::Int2(-1000),
301			Value::Int4(-100000),
302			Value::Int8(i64::MIN),
303			Value::Int16(i128::MAX),
304			Value::Uint1(255),
305			Value::Uint2(65535),
306			Value::Uint4(u32::MAX),
307			Value::Uint8(u64::MAX),
308			Value::Uint16(u128::MAX),
309		];
310
311		for case in cases {
312			let mut row = schema.allocate();
313			schema.set_any(&mut row, 0, case);
314			assert_eq!(&schema.get_any(&row, 0), case);
315		}
316	}
317
318	#[test]
319	fn test_any_floats() {
320		let schema = Schema::testing(&[Type::Any]);
321
322		let f4 = Value::Float4(OrderedF32::try_from(3.14f32).unwrap());
323		let mut row = schema.allocate();
324		schema.set_any(&mut row, 0, &f4);
325		assert_eq!(schema.get_any(&row, 0), f4);
326
327		let f8 = Value::Float8(OrderedF64::try_from(E).unwrap());
328		let mut row2 = schema.allocate();
329		schema.set_any(&mut row2, 0, &f8);
330		assert_eq!(schema.get_any(&row2, 0), f8);
331	}
332
333	#[test]
334	fn test_any_temporal() {
335		let schema = Schema::testing(&[Type::Any]);
336
337		let date = Value::Date(Date::new(2025, 7, 4).unwrap());
338		let mut row = schema.allocate();
339		schema.set_any(&mut row, 0, &date);
340		assert_eq!(schema.get_any(&row, 0), date);
341
342		let dt = Value::DateTime(DateTime::new(2025, 1, 1, 12, 0, 0, 0).unwrap());
343		let mut row2 = schema.allocate();
344		schema.set_any(&mut row2, 0, &dt);
345		assert_eq!(schema.get_any(&row2, 0), dt);
346
347		let t = Value::Time(Time::new(14, 30, 45, 123456789).unwrap());
348		let mut row3 = schema.allocate();
349		schema.set_any(&mut row3, 0, &t);
350		assert_eq!(schema.get_any(&row3, 0), t);
351
352		let dur = Value::Duration(Duration::from_seconds(3600));
353		let mut row4 = schema.allocate();
354		schema.set_any(&mut row4, 0, &dur);
355		assert_eq!(schema.get_any(&row4, 0), dur);
356	}
357
358	#[test]
359	fn test_any_uuid() {
360		let schema = Schema::testing(&[Type::Any]);
361
362		let u4 = Value::Uuid4(Uuid4::generate());
363		let mut row = schema.allocate();
364		schema.set_any(&mut row, 0, &u4);
365		assert_eq!(schema.get_any(&row, 0), u4);
366
367		let u7 = Value::Uuid7(Uuid7::generate());
368		let mut row2 = schema.allocate();
369		schema.set_any(&mut row2, 0, &u7);
370		assert_eq!(schema.get_any(&row2, 0), u7);
371	}
372
373	#[test]
374	fn test_any_utf8() {
375		let schema = Schema::testing(&[Type::Any]);
376		let v = Value::Utf8("hello, world!".to_string());
377		let mut row = schema.allocate();
378		schema.set_any(&mut row, 0, &v);
379		assert_eq!(schema.get_any(&row, 0), v);
380	}
381
382	#[test]
383	fn test_any_blob() {
384		let schema = Schema::testing(&[Type::Any]);
385		let v = Value::Blob(Blob::from_slice(&[0xDE, 0xAD, 0xBE, 0xEF]));
386		let mut row = schema.allocate();
387		schema.set_any(&mut row, 0, &v);
388		assert_eq!(schema.get_any(&row, 0), v);
389	}
390
391	#[test]
392	fn test_any_none_via_set_value() {
393		let schema = Schema::testing(&[Type::Any]);
394		let mut row = schema.allocate();
395		schema.set_value(&mut row, 0, &Value::none());
396		assert!(!row.is_defined(0));
397		assert_eq!(schema.get_value(&row, 0), Value::none());
398	}
399
400	#[test]
401	fn test_any_roundtrip_via_set_get_value() {
402		let schema = Schema::testing(&[Type::Any]);
403
404		let cases: &[Value] = &[
405			Value::Boolean(false),
406			Value::Int4(42),
407			Value::Utf8("test".to_string()),
408			Value::Uint8(1234567890),
409		];
410
411		for inner in cases {
412			let wrapped = Value::any(inner.clone());
413			let mut row = schema.allocate();
414			schema.set_value(&mut row, 0, &wrapped);
415			let retrieved = schema.get_value(&row, 0);
416			assert_eq!(retrieved, wrapped, "roundtrip failed for {:?}", inner);
417		}
418	}
419
420	#[test]
421	fn test_any_multiple_fields() {
422		let schema = Schema::testing(&[Type::Any, Type::Int4, Type::Any]);
423		let mut row = schema.allocate();
424
425		schema.set_any(&mut row, 0, &Value::Utf8("first".to_string()));
426		schema.set_i32(&mut row, 1, 99);
427		schema.set_any(&mut row, 2, &Value::Boolean(true));
428
429		assert_eq!(schema.get_any(&row, 0), Value::Utf8("first".to_string()));
430		assert_eq!(schema.get_i32(&row, 1), 99);
431		assert_eq!(schema.get_any(&row, 2), Value::Boolean(true));
432	}
433}