-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 join_where
incorrectly dropping transformations on RHS of equality expressions
#21067
Conversation
82b0571
to
d3bd001
Compare
@@ -329,45 +329,3 @@ def test_collapse_joins_combinations() -> None: | |||
print() | |||
|
|||
raise | |||
|
|||
|
|||
def test_select_after_join_where_20831() -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to test_joins.py
below
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #21067 +/- ##
==========================================
- Coverage 79.27% 79.26% -0.02%
==========================================
Files 1586 1586
Lines 225627 225669 +42
Branches 2588 2588
==========================================
+ Hits 178877 178885 +8
- Misses 46160 46194 +34
Partials 590 590 ☔ View full report in Codecov by Sentry. |
join_where
when expression RHS contains transformationsjoin_where
drops transformations on RHS expression of equalities
join_where
drops transformations on RHS expression of equalitiesjoin_where
incorrectly dropping transformations on RHS expression of equalities
join_where
incorrectly dropping transformations on RHS expression of equalitiesjoin_where
incorrectly dropping transformations on RHS of equality expressions
@@ -197,6 +197,10 @@ impl ExprIR { | |||
self.output_name = OutputName::Alias(name) | |||
} | |||
|
|||
pub(crate) fn set_columnlhs(&mut self, name: PlSmallStr) { | |||
self.output_name = OutputName::ColumnLhs(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should debug_assert
that the current output_name
also is lhs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
// We ensure we do not mutate any nodes in-place by deep cloning. The nodes may be used in | ||
// other locations and mutating them will cause really confusing bugs, such as | ||
// https://github.com/pola-rs/polars/issues/20831. | ||
*expr = to_expr_ir(node_to_expr(expr.node(), expr_arena), expr_arena).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of deep cloning can't we ensure we don't mutate? E.g. by adding a new node instead. The remove_suffix
looks very suspect to me and I think we should just create a new node expression there.
I like to prevent going back into expr
as there might be information lost and we do a lot of heap allocations. If we do that all over the optimizer it would get pretty expensive (and incorrect because of the information loss).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, I'm reasoning that any node within an expression tree may be referenced at another location, so if any node in the tree is mutated, then all of the parent nodes must be cloned to a unique expression tree.
I made this assumption very conservative as I don't think we have node-sharing behavior of an Arena
formally defined in the codebase 😶. E.g. if we define that only AExpr::Column
nodes can be shared in multiple locations, then it's safe to just mutate via replacing the column node within an expression without cloning the entire expression tree.
But for the cloning itself I can work on adding a way to do it without converting to Expr 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a visitor that rewrites the Aexpr and then assigns that whole new rewritten to new slots in the arena's. Maybe we can use that instead of mutating?
d3bd001
to
bb725ce
Compare
348bb4a
to
0b3e6b7
Compare
join_where
- transformations on RHS of equality expressions are dropped #21066This restores the original code to before #20865 with an added clone step, and serves as a proper fix for the original issue at:
join_where
fails with DuplicateError #20831I had previously misunderstood the cause of the original issue, but I believe I now have the correct understanding of what happened. Here is an illustration to show what was happening in the original issue:
Essentially, the bug is an in-place mutation of a
Node
in theexpr_arena
bycollapse_joins
, where the sameNode
was also being referenced by a projection in another place.The previous PR (#20865) resolved the above issue by always inserting a new ColumnNode. However, it made the incorrect assumption that the RHS of the equality was always a
col()
, and would drop any extra transformations being applied on the RHS (e.g.col(A_right) + 1
gets incorrectly turned intocol(A_right)
).The proper fix in this PR is to restore the original logic, with an added deep-clone before the mutation takes place to ensure the underlying
Node
being mutated isn't being referenced by e.g. aSelect {}
IR somewhere else.Arena::<T>::get_mut()
In light of this issue, a quick search across the codebase shows
collapse_joins
as the only place that usesArena::get_mut()
on expressions arenas:All other uses are generally with logical plan (LP / IR) arenas. I haven't checked in detail whether they are used properly in those places as we don't seem to have any bugs relating to logical plans being incorrectly mutated.