reifydb_core/value/column/data/
reorder.rs

1// Copyright (c) reifydb.com 2025
2// This file is licensed under the AGPL-3.0-or-later, see license.md file
3
4use crate::value::column::ColumnData;
5
6impl ColumnData {
7	pub fn reorder(&mut self, indices: &[usize]) {
8		match self {
9			ColumnData::Bool(container) => container.reorder(indices),
10			ColumnData::Float4(container) => container.reorder(indices),
11			ColumnData::Float8(container) => container.reorder(indices),
12			ColumnData::Int1(container) => container.reorder(indices),
13			ColumnData::Int2(container) => container.reorder(indices),
14			ColumnData::Int4(container) => container.reorder(indices),
15			ColumnData::Int8(container) => container.reorder(indices),
16			ColumnData::Int16(container) => container.reorder(indices),
17			ColumnData::Utf8 {
18				container,
19				..
20			} => container.reorder(indices),
21			ColumnData::Uint1(container) => container.reorder(indices),
22			ColumnData::Uint2(container) => container.reorder(indices),
23			ColumnData::Uint4(container) => container.reorder(indices),
24			ColumnData::Uint8(container) => container.reorder(indices),
25			ColumnData::Uint16(container) => container.reorder(indices),
26			ColumnData::Date(container) => container.reorder(indices),
27			ColumnData::DateTime(container) => container.reorder(indices),
28			ColumnData::Time(container) => container.reorder(indices),
29			ColumnData::Duration(container) => container.reorder(indices),
30			ColumnData::Undefined(container) => container.reorder(indices),
31			ColumnData::RowNumber(container) => container.reorder(indices),
32			ColumnData::IdentityId(container) => container.reorder(indices),
33			ColumnData::Uuid4(container) => container.reorder(indices),
34			ColumnData::Uuid7(container) => container.reorder(indices),
35			ColumnData::Blob {
36				container,
37				..
38			} => container.reorder(indices),
39			ColumnData::Int {
40				container,
41				..
42			} => container.reorder(indices),
43			ColumnData::Uint {
44				container,
45				..
46			} => container.reorder(indices),
47			ColumnData::Decimal {
48				container,
49				..
50			} => container.reorder(indices),
51			ColumnData::Any(container) => container.reorder(indices),
52		}
53	}
54}
55
56#[cfg(test)]
57mod tests {
58	use reifydb_type::Value;
59
60	use crate::value::column::ColumnData;
61
62	#[test]
63	fn test_reorder_bool() {
64		let mut col = ColumnData::bool([true, false, true]);
65		col.reorder(&[2, 0, 1]);
66
67		assert_eq!(col.len(), 3);
68		assert_eq!(col.get_value(0), Value::Boolean(true));
69		assert_eq!(col.get_value(1), Value::Boolean(true));
70		assert_eq!(col.get_value(2), Value::Boolean(false));
71	}
72
73	#[test]
74	fn test_reorder_float4() {
75		let mut col = ColumnData::float4([1.0, 2.0, 3.0]);
76		col.reorder(&[2, 0, 1]);
77
78		assert_eq!(col.len(), 3);
79		// Check data after reordering
80		match col.get_value(0) {
81			Value::Float4(v) => assert_eq!(v.value(), 3.0),
82			_ => panic!("Expected Float4"),
83		}
84		match col.get_value(1) {
85			Value::Float4(v) => assert_eq!(v.value(), 1.0),
86			_ => panic!("Expected Float4"),
87		}
88		match col.get_value(2) {
89			Value::Float4(v) => assert_eq!(v.value(), 2.0),
90			_ => panic!("Expected Float4"),
91		}
92	}
93
94	#[test]
95	fn test_reorder_int4() {
96		let mut col = ColumnData::int4([1, 2, 3]);
97		col.reorder(&[2, 0, 1]);
98
99		assert_eq!(col.len(), 3);
100		assert_eq!(col.get_value(0), Value::Int4(3));
101		assert_eq!(col.get_value(1), Value::Int4(1));
102		assert_eq!(col.get_value(2), Value::Int4(2));
103	}
104
105	#[test]
106	fn test_reorder_string() {
107		let mut col = ColumnData::utf8(["a".to_string(), "b".to_string(), "c".to_string()]);
108		col.reorder(&[2, 0, 1]);
109
110		assert_eq!(col.len(), 3);
111		assert_eq!(col.get_value(0), Value::Utf8("c".to_string()));
112		assert_eq!(col.get_value(1), Value::Utf8("a".to_string()));
113		assert_eq!(col.get_value(2), Value::Utf8("b".to_string()));
114	}
115
116	#[test]
117	fn test_reorder_undefined() {
118		let mut col = ColumnData::undefined(3);
119		col.reorder(&[2, 0, 1]);
120		assert_eq!(col.len(), 3);
121
122		col.reorder(&[1, 0]);
123		assert_eq!(col.len(), 2);
124	}
125
126	#[test]
127	fn test_reorder_identity_id() {
128		use reifydb_type::IdentityId;
129
130		let id1 = IdentityId::generate();
131		let id2 = IdentityId::generate();
132		let id3 = IdentityId::generate();
133
134		let mut col = ColumnData::identity_id([id1, id2, id3]);
135		col.reorder(&[2, 0, 1]);
136
137		assert_eq!(col.len(), 3);
138		assert_eq!(col.get_value(0), Value::IdentityId(id3));
139		assert_eq!(col.get_value(1), Value::IdentityId(id1));
140		assert_eq!(col.get_value(2), Value::IdentityId(id2));
141	}
142}