Skip to content
Merged
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
2 changes: 2 additions & 0 deletions haystack/utils/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def _prepare_ordering_comparison(value: Any, filter_value: Any) -> tuple[Any, An
value = _parse_date(value)
if not isinstance(filter_value, datetime):
filter_value = _parse_date(filter_value)

if isinstance(value, datetime) and isinstance(filter_value, datetime):
value, filter_value = _ensure_both_dates_naive_or_aware(value, filter_value)

if isinstance(filter_value, list):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
Fixed a ``TypeError`` raised when filtering documents with the ordering
operators (``>``, ``>=``, ``<``, ``<=``) where both the filter value and the
document meta value are ``datetime`` objects but one is timezone-aware and the
other is naive. The naive/aware reconciliation is now applied to any pair of
``datetime`` operands, matching the behavior already used for ISO 8601 string
dates.
12 changes: 12 additions & 0 deletions test/utils/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@
True,
id="> operator with ISO 8601 string filter value and datetime Document value",
),
pytest.param(
{"field": "meta.date", "operator": ">", "value": datetime(2023, 1, 1, tzinfo=timezone.utc)},
Document(meta={"date": datetime(2024, 1, 1)}),
True,
id="> operator with aware datetime filter value and naive datetime Document value",
),
pytest.param(
{"field": "meta.date", "operator": ">", "value": datetime(2023, 1, 1)},
Document(meta={"date": datetime(2024, 1, 1, tzinfo=timezone.utc)}),
True,
id="> operator with naive datetime filter value and aware datetime Document value",
),
pytest.param(
{"field": "meta.page", "operator": ">", "value": 10},
Document(),
Expand Down
Loading