Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions rust/datafusion/src/datasource/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use crate::error::{ExecutionError, Result};
use crate::physical_plan::memory::MemoryExec;
use crate::physical_plan::ExecutionPlan;

use tokio::task::{self, JoinHandle};

/// In-memory table
pub struct MemTable {
schema: SchemaRef,
Expand Down Expand Up @@ -59,13 +61,24 @@ impl MemTable {
pub async fn load(t: &dyn TableProvider, batch_size: usize) -> Result<Self> {
let schema = t.schema();
let exec = t.scan(&None, batch_size)?;
let partition_count = exec.output_partitioning().partition_count();

let mut tasks = Vec::with_capacity(partition_count);
for partition in 0..partition_count {
let exec = exec.clone();
let task: JoinHandle<Result<Vec<RecordBatch>>> = task::spawn(async move {
let it = exec.execute(partition).await?;
it.into_iter()
.collect::<ArrowResult<Vec<RecordBatch>>>()
.map_err(ExecutionError::from)
});
tasks.push(task)
}

let mut data: Vec<Vec<RecordBatch>> =
Vec::with_capacity(exec.output_partitioning().partition_count());
for partition in 0..exec.output_partitioning().partition_count() {
let it = exec.execute(partition).await?;
let partition_batches = it.into_iter().collect::<ArrowResult<Vec<_>>>()?;
data.push(partition_batches);
let mut data: Vec<Vec<RecordBatch>> = Vec::with_capacity(partition_count);
for task in tasks {
let result = task.await.expect("MemTable::load could not join task")?;
data.push(result);
}

MemTable::new(schema.clone(), data)
Expand Down