Repository navigation
Fix TimeOnly.IsBetween for midnight-spanning ranges - #4127
Conversation
Use OR for constant ranges spanning midnight and preserve AND for normal and equal-bound ranges. Handle both directions for parameter/column bounds. Add result and SQL regressions for boundaries, nulls, precision, negation and compiled queries with changing parameters. Part of npgsql#4090
roji
left a comment
There was a problem hiding this comment.
Recommendation: Proceed with this fix. I found no blocking correctness or scope issues at bab7a23.
Problem
The premise is correct. The existing translator always emits time >= start AND time < end. That cannot match an overnight range, although .NET treats it as wrapping around midnight. For example:
context.Set<ClockRow>()
.Where(r => r.Time.IsBetween(new TimeOnly(22, 0), new TimeOnly(2, 0)));Rows at 23:00 and 00:30 should match, but neither does before this fix. This addresses the IsBetween portion of #4090; its other temporal issues should remain separate.
Expected approach
Keep the change in the method translator: use AND for ordinary/equal constant bounds and OR for overnight constants. For parameters and column expressions, emit both alternatives guarded by the ordering of the bounds. Preserve inclusive start, exclusive end, and an empty equal-bound range. Focused result/SQL tests should cover boundaries, dynamic bounds, nulls, negation, and parameter reuse. No public API, persisted-format, migration, or conceptual-documentation change is needed.
PR alignment and before/after SQL
The implementation follows that approach with a small localized change. Its type-mapping and predicate construction also follow EF's SQL Server implementation. It introduces no command-cache or parameter-processing plumbing.
Captured SQL for the constant overnight query, omitting only the identical ORDER BY:
-- Before: incorrectly returns no rows
SELECT c."Time"
FROM "ClockRows" AS c
WHERE c."Time" >= TIME '22:00:00' AND c."Time" < TIME '02:00:00'
-- After
SELECT c."Time"
FROM "ClockRows" AS c
WHERE c."Time" >= TIME '22:00:00' OR c."Time" < TIME '02:00:00'For captured TimeOnly start, end parameters, the predicate changes as follows (line-wrapped for readability):
-- Before
WHERE c."Time" >= @start AND c."Time" < @end
-- After: the same SQL handles ordinary, overnight, and equal bounds
WHERE (@start <= @end AND c."Time" >= @start AND c."Time" < @end)
OR (@start > @end AND (c."Time" >= @start OR c."Time" < @end))Ordinary constant ranges retain their existing AND SQL; equal constant bounds remain empty.
Gaps
No required changes identified. The regression coverage covers the relevant cases, including a compiled query executed with changing parameters. The larger dynamic predicate is justified by runtime/per-row bounds, rather than unnecessary abstraction.
Manual validation
I wrote and ran a separate .NET 11 consumer program against both the base revision and PR head, using isolated PostgreSQL 18.6 containers and a mapped temporary table containing 13 explicit clock values. Nine scenarios covered ordinary/overnight/equal constants and parameters, negation, column bounds, and calculated bounds. Five scenarios disagreed with the .NET API before the fix; all nine matched afterward. The overnight query changed from zero rows to the six expected values, including 22:00 and excluding 02:00.
Separately, all 43 TimeOnly translation tests passed without skips using the repository's automatic isolated PostGIS testcontainer. This assessment is based on executed consumer queries as well as code and tests, not on the PR's reported validation or benchmarks.
Posted by copilot
|
@emremenekse if you post on #4129 I'll assign you to it for creddit. |
Backport #4127 to hotfix/10.0.4, retaining cached MethodInfo matching and adapting raw SQL test baselines to the existing TimeOnly casts. Fixes #4129. (cherry picked from commit bab7a23) Co-authored-by: Emre Menekşe <[email protected]> Co-authored-by: Copilot App <[email protected]>
TimeOnly.IsBetween currently uses time >= start AND time < end for every range. A query for 22:00–02:00 consequently misses matching values such as 23:00 and 00:30. Use OR when constant bounds span midnight, retain AND for normal/equal-bound ranges, and express both alternatives for parameter or column bounds. The start remains inclusive and the end exclusive; equal bounds remain empty.
Fixes #4129. Split from #4090; this addresses only TimeOnly.IsBetween.
The change infers a consistent type mapping for the receiver and bounds. It does not change parameter-based SQL processing or command caching.
Validation
Local Windows validation with the pinned SDK 11.0.100 and isolated PostgreSQL 18.6 testcontainers:
Tests check results and exact SQL for normal/midnight ranges, boundaries (including exclusion of 21:00 and 02:00), equal bounds, microsecond precision, nulls, negation, column/calculated bounds, and one compiled query executed with changing range parameters.
Local performance check
Compared warm EF Count calls with hand-written correct range predicates over 100,000 synthetic rows: six shapes, indexed/unindexed, automatic preparation disabled/enabled (24 comparisons). Each pair used ten warmups and seven alternating batches, with 100 calls per indexed batch and 20 per unindexed batch. All counts and twelve explicit boundary rows matched C# results. Captured EXPLAIN plans used the same scan types as the reference: index-only scans for normal/narrow/equal ranges and two bitmap index scans for midnight ranges.
Indexed local medians with automatic preparation enabled:
The largest relative overhead in the measured cases was about 6.1% (34 us). These are local medians, not confidence intervals or an application-wide speedup claim. The incorrect pre-fix predicate is not a working performance reference.
The measured parameterized prepared Count statement used custom plans. Forced generic plans, cold compilation, allocations, concurrent load, and performance of complex per-row bounds were not measured. The full functional suite, other PostgreSQL versions and GitHub CI have not been run for this local change.