reifydb-type 0.4.11

Core type system and value representations for ReifyDB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::{
	fmt::{Display, Formatter},
	str::FromStr,
};

use serde::{Deserialize, Serialize};

pub mod get;
pub mod input_types;
pub mod promote;
pub mod super_type;

use std::fmt;

use crate::value::Value;

/// All possible RQL data types
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Type {
	/// A boolean: true or false.
	Boolean,
	/// A 4-byte floating point
	Float4,
	/// An 8-byte floating point
	Float8,
	/// A 1-byte signed integer
	Int1,
	/// A 2-byte signed integer
	Int2,
	/// A 4-byte signed integer
	Int4,
	/// An 8-byte signed integer
	Int8,
	/// A 16-byte signed integer
	Int16,
	/// A UTF-8 encoded text.
	Utf8,
	/// A 1-byte unsigned integer
	Uint1,
	/// A 2-byte unsigned integer
	Uint2,
	/// A 4-byte unsigned integer
	Uint4,
	/// A 8-byte unsigned integer
	Uint8,
	/// A 16-byte unsigned integer
	Uint16,
	/// A date value (year, month, day)
	Date,
	/// A date and time value with nanosecond precision in SVTC
	DateTime,
	/// A time value (hour, minute, second, nanosecond)
	Time,
	/// A duration representing a duration
	Duration,
	/// An identity identifier (UUID v7)
	IdentityId,
	/// A UUID version 4 (random)
	Uuid4,
	/// A UUID version 7 (timestamp-based)
	Uuid7,
	/// A binary large object (BLOB)
	Blob,
	/// An arbitrary-precision signed integer
	Int,
	/// An arbitrary-precision unsigned integer
	Uint,
	/// An arbitrary-precision decimal with precision and scale
	Decimal,
	/// An optional type that can hold None or a value of the inner type
	Option(Box<Type>),
	/// A container that can hold any value type
	Any,
	/// A dictionary entry identifier
	DictionaryId,
	/// An ordered list of values of a given element type
	List(Box<Type>),
	/// A record type with named fields
	Record(Vec<(String, Type)>),
	/// A tuple of heterogeneous types
	Tuple(Vec<Type>),
}

impl Type {
	pub fn list_of(ty: Type) -> Self {
		Type::List(Box::new(ty))
	}

	pub fn is_number(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_number(),
			_ => matches!(
				self,
				Type::Float4
					| Type::Float8 | Type::Int1 | Type::Int2
					| Type::Int4 | Type::Int8 | Type::Int16
					| Type::Uint1 | Type::Uint2 | Type::Uint4
					| Type::Uint8 | Type::Uint16 | Type::Int
					| Type::Uint | Type::Decimal
			),
		}
	}

	pub fn is_bool(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_bool(),
			_ => matches!(self, Type::Boolean),
		}
	}

	pub fn is_signed_integer(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_signed_integer(),
			_ => matches!(
				self,
				Type::Int1 | Type::Int2 | Type::Int4 | Type::Int8 | Type::Int16 | Type::Int
			),
		}
	}

	pub fn is_unsigned_integer(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_unsigned_integer(),
			_ => matches!(
				self,
				Type::Uint1 | Type::Uint2 | Type::Uint4 | Type::Uint8 | Type::Uint16 | Type::Uint
			),
		}
	}

	pub fn is_integer(&self) -> bool {
		self.is_signed_integer() || self.is_unsigned_integer()
	}

	pub fn is_floating_point(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_floating_point(),
			_ => matches!(self, Type::Float4 | Type::Float8),
		}
	}

	pub fn is_utf8(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_utf8(),
			_ => matches!(self, Type::Utf8),
		}
	}

	pub fn is_temporal(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_temporal(),
			_ => matches!(self, Type::Date | Type::DateTime | Type::Time | Type::Duration),
		}
	}

	pub fn is_uuid(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_uuid(),
			_ => matches!(self, Type::Uuid4 | Type::Uuid7),
		}
	}

	pub fn is_blob(&self) -> bool {
		match self {
			Type::Option(inner) => inner.is_blob(),
			_ => matches!(self, Type::Blob),
		}
	}

	pub fn is_option(&self) -> bool {
		matches!(self, Type::Option(_))
	}

	/// Returns the inner type if this is an Option type, otherwise returns self
	pub fn inner_type(&self) -> &Type {
		match self {
			Type::Option(inner) => inner,
			other => other,
		}
	}
}

impl Type {
	pub fn to_u8(&self) -> u8 {
		match self {
			Type::Option(inner) => 0x80 | inner.to_u8(),
			Type::Float4 => 1,
			Type::Float8 => 2,
			Type::Int1 => 3,
			Type::Int2 => 4,
			Type::Int4 => 5,
			Type::Int8 => 6,
			Type::Int16 => 7,
			Type::Utf8 => 8,
			Type::Uint1 => 9,
			Type::Uint2 => 10,
			Type::Uint4 => 11,
			Type::Uint8 => 12,
			Type::Uint16 => 13,
			Type::Boolean => 14,
			Type::Date => 15,
			Type::DateTime => 16,
			Type::Time => 17,
			Type::Duration => 18,
			Type::IdentityId => 19,
			Type::Uuid4 => 20,
			Type::Uuid7 => 21,
			Type::Blob => 22,
			Type::Int => 23,
			Type::Decimal => 24,
			Type::Uint => 25,
			Type::Any => 26,
			Type::DictionaryId => 27,
			Type::List(_) => 28,
			Type::Record(_) => 29,
			Type::Tuple(_) => 30,
		}
	}
}

impl Type {
	pub fn from_u8(value: u8) -> Self {
		if value & 0x80 != 0 {
			return Type::Option(Box::new(Type::from_u8(value & 0x7F)));
		}
		match value {
			1 => Type::Float4,
			2 => Type::Float8,
			3 => Type::Int1,
			4 => Type::Int2,
			5 => Type::Int4,
			6 => Type::Int8,
			7 => Type::Int16,
			8 => Type::Utf8,
			9 => Type::Uint1,
			10 => Type::Uint2,
			11 => Type::Uint4,
			12 => Type::Uint8,
			13 => Type::Uint16,
			14 => Type::Boolean,
			15 => Type::Date,
			16 => Type::DateTime,
			17 => Type::Time,
			18 => Type::Duration,
			19 => Type::IdentityId,
			20 => Type::Uuid4,
			21 => Type::Uuid7,
			22 => Type::Blob,
			23 => Type::Int,
			24 => Type::Decimal,
			25 => Type::Uint,
			26 => Type::Any,
			27 => Type::DictionaryId,
			28 => Type::list_of(Type::Any),
			29 => Type::Record(Vec::new()),
			30 => Type::Tuple(Vec::new()),
			_ => unreachable!(),
		}
	}
}

impl Type {
	pub fn size(&self) -> usize {
		match self {
			Type::Boolean => 1,
			Type::Float4 => 4,
			Type::Float8 => 8,
			Type::Int1 => 1,
			Type::Int2 => 2,
			Type::Int4 => 4,
			Type::Int8 => 8,
			Type::Int16 => 16,
			Type::Utf8 => 8, // offset: u32 + length: u32
			Type::Uint1 => 1,
			Type::Uint2 => 2,
			Type::Uint4 => 4,
			Type::Uint8 => 8,
			Type::Uint16 => 16,
			Type::Date => 4,
			Type::DateTime => 8, // nanos: u64
			Type::Time => 8,
			Type::Duration => 16,   // months: i32 + days: i32 + nanos: i64
			Type::IdentityId => 16, // UUID v7 is 16 bytes
			Type::Uuid4 => 16,
			Type::Uuid7 => 16,
			Type::Blob => 8, // offset: u32 + length: u32
			Type::Int => 16, // i128 inline or dynamic
			// storage with offset + length
			Type::Uint => 16, // u128 inline or dynamic
			// storage with offset + length
			Type::Decimal => 16, // i128 inline or dynamic
			// storage with offset + length
			Type::Option(inner) => inner.size(), // size determined by inner type
			Type::Any => 8,                      // pointer size on 64-bit systems
			Type::List(_) => 8,                  // pointer size (Vec is heap-allocated)
			Type::Record(_) => 8,                // pointer size (Vec is heap-allocated)
			Type::Tuple(_) => 8,                 // pointer size (Vec is heap-allocated)
			Type::DictionaryId => 16,            /* max possible; actual size determined by constraint's
			                                       * id_type */
		}
	}

	pub fn alignment(&self) -> usize {
		match self {
			Type::Boolean => 1,
			Type::Float4 => 4,
			Type::Float8 => 8,
			Type::Int1 => 1,
			Type::Int2 => 2,
			Type::Int4 => 4,
			Type::Int8 => 8,
			Type::Int16 => 16,
			Type::Utf8 => 4, // u32 alignment
			Type::Uint1 => 1,
			Type::Uint2 => 2,
			Type::Uint4 => 4,
			Type::Uint8 => 8,
			Type::Uint16 => 16,
			Type::Date => 4,
			Type::DateTime => 8,
			Type::Time => 8,
			Type::Duration => 8,
			Type::IdentityId => 8, // Same alignment as UUID
			Type::Uuid4 => 8,
			Type::Uuid7 => 8,
			Type::Blob => 4, // u32 alignment
			Type::Int => 16, // i128 alignment for
			// inline storage
			Type::Uint => 16, // u128 alignment for
			// inline storage
			Type::Decimal => 16, // i128 alignment for
			// inline storage
			Type::Option(inner) => inner.alignment(),
			Type::Any => 8, // pointer alignment
			Type::DictionaryId => 16,
			Type::List(_) => 8,   // pointer alignment
			Type::Record(_) => 8, // pointer alignment
			Type::Tuple(_) => 8,  // pointer alignment
		}
	}
}

impl Display for Type {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		match self {
			Type::Boolean => f.write_str("Boolean"),
			Type::Float4 => f.write_str("Float4"),
			Type::Float8 => f.write_str("Float8"),
			Type::Int1 => f.write_str("Int1"),
			Type::Int2 => f.write_str("Int2"),
			Type::Int4 => f.write_str("Int4"),
			Type::Int8 => f.write_str("Int8"),
			Type::Int16 => f.write_str("Int16"),
			Type::Utf8 => f.write_str("Utf8"),
			Type::Uint1 => f.write_str("Uint1"),
			Type::Uint2 => f.write_str("Uint2"),
			Type::Uint4 => f.write_str("Uint4"),
			Type::Uint8 => f.write_str("Uint8"),
			Type::Uint16 => f.write_str("Uint16"),
			Type::Date => f.write_str("Date"),
			Type::DateTime => f.write_str("DateTime"),
			Type::Time => f.write_str("Time"),
			Type::Duration => f.write_str("Duration"),
			Type::IdentityId => f.write_str("IdentityId"),
			Type::Uuid4 => f.write_str("Uuid4"),
			Type::Uuid7 => f.write_str("Uuid7"),
			Type::Blob => f.write_str("Blob"),
			Type::Int => f.write_str("Int"),
			Type::Uint => f.write_str("Uint"),
			Type::Decimal => f.write_str("Decimal"),
			Type::Option(inner) => write!(f, "Option({inner})"),
			Type::Any => f.write_str("Any"),
			Type::DictionaryId => f.write_str("DictionaryId"),
			Type::List(inner) => write!(f, "List({inner})"),
			Type::Record(fields) => {
				f.write_str("Record(")?;
				for (i, (name, ty)) in fields.iter().enumerate() {
					if i > 0 {
						f.write_str(", ")?;
					}
					write!(f, "{}: {}", name, ty)?;
				}
				f.write_str(")")
			}
			Type::Tuple(types) => {
				f.write_str("Tuple(")?;
				for (i, ty) in types.iter().enumerate() {
					if i > 0 {
						f.write_str(", ")?;
					}
					write!(f, "{}", ty)?;
				}
				f.write_str(")")
			}
		}
	}
}

impl From<&Value> for Type {
	fn from(value: &Value) -> Self {
		match value {
			Value::None {
				inner,
			} => Type::Option(Box::new(inner.clone())),
			Value::Boolean(_) => Type::Boolean,
			Value::Float4(_) => Type::Float4,
			Value::Float8(_) => Type::Float8,
			Value::Int1(_) => Type::Int1,
			Value::Int2(_) => Type::Int2,
			Value::Int4(_) => Type::Int4,
			Value::Int8(_) => Type::Int8,
			Value::Int16(_) => Type::Int16,
			Value::Utf8(_) => Type::Utf8,
			Value::Uint1(_) => Type::Uint1,
			Value::Uint2(_) => Type::Uint2,
			Value::Uint4(_) => Type::Uint4,
			Value::Uint8(_) => Type::Uint8,
			Value::Uint16(_) => Type::Uint16,
			Value::Date(_) => Type::Date,
			Value::DateTime(_) => Type::DateTime,
			Value::Time(_) => Type::Time,
			Value::Duration(_) => Type::Duration,
			Value::IdentityId(_) => Type::IdentityId,
			Value::Uuid4(_) => Type::Uuid4,
			Value::Uuid7(_) => Type::Uuid7,
			Value::Blob(_) => Type::Blob,
			Value::Int(_) => Type::Int,
			Value::Uint(_) => Type::Uint,
			Value::Decimal(_) => Type::Decimal,
			Value::Any(_) => Type::Any,
			Value::DictionaryId(_) => Type::DictionaryId,
			Value::Type(t) => t.clone(),
			Value::List(items) => {
				let element_type = items.first().map(Type::from).unwrap_or(Type::Any);
				Type::list_of(element_type)
			}
			Value::Record(fields) => {
				Type::Record(fields.iter().map(|(k, v)| (k.clone(), Type::from(v))).collect())
			}
			Value::Tuple(items) => Type::Tuple(items.iter().map(Type::from).collect()),
		}
	}
}

impl FromStr for Type {
	type Err = ();

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		let upper = s.to_uppercase();
		// Handle Option<T> syntax
		if upper.starts_with("OPTION<") && upper.ends_with('>') {
			let inner_str = &s[7..s.len() - 1]; // extract between "OPTION<" and ">"
			let inner = Type::from_str(inner_str)?;
			return Ok(Type::Option(Box::new(inner)));
		}
		match upper.as_str() {
			"BOOL" | "BOOLEAN" => Ok(Type::Boolean),
			"FLOAT4" => Ok(Type::Float4),
			"FLOAT8" => Ok(Type::Float8),
			"INT1" => Ok(Type::Int1),
			"INT2" => Ok(Type::Int2),
			"INT4" => Ok(Type::Int4),
			"INT8" => Ok(Type::Int8),
			"INT16" => Ok(Type::Int16),
			"UTF8" | "TEXT" => Ok(Type::Utf8),
			"UINT1" => Ok(Type::Uint1),
			"UINT2" => Ok(Type::Uint2),
			"UINT4" => Ok(Type::Uint4),
			"UINT8" => Ok(Type::Uint8),
			"UINT16" => Ok(Type::Uint16),
			"DATE" => Ok(Type::Date),
			"DATETIME" => Ok(Type::DateTime),
			"TIME" => Ok(Type::Time),
			"DURATION" | "INTERVAL" => Ok(Type::Duration),
			"IDENTITYID" | "IDENTITY_ID" => Ok(Type::IdentityId),
			"UUID4" => Ok(Type::Uuid4),
			"UUID7" => Ok(Type::Uuid7),
			"BLOB" => Ok(Type::Blob),
			"INT" => Ok(Type::Int),
			"UINT" => Ok(Type::Uint),
			"DECIMAL" => Ok(Type::Decimal),
			"ANY" => Ok(Type::Any),
			"DICTIONARYID" | "DICTIONARY_ID" => Ok(Type::DictionaryId),
			_ => Err(()),
		}
	}
}