-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathcolumn.rs
138 lines (112 loc) · 3.16 KB
/
column.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2022 RisingLight Project Authors. Licensed under Apache-2.0.
use serde::{Deserialize, Serialize};
use crate::types::{ColumnId, DataType};
/// A descriptor of a column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ColumnDesc {
datatype: DataType,
name: String,
is_primary: bool,
}
impl ColumnDesc {
pub const fn new(datatype: DataType, name: String, is_primary: bool) -> Self {
ColumnDesc {
datatype,
name,
is_primary,
}
}
pub fn set_primary(&mut self, is_primary: bool) {
self.is_primary = is_primary;
}
pub fn is_primary(&self) -> bool {
self.is_primary
}
pub fn set_nullable(&mut self, is_nullable: bool) {
self.datatype.nullable = is_nullable;
}
pub fn is_nullable(&self) -> bool {
self.datatype.nullable
}
pub fn datatype(&self) -> &DataType {
&self.datatype
}
pub fn name(&self) -> &str {
&self.name
}
}
impl DataType {
pub const fn to_column(self, name: String) -> ColumnDesc {
ColumnDesc::new(self, name, false)
}
pub const fn to_column_primary_key(self, name: String) -> ColumnDesc {
ColumnDesc::new(self, name, true)
}
}
/// The catalog of a column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ColumnCatalog {
id: ColumnId,
desc: ColumnDesc,
}
impl ColumnCatalog {
pub fn new(id: ColumnId, desc: ColumnDesc) -> ColumnCatalog {
ColumnCatalog { id, desc }
}
pub fn id(&self) -> ColumnId {
self.id
}
pub fn set_id(&mut self, id: ColumnId) {
self.id = id
}
pub fn name(&self) -> &str {
&self.desc.name
}
pub fn desc(&self) -> &ColumnDesc {
&self.desc
}
pub fn datatype(&self) -> DataType {
self.desc.datatype
}
pub fn set_primary(&mut self, is_primary: bool) {
self.desc.set_primary(is_primary);
}
pub fn is_primary(&self) -> bool {
self.desc.is_primary()
}
pub fn set_nullable(&mut self, is_nullable: bool) {
self.desc.set_nullable(is_nullable);
}
pub fn is_nullable(&self) -> bool {
self.desc.is_nullable()
}
}
/// Find the id of the sort key among column catalogs
pub fn find_sort_key_id(column_infos: &[ColumnCatalog]) -> Option<usize> {
let mut key = None;
for (id, column_info) in column_infos.iter().enumerate() {
if column_info.is_primary() {
if key.is_some() {
panic!("only one primary key is supported");
}
key = Some(id);
}
}
key
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::DataTypeKind;
#[test]
fn test_column_catalog() {
let col_desc = DataTypeKind::Int32.not_null().to_column("grade".into());
let mut col_catalog = ColumnCatalog::new(0, col_desc);
assert_eq!(col_catalog.id(), 0);
assert!(!col_catalog.is_primary());
assert!(!col_catalog.is_nullable());
assert_eq!(col_catalog.name(), "grade");
col_catalog.set_primary(true);
assert!(col_catalog.is_primary());
}
}