Skip to content

Apache DataFusion SQL Query Engine

fix(physical-plan): CTAS panic on wasm32-unknown-unknown (#24275)

Kent Wu7e015b74af09
diff --git a/datafusion/physical-plan/src/execution_plan.rs b/datafusion/physical-plan/src/execution_plan.rs
index b1d7c32882..ff803746aa 100644
--- a/datafusion/physical-plan/src/execution_plan.rs
+++ b/datafusion/physical-plan/src/execution_plan.rs
@@ -1655,6 +1655,13 @@ pub async fn collect_partitioned(
plan: Arc<dyn ExecutionPlan>,
context: Arc<TaskContext>,
) -> Result<Vec<Vec<RecordBatch>>> {
+ // Avoid `JoinSet::spawn` for single partition
+ if plan.output_partitioning().partition_count() == 1 {
+ let stream = plan.execute(0, context)?;
+ let batches: Vec<RecordBatch> = stream.try_collect().await?;
+ return Ok(vec![batches]);
+ }
+
let streams = execute_stream_partitioned(plan, context)?;
let mut join_set = JoinSet::new();
diff --git a/datafusion/wasmtest/src/lib.rs b/datafusion/wasmtest/src/lib.rs
index f545ccf193..d8da5d4b4f 100644
--- a/datafusion/wasmtest/src/lib.rs
+++ b/datafusion/wasmtest/src/lib.rs
@@ -206,6 +206,34 @@ mod test {
let _ = collect(physical_plan, task_ctx).await.unwrap();
}
+ #[wasm_bindgen_test(unsupported = tokio::test)]
+ async fn test_create_table_as_select() {
+ let ctx = get_ctx();
+ ctx.sql("CREATE TABLE t AS SELECT 1 AS a, 'x' AS b")
+ .await
+ .unwrap()
+ .collect()
+ .await
+ .unwrap();
+
+ let result = ctx
+ .sql("SELECT * FROM t")
+ .await
+ .unwrap()
+ .collect()
+ .await
+ .unwrap();
+
+ assert_eq!(
+ batches_to_string(&result),
+ "+---+---+\n\
+ | a | b |\n\
+ +---+---+\n\
+ | 1 | x |\n\
+ +---+---+"
+ );
+ }
+
#[wasm_bindgen_test(unsupported = tokio::test)]
async fn test_parquet_write() {
let (schema, batch) = create_test_data();