use std::sync::{Arc, Mutex};
use crate::error::Result;
use crate::execution::physical_plan::{BatchIterator, ExecutionPlan, Partition};
use arrow::datatypes::Schema;
pub struct DatasourceExec {
schema: Arc<Schema>,
partitions: Vec<Arc<Mutex<dyn BatchIterator>>>,
}
impl DatasourceExec {
pub fn new(
schema: Arc<Schema>,
partitions: Vec<Arc<Mutex<dyn BatchIterator>>>,
) -> Self {
Self { schema, partitions }
}
}
impl ExecutionPlan for DatasourceExec {
fn schema(&self) -> Arc<Schema> {
self.schema.clone()
}
fn partitions(&self) -> Result<Vec<Arc<dyn Partition>>> {
Ok(self
.partitions
.iter()
.map(|it| {
Arc::new(DatasourcePartition::new(it.clone())) as Arc<dyn Partition>
})
.collect::<Vec<_>>())
}
}
pub struct DatasourcePartition {
batch_iter: Arc<Mutex<dyn BatchIterator>>,
}
impl DatasourcePartition {
fn new(batch_iter: Arc<Mutex<dyn BatchIterator>>) -> Self {
Self { batch_iter }
}
}
impl Partition for DatasourcePartition {
fn execute(&self) -> Result<Arc<Mutex<dyn BatchIterator>>> {
Ok(self.batch_iter.clone())
}
}