Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix invalid state due to cached IR #18262

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]}