Skip to main content

reifydb_core/value/column/
row.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4//! Row accessor methods for Columns.
5//!
6//! Provides typed getters for extracting values from columns by name and row index.
7
8use reifydb_type::value::{
9	Value,
10	blob::Blob,
11	date::Date,
12	datetime::DateTime,
13	decimal::Decimal,
14	duration::Duration,
15	identity::IdentityId,
16	int::Int,
17	ordered_f64::OrderedF64,
18	row_number::RowNumber,
19	time::Time,
20	uint::Uint,
21	uuid::{Uuid4, Uuid7},
22};
23
24use crate::value::column::columns::Columns;
25
26impl Columns {
27	/// Get a boolean value from a column at the given row index
28	pub fn get_bool(&self, name: &str, row_idx: usize) -> Option<bool> {
29		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
30			Value::Boolean(v) => Some(v),
31			_ => None,
32		})
33	}
34
35	/// Get an f32 value from a column at the given row index
36	pub fn get_f32(&self, name: &str, row_idx: usize) -> Option<f32> {
37		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
38			Value::Float4(v) => Some(v.into()),
39			_ => None,
40		})
41	}
42
43	/// Get an f64 value from a column at the given row index
44	pub fn get_f64(&self, name: &str, row_idx: usize) -> Option<f64> {
45		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
46			Value::Float8(v) => Some(v.into()),
47			Value::Float4(v) => Some(f64::from(f32::from(v))),
48			_ => None,
49		})
50	}
51
52	/// Get a Float8 (OrderedF64) value from a column at the given row index
53	pub fn get_float8(&self, name: &str, row_idx: usize) -> Option<OrderedF64> {
54		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
55			Value::Float8(v) => Some(v),
56			_ => None,
57		})
58	}
59
60	/// Get an i8 value from a column at the given row index
61	pub fn get_i8(&self, name: &str, row_idx: usize) -> Option<i8> {
62		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
63			Value::Int1(v) => Some(v),
64			_ => None,
65		})
66	}
67
68	/// Get an i16 value from a column at the given row index
69	pub fn get_i16(&self, name: &str, row_idx: usize) -> Option<i16> {
70		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
71			Value::Int2(v) => Some(v),
72			Value::Int1(v) => Some(v as i16),
73			_ => None,
74		})
75	}
76
77	/// Get an i32 value from a column at the given row index
78	pub fn get_i32(&self, name: &str, row_idx: usize) -> Option<i32> {
79		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
80			Value::Int4(v) => Some(v),
81			Value::Int2(v) => Some(v as i32),
82			Value::Int1(v) => Some(v as i32),
83			_ => None,
84		})
85	}
86
87	/// Get an i64 value from a column at the given row index
88	pub fn get_i64(&self, name: &str, row_idx: usize) -> Option<i64> {
89		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
90			Value::Int8(v) => Some(v),
91			Value::Int4(v) => Some(v as i64),
92			Value::Int2(v) => Some(v as i64),
93			Value::Int1(v) => Some(v as i64),
94			_ => None,
95		})
96	}
97
98	/// Get an i128 value from a column at the given row index
99	pub fn get_i128(&self, name: &str, row_idx: usize) -> Option<i128> {
100		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
101			Value::Int16(v) => Some(v),
102			Value::Int8(v) => Some(v as i128),
103			Value::Int4(v) => Some(v as i128),
104			Value::Int2(v) => Some(v as i128),
105			Value::Int1(v) => Some(v as i128),
106			_ => None,
107		})
108	}
109
110	/// Get a u8 value from a column at the given row index
111	pub fn get_u8(&self, name: &str, row_idx: usize) -> Option<u8> {
112		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
113			Value::Uint1(v) => Some(v),
114			_ => None,
115		})
116	}
117
118	/// Get a u16 value from a column at the given row index
119	pub fn get_u16(&self, name: &str, row_idx: usize) -> Option<u16> {
120		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
121			Value::Uint2(v) => Some(v),
122			Value::Uint1(v) => Some(v as u16),
123			_ => None,
124		})
125	}
126
127	/// Get a u32 value from a column at the given row index
128	pub fn get_u32(&self, name: &str, row_idx: usize) -> Option<u32> {
129		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
130			Value::Uint4(v) => Some(v),
131			Value::Uint2(v) => Some(v as u32),
132			Value::Uint1(v) => Some(v as u32),
133			_ => None,
134		})
135	}
136
137	/// Get a u64 value from a column at the given row index
138	pub fn get_u64(&self, name: &str, row_idx: usize) -> Option<u64> {
139		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
140			Value::Uint8(v) => Some(v),
141			Value::Uint4(v) => Some(v as u64),
142			Value::Uint2(v) => Some(v as u64),
143			Value::Uint1(v) => Some(v as u64),
144			_ => None,
145		})
146	}
147
148	/// Get a u128 value from a column at the given row index
149	pub fn get_u128(&self, name: &str, row_idx: usize) -> Option<u128> {
150		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
151			Value::Uint16(v) => Some(v),
152			Value::Uint8(v) => Some(v as u128),
153			Value::Uint4(v) => Some(v as u128),
154			Value::Uint2(v) => Some(v as u128),
155			Value::Uint1(v) => Some(v as u128),
156			_ => None,
157		})
158	}
159
160	/// Get a UTF-8 string value from a column at the given row index
161	pub fn get_string(&self, name: &str, row_idx: usize) -> Option<String> {
162		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
163			Value::Utf8(s) => Some(s),
164			_ => None,
165		})
166	}
167
168	/// Get a Date value from a column at the given row index
169	pub fn get_date(&self, name: &str, row_idx: usize) -> Option<Date> {
170		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
171			Value::Date(v) => Some(v),
172			_ => None,
173		})
174	}
175
176	/// Get a DateTime value from a column at the given row index
177	pub fn get_datetime(&self, name: &str, row_idx: usize) -> Option<DateTime> {
178		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
179			Value::DateTime(v) => Some(v),
180			_ => None,
181		})
182	}
183
184	/// Get a Time value from a column at the given row index
185	pub fn get_time(&self, name: &str, row_idx: usize) -> Option<Time> {
186		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
187			Value::Time(v) => Some(v),
188			_ => None,
189		})
190	}
191
192	/// Get a Duration value from a column at the given row index
193	pub fn get_duration(&self, name: &str, row_idx: usize) -> Option<Duration> {
194		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
195			Value::Duration(v) => Some(v),
196			_ => None,
197		})
198	}
199
200	/// Get an IdentityId value from a column at the given row index
201	pub fn get_identity_id(&self, name: &str, row_idx: usize) -> Option<IdentityId> {
202		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
203			Value::IdentityId(v) => Some(v),
204			_ => None,
205		})
206	}
207
208	/// Get a Uuid4 value from a column at the given row index
209	pub fn get_uuid4(&self, name: &str, row_idx: usize) -> Option<Uuid4> {
210		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
211			Value::Uuid4(v) => Some(v),
212			_ => None,
213		})
214	}
215
216	/// Get a Uuid7 value from a column at the given row index
217	pub fn get_uuid7(&self, name: &str, row_idx: usize) -> Option<Uuid7> {
218		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
219			Value::Uuid7(v) => Some(v),
220			_ => None,
221		})
222	}
223
224	/// Get a Blob value from a column at the given row index
225	pub fn get_blob(&self, name: &str, row_idx: usize) -> Option<Blob> {
226		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
227			Value::Blob(v) => Some(v),
228			_ => None,
229		})
230	}
231
232	/// Get an arbitrary-precision signed integer from a column at the given row index
233	pub fn get_int(&self, name: &str, row_idx: usize) -> Option<Int> {
234		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
235			Value::Int(v) => Some(v),
236			_ => None,
237		})
238	}
239
240	/// Get an arbitrary-precision unsigned integer from a column at the given row index
241	pub fn get_uint(&self, name: &str, row_idx: usize) -> Option<Uint> {
242		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
243			Value::Uint(v) => Some(v),
244			_ => None,
245		})
246	}
247
248	/// Get an arbitrary-precision decimal from a column at the given row index
249	pub fn get_decimal(&self, name: &str, row_idx: usize) -> Option<Decimal> {
250		self.column(name).and_then(|col| match col.data().get_value(row_idx) {
251			Value::Decimal(v) => Some(v),
252			_ => None,
253		})
254	}
255
256	/// Get the raw Value from a column at the given row index
257	pub fn get_value(&self, name: &str, row_idx: usize) -> Option<Value> {
258		self.column(name).map(|col| col.data().get_value(row_idx))
259	}
260
261	/// Check if the value at the given column and row is undefined/null
262	pub fn is_undefined(&self, name: &str, row_idx: usize) -> bool {
263		self.column(name).map(|col| matches!(col.data().get_value(row_idx), Value::None { .. })).unwrap_or(true)
264	}
265
266	/// Get the row number at the given index
267	pub fn get_row_number(&self, row_idx: usize) -> Option<RowNumber> {
268		self.row_numbers.get(row_idx).copied()
269	}
270}