1use crate::table_metadata::{ColumnInTable, ColumnKind};
2
3const CLUSTER_KEY_PROPERTY_POSITION: i32 = 1_000;
5const NON_PK_PROPERTY_POSITION: i32 = 1_000_000;
7
8#[allow(clippy::doc_lazy_continuation)]
9#[allow(clippy::ptr_arg)]
17pub(crate) fn sort_columns(c: &mut Vec<ColumnInTable>) {
18 for column in c.iter_mut() {
23 if column.position == -1 {
24 assert_eq!(ColumnKind::Regular, column.kind());
25 column.position = NON_PK_PROPERTY_POSITION;
26 } else if column.kind() == ColumnKind::Clustering {
27 assert_ne!(-1, column.position);
28 column.position += CLUSTER_KEY_PROPERTY_POSITION;
29 } else {
30 assert_ne!(-1, column.position);
31 assert_eq!(ColumnKind::PartitionKey, column.kind())
32 }
33 }
34
35 c.sort_by(|a, b| a.position.cmp(&b.position));
36}
37
38#[cfg(test)]
39mod test {
40 use super::*;
41
42 #[test]
43 fn test_sort_columns() {
44 let mut columns = vec![
45 ColumnInTable {
46 column_name: "".to_string(),
47 kind: ColumnKind::PartitionKey.to_string(),
48 position: 0,
49 data_type: "".to_string(),
50 },
51 ColumnInTable {
52 column_name: "".to_string(),
53 kind: ColumnKind::Clustering.to_string(),
54 position: 2,
55 data_type: "".to_string(),
56 },
57 ColumnInTable {
58 column_name: "".to_string(),
59 kind: ColumnKind::Regular.to_string(),
60 position: -1,
61 data_type: "".to_string(),
62 },
63 ColumnInTable {
64 column_name: "".to_string(),
65 kind: ColumnKind::Clustering.to_string(),
66 position: 1,
67 data_type: "".to_string(),
68 },
69 ColumnInTable {
70 column_name: "".to_string(),
71 kind: ColumnKind::PartitionKey.to_string(),
72 position: 1,
73 data_type: "".to_string(),
74 },
75 ColumnInTable {
76 column_name: "".to_string(),
77 kind: ColumnKind::Clustering.to_string(),
78 position: 0,
79 data_type: "".to_string(),
80 },
81 ];
82
83 sort_columns(&mut columns);
84
85 assert_eq!(0, columns[0].position);
86 assert_eq!(1, columns[1].position);
87 assert_eq!(CLUSTER_KEY_PROPERTY_POSITION, columns[2].position);
88 assert_eq!(CLUSTER_KEY_PROPERTY_POSITION + 1, columns[3].position);
89 assert_eq!(CLUSTER_KEY_PROPERTY_POSITION + 2, columns[4].position);
90
91 assert_eq!(NON_PK_PROPERTY_POSITION, columns[5].position);
92 }
93}