Skip to content

Commit

Permalink
fix: Fix invalid state due to cached IR (#18262)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 authored Aug 19, 2024
1 parent 02f95bf commit a8ce835
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/polars-plan/src/plans/conversion/dsl_to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,8 +794,11 @@ pub fn to_alp_impl(
IR::Sink { input, payload }
},
DslPlan::IR { node, dsl, version } => {
return if let (true, Some(node)) = (version == lp_arena.version(), node) {
Ok(node)
return if node.is_some()
&& version == lp_arena.version()
&& convert.used_arenas.insert(version)
{
Ok(node.unwrap())
} else {
to_alp_impl(owned(dsl), expr_arena, lp_arena, convert)
}
Expand Down
7 changes: 7 additions & 0 deletions crates/polars-plan/src/plans/conversion/stack_opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ pub(super) struct ConversionOptimizer {
scratch: Vec<Node>,
simplify: Option<SimplifyExprRule>,
coerce: Option<TypeCoercionRule>,
// IR's can be cached in the DSL.
// But if they are used multiple times in DSL (e.g. concat/join)
// then it can occur that we take a slot multiple times.
// So we keep track of the arena versions used and allow only
// one unique IR cache to be reused.
pub(super) used_arenas: PlHashSet<u32>,
}

impl ConversionOptimizer {
Expand All @@ -27,6 +33,7 @@ impl ConversionOptimizer {
scratch: Vec::with_capacity(8),
simplify,
coerce,
used_arenas: Default::default(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,9 @@ def test_schema_in_map_elements_returns_scalar() -> None:

assert (q.collect_schema()) == schema
assert q.collect().schema == schema


def test_ir_cache_unique_18198() -> None:
lf = pl.LazyFrame({"a": [1]})
lf.collect_schema()
assert pl.concat([lf, lf]).collect().to_dict(as_series=False) == {"a": [1, 1]}

0 comments on commit a8ce835

Please sign in to comment.