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::IdentityId(container) => container.reorder(indices),
32			ColumnData::Uuid4(container) => container.reorder(indices),
33			ColumnData::Uuid7(container) => container.reorder(indices),
34			ColumnData::Blob {
35				container,
36				..
37			} => container.reorder(indices),
38			ColumnData::Int {
39				container,
40				..
41			} => container.reorder(indices),
42			ColumnData::Uint {
43				container,
44				..
45			} => container.reorder(indices),
46			ColumnData::Decimal {
47				container,
48				..
49			} => container.reorder(indices),
50			ColumnData::Any(container) => container.reorder(indices),
51		}
52	}
53}
54
55#[cfg(test)]
56mod tests {
57	use reifydb_type::Value;
58
59	use crate::value::column::ColumnData;
60
61	#[test]
62	fn test_reorder_bool() {
63		let mut col = ColumnData::bool([true, false, true]);
64		col.reorder(&[2, 0, 1]);
65
66		assert_eq!(col.len(), 3);
67		assert_eq!(col.get_value(0), Value::Boolean(true));
68		assert_eq!(col.get_value(1), Value::Boolean(true));
69		assert_eq!(col.get_value(2), Value::Boolean(false));
70	}
71
72	#[test]
73	fn test_reorder_float4() {
74		let mut col = ColumnData::float4([1.0, 2.0, 3.0]);
75		col.reorder(&[2, 0, 1]);
76
77		assert_eq!(col.len(), 3);
78		// Check data after reordering
79		match col.get_value(0) {
80			Value::Float4(v) => assert_eq!(v.value(), 3.0),
81			_ => panic!("Expected Float4"),
82		}
83		match col.get_value(1) {
84			Value::Float4(v) => assert_eq!(v.value(), 1.0),
85			_ => panic!("Expected Float4"),
86		}
87		match col.get_value(2) {
88			Value::Float4(v) => assert_eq!(v.value(), 2.0),
89			_ => panic!("Expected Float4"),
90		}
91	}
92
93	#[test]
94	fn test_reorder_int4() {
95		let mut col = ColumnData::int4([1, 2, 3]);
96		col.reorder(&[2, 0, 1]);
97
98		assert_eq!(col.len(), 3);
99		assert_eq!(col.get_value(0), Value::Int4(3));
100		assert_eq!(col.get_value(1), Value::Int4(1));
101		assert_eq!(col.get_value(2), Value::Int4(2));
102	}
103
104	#[test]
105	fn test_reorder_string() {
106		let mut col = ColumnData::utf8(["a".to_string(), "b".to_string(), "c".to_string()]);
107		col.reorder(&[2, 0, 1]);
108
109		assert_eq!(col.len(), 3);
110		assert_eq!(col.get_value(0), Value::Utf8("c".to_string()));
111		assert_eq!(col.get_value(1), Value::Utf8("a".to_string()));
112		assert_eq!(col.get_value(2), Value::Utf8("b".to_string()));
113	}
114
115	#[test]
116	fn test_reorder_undefined() {
117		let mut col = ColumnData::undefined(3);
118		col.reorder(&[2, 0, 1]);
119		assert_eq!(col.len(), 3);
120
121		col.reorder(&[1, 0]);
122		assert_eq!(col.len(), 2);
123	}
124
125	#[test]
126	fn test_reorder_identity_id() {
127		use reifydb_type::IdentityId;
128
129		let id1 = IdentityId::generate();
130		let id2 = IdentityId::generate();
131		let id3 = IdentityId::generate();
132
133		let mut col = ColumnData::identity_id([id1, id2, id3]);
134		col.reorder(&[2, 0, 1]);
135
136		assert_eq!(col.len(), 3);
137		assert_eq!(col.get_value(0), Value::IdentityId(id3));
138		assert_eq!(col.get_value(1), Value::IdentityId(id1));
139		assert_eq!(col.get_value(2), Value::IdentityId(id2));
140	}
141}