Apache DataFusion SQL Query Engine
Fix `DataFrame::cache` errors with `Plan("Mismatch between schema and batches")` (#8510)
Asura79692bc67ef5a9f1
diff --git a/datafusion/core/src/dataframe/mod.rs b/datafusion/core/src/dataframe/mod.rsindex 3e286110f7..4b8a9c5b7d 100644--- a/datafusion/core/src/dataframe/mod.rs+++ b/datafusion/core/src/dataframe/mod.rs@@ -1276,11 +1276,12 @@ impl DataFrame {/// ```pub async fn cache(self) -> Result<DataFrame> {let context = SessionContext::new_with_state(self.session_state.clone());- let mem_table = MemTable::try_new(- SchemaRef::from(self.schema().clone()),- self.collect_partitioned().await?,- )?;-+ // The schema is consistent with the output+ let plan = self.clone().create_physical_plan().await?;+ let schema = plan.schema();+ let task_ctx = Arc::new(self.task_ctx());+ let partitions = collect_partitioned(plan, task_ctx).await?;+ let mem_table = MemTable::try_new(schema, partitions)?;context.read_table(Arc::new(mem_table))}}@@ -2638,6 +2639,17 @@ mod tests {Ok(())}+ #[tokio::test]+ async fn test_cache_mismatch() -> Result<()> {+ let ctx = SessionContext::new();+ let df = ctx+ .sql("SELECT CASE WHEN true THEN NULL ELSE 1 END")+ .await?;+ let cache_df = df.cache().await;+ assert!(cache_df.is_ok());+ Ok(())+ }+#[tokio::test]async fn cache_test() -> Result<()> {let df = test_table()