Skip to content

Commit 3cfbaa2

Browse files
authored
fix: reconcile tz-aware and naive datetimes in ordering filters (#11943)
1 parent 4306fa3 commit 3cfbaa2

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

haystack/utils/filters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ def _prepare_ordering_comparison(value: Any, filter_value: Any) -> tuple[Any, An
6363
value = _parse_date(value)
6464
if not isinstance(filter_value, datetime):
6565
filter_value = _parse_date(filter_value)
66+
67+
if isinstance(value, datetime) and isinstance(filter_value, datetime):
6668
value, filter_value = _ensure_both_dates_naive_or_aware(value, filter_value)
6769

6870
if isinstance(filter_value, list):
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
fixes:
3+
- |
4+
Fixed a ``TypeError`` raised when filtering documents with the ordering
5+
operators (``>``, ``>=``, ``<``, ``<=``) where both the filter value and the
6+
document meta value are ``datetime`` objects but one is timezone-aware and the
7+
other is naive. The naive/aware reconciliation is now applied to any pair of
8+
``datetime`` operands, matching the behavior already used for ISO 8601 string
9+
dates.

test/utils/test_filters.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@
143143
True,
144144
id="> operator with ISO 8601 string filter value and datetime Document value",
145145
),
146+
pytest.param(
147+
{"field": "meta.date", "operator": ">", "value": datetime(2023, 1, 1, tzinfo=timezone.utc)},
148+
Document(meta={"date": datetime(2024, 1, 1)}),
149+
True,
150+
id="> operator with aware datetime filter value and naive datetime Document value",
151+
),
152+
pytest.param(
153+
{"field": "meta.date", "operator": ">", "value": datetime(2023, 1, 1)},
154+
Document(meta={"date": datetime(2024, 1, 1, tzinfo=timezone.utc)}),
155+
True,
156+
id="> operator with naive datetime filter value and aware datetime Document value",
157+
),
146158
pytest.param(
147159
{"field": "meta.page", "operator": ">", "value": 10},
148160
Document(),

0 commit comments

Comments
 (0)