Skip to main content

reifydb_core/value/column/
row.rs

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