Skip to content

[unsupervised AI] Don't leak FromPandas length stats across a partition filter - #12692

Draft
dylanpulver wants to merge 1 commit into
dask:mainfrom
dylanpulver:fix/12275-frompandas-length-stats
Draft

dylanpulver wants to merge 1 commit into
dask:mainfrom
dylanpulver:fix/12275-frompandas-length-stats

Conversation

@dylanpulver

@dylanpulver dylanpulver commented Oct 31, 2026 •

Copy link
Copy Markdown

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_lengths assigns to self._pd_length_stats, which is a declared
_parameter. Expr.__setattr__ routes parameter assignment into self.operands,
so the assignment mutates the expression in place after __new__ has already
cached _name and _determ_token.

Partitions._simplify_down then copies those stale, unfiltered lengths into the
partition-filtered expression through substitute_parameters, and _get_lengths
short-circuits on them. Len therefore folds to the length of the whole frame:

import pandas as pd, dask.dataframe as dd
pdf = pd.DataFrame({"x": range(100), "y": range(100)})
df = dd.from_pandas(pdf, npartitions=10)
len(df)                      # 100
df2 = df[["x"]] + 1
len(df2.partitions[0])       # 100, should be 10

That is the same assertion and the same two numbers reported in #12275:
assert len(df2.partitions[0]) == len(first) giving assert 100 == 10.

Why the reported failure was intermittent

test_len never takes the length of the unfiltered frame itself. It only fails
when something earlier in the same worker has already populated
_pd_length_stats on the shared df fixture, which depends on how tests are
distributed. Hence "very flaky" rather than a hard failure.

The change

Cache the statistics in a cached_property, which writes to __dict__ and
leaves the operands, and so the expression's token, alone.

The new test_len_of_partition_after_len_of_frame makes the precondition
explicit by taking the length of the frame first, so the bug is pinned
deterministically instead of by luck. With that in place the xfail on
test_len is removed.

Verification

Run with pixi run -e mindeps-dataframe, reverting only
dask/dataframe/dask_expr/io/io.py to main to control:

  • The reproducer above prints 100 three times out of three without the fix, and
    10 three times out of three with it.
  • test_len_of_partition_after_len_of_frame alone: fails without the fix with
    AssertionError: assert 100 == 10, passes with it.
  • test_len alone: passes either way, which is what makes it a poor guard on
    its own.
  • Both together, in that order, without the fix: 2 failed. The new test
    establishes the state that then makes test_len fail, which is the ordering
    CI was hitting by chance.
  • Both together with the fix: 2 passed.
  • pixi run lint passes 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_frame and
un-skips test_len, and pixi run lint passes.

Prior art

#12290 (open draft, last updated 2026-02-28) also closes #12275, from the other
end: it changes Partitions in dask/dataframe/dask_expr/_expr.py so that
partition length metadata is only reported when it can be inferred correctly.

This PR instead stops FromPandas producing the bad metadata in the first
place. The assignment to _pd_length_stats goes through Expr.__setattr__ into
self.operands after __new__ has cached _name and _determ_token, so
besides 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.

``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]>
@github-actions

Copy link
Copy Markdown
Contributor

Unit Test Results

See 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
 19 915 tests +     1   18 482 ✅ +     1   1 433 💤 ±    0  0 ❌ ±0 
360 918 runs   - 18 435  314 877 ✅  - 17 210  46 041 💤  - 1 225  0 ❌ ±0 

Results for commit e45ff43. ± Comparison against base commit 6ce3740.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test_collection.py::test_len

1 participant