awkward icon indicating copy to clipboard operation
awkward copied to clipboard

ak.zip needs to check for empty input and do something appropriate

Open jpivarski opened this issue 3 years ago • 2 comments

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jpivarski/irishep/uproot4/uproot4/behaviors/TBranch.py", line 1164, in arrays
    return library.group(output, expression_context, how)
  File "/home/jpivarski/irishep/uproot4/uproot4/interpretation/library.py", line 528, in group
    return awkward1.zip(
  File "/home/jpivarski/miniconda3/lib/python3.8/site-packages/awkward1/operations/structure.py", line 348, in zip
    out = awkward1._util.broadcast_and_apply(layouts, getfunction, behavior)
  File "/home/jpivarski/miniconda3/lib/python3.8/site-packages/awkward1/_util.py", line 972, in broadcast_and_apply
    out = apply(broadcast_pack(inputs, isscalar), 0)
  File "/home/jpivarski/miniconda3/lib/python3.8/site-packages/awkward1/_util.py", line 564, in apply
    checklength([x for x in inputs if isinstance(x, awkward1.layout.Content)])
  File "/home/jpivarski/miniconda3/lib/python3.8/site-packages/awkward1/_util.py", line 480, in checklength
    length = len(inputs[0])

The problem is in awkward1/operations/structure.py", line 348, in zip; there needs to be a check for length zero arguments.

This ought to be super easy.

jpivarski avatar Sep 26 '20 21:09 jpivarski

I must have written this in a hurry. I think it means that the following doesn't work:

>>> ak.zip([])

and

>>> ak.zip({})

While it might seem silly that someone would try to do that, my stack trace shows that this is coming from Uproot.

The zip is not going to know what the length of the array is supposed to be. When coming from Uproot, the length should be set by num_entries. This really ought to be fixed in Uproot, not in Awkward Array. (Maybe the ak.zip documentation should add the word "non-empty".) Transferring the issue...

jpivarski avatar Dec 11 '20 22:12 jpivarski

It's a very old issue, oddly on Uproot's bug-tracker instead of Awkward's, but it's still the case that ak.zip([]) and ak.zip({}) don't explicitly say that empty sets of arrays are not allowed (or create a length-zero tuple/record array of no fields, but I think an error message would be better).

>>> ak.zip([])
Traceback (most recent call last):
  File "/home/jpivarski/irishep/awkward/src/awkward/_dispatch.py", line 62, in dispatch
    next(gen_or_result)
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_zip.py", line 151, in zip
    return _impl(
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_zip.py", line 241, in _impl
    out = ak._broadcasting.broadcast_and_apply(
  File "/home/jpivarski/irishep/awkward/src/awkward/_broadcasting.py", line 978, in broadcast_and_apply
    backend = backend_of(*inputs, coerce_to_common=False)
  File "/home/jpivarski/irishep/awkward/src/awkward/_backends/dispatch.py", line 110, in backend_of
    raise ValueError("could not find backend for", objects)
ValueError: ('could not find backend for', ())

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jpivarski/irishep/awkward/src/awkward/_dispatch.py", line 38, in dispatch
    with OperationErrorContext(name, args, kwargs):
  File "/home/jpivarski/irishep/awkward/src/awkward/_errors.py", line 85, in __exit__
    self.handle_exception(exception_type, exception_value)
  File "/home/jpivarski/irishep/awkward/src/awkward/_errors.py", line 95, in handle_exception
    raise self.decorate_exception(cls, exception)
ValueError: ('could not find backend for', ())

This error occurred while calling

    ak.zip(
        []
    )

and

>>> ak.zip({})
Traceback (most recent call last):
  File "/home/jpivarski/irishep/awkward/src/awkward/_dispatch.py", line 62, in dispatch
    next(gen_or_result)
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_zip.py", line 151, in zip
    return _impl(
  File "/home/jpivarski/irishep/awkward/src/awkward/operations/ak_zip.py", line 241, in _impl
    out = ak._broadcasting.broadcast_and_apply(
  File "/home/jpivarski/irishep/awkward/src/awkward/_broadcasting.py", line 978, in broadcast_and_apply
    backend = backend_of(*inputs, coerce_to_common=False)
  File "/home/jpivarski/irishep/awkward/src/awkward/_backends/dispatch.py", line 110, in backend_of
    raise ValueError("could not find backend for", objects)
ValueError: ('could not find backend for', ())

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jpivarski/irishep/awkward/src/awkward/_dispatch.py", line 38, in dispatch
    with OperationErrorContext(name, args, kwargs):
  File "/home/jpivarski/irishep/awkward/src/awkward/_errors.py", line 85, in __exit__
    self.handle_exception(exception_type, exception_value)
  File "/home/jpivarski/irishep/awkward/src/awkward/_errors.py", line 95, in handle_exception
    raise self.decorate_exception(cls, exception)
ValueError: ('could not find backend for', ())

This error occurred while calling

    ak.zip(
        {}
    )

jpivarski avatar Jan 30 '24 15:01 jpivarski