use std::any::Any;
use std::sync::Arc;
use crate::error::{ExecutionError, Result};
use crate::physical_plan::{ExecutionPlan, Partitioning, PhysicalExpr};
use arrow::datatypes::{Field, Schema, SchemaRef};
use arrow::error::Result as ArrowResult;
use arrow::record_batch::{RecordBatch, RecordBatchReader};
use super::SendableRecordBatchReader;
use async_trait::async_trait;
#[derive(Debug)]
pub struct ProjectionExec {
expr: Vec<(Arc<dyn PhysicalExpr>, String)>,
schema: SchemaRef,
input: Arc<dyn ExecutionPlan>,
}
impl ProjectionExec {
pub fn try_new(
expr: Vec<(Arc<dyn PhysicalExpr>, String)>,
input: Arc<dyn ExecutionPlan>,
) -> Result<Self> {
let input_schema = input.schema();
let fields: Result<Vec<_>> = expr
.iter()
.map(|(e, name)| {
Ok(Field::new(
name,
e.data_type(&input_schema)?,
e.nullable(&input_schema)?,
))
})
.collect();
let schema = Arc::new(Schema::new(fields?));
Ok(Self {
expr,
schema,
input: input.clone(),
})
}
}
#[async_trait]
impl ExecutionPlan for ProjectionExec {
fn as_any(&self) -> &dyn Any {
self
}
fn schema(&self) -> SchemaRef {
self.schema.clone()
}
fn children(&self) -> Vec<Arc<dyn ExecutionPlan>> {
vec![self.input.clone()]
}
fn output_partitioning(&self) -> Partitioning {
self.input.output_partitioning()
}
fn with_new_children(
&self,
children: Vec<Arc<dyn ExecutionPlan>>,
) -> Result<Arc<dyn ExecutionPlan>> {
match children.len() {
1 => Ok(Arc::new(ProjectionExec::try_new(
self.expr.clone(),
children[0].clone(),
)?)),
_ => Err(ExecutionError::General(
"ProjectionExec wrong number of children".to_string(),
)),
}
}
async fn execute(&self, partition: usize) -> Result<SendableRecordBatchReader> {
Ok(Box::new(ProjectionIterator {
schema: self.schema.clone(),
expr: self.expr.iter().map(|x| x.0.clone()).collect(),
input: self.input.execute(partition).await?,
}))
}
}
struct ProjectionIterator {
schema: SchemaRef,
expr: Vec<Arc<dyn PhysicalExpr>>,
input: SendableRecordBatchReader,
}
impl Iterator for ProjectionIterator {
type Item = ArrowResult<RecordBatch>;
fn next(&mut self) -> Option<Self::Item> {
match self.input.next() {
Some(Ok(batch)) => Some(
self.expr
.iter()
.map(|expr| expr.evaluate(&batch))
.collect::<Result<Vec<_>>>()
.map_or_else(
|e| Err(ExecutionError::into_arrow_external_error(e)),
|arrays| RecordBatch::try_new(self.schema.clone(), arrays),
),
),
other => other,
}
}
}
impl RecordBatchReader for ProjectionIterator {
fn schema(&self) -> SchemaRef {
self.schema.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::physical_plan::csv::{CsvExec, CsvReadOptions};
use crate::physical_plan::expressions::col;
use crate::test;
#[tokio::test]
async fn project_first_column() -> Result<()> {
let schema = test::aggr_test_schema();
let partitions = 4;
let path = test::create_partitioned_csv("aggregate_test_100.csv", partitions)?;
let csv =
CsvExec::try_new(&path, CsvReadOptions::new().schema(&schema), None, 1024)?;
let projection =
ProjectionExec::try_new(vec![(col("c1"), "c1".to_string())], Arc::new(csv))?;
let mut partition_count = 0;
let mut row_count = 0;
for partition in 0..projection.output_partitioning().partition_count() {
partition_count += 1;
let iterator = projection.execute(partition).await?;
row_count += iterator
.into_iter()
.map(|batch| {
let batch = batch.unwrap();
assert_eq!(1, batch.num_columns());
batch.num_rows()
})
.sum::<usize>();
}
assert_eq!(partitions, partition_count);
assert_eq!(100, row_count);
Ok(())
}
}