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() #19629

Merged
merged 21 commits into from
Nov 6, 2024

Conversation

nameexhaustion
Copy link
Collaborator

@nameexhaustion nameexhaustion commented Nov 4, 2024

Fixes #19562

  • Explode during dtype resolution mistakenly indicated that it did not need auto-imploding (see the nested variable in the code)
  • Also adjusts the output dtype getting/checking logic for FunctionExpr to adhere to:
    • If the function indicates RETURNS_SCALAR, its output dtype shall always be scalar (i.e. .agg(col(f64).sum()) -> Float64)
    • Otherwise, if we are in a Context::Aggregation, we auto-implode the dtype (i.e. .agg(col(f64).shuffle()) -> list[Float64])
    • Assertion was also adjusted during execution in ApplyExpr
    • To do this properly we also propagate the Context recursively

@github-actions github-actions bot added internal An internal refactor or improvement python Related to Python Polars rust Related to Rust Polars labels Nov 4, 2024
@nameexhaustion nameexhaustion added the reference Reference issue for recurring topics label Nov 4, 2024
@nameexhaustion nameexhaustion force-pushed the agg-field-dtype-2 branch 4 times, most recently from f97929c to 7a7eb1e Compare November 5, 2024 08:20
@nameexhaustion nameexhaustion removed the reference Reference issue for recurring topics label Nov 5, 2024
@nameexhaustion nameexhaustion changed the title refactor: Try other method fix: Fix incorrect lazy schema for explode() in agg() Nov 5, 2024
@github-actions github-actions bot added the fix Bug fix label Nov 5, 2024
@nameexhaustion nameexhaustion removed the internal An internal refactor or improvement label Nov 5, 2024
@nameexhaustion nameexhaustion marked this pull request as ready for review November 5, 2024 12:55
Copy link

codecov bot commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 79.86%. Comparing base (6b0a906) to head (850f15e).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #19629   +/-   ##
=======================================
  Coverage   79.86%   79.86%           
=======================================
  Files        1537     1537           
  Lines      211923   211925    +2     
  Branches     2446     2446           
=======================================
+ Hits       169249   169254    +5     
+ Misses      42120    42117    -3     
  Partials      554      554           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ritchie46
Copy link
Member

So the fix boils down to correctly passing the proper Context right?

@nameexhaustion

This comment was marked as outdated.

},
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.

and also Explode doesn't actually return a scalar so we don't subtract the nested state

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. Does that work? It removes a level of nesting?

Copy link
Collaborator Author

@nameexhaustion nameexhaustion Nov 6, 2024

Choose a reason for hiding this comment

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

I'm not entirely around this nested variable - but it basically does this (you can see above)

if nested >= 1 {
    field.coerce(field.dtype().clone().implode());
}

As I understand it just means we have to set nested to 0 if we don't need auto-implode. I also suspect the exact value of the nested value doesn't matter other than this property, but I'm not sure :/

At the beginning it's initialized to 1 if we are Context::Aggregation and 0 otherwise. So if a result doesn't need an auto-implode (e.g. sum() aggregation), we just set it to 0.

Copy link
Member

Choose a reason for hiding this comment

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

Now that I think of it, I think it does need to subtract if nested >= 2.

@nameexhaustion nameexhaustion marked this pull request as draft November 6, 2024 10:57
@nameexhaustion nameexhaustion marked this pull request as ready for review November 6, 2024 12:34
@ritchie46 ritchie46 merged commit 55625a1 into pola-rs:main Nov 6, 2024
30 checks passed
tylerriccio33 pushed a commit to tylerriccio33/polars that referenced this pull request Nov 8, 2024
@nameexhaustion nameexhaustion deleted the agg-field-dtype-2 branch November 18, 2024 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix Bug fix python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LazyFrame has incorrect schema following aggregation of an exploded list column expression
2 participants