Skip to content

Raise for empty RNTuple field filters - #1849

Open
aviralgarg05 wants to merge 3 commits into
scikit-hep:mainfrom
aviralgarg05:fix/rntuple-empty-filter
Open

aviralgarg05 wants to merge 3 commits into
scikit-hep:mainfrom
aviralgarg05:fix/rntuple-empty-filter

Conversation

@aviralgarg05

Copy link
Copy Markdown

Summary

  • raise a ValueError when RNTuple field filters select no fields
  • use one shared check in num_entries_for, arrays, and iterate
  • cover top-level and struct-field selections without changing TTree behavior

Closes #1786

Validation

  • uv run pytest -q tests/test_1786_rntuple_empty_filters.py
  • existing broader RNTuple and full-suite checks were run before this final focused verification

AI assistance

I used AI assistance for codebase exploration and iterative implementation. I reviewed the final diff, verified the intended RNTuple-only scope, and ran the listed tests myself.

@ariostas ariostas 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.

🤖 AI text below 🤖

Thanks for the PR! I checked these against the PR head (2d7ccc5) and its parent. Raising when the filters match nothing is the direction we suggested in #1786, so the overall approach looks good. There's one regression to fix, plus a few smaller things inline.

The regression: the check also raises on leaf fields for filters that match them, e.g. nt["top"].arrays(filter_name="*"). Details are in the inline comment. One way to fix it, which #1786 also suggested, is to check whether the selected form has any columns instead of counting keys(). to_akform always includes the field itself, so a leaf field would pass, while a struct field whose subfields are all filtered out still has no columns and would raise.

For the record, TTree keeps returning empty results in this case (arrays gives an empty array, num_entries_for gives 1), so the two formats will differ until TTree gets a follow-up. That's expected for this PR; we'll mention it in the release notes. (uproot.dask already raised in this case, so it stays consistent.)

Comment thread src/uproot/behaviors/RNTuple.py Outdated

if (
len(
hasfields.keys(

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.

🤖 AI text below 🤖

Regression: a leaf field now raises for any filter. On a field, hasfields.keys(...) only lists subfields, and a leaf field has none. So this check always fails, even for filters that match the field itself. to_akform always includes the field (keys = [self.path] + ...), so these calls returned data before:

import uproot
with uproot.recreate("t.root") as f:
    f.mkrntuple("ntuple", {"top": [1, 2], "vec": [[1, 2], [3]]})
nt = uproot.open("t.root")["ntuple"]

nt["top"].arrays(filter_name="*")        # main: [{top: 1}, {top: 2}]; PR: ValueError
nt["top"].arrays(expressions="top")      # same (expressions is copied into filter_name)
nt["top"].arrays(filter_typename="*")    # same
nt["top"].num_entries_for("1 kB", filter_name="*")      # main: 125; PR: ValueError
list(nt["top"].iterate(filter_name="*", step_size=1))  # same
nt["vec"].arrays(filter_name="*")        # same for list fields

A struct field selected by its own name, or by a full path, also raises now:

with uproot.recreate("t.root") as f:
    f.mkrntuple("ntuple", {"struct": [{"field": 1}, {"field": 2}]})
nt = uproot.open("t.root")["ntuple"]
nt["struct"].arrays(filter_name="struct")        # main: [{struct: {}}, ...]; PR: ValueError
nt["struct"].arrays(filter_name="struct.field")  # main: [{struct: {}}, ...]; PR: ValueError

The struct cases returned empty records on main, so they didn't work before either, and raising is arguably better. Field filters only matching relative subfield names is a separate, older issue.

Suggested fix: when hasfields is a field and not the RNTuple, count the field itself as a match. For example, skip the check if the filters accept the field. Please also add a leaf-field case to the test.

Comment thread src/uproot/behaviors/RNTuple.py Outdated
)
== 0
):
raise ValueError(

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.

🤖 AI text below 🤖

nit: the message includes the reprs of filters left at their defaults, so it shows memory addresses:

ValueError: no fields matched the given filters: filter_name='x', filter_typename=<function no_filter at 0x129082770>, filter_field=<function no_filter at 0x129082770>, filter_branch=uproot._util.unset

Could it list only the filters that aren't no_filter/unset?

if len(akform.contents) == 0:
return

return _num_entries_for(

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.

🤖 AI text below 🤖

nit: removing the len(akform.contents) == 0 guard changes the result for an RNTuple with no fields and default filters. It now returns 1 instead of None, because _num_entries_for maps total_bytes == 0 to 1:

import uproot
with uproot.recreate("e.root") as f:
    f.mkrntuple("e", {})
uproot.open("e.root")["e"].num_entries_for("1 kB")  # main: None; PR: 1

This is a rare case, but it would be good to either keep the guard or test the new value deliberately.

Comment thread src/uproot/behaviors/RNTuple.py Outdated
"Expressions are not supported yet. They are currently equivalent to filter_name."
)

_check_for_no_fields(

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.

🤖 AI text below 🤖

nit: iterate runs the check here, and then each self.arrays(...) call in the loop below runs it again. For a 2-entry iterate(step_size=1, filter_branch=...) that's 9 keys() calls instead of 6, and 6 DeprecationWarnings instead of 3. The cost is small, but a private flag to skip the check in the inner arrays calls would avoid it.

A field lists only its subfields in keys(), so a leaf has none and the check
rejected filters that did match the field itself: to_akform always includes the
field, and nt["top"].arrays(filter_name="*") raised instead of reading. Only
something with subfields can fail to match them, so the check now stays quiet
when there were none to match.

Measuring the selected form instead would be wrong twice over: an empty struct
contributes no columns, and awkward's columns() raises on a union form.

Also restore the empty-form guard in num_entries_for, so an RNTuple with no
fields returns None rather than 1; report only the filters that were actually
given, so the message no longer shows the repr of a default; and let iterate
tell its inner arrays calls that these filters are already checked.
@aviralgarg05

Copy link
Copy Markdown
Author

Thanks @ariostas, the regression was real and all four points are fixed. On the approach though, measuring the selected form broke two existing tests: test_empty_struct, because an empty struct legitimately contributes no columns so the check cannot tell it from a non-match, and test_selected_field_matches_its_array, because awkward’s columns() raises zip() argument 2 is shorter than argument 1 on a union form. So I kept keys() and fixed its blind spot instead: the check now stays quiet when the object had no subfields to match, since to_akform always includes the field itself, which lets a leaf read while a struct whose subfields are all filtered out still raises. The other three are as you described, with num_entries_for back to None on a fieldless RNTuple, only the given filters in the message, and a private _filters_checked flag that takes a two-step iterate(filter_branch=...) from six DeprecationWarnings to four. Worth flagging: a filter matching nothing on a leaf no longer raises at all, since the field is always in the form, and I think that is inherent to the behaviour rather than something I can have both ways. 417 RNTuple tests pass, with new coverage for the leaf regression, the message and the fieldless case; I could not run ruff here because this machine cannot reach PyPI, so lint rests on CI.

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.

RNTuple: arrays() reads an extra cluster when entry_stop is on a cluster boundary

2 participants