Skip to content

fix(filters): raise FilterError when comparing naive and aware datetimes in ordering operators - #12254

Closed
Aftabbs wants to merge 3 commits into
deepset-ai:mainfrom
Aftabbs:fix/filter-naive-aware-datetime-inconsistency
Closed

fix(filters): raise FilterError when comparing naive and aware datetimes in ordering operators#12254
Aftabbs wants to merge 3 commits into
deepset-ai:mainfrom
Aftabbs:fix/filter-naive-aware-datetime-inconsistency

Conversation

@Aftabbs

@Aftabbs Aftabbs commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes

Before this fix, the metadata filter ordering operators (>, >=, <, <=) were inconsistent with the equality operators (==, !=) when one datetime was timezone-naive and the other was timezone-aware.

Root cause: _ensure_both_dates_naive_or_aware (used only by the ordering path) silently copied the timezone from one side to the other. The equality path (refactored in #11963) already raises FilterError for the same mixed-awareness pair.

Consequence: For a document with meta.d = "2023-01-01T00:00:00" (naive) and a filter value of "2023-01-01T00:00:00+00:00" (UTC-aware):

  • ==FilterError ✓ (correct)
  • >=True ✗ (timezone was copied, comparing equal instants)
  • <=True ✗ (same)
  • >False ✗ (no error, wrong branch)

This violates the identity a == b ⟺ a >= b ∧ a <= b.

Fix: _ensure_both_dates_naive_or_aware now raises FilterError when the two datetimes differ in timezone awareness, consistent with the policy already in place for == and !=. The error message mirrors the existing one and tells the user what to do.

How did you test it?

  • Removed three existing parametrized test cases that expected True for mixed-awareness ordering (they tested the old, now-incorrect behaviour).
  • Updated one parametrize entry that used a mixed-awareness pair under >= with a valid same-awareness pair.
  • Added a dedicated regression test test_ordering_and_equality_consistent_for_mixed_timezone_awareness that asserts FilterError is raised for all four ordering operators (>, >=, <, <=) when the document value is naive and the filter value is aware.
  • Full test suite: pytest test/utils/test_filters.py115 passed.
  • Also ran pytest test/document_stores/test_in_memory.py -k "date or filter"68 passed.

Notes for the reviewer

The change is confined to _ensure_both_dates_naive_or_aware in haystack/utils/filters.py (≈10 line diff). The equality path (_dates_are_equal) is unchanged. The release note is in releasenotes/notes/filter-naive-aware-datetime-ordering-75719e82d84f43d6.yaml.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used a conventional commit type for my PR title (fix:).
  • I have added a release note file.

…mes in ordering operators

Ordering operators (>, >=, <, <=) were inconsistent with the equality operators
(== and !=): they silently copied the timezone from one datetime to the other,
which allowed a >= b and a <= b to both return True while a == b returned False.
This violated the identity a == b <=> a >= b and a <= b.

The root cause is _ensure_both_dates_naive_or_aware, which was introduced
separately from the == / != refusal logic. Fix: raise FilterError for
mixed-awareness pairs in ordering operators, matching the policy already in
place for == and !=-

Closes deepset-ai#12246
@Aftabbs
Aftabbs requested a review from a team as a code owner August 6, 2026 07:29
@Aftabbs
Aftabbs requested review from sjrl and removed request for a team August 6, 2026 07:29
@vercel

vercel Bot commented Aug 6, 2026

Copy link
Copy Markdown

@Aftabbs is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Aug 6, 2026
Aftabbs added 2 commits August 7, 2026 07:17
…rror behavior

After the naive/aware datetime ordering fix, mixing a naive filter value
('2025-02-01') with timezone-aware document values now raises FilterError.
Split the old test into two:
- test_run_datetime_with_timezone_raises_filter_error: verifies the new
  error is raised when filter value is naive and document values are aware.
- test_run_datetime_with_matching_timezone: verifies the happy path when
  both filter and document values carry explicit timezone info.
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  filters.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl

sjrl commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution! However, we decided to go with a different approach for solving the issue in this PR #12257

@sjrl sjrl closed this Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ordering and equality filters disagree on naive vs tz-aware datetimes

2 participants