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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
use std::rc::Rc;
use crate::ast::command::{
agg, append, drop, from, join, let_cmd, limit, parse, select, sort, where_cmd, window, within,
};
use crate::env::Environment;
use crate::translation::{PendingQueryResult, PendingStatementResult};
use hamelin_lib::antlr::hamelinparser::{
CommandContextAll, FromCommandContextAttrs, PipelineAltContextAttrs, PipelineContextAll,
UnionCommandContextAttrs,
};
use hamelin_lib::err::TranslationError;
use hamelin_lib::sql::expression::SQLExpression;
use super::command::from::{FromAliasing, FromAnonymous};
use super::command::{explode, match_cmd_cte, nest, unnest};
use super::QueryTranslationContext;
pub struct HamelinPipeline {
pub tree: Rc<PipelineContextAll<'static>>,
pub cte: Environment,
pub context: Rc<QueryTranslationContext>,
pub within: Option<SQLExpression>,
}
impl HamelinPipeline {
pub fn new(
tree: Rc<PipelineContextAll<'static>>,
cte: Environment,
context: Rc<QueryTranslationContext>,
) -> Self {
Self {
tree,
cte,
context,
within: None,
}
}
pub fn translate(&self) -> PendingStatementResult {
match self.tree.as_ref() {
PipelineContextAll::PipelineAltContext(ctx) => {
let mut pending_query_result = PendingQueryResult::default();
let size = ctx.command_all().len();
for (i, command) in ctx.command_all().into_iter().enumerate() {
match command.as_ref() {
CommandContextAll::WhereCommandContext(ctx) => pending_query_result
.consume_result(where_cmd::translate(
&ctx,
&self,
&pending_query_result.translation,
)),
CommandContextAll::LetCommandContext(ctx) => let_cmd::translate(
&ctx,
&self,
&mut pending_query_result,
),
CommandContextAll::DropCommandContext(ctx) => pending_query_result
.consume_result(drop::translate(
&ctx,
&pending_query_result.translation,
)),
CommandContextAll::SortCommandContext(ctx) => pending_query_result
.consume_result(sort::translate(
&ctx,
&self,
&pending_query_result.translation,
)),
CommandContextAll::SelectCommandContext(ctx) => {
select::translate(
&ctx,
&self,
&mut pending_query_result,
)
},
CommandContextAll::WindowCommandContext(ctx) => {
window::translate(
&ctx,
&self,
&mut pending_query_result,
)
},
CommandContextAll::LimitCommandContext(ctx) => pending_query_result
.consume_result(limit::translate(
&ctx,
&self,
&pending_query_result.translation,
)),
CommandContextAll::WithinCommandContext(ctx) => pending_query_result
.consume_result(within::translate(
&ctx,
&pending_query_result.translation,
self.context.clone(),
)),
CommandContextAll::AggCommandContext(ctx) => {
agg::translate(&ctx, &self, &mut pending_query_result)
}
CommandContextAll::FromCommandContext(ctx) => pending_query_result
.consume_result(from::uber_from(
ctx.fromClause_all(),
&self,
&pending_query_result.translation,
FromAliasing::AliasingAllowed,
FromAnonymous::DoNotNest,
)),
CommandContextAll::UnionCommandContext(ctx) => pending_query_result.consume_result(from::uber_from(
ctx.fromClause_all(),
&self,
&pending_query_result.translation,
FromAliasing::RaiseErrorOnAlias(
"Not allowed to assign in UNION. UNION does not support nesting.",
),
FromAnonymous::DoNotNest,
)),
CommandContextAll::ParseCommandContext(ctx) => {
pending_query_result.consume_result(parse::translate(
&ctx,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::JoinCommandContext(base_parser_rule_context) => {
pending_query_result.consume_result(join::translate(
&base_parser_rule_context,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::UnnestCommandContext(base_parser_rule_context) => {
pending_query_result.consume_result(unnest::translate(
&base_parser_rule_context,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::ExplodeCommandContext(base_parser_rule_context) => {
pending_query_result.consume_result(explode::translate(
&base_parser_rule_context,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::MatchCommandContext(base_parser_rule_context) => {
pending_query_result.consume_result(match_cmd_cte::translate_with_cte(
&base_parser_rule_context,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::NestCommandContext(base_parser_rule_context) => {
pending_query_result.consume_result(nest::translate(
&base_parser_rule_context,
&self,
&pending_query_result.translation,
))
}
CommandContextAll::AppendCommandContext(ctx) => {
// Append is special. It creates DML, not SQL.
// Therefore, no matter what, we bail out here.
// That said: before we bail out, we need to make sure that it is in the last position, and that
// the query author wasn't trying to do something weird.
if i != size - 1 {
pending_query_result.errors.add(TranslationError::msg(
ctx,
"APPEND must be the last command in a pipeline",
));
}
return match append::translate(
&ctx,
&self,
&pending_query_result.translation,
) {
Ok(translation) => PendingStatementResult::default()
.with_translation(translation)
.with_errors(pending_query_result.errors),
Err(e) => {
pending_query_result.errors.extend(e);
PendingStatementResult::default()
.with_errors(pending_query_result.errors)
}
};
}
CommandContextAll::Error(_) => {
pending_query_result.errors.add(TranslationError::msg(
ctx,
"parse error",
));
}
};
}
if pending_query_result
.translation
.query
.projections
.is_empty()
&& pending_query_result.errors.is_empty()
{
pending_query_result
.errors
.add(TranslationError::msg(ctx, "query defines no projections"));
}
PendingStatementResult::default()
.with_translation(pending_query_result.translation.into())
.with_errors(pending_query_result.errors)
}
PipelineContextAll::Error(ctx) => PendingStatementResult::default()
.with_error(TranslationError::msg(ctx, "parse error")),
}
}
pub fn contains_within_command(&self) -> bool {
match self.tree.as_ref() {
PipelineContextAll::PipelineAltContext(ctx) => {
for command in ctx.command_all() {
match command.as_ref() {
CommandContextAll::WithinCommandContext(_) => return true,
_ => {}
}
}
return false;
}
PipelineContextAll::Error(_) => {
return false;
}
}
}
pub fn with_within(mut self, within: SQLExpression) -> Self {
self.within = Some(within);
self
}
pub fn add_within_if_none_present(self, within: SQLExpression) -> Self {
if self.contains_within_command() {
self
} else {
self.with_within(within)
}
}
}