use std::rc::Rc;
use super::super::datasources::common::*;
use super::super::errors::*;
use super::super::exec::*;
use super::super::types::*;
use arrow::datatypes::*;
pub struct ProjectRelation {
schema: Rc<Schema>,
input: Box<SimpleRelation>,
expr: Vec<CompiledExpr>,
}
impl ProjectRelation {
pub fn new(input: Box<SimpleRelation>, expr: Vec<CompiledExpr>, schema: Rc<Schema>) -> Self {
ProjectRelation { input, expr, schema }
}
}
impl SimpleRelation for ProjectRelation {
fn scan<'a>(&'a mut self) -> Box<Iterator<Item = Result<Rc<RecordBatch>>> + 'a> {
let project_expr = &self.expr;
let projection_iter = self.input.scan().map(move |r| match r {
Ok(ref batch) => {
let projected_columns: Result<Vec<Value>> =
project_expr.iter()
.map(|e| (*e)(batch.as_ref())).collect();
let projected_batch: Rc<RecordBatch> = Rc::new(DefaultRecordBatch {
schema: Rc::new(Schema::empty()), data: projected_columns?,
row_count: batch.num_rows(),
});
Ok(projected_batch)
}
Err(_) => r,
});
Box::new(projection_iter)
}
fn schema<'a>(&'a self) -> &'a Schema {
self.schema.as_ref()
}
}