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
use std::collections::HashMap;
use std::string::String;
use super::sql::*;
use super::rel::*;
pub struct SqlToRel {
default_schema: Option<String>,
schemas: HashMap<String, TupleType>
}
impl SqlToRel {
pub fn new(schemas: HashMap<String, TupleType>) -> Self {
SqlToRel { default_schema: None, schemas }
}
pub fn sql_to_rel(&self, sql: &ASTNode) -> Result<Box<Rel>, String> {
match sql {
&ASTNode::SQLSelect { ref projection, ref relation, ref selection, .. } => {
let input = match relation {
&Some(ref r) => self.sql_to_rel(r)?,
&None => Box::new(Rel::EmptyRelation)
};
let input_schema = input.schema();
let expr : Vec<Rex> = projection.iter()
.map(|e| self.sql_to_rex(&e, &input_schema) )
.collect::<Result<Vec<Rex>,String>>()?;
let projection_schema = TupleType {
columns: expr.iter().map( |e| match e {
&Rex::TupleValue(i) => input_schema.columns[i].clone(),
&Rex::ScalarFunction { ref name, .. } => ColumnMeta {
name: name.clone(),
data_type: DataType::Double,
nullable: true
},
_ => unimplemented!()
}).collect()
};
match selection {
&Some(ref filter_expr) => {
let selection_rel = Rel::Selection {
expr: self.sql_to_rex(&filter_expr, &input_schema.clone())?,
input: input,
schema: input_schema.clone()
};
Ok(Box::new(Rel::Projection {
expr: expr,
input: Box::new(selection_rel),
schema: projection_schema.clone()
}))
},
_ => {
Ok(Box::new(Rel::Projection {
expr: expr,
input: input,
schema: projection_schema.clone()
}))
}
}
},
&ASTNode::SQLIdentifier { ref id, .. } => {
match self.schemas.get(id) {
Some(schema) => Ok(Box::new(Rel::TableScan {
schema_name: String::from("default"),
table_name: id.clone(),
schema: schema.clone()
})),
None => panic!("no schema found for table")
}
},
_ => Err(format!("sql_to_rel does not support this relation: {:?}", sql))
}
}
pub fn sql_to_rex(&self, sql: &ASTNode, tt: &TupleType) -> Result<Rex, String> {
match sql {
&ASTNode::SQLLiteralInt(n) =>
Ok(Rex::Literal(Value::UnsignedLong(n as u64))),
&ASTNode::SQLIdentifier { ref id, .. } => {
match tt.columns.iter().position(|c| c.name.eq(id) ) {
Some(index) => Ok(Rex::TupleValue(index)),
None => Err(String::from("Invalid identifier"))
}
},
&ASTNode::SQLBinaryExpr { ref left, ref op, ref right } => {
let operator = match op {
&SQLOperator::GT => Operator::Gt,
_ => unimplemented!()
};
Ok(Rex::BinaryExpr {
left: Box::new(self.sql_to_rex(&left, &tt)?),
op: operator,
right: Box::new(self.sql_to_rex(&right, &tt)?),
})
},
&ASTNode::SQLFunction { ref id, ref args } => {
let rex_args = args.iter()
.map(|a| self.sql_to_rex(a, tt))
.collect::<Result<Vec<Rex>, String>>()?;
Ok(Rex::ScalarFunction { name: id.clone(), args: rex_args })
},
_ => Err(String::from(format!("Unsupported ast node {:?}", sql)))
}
}
}