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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#[macro_use]
extern crate nom;

#[macro_use]
#[cfg(test)]
extern crate maplit;

#[macro_use]
extern crate failure;

extern crate crossbeam_channel;

mod data;
mod lang;
mod operator;
mod render;
mod typecheck;

pub mod pipeline {
    use crossbeam_channel::{bounded, Receiver, RecvTimeoutError};
    use data::{Record, Row, Value};
    use failure::Error;
    use lang;
    use lang::*;
    use operator;
    use render::{RenderConfig, Renderer};
    use std::io::BufRead;
    use std::thread;
    use std::time::Duration;
    use typecheck;

    #[derive(Debug, Fail)]
    pub enum CompileError {
        #[fail(display = "Failure parsing query. Parse failed at: {}", message)]
        Parse { message: String },

        #[fail(display = "Non aggregate operators can't follow aggregate operators")]
        NonAggregateAfterAggregate,

        #[fail(display = "Unexpected failure: {}", message)]
        Unexpected { message: String },
    }

    pub struct Pipeline {
        filter: lang::Search,
        pre_aggregates: Vec<Box<operator::UnaryPreAggOperator>>,
        aggregators: Vec<Box<operator::AggregateOperator>>,
        renderer: Renderer,
    }

    impl From<lang::ComparisonOp> for operator::BoolExpr {
        fn from(op: ComparisonOp) -> Self {
            match op {
                ComparisonOp::Eq => operator::BoolExpr::Eq,
                ComparisonOp::Neq => operator::BoolExpr::Neq,
                ComparisonOp::Gt => operator::BoolExpr::Gt,
                ComparisonOp::Lt => operator::BoolExpr::Lt,
                ComparisonOp::Gte => operator::BoolExpr::Gte,
                ComparisonOp::Lte => operator::BoolExpr::Lte,
            }
        }
    }

    impl From<lang::Expr> for operator::Expr {
        fn from(inp: lang::Expr) -> Self {
            match inp {
                lang::Expr::Column(s) => operator::Expr::Column(s),
                lang::Expr::Binary { op, left, right } => match op {
                    BinaryOp::Comparison(com_op) => {
                        operator::Expr::Comparison(operator::BinaryExpr::<operator::BoolExpr> {
                            left: Box::new((*left).into()),
                            right: Box::new((*right).into()),
                            operator: com_op.into(),
                        })
                    }
                },
                lang::Expr::Value(value) => {
                    let boxed = Box::new(value);
                    let static_value: &'static mut Value = Box::leak(boxed);
                    operator::Expr::Value(static_value)
                }
            }
        }
    }

    impl Pipeline {
        fn convert_inline(
            op: lang::InlineOperator,
        ) -> Result<Box<operator::UnaryPreAggOperator>, operator::TypeError> {
            match op {
                InlineOperator::Json { input_column } => {
                    Ok(Box::new(operator::ParseJson::new(input_column)))
                }
                InlineOperator::Parse {
                    pattern,
                    fields,
                    input_column,
                } => Ok(Box::new(operator::Parse::new(
                    &pattern,
                    fields,
                    input_column.map(|c| c.force()),
                )?)),
                InlineOperator::Fields { fields, mode } => {
                    let omode = match mode {
                        FieldMode::Except => operator::FieldMode::Except,
                        FieldMode::Only => operator::FieldMode::Only,
                    };
                    Ok(Box::new(operator::Fields::new(&fields, omode)))
                }
                InlineOperator::Where { expr } => {
                    let expr: operator::Expr = expr.into();
                    typecheck::create_where(expr)
                }
            }
        }

        fn convert_inline_adapted(
            op: lang::InlineOperator,
        ) -> Result<Box<operator::AggregateOperator>, operator::TypeError> {
            match op {
                InlineOperator::Json { input_column } => {
                    Ok(operator::ParseJson::new(input_column).into())
                }
                InlineOperator::Parse {
                    pattern,
                    fields,
                    input_column,
                } => Ok(
                    operator::Parse::new(&pattern, fields, input_column.map(|c| c.force()))?.into(),
                ),
                InlineOperator::Fields { fields, mode } => {
                    let omode = match mode {
                        FieldMode::Except => operator::FieldMode::Except,
                        FieldMode::Only => operator::FieldMode::Only,
                    };
                    Ok(operator::Fields::new(&fields, omode).into())
                }
                InlineOperator::Where { expr } => {
                    let expr: operator::Expr = expr.into();
                    typecheck::create_where_adapt(expr)
                }
            }
        }

        fn convert_sort(op: lang::SortOperator) -> Box<operator::AggregateOperator> {
            let mode = match op.direction {
                SortMode::Ascending => operator::SortDirection::Ascending,
                SortMode::Descending => operator::SortDirection::Descending,
            };
            Box::new(operator::Sorter::new(op.sort_cols, mode))
        }

        fn convert_agg_function(func: lang::AggregateFunction) -> Box<operator::AggregateFunction> {
            match func {
                AggregateFunction::Count => Box::new(operator::Count::new()),
                AggregateFunction::Average { column } => Box::new(operator::Average::empty(column)),
                AggregateFunction::Sum { column } => Box::new(operator::Sum::empty(column)),
                AggregateFunction::Percentile {
                    column, percentile, ..
                } => Box::new(operator::Percentile::empty(column.force(), percentile)),
                AggregateFunction::CountDistinct { column } => {
                    Box::new(operator::CountDistinct::empty(&column.force()))
                }
            }
        }

        fn convert_multi_agg(op: lang::MultiAggregateOperator) -> Box<operator::AggregateOperator> {
            let agg_functions = op.aggregate_functions
                .into_iter()
                .map(|(k, func)| (k, Pipeline::convert_agg_function(func)));
            let key_cols: Vec<operator::Expr> =
                op.key_cols.into_iter().map(|expr| expr.into()).collect();
            Box::new(operator::MultiGrouper::new(
                &key_cols[..],
                op.key_col_headers,
                agg_functions.collect(),
            ))
        }

        pub fn new(pipeline: &str) -> Result<Self, Error> {
            let parsed = lang::parse_query(pipeline).map_err(|e| CompileError::Parse {
                message: format!("{:?}", e),
            });
            let query = parsed?;
            let mut in_agg = false;
            let mut pre_agg: Vec<Box<operator::UnaryPreAggOperator>> = Vec::new();
            let mut post_agg: Vec<Box<operator::AggregateOperator>> = Vec::new();
            let final_op = {
                let last_op = &(query.operators).last();
                match last_op {
                    &Some(&Operator::MultiAggregate(ref agg_op)) => {
                        Some(Pipeline::convert_sort(SortOperator {
                            sort_cols: agg_op
                                .aggregate_functions
                                .iter()
                                .map(|&(ref k, _)| k)
                                .cloned()
                                .collect(),
                            direction: SortMode::Descending,
                        }))
                    }
                    _other => None,
                }
            };
            for op in query.operators {
                match op {
                    Operator::Inline(inline_op) => if !in_agg {
                        pre_agg.push(Pipeline::convert_inline(inline_op)?);
                    } else {
                        post_agg.push(Pipeline::convert_inline_adapted(inline_op)?)
                    },
                    Operator::MultiAggregate(agg_op) => {
                        in_agg = true;
                        post_agg.push(Pipeline::convert_multi_agg(agg_op))
                    }
                    Operator::Sort(sort_op) => post_agg.push(Pipeline::convert_sort(sort_op)),
                }
            }
            if let Some(op) = final_op {
                post_agg.push(op)
            };
            Result::Ok(Pipeline {
                filter: query.search,
                pre_aggregates: pre_agg,
                aggregators: post_agg,
                renderer: Renderer::new(
                    RenderConfig {
                        floating_points: 2,
                        min_buffer: 4,
                        max_buffer: 8,
                    },
                    Duration::from_millis(50),
                ),
            })
        }

        fn matches(pattern: &lang::Search, raw: &str) -> bool {
            match *pattern {
                lang::Search::MatchAll => true,
                lang::Search::MatchFilter(ref filter) => raw.contains(filter),
            }
        }
        fn render_noagg(mut renderer: Renderer, rx: &Receiver<Row>) {
            loop {
                let next = rx.recv_timeout(Duration::from_millis(50));
                match next {
                    Ok(row) => {
                        renderer.render(&row, false);
                    }
                    Err(RecvTimeoutError::Timeout) => {}
                    Err(RecvTimeoutError::Disconnected) => break,
                }
            }
        }

        fn render_aggregate(
            mut head: Box<operator::AggregateOperator>,
            mut rest: Vec<Box<operator::AggregateOperator>>,
            mut renderer: Renderer,
            rx: &Receiver<Row>,
        ) {
            loop {
                let next = rx.recv_timeout(Duration::from_millis(50));
                match next {
                    Ok(row) => {
                        (*head).process(row);
                        if renderer.should_print() {
                            renderer.render(&Pipeline::run_agg_pipeline(&head, &mut rest), false);
                        }
                    }
                    Err(RecvTimeoutError::Timeout) => {
                        if renderer.should_print() {
                            renderer.render(&Pipeline::run_agg_pipeline(&head, &mut rest), false);
                        }
                    }
                    Err(RecvTimeoutError::Disconnected) => break,
                }
            }
            renderer.render(&Pipeline::run_agg_pipeline(&head, &mut rest), true);
        }

        pub fn process<T: BufRead>(self, mut buf: T) {
            let (tx, rx) = bounded(1000);
            let mut aggregators = self.aggregators;
            let preaggs = self.pre_aggregates;
            let search = self.filter;
            let renderer = self.renderer;
            let t = if !aggregators.is_empty() {
                if aggregators.len() == 1 {
                    panic!("Every aggregate pipeline should have a real operator and a sort");
                }
                let head = aggregators.remove(0);
                thread::spawn(move || Pipeline::render_aggregate(head, aggregators, renderer, &rx))
            } else {
                thread::spawn(move || Pipeline::render_noagg(renderer, &rx))
            };

            // This is pretty slow in practice. We could move line splitting until after
            // we find a match. Another option is moving the transformation to String until
            // after we match (staying as Vec<u8> until then)
            let mut line = String::with_capacity(1024);
            while buf.read_line(&mut line).unwrap() > 0 {
                if let Some(row) = Pipeline::proc_preagg(&line, &search, &preaggs) {
                    tx.send(row).unwrap();
                }
                line.clear();
            }
            // Drop tx when causes the thread to exit.
            drop(tx);
            t.join().unwrap();
        }

        fn proc_preagg(
            s: &str,
            pattern: &lang::Search,
            pre_aggs: &[Box<operator::UnaryPreAggOperator>],
        ) -> Option<Row> {
            if Pipeline::matches(pattern, s) {
                let mut rec = Record::new(s);
                for pre_agg in pre_aggs {
                    match (*pre_agg).process(rec) {
                        Ok(Some(next_rec)) => rec = next_rec,
                        Ok(None) => return None,
                        Err(_) => return None,
                    }
                }
                Some(Row::Record(rec))
            } else {
                None
            }
        }

        pub fn run_agg_pipeline(
            head: &Box<operator::AggregateOperator>,
            rest: &mut [Box<operator::AggregateOperator>],
        ) -> Row {
            let mut row = Row::Aggregate((*head).emit());
            for agg in (*rest).iter_mut() {
                (*agg).process(row);
                row = Row::Aggregate((*agg).emit());
            }
            row
        }
    }
}