Skip to content

fix: empty result for ak.ravel/ak.flatten(axis=None) with nothing to merge - #4292

Merged
TaiSakuma merged 1 commit into
scikit-hep:mainfrom
TaiSakuma:fix-empty-parts-merge
Aug 11, 2026
Merged

TaiSakuma merged 1 commit into
scikit-hep:mainfrom
TaiSakuma:fix-empty-parts-merge

Conversation

@TaiSakuma

Copy link
Copy Markdown
Member

Fixes #4291. Fixes #4263.

ak.ravel and ak.flatten(axis=None) collect every branch's leaf arrays with ak._do.remove_structure and merge them with ak._do.mergemany, whose first statement is assert len(contents) != 0. Two states leave the collection empty and hit that bare AssertionError: an array of zero-field records, which contribute no parts (#4291), and a union that no values reach when some branch is a string or a multi-field record (#4263).

This PR adds the same guard at both call sites: when remove_structure returns no parts, the result is an empty array (EmptyArray, type 0 * unknown) instead of the merge. ak._do.reduce, the other remove_structure consumer, already handles this state the same way (elif len(parts) == 0: layout = ak.contents.EmptyArray()). The EmptyArray is created on the input's backend, so typetracer and CUDA inputs keep their backend. The guard only activates in a state that previously always raised, so no currently-working input changes behavior.

Choices worth noting:

Following the known-issues contract in tests/properties/operations/known_issues.py, the has_issue_4263 predicate, its _reaches_empty_union helper, and its two uses in the ak.ravel and ak.flatten property-test modules are deleted, so the previously skipped draws are tested again. No predicate existed for #4291.

Regression tests cover both issues: tests/test_4291_ravel_zero_field_records.py (record and tuple flavors, length 0, nested below var-length and regular lists, a record of zero-field records, the mixed-record case, the option cases, and typetracer backend preservation) and tests/test_4263_ravel_empty_union.py (empty unions with string, multi-field-record, and numeric branches, a union below a list that no values reach, and unions with a zero-field-record branch).

🤖 Generated with Claude Code

… to merge

Zero-field records (scikit-hep#4291) and unions that no values reach (scikit-hep#4263)
contribute no parts to merge; return an empty array instead of failing
`ak._do.mergemany`'s assertion.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions github-actions Bot added the type/fix PR title type: fix (set automatically) label Aug 10, 2026
@TaiSakuma
TaiSakuma requested a review from ianna August 10, 2026 15:20
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.01%. Comparing base (0ce96ed) to head (ec9c8af).

Additional details and impacted files
Files with missing lines Coverage Δ
src/awkward/operations/ak_flatten.py 98.27% <100.00%> (+0.06%) ⬆️
src/awkward/operations/ak_ravel.py 96.00% <100.00%> (+0.34%) ⬆️

@github-actions

Copy link
Copy Markdown
Contributor

The documentation preview is ready to be viewed at http://preview.awkward-array.org.s3-website.us-east-1.amazonaws.com/PR4292

@ianna ianna left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@TaiSakuma - Looks good! Thanks for fixing it.

@TaiSakuma
TaiSakuma merged commit 2357b9c into scikit-hep:main Aug 11, 2026
39 checks passed
@TaiSakuma
TaiSakuma deleted the fix-empty-parts-merge branch October 8, 2026 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type/fix PR title type: fix (set automatically)

Projects

None yet

2 participants