diff --git a/haystack/utils/filters.py b/haystack/utils/filters.py index bfd1e1e60fe..0edb2e7a9d4 100644 --- a/haystack/utils/filters.py +++ b/haystack/utils/filters.py @@ -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 @@ -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 '>', '>=', '<', '<=' @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/releasenotes/notes/filter-incomparable-document-value-68c8cf99b55004f7.yaml b/releasenotes/notes/filter-incomparable-document-value-68c8cf99b55004f7.yaml new file mode 100644 index 00000000000..60ec2543132 --- /dev/null +++ b/releasenotes/notes/filter-incomparable-document-value-68c8cf99b55004f7.yaml @@ -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. diff --git a/test/utils/test_filters.py b/test/utils/test_filters.py index 1570224f31e..03f4946b2c7 100644 --- a/test/utils/test_filters.py +++ b/test/utils/test_filters.py @@ -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),