Add generic date_trunc translation#3858
Merged
Merged
Conversation
b73f4e4 to
c88299f
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces a generic EF.Functions.DateTrunc<T>() API to allow LINQ queries to express PostgreSQL date_trunc directly across BCL temporal types and NodaTime types, with special handling to emit the 2-argument overload when the optional timeZone argument is omitted.
Changes:
- Added
NpgsqlDbFunctionsExtensions.DateTrunc<T>(...)public API for timestamp/interval truncation with optional timezone. - Implemented method-call translation for the new API in both the core provider (
DateTime/DateTimeOffset/TimeSpan) and the NodaTime plugin. - Added functional tests covering
date_trunctranslation for BCL and NodaTime types (with/without timezone where applicable).
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.PG.FunctionalTests/Query/Translations/TimestampTranslationsTest.cs | Adds SQL-assertion tests for EF.Functions.DateTrunc on BCL timestamp/interval scenarios. |
| test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/ZonedDateTimeTranslationsTest.cs | Adds DateTrunc translation coverage for ZonedDateTime. |
| test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/PeriodTranslationsTest.cs | Adds DateTrunc translation coverage for Period (interval). |
| test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/LocalDateTimeTranslationsTest.cs | Adds DateTrunc translation coverage for LocalDateTime. |
| test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/InstantTranslationsTest.cs | Adds DateTrunc translation coverage for Instant (with/without timezone). |
| test/EFCore.PG.FunctionalTests/Query/Translations/NodaTime/DurationTranslationsTest.cs | Adds DateTrunc translation coverage for Duration (interval). |
| src/EFCore.PG/Query/ExpressionTranslators/Internal/NpgsqlDateTimeMethodTranslator.cs | Adds core translation for the new DateTrunc<T> DbFunctions API. |
| src/EFCore.PG/Extensions/DbFunctionsExtensions/NpgsqlDbFunctionsExtensions.cs | Introduces the new public DateTrunc<T> DbFunctions extension method. |
| src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMethodCallTranslatorPlugin.cs | Adds NodaTime translation for the new DateTrunc<T> DbFunctions API. |
Copilot's findings
- Files reviewed: 9/9 changed files
- Comments generated: 4
6576dbd to
8e5b8dc
Compare
Expose EF.Functions.DateTrunc as a generic provider function and translate it for BCL and NodaTime timestamp/interval types. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Gate timezone date_trunc translation to timestamptz-backed mappings and add negative coverage for timestamp and interval inputs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Handle nullable generic DateTrunc return types in the BCL and NodaTime translators, and document the PostgreSQL version requirement for timezone truncation. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
89e423a to
567f73e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a generic
EF.Functions.DateTrunc<T>()API so LINQ queries can express PostgreSQLdate_truncdirectly for timestamp and interval values. This covers BCLDateTime,DateTimeOffset, andTimeSpan, and lets the NodaTime plugin translate the same generic API forLocalDateTime,Instant,ZonedDateTime,Period, andDuration.The translation emits the 2-argument PostgreSQL overload when
timeZoneis omitted, and the 3-argument overload when a timezone is supplied. This avoids accidentally translating omitted optional arguments asNULLtimezone arguments.Validation:
dotnet build test/EFCore.PG.FunctionalTests/EFCore.PG.FunctionalTests.csproj --no-restore --verbosity minimaldotnet test test/EFCore.PG.FunctionalTests/EFCore.PG.FunctionalTests.csproj --filter DateTrunc --no-build --verbosity minimalCloses #3831