Repository navigation
[unsupervised AI] Don't leak FromPandas length stats across a partition filter - #12692
Draft
dylanpulver wants to merge 1 commit into
Draft
dylanpulver wants to merge 1 commit into
dylanpulver wants to merge 1 commit into
Conversation
``FromPandas._get_lengths`` assigned to ``self._pd_length_stats``, which is a
declared ``_parameter``. ``Expr.__setattr__`` routes parameter assignment into
``self.operands``, so the assignment mutated the expression in place after
``__new__`` had already cached ``_name``/``_determ_token``.
``Partitions._simplify_down`` then copies the stale, *unfiltered* lengths into
the partition-filtered expression via ``substitute_parameters``, and
``_get_lengths`` short-circuits on them, so ``Len`` folds to the length of the
whole frame:
df = dd.from_pandas(pdf, npartitions=10) # 100 rows
len(df) # 100
df2 = df[["x"]] + 1
len(df2.partitions[0]) # 100, should be 10
Cache the statistics in a ``cached_property`` instead, which writes to
``__dict__`` and therefore leaves the operands (and the expression's token)
alone. Un-XFAILs ``test_len``.
Closes dask#12275
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Contributor
Unit Test ResultsSee test report for an extended history of previous test failures. This is useful for diagnosing flaky tests. 24 files - 1 24 suites - 1 5h 58m 36s ⏱️ - 23m 18s Results for commit e45ff43. ± Comparison against base commit 6ce3740. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warning
This PR was written autonomously by an AI agent and has not been reviewed
by a human yet. Maintainers should ignore it until the human author has reviewed,
understood, and approved everything that the AI agent wrote.
Closes #12275.
What is wrong
FromPandas._get_lengthsassigns toself._pd_length_stats, which is a declared_parameter.Expr.__setattr__routes parameter assignment intoself.operands,so the assignment mutates the expression in place after
__new__has alreadycached
_nameand_determ_token.Partitions._simplify_downthen copies those stale, unfiltered lengths into thepartition-filtered expression through
substitute_parameters, and_get_lengthsshort-circuits on them.
Lentherefore folds to the length of the whole frame:That is the same assertion and the same two numbers reported in #12275:
assert len(df2.partitions[0]) == len(first)givingassert 100 == 10.Why the reported failure was intermittent
test_lennever takes the length of the unfiltered frame itself. It only failswhen something earlier in the same worker has already populated
_pd_length_statson the shareddffixture, which depends on how tests aredistributed. Hence "very flaky" rather than a hard failure.
The change
Cache the statistics in a
cached_property, which writes to__dict__andleaves the operands, and so the expression's token, alone.
The new
test_len_of_partition_after_len_of_framemakes the preconditionexplicit by taking the length of the frame first, so the bug is pinned
deterministically instead of by luck. With that in place the
xfailontest_lenis removed.Verification
Run with
pixi run -e mindeps-dataframe, reverting onlydask/dataframe/dask_expr/io/io.pytomainto control:10 three times out of three with it.
test_len_of_partition_after_len_of_framealone: fails without the fix withAssertionError: assert 100 == 10, passes with it.test_lenalone: passes either way, which is what makes it a poor guard onits own.
establishes the state that then makes
test_lenfail, which is the orderingCI was hitting by chance.
pixi run lintpasses all five hooks.Template checklist
Left unticked for the human author rather than self-certified. The substance:
this closes #12275, it adds
test_len_of_partition_after_len_of_frameandun-skips
test_len, andpixi run lintpasses.Prior art
#12290 (open draft, last updated 2026-02-28) also closes #12275, from the other
end: it changes
Partitionsindask/dataframe/dask_expr/_expr.pyso thatpartition length metadata is only reported when it can be inferred correctly.
This PR instead stops
FromPandasproducing the bad metadata in the firstplace. The assignment to
_pd_length_statsgoes throughExpr.__setattr__intoself.operandsafter__new__has cached_nameand_determ_token, sobesides the wrong lengths it leaves the expression's token describing operands
the expression no longer has. Fixing the producer removes the stale metadata
that #12290 then has to defend against.
Maintainers may well prefer one, the other, or both. Flagging the overlap rather
than leaving it to be discovered.