Path: blob/main/crates/polars-lazy/src/frame/cached_arenas.rs
6939 views
use super::*;12pub(crate) struct CachedArena {3lp_arena: Arena<IR>,4expr_arena: Arena<AExpr>,5}67impl LazyFrame {8pub fn set_cached_arena(&self, lp_arena: Arena<IR>, expr_arena: Arena<AExpr>) {9let mut cached = self.cached_arena.lock().unwrap();10*cached = Some(CachedArena {11lp_arena,12expr_arena,13});14}1516pub fn schema_with_arenas(17&mut self,18lp_arena: &mut Arena<IR>,19expr_arena: &mut Arena<AExpr>,20) -> PolarsResult<SchemaRef> {21let node = to_alp(22self.logical_plan.clone(),23expr_arena,24lp_arena,25&mut OptFlags::schema_only(),26)?;2728let schema = lp_arena.get(node).schema(lp_arena).into_owned();29// Cache the logical plan so that next schema call is cheap.30self.logical_plan = DslPlan::IR {31node: Some(node),32dsl: Arc::new(self.logical_plan.clone()),33version: lp_arena.version(),34};35Ok(schema)36}3738/// Get a handle to the schema — a map from column names to data types — of the current39/// `LazyFrame` computation.40///41/// Returns an `Err` if the logical plan has already encountered an error (i.e., if42/// `self.collect()` would fail), `Ok` otherwise.43pub fn collect_schema(&mut self) -> PolarsResult<SchemaRef> {44let mut cached_arenas = self.cached_arena.lock().unwrap();4546match &mut *cached_arenas {47None => {48let mut lp_arena = Default::default();49let mut expr_arena = Default::default();50// Code duplication because of bchk. :(51let node = to_alp(52self.logical_plan.clone(),53&mut expr_arena,54&mut lp_arena,55&mut OptFlags::schema_only(),56)?;5758let schema = lp_arena.get(node).schema(&lp_arena).into_owned();59// Cache the logical plan so that next schema call is cheap.60self.logical_plan = DslPlan::IR {61node: Some(node),62dsl: Arc::new(self.logical_plan.clone()),63version: lp_arena.version(),64};65*cached_arenas = Some(CachedArena {66lp_arena,67expr_arena,68});6970Ok(schema)71},72Some(arenas) => {73match self.logical_plan {74// We have got arenas and don't need to convert the DSL.75DslPlan::IR {76node: Some(node), ..77} => Ok(arenas78.lp_arena79.get(node)80.schema(&arenas.lp_arena)81.into_owned()),82_ => {83// We have got arenas, but still need to convert (parts) of the DSL.84// Code duplication because of bchk. :(85let node = to_alp(86self.logical_plan.clone(),87&mut arenas.expr_arena,88&mut arenas.lp_arena,89&mut OptFlags::schema_only(),90)?;9192let schema = arenas93.lp_arena94.get(node)95.schema(&arenas.lp_arena)96.into_owned();97// Cache the logical plan so that next schema call is cheap.98self.logical_plan = DslPlan::IR {99node: Some(node),100dsl: Arc::new(self.logical_plan.clone()),101version: arenas.lp_arena.version(),102};103Ok(schema)104},105}106},107}108}109110pub(super) fn get_arenas(&mut self) -> (Arena<IR>, Arena<AExpr>) {111match self.cached_arena.lock().unwrap().as_mut() {112Some(arenas) => (arenas.lp_arena.clone(), arenas.expr_arena.clone()),113None => (Arena::with_capacity(16), Arena::with_capacity(16)),114}115}116}117118119