Skip to content

Commit

Permalink
check for array length before unwinding
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminpkane committed Nov 5, 2024
1 parent 31f2d7b commit 9e00d00
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions fiftyone/server/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ def _first(
if sort:
pipeline.append({"$match": {path: {"$ne": None}}})

matched_arrays = _match_arrays(dataset, path, is_frame_field)
if matched_arrays:
pipeline += matched_arrays

pipeline.append({"$limit": 1})

unwound = _unwind(dataset, path, is_frame_field)
Expand Down Expand Up @@ -410,6 +414,25 @@ def _parse_result(data, check=True):
return None


def _match_arrays(dataset: fo.Dataset, path: str, is_frame_field: bool):
keys = path.split(".")
path = None
pipeline = []

if is_frame_field:
path = keys[0]
keys = keys[1:]

for key in keys:
path = ".".join([path, key]) if path else key
field = dataset.get_field(path)
while isinstance(field, fof.ListField):
pipeline.append({"$match": {f"{path}.0": {"$exists": True}}})
field = field.field

return pipeline


def _unwind(dataset: fo.Dataset, path: str, is_frame_field: bool):
keys = path.split(".")
path = None
Expand Down

0 comments on commit 9e00d00

Please sign in to comment.