Skip to content

Commit

Permalink
feat!: raise error to eager len calls (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikrommyd authored Nov 15, 2023
1 parent 40e6b35 commit 5bd87d0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/dask_awkward/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,12 @@ def repartition(

def __len__(self) -> int:
if not self.known_divisions:
self.eager_compute_divisions()
raise NotImplementedError(
"Cannot determine length of collection with unknown partition sizes without executing the graph.\n"
"Use `dask_awkward.num(..., axis=0)` if you want a lazy Scalar of the length.\n"
"If you want to eagerly compute the partition sizes to have the ability to call `len` on the collection"
", use `.eager_compute_divisions()` on the collection."
)
return self.divisions[-1] # type: ignore

def _shorttypestr(self, max: int = 10) -> str:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,20 @@ def test_compute_typetracer(daa: Array) -> None:

def test_len(ndjson_points_file: str) -> None:
daa = dak.from_json([ndjson_points_file] * 2)
assert len(daa) == 10
assert not daa.known_divisions
with pytest.raises(
NotImplementedError,
match=(
"Cannot determine length of collection with unknown partition sizes without executing the graph.\\n"
"Use `dask_awkward.num\\(\\.\\.\\., axis=0\\)` if you want a lazy Scalar of the length.\\n"
"If you want to eagerly compute the partition sizes to have the ability to call `len` on the collection"
", use `\\.eager_compute_divisions\\(\\)` on the collection."
),
):
assert len(daa) == 10
daa.eager_compute_divisions()
assert daa.known_divisions
assert len(daa) == 10 # type: ignore


def test_meta_exists(daa: Array) -> None:
Expand Down
8 changes: 7 additions & 1 deletion tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,18 @@ def test_from_awkward_empty_array(daa: dak.Array) -> None:
assert len(c1) == 0
a1 = dak.from_awkward(c1, npartitions=1)
assert_eq(a1, c1)
assert len(a1) == 0
assert not a1.known_divisions
a1.eager_compute_divisions()
assert a1.known_divisions
assert len(a1) == 0 # type: ignore

# with a form
c2 = ak.typetracer.length_zero_if_typetracer(daa.layout)
assert len(c2) == 0
a2 = dak.from_awkward(c2, npartitions=1)
assert not a2.known_divisions
a2.eager_compute_divisions()
assert a2.known_divisions
assert len(a2) == 0
daa.layout.form == a2.layout.form

Expand Down
12 changes: 12 additions & 0 deletions tests/test_io_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ def concrete_data(json_data_dir: Path) -> ak.Array:
def test_json_sanity(json_data_dir: Path, concrete_data: ak.Array) -> None:
source = os.path.join(str(json_data_dir))
ds = dak.from_json(source)
assert not ds.known_divisions
with pytest.raises(
NotImplementedError,
match=(
"Cannot determine length of collection with unknown partition sizes without executing the graph.\\n"
"Use `dask_awkward.num\\(\\.\\.\\., axis=0\\)` if you want a lazy Scalar of the length.\\n"
"If you want to eagerly compute the partition sizes to have the ability to call `len` on the collection"
", use `\\.eager_compute_divisions\\(\\)` on the collection."
),
):
assert ds
ds.eager_compute_divisions()
assert ds

assert_eq(ds, concrete_data)
Expand Down

0 comments on commit 5bd87d0

Please sign in to comment.