Repository navigation
Raise for empty RNTuple field filters - #1849
aviralgarg05 wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🤖 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.)
|
|
||
| if ( | ||
| len( | ||
| hasfields.keys( |
There was a problem hiding this comment.
🤖 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 fieldsA 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: ValueErrorThe 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.
| ) | ||
| == 0 | ||
| ): | ||
| raise ValueError( |
There was a problem hiding this comment.
🤖 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( |
There was a problem hiding this comment.
🤖 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: 1This is a rare case, but it would be good to either keep the guard or test the new value deliberately.
| "Expressions are not supported yet. They are currently equivalent to filter_name." | ||
| ) | ||
|
|
||
| _check_for_no_fields( |
There was a problem hiding this comment.
🤖 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.
|
Thanks @ariostas, the regression was real and all four points are fixed. On the approach though, measuring the selected form broke two existing tests: |
Summary
ValueErrorwhen RNTuple field filters select no fieldsnum_entries_for,arrays, anditerateCloses #1786
Validation
uv run pytest -q tests/test_1786_rntuple_empty_filters.pyAI 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.