Skip to content

Commit f9e3428

Browse files
rojihaas-daniel
andauthored
Cast NodaTime DateInterval.End to date. (#3024)
Fixes #3015 Co-authored-by: Daniel Haas <[email protected]>
1 parent a69ba3e commit f9e3428

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMemberTranslatorPlugin.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,13 @@ SqlExpression Upper()
251251

252252
if (member == DateInterval_End)
253253
{
254-
return
254+
// PostgreSQL creates a result of type 'timestamp without time zone' when subtracting intervals from dates, so add a cast back
255+
// to date.
256+
return _sqlExpressionFactory.Convert(
255257
_sqlExpressionFactory.Subtract(
256258
Upper(),
257-
_sqlExpressionFactory.Constant(Period.FromDays(1), _periodTypeMapping));
259+
_sqlExpressionFactory.Constant(Period.FromDays(1), _periodTypeMapping)), typeof(LocalDate),
260+
_typeMappingSource.FindMapping(typeof(LocalDate)));
258261
}
259262

260263
if (member == DateInterval_Length)

test/EFCore.PG.NodaTime.FunctionalTests/NodaTimeQueryNpgsqlTest.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,22 @@ await AssertQuery(
13001300
"""
13011301
SELECT n."Id", n."DateInterval", n."Duration", n."Instant", n."InstantRange", n."Interval", n."LocalDate", n."LocalDate2", n."LocalDateRange", n."LocalDateTime", n."LocalTime", n."Long", n."OffsetTime", n."Period", n."TimeZoneId", n."ZonedDateTime"
13021302
FROM "NodaTimeTypes" AS n
1303-
WHERE upper(n."DateInterval") - INTERVAL 'P1D' = DATE '2018-04-24'
1303+
WHERE CAST(upper(n."DateInterval") - INTERVAL 'P1D' AS date) = DATE '2018-04-24'
1304+
""");
1305+
}
1306+
1307+
[ConditionalTheory]
1308+
[MemberData(nameof(IsAsyncData))]
1309+
public async Task DateInterval_End_Select(bool async)
1310+
{
1311+
await AssertQuery(
1312+
async,
1313+
ss => ss.Set<NodaTimeTypes>().Select(t => t.DateInterval.End));
1314+
1315+
AssertSql(
1316+
"""
1317+
SELECT CAST(upper(n."DateInterval") - INTERVAL 'P1D' AS date)
1318+
FROM "NodaTimeTypes" AS n
13041319
""");
13051320
}
13061321

0 commit comments

Comments
 (0)