#[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);
}
}