Repository navigation
fix: empty result for ak.ravel/ak.flatten(axis=None) with nothing to merge - #4292
Merged
Merged
Conversation
… 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>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files
|
Contributor
|
The documentation preview is ready to be viewed at http://preview.awkward-array.org.s3-website.us-east-1.amazonaws.com/PR4292 |
ianna
approved these changes
Aug 11, 2026
ianna
left a comment
Member
There was a problem hiding this comment.
@TaiSakuma - Looks good! Thanks for fixing it.
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.
Fixes #4291. Fixes #4263.
ak.ravelandak.flatten(axis=None)collect every branch's leaf arrays withak._do.remove_structureand merge them withak._do.mergemany, whose first statement isassert len(contents) != 0. Two states leave the collection empty and hit that bareAssertionError: 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_structurereturns no parts, the result is an empty array (EmptyArray, type0 * unknown) instead of the merge.ak._do.reduce, the otherremove_structureconsumer, already handles this state the same way (elif len(parts) == 0: layout = ak.contents.EmptyArray()). TheEmptyArrayis 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:
0 * unknown, not a typed empty. A typed record result would be wrong anyway: these operations erase record structure, and a zero-field record beside an ordinary leaf already flattens to nothing ({x: float64, y: {}}returns the barexvalues).ak.ravelraisesAssertionErrorwith no message on a union that no values reach #4263:ak.flatten(ak.Array([1, "s"])[:0], axis=None)now returns0 * unknown, while the all-numeric analogak.flatten(ak.Array([1, [2, 3]])[:0], axis=None)returns0 * int64via the union fast path from perf: vectorize union flattening and cache nplike feature probes #4109.ak.ravel(ak.Array([{}, None]))still returns[{}, None](type2 * ?{}): withdrop_nones=Falsethe option and the record under it survive as one part, the mechanism reported inak.ravelraisesAssertionErroron a record under an option beside a non-record leaf #4283, which this PR does not change.Following the known-issues contract in
tests/properties/operations/known_issues.py, thehas_issue_4263predicate, its_reaches_empty_unionhelper, and its two uses in theak.ravelandak.flattenproperty-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) andtests/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