Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions haystack/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#
# SPDX-License-Identifier: Apache-2.0

from collections.abc import Callable
from dataclasses import fields
from datetime import datetime
from operator import ge, gt, le, lt
from typing import Any

import dateutil.parser
Expand Down Expand Up @@ -156,6 +158,17 @@ def _prepare_ordering_comparison(
return value, filter_value, True


def _compare(compare: Callable[[Any, Any], bool], value: Any, filter_value: Any) -> bool:
try:
return compare(value, filter_value)
except TypeError as exc:
msg = (
f"Can't compare a value of type '{type(value).__name__}' with a value of type "
f"'{type(filter_value).__name__}' using operators '>', '>=', '<', '<='"
)
raise FilterError(msg) from exc


def _greater_than(value: Any, filter_value: Any, strict_datetime_comparison: bool) -> bool:
if value is None or filter_value is None:
# We can't compare None values reliably using operators '>', '>=', '<', '<='
Expand All @@ -166,7 +179,7 @@ def _greater_than(value: Any, filter_value: Any, strict_datetime_comparison: boo
)
if not comparable:
return False
return value > filter_value
return _compare(compare=gt, value=value, filter_value=filter_value)


def _parse_date(value: str) -> datetime:
Expand Down Expand Up @@ -212,7 +225,7 @@ def _greater_than_equal(value: Any, filter_value: Any, strict_datetime_compariso
)
if not comparable:
return False
return value >= filter_value
return _compare(compare=ge, value=value, filter_value=filter_value)


def _less_than(value: Any, filter_value: Any, strict_datetime_comparison: bool) -> bool:
Expand All @@ -225,7 +238,7 @@ def _less_than(value: Any, filter_value: Any, strict_datetime_comparison: bool)
)
if not comparable:
return False
return value < filter_value
return _compare(compare=lt, value=value, filter_value=filter_value)


def _less_than_equal(value: Any, filter_value: Any, strict_datetime_comparison: bool) -> bool:
Expand All @@ -238,7 +251,7 @@ def _less_than_equal(value: Any, filter_value: Any, strict_datetime_comparison:
)
if not comparable:
return False
return value <= filter_value
return _compare(compare=le, value=value, filter_value=filter_value)


def _in(value: Any, filter_value: Any, strict_datetime_comparison: bool) -> bool:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
fixes:
- |
``filter_documents()`` on ``InMemoryDocumentStore`` (and any code using ``document_matches_filter``) raised a
raw ``TypeError`` for ``>``, ``>=``, ``<`` and ``<=`` filters when a document stored a list or dict in the
filtered field, for example ``{"field": "meta.page", "operator": ">", "value": 1}`` with a document whose
``meta["page"]`` is ``[1, 2]``. It now raises ``FilterError``, like other incomparable values.
9 changes: 9 additions & 0 deletions test/utils/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,15 @@ def test_document_matches_filter_raises_filter_error_for_datetime_and_numeric_va
document_matches_filter(filters, Document(meta={"date": datetime(2025, 1, 1)}))


@pytest.mark.parametrize("operator", [">", ">=", "<", "<="])
@pytest.mark.parametrize("document_value", [[1, 2], {"a": 1}])
def test_document_matches_filter_raises_filter_error_for_incomparable_document_value(operator, document_value):
filters = {"field": "meta.page", "operator": operator, "value": 1}

with pytest.raises(FilterError, match=f"Can't compare a value of type '{type(document_value).__name__}'"):
document_matches_filter(filters, Document(meta={"page": document_value}))


def test_dotted_field_with_unknown_root_is_treated_as_missing():
# A dotted field whose root is not a Document attribute must be treated as a missing field
# (matching the documented "treat it as None" behavior and non-dotted unknown fields),
Expand Down