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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#[derive(Debug,Clone,Serialize,Deserialize)]
pub enum DataType {
UnsignedLong,
String,
Double
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct ColumnMeta {
pub name: String,
pub data_type: DataType,
pub nullable: bool
}
impl ColumnMeta {
pub fn new(name: &str, data_type: DataType, nullable: bool) -> Self {
ColumnMeta {
name: name.to_string(),
data_type: data_type,
nullable: nullable
}
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct TupleType {
pub columns: Vec<ColumnMeta>
}
impl TupleType {
pub fn empty() -> Self { TupleType { columns: vec![] } }
pub fn new(columns: Vec<ColumnMeta>) -> Self { TupleType { columns: columns } }
pub fn column(&self, name: &str) -> Option<(usize, &ColumnMeta)> {
self.columns.iter()
.enumerate()
.find(|&(_,c)| c.name == name)
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct FunctionMeta {
pub name: String,
pub args: Vec<ColumnMeta>,
pub return_type: DataType
}
#[derive(Debug,Clone)]
pub struct Tuple {
pub values: Vec<Value>
}
impl Tuple {
pub fn new(v: Vec<Value>) -> Self {
Tuple { values: v }
}
pub fn to_string(&self) -> String {
let value_strings : Vec<String> = self.values.iter()
.map(|v| v.to_string())
.collect();
value_strings.join(",")
}
}
#[derive(Debug,Clone,PartialEq,PartialOrd,Serialize,Deserialize)]
pub enum Value {
UnsignedLong(u64),
String(String),
Boolean(bool),
Double(f64)
}
impl Value {
fn to_string(&self) -> String {
match self {
&Value::UnsignedLong(l) => l.to_string(),
&Value::Double(d) => d.to_string(),
&Value::Boolean(b) => b.to_string(),
&Value::String(ref s) => s.clone(),
}
}
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub enum Operator {
Eq,
NotEq,
Lt,
LtEq,
Gt,
GtEq,
}
#[derive(Debug,Clone,Serialize, Deserialize)]
pub enum Rex {
TupleValue(usize),
Literal(Value),
BinaryExpr { left: Box<Rex>, op: Operator, right: Box<Rex> },
ScalarFunction { name: String, args: Vec<Rex> }
}
impl Rex {
pub fn eq(&self, other: &Rex) -> Rex {
Rex::BinaryExpr {
left: Box::new(self.clone()),
op: Operator::Eq,
right: Box::new(other.clone())
}
}
}
#[derive(Debug,Clone,Serialize, Deserialize)]
pub enum Rel {
Projection { expr: Vec<Rex>, input: Box<Rel>, schema: TupleType },
Selection { expr: Rex, input: Box<Rel>, schema: TupleType },
TableScan { schema_name: String, table_name: String, schema: TupleType },
CsvFile { filename: String, schema: TupleType },
EmptyRelation
}
impl Rel {
pub fn schema(&self) -> TupleType {
match self {
&Rel::EmptyRelation => TupleType::empty(),
&Rel::TableScan { ref schema, .. } => schema.clone(),
&Rel::CsvFile { ref schema, .. } => schema.clone(),
&Rel::Projection { ref schema, .. } => schema.clone(),
&Rel::Selection { ref schema, .. } => schema.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::Rel::*;
use super::Rex::*;
use super::Value::*;
extern crate serde_json;
#[test]
fn serde() {
let tt = TupleType {
columns: vec![
ColumnMeta { name: "id".to_string(), data_type: DataType::UnsignedLong, nullable: false },
ColumnMeta { name: "name".to_string(), data_type: DataType::String, nullable: false }
]
};
let csv = CsvFile { filename: "test/people.csv".to_string(), schema: tt.clone() };
let filter_expr = BinaryExpr {
left: Box::new(TupleValue(0)),
op: Operator::Eq,
right: Box::new(Literal(UnsignedLong(2)))
};
let plan = Selection {
expr: filter_expr,
input: Box::new(csv),
schema: tt.clone()
};
let s = serde_json::to_string(&plan).unwrap();
println!("serialized: {}", s);
}
}