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 incorrect lazy schema for explode() in agg() #19623

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion crates/polars-expr/src/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl ApplyExpr {
// })?
let out: ListChunked = POOL.install(|| iter.collect::<PolarsResult<_>>())?;

debug_assert_eq!(out.dtype(), &DataType::List(Box::new(dtype)));
debug_assert_eq!(&dtype, out.dtype());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the output field of the expression should created in an aggregation-context-aware manner, so I've updated this assertion.


out
} else {
Expand Down
28 changes: 20 additions & 8 deletions crates/polars-expr/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn create_physical_expr_inner(
// TODO! Order by
let group_by = create_physical_expressions_from_nodes(
partition_by,
Context::Default,
Context::Aggregation,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is under AExpr::Window

expr_arena,
schema,
state,
Expand Down Expand Up @@ -473,10 +473,13 @@ fn create_physical_expr_inner(
options,
} => {
let is_scalar = is_scalar_ae(expression, expr_arena);
let output_dtype =
let mut output_field =
expr_arena
.get(expression)
.to_field(schema, Context::Default, expr_arena)?;
if let Context::Aggregation = ctxt {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should just pass the context to line 497 instead of updating the dtype here.

output_field.dtype = DataType::List(Box::new(output_field.dtype));
}

let is_reducing_aggregation = options.flags.contains(FunctionFlags::RETURNS_SCALAR)
&& matches!(options.collect_groups, ApplyOptions::GroupWise);
Expand All @@ -501,21 +504,24 @@ fn create_physical_expr_inner(
*options,
state.allow_threading,
schema.clone(),
output_dtype,
output_field,
is_scalar,
)))
},
Function {
input,
function,
options,
..
} => {
let is_scalar = is_scalar_ae(expression, expr_arena);
let output_field =
let mut output_field =
expr_arena
.get(expression)
.to_field(schema, Context::Default, expr_arena)?;
if let Context::Aggregation = ctxt {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idem

output_field.dtype = DataType::List(Box::new(output_field.dtype));
}

let is_reducing_aggregation = options.flags.contains(FunctionFlags::RETURNS_SCALAR)
&& matches!(options.collect_groups, ApplyOptions::GroupWise);
// Will be reset in the function so get that here.
Expand Down Expand Up @@ -565,9 +571,15 @@ fn create_physical_expr_inner(
move |c: &mut [polars_core::frame::column::Column]| c[0].explode().map(Some),
) as Arc<dyn ColumnsUdf>);

let field = expr_arena
.get(expression)
.to_field(schema, ctxt, expr_arena)?;
let mut field =
expr_arena
.get(expression)
.to_field(schema, Context::Default, expr_arena)?;

if let Context::Aggregation = ctxt {
field.dtype = DataType::List(Box::new(field.dtype));
}

Ok(Arc::new(ApplyExpr::new(
vec![input],
function,
Expand Down
1 change: 0 additions & 1 deletion crates/polars-plan/src/plans/aexpr/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl AExpr {
},
Explode(expr) => {
let field = arena.get(*expr).to_field_impl(schema, arena, nested)?;
*nested = nested.saturating_sub(1);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix for the linked issue explode()


if let List(inner) = field.dtype() {
Ok(Field::new(field.name().clone(), *inner.clone()))
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/lazyframe/test_lazyframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,3 +1424,11 @@ def test_lf_unnest() -> None:
]
)
assert_frame_equal(lf.unnest("a", "b").collect(), expected)


def test_lf_schema_explode_in_agg_19562() -> None:
lf = pl.LazyFrame({"a": 1, "b": [[1]]})
q = lf.group_by("a").agg(pl.col("b").explode())

assert q.collect_schema() == {"a": pl.Int32, "b": pl.List(pl.Int64)}
assert_frame_equal(q.collect(), pl.DataFrame({"a": 1, "b": [[1]]}))