3939
4040if TYPE_CHECKING :
4141 from sqlalchemy .orm .attributes import InstrumentedAttribute
42- from sqlalchemy .sql import Select
42+ from sqlalchemy .sql import ColumnElement , Select
4343
4444
4545def _safe_parse_datetime (date_to_check : str ) -> datetime :
@@ -136,12 +136,27 @@ class NullableDatetimeRangeFilter(RangeFilter):
136136 ``OR`` predicates so each branch can be satisfied by an independent index scan.
137137
138138 NULL semantics: ``start_date=NULL`` means the task has not started yet; ``end_date=NULL`` means
139- the task is still running. For lower bounds the NULL branch passes unconditionally — a not-yet-
140- started/ended task will eventually satisfy any past lower bound. For upper bounds the NULL branch
141- is ``col IS NULL AND now() <= x``, preserving the COALESCE(col, now()) semantics without the
142- function-wrap index penalty.
139+ the task is still running. For lower bounds the NULL branch usually passes unconditionally, but
140+ callers may narrow it with an extra clause when NULL cannot still evolve into an in-range value.
141+ For upper bounds the NULL branch is ``col IS NULL AND now() <= x``, preserving the
142+ COALESCE(col, now()) semantics without the function-wrap index penalty.
143143 """
144144
145+ def __init__ (
146+ self ,
147+ value : Range | None ,
148+ attribute : InstrumentedAttribute ,
149+ null_lower_bound_clause : ColumnElement [bool ] | None = None ,
150+ ) -> None :
151+ super ().__init__ (value , attribute )
152+ self .null_lower_bound_clause = null_lower_bound_clause
153+
154+ def _get_null_lower_bound_clause (self ) -> ColumnElement [bool ]:
155+ null_clause : ColumnElement [bool ] = self .attribute .is_ (None )
156+ if self .null_lower_bound_clause is not None :
157+ null_clause = and_ (null_clause , self .null_lower_bound_clause )
158+ return null_clause
159+
145160 def to_orm (self , select : Select ) -> Select :
146161 if self .skip_none is False :
147162 raise ValueError (f"Cannot set 'skip_none' to False on a { type (self )} " )
@@ -151,10 +166,10 @@ def to_orm(self, select: Select) -> Select:
151166
152167 if self .value .lower_bound_gte :
153168 x = self .value .lower_bound_gte
154- select = select .where (or_ (self .attribute >= x , self .attribute . is_ ( None )))
169+ select = select .where (or_ (self .attribute >= x , self ._get_null_lower_bound_clause ( )))
155170 if self .value .lower_bound_gt :
156171 x = self .value .lower_bound_gt
157- select = select .where (or_ (self .attribute > x , self .attribute . is_ ( None )))
172+ select = select .where (or_ (self .attribute > x , self ._get_null_lower_bound_clause ( )))
158173 if self .value .upper_bound_lte :
159174 x = self .value .upper_bound_lte
160175 select = select .where (or_ (self .attribute <= x , and_ (self .attribute .is_ (None ), func .now () <= x )))
@@ -166,7 +181,11 @@ def to_orm(self, select: Select) -> Select:
166181
167182
168183def datetime_range_filter_factory (
169- filter_name : str , model : Base , attribute_name : str | None = None
184+ filter_name : str ,
185+ model : Base ,
186+ attribute_name : str | None = None ,
187+ * ,
188+ null_lower_bound_clause : ColumnElement [bool ] | None = None ,
170189) -> Callable [[datetime | None , datetime | None , datetime | None , datetime | None ], RangeFilter ]:
171190 def depends_datetime (
172191 lower_bound_gte : datetime | None = Query (alias = f"{ filter_name } _gte" , default = None ),
@@ -182,7 +201,9 @@ def depends_datetime(
182201 upper_bound_lt = upper_bound_lt ,
183202 )
184203 if filter_name in ("start_date" , "end_date" ):
185- return NullableDatetimeRangeFilter (range_val , attr )
204+ return NullableDatetimeRangeFilter (
205+ range_val , attr , null_lower_bound_clause = null_lower_bound_clause
206+ )
186207 return RangeFilter (range_val , attr )
187208
188209 return depends_datetime
0 commit comments