Cache DataFrame as a memory table. Default behavior could be changed using a [`crate::execution::session_state::CacheFactory`] configured via [`SessionState`]. ``` # use datafusion::prelude::*; # use datafusion::error::Result; # #[tokio::main] # async fn main() -> Result<()> { let ctx = SessionContext::new(); let df = ctx .read_csv("tests/data/example.csv", CsvReadOptions::new()) .await?; let df
(self)
| 2393 | /// # } |
| 2394 | /// ``` |
| 2395 | pub async fn cache(self) -> Result<DataFrame> { |
| 2396 | if let Some(cache_factory) = self.session_state.cache_factory() { |
| 2397 | let new_plan = |
| 2398 | cache_factory.create(self.plan, self.session_state.as_ref())?; |
| 2399 | Ok(Self::new(*self.session_state, new_plan)) |
| 2400 | } else { |
| 2401 | let context = SessionContext::new_with_state((*self.session_state).clone()); |
| 2402 | // The schema is consistent with the output |
| 2403 | let plan = self.create_physical_plan().await?; |
| 2404 | let schema = plan.schema(); |
| 2405 | let task_ctx = Arc::new(self.task_ctx()); |
| 2406 | let partitions = collect_partitioned(plan, task_ctx).await?; |
| 2407 | let mem_table = MemTable::try_new(schema, partitions)?; |
| 2408 | context.read_table(Arc::new(mem_table)) |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /// Apply an alias to the DataFrame. |
| 2413 | /// |