Skip to content

Add generic date_trunc translation#3858

Merged
roji merged 3 commits into
npgsql:mainfrom
roji:roji/investigate-3831
Jun 12, 2026
Merged

Add generic date_trunc translation#3858
roji merged 3 commits into
npgsql:mainfrom
roji:roji/investigate-3831

Conversation

@roji

@roji roji commented Jun 12, 2026

Copy link
Copy Markdown
Member

Adds a generic EF.Functions.DateTrunc<T>() API so LINQ queries can express PostgreSQL date_trunc directly for timestamp and interval values. This covers BCL DateTime, DateTimeOffset, and TimeSpan, and lets the NodaTime plugin translate the same generic API for LocalDateTime, Instant, ZonedDateTime, Period, and Duration.

The translation emits the 2-argument PostgreSQL overload when timeZone is omitted, and the 3-argument overload when a timezone is supplied. This avoids accidentally translating omitted optional arguments as NULL timezone arguments.

Validation:

  • dotnet build test/EFCore.PG.FunctionalTests/EFCore.PG.FunctionalTests.csproj --no-restore --verbosity minimal
  • dotnet test test/EFCore.PG.FunctionalTests/EFCore.PG.FunctionalTests.csproj --filter DateTrunc --no-build --verbosity minimal

Closes #3831

Copilot AI review requested due to automatic review settings June 12, 2026 08:40
@roji roji force-pushed the roji/investigate-3831 branch from b73f4e4 to c88299f Compare June 12, 2026 08:40
@roji roji enabled auto-merge (squash) June 12, 2026 08:42

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_trunc translation 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

@roji roji disabled auto-merge June 12, 2026 08:46
Copilot AI review requested due to automatic review settings June 12, 2026 08:56
@roji roji force-pushed the roji/investigate-3831 branch from 6576dbd to 8e5b8dc Compare June 12, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 9/9 changed files
  • Comments generated: 3

Comment thread src/EFCore.PG.NodaTime/Query/Internal/NpgsqlNodaTimeMethodCallTranslatorPlugin.cs Outdated
roji and others added 3 commits June 12, 2026 11:17
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>
Copilot AI review requested due to automatic review settings June 12, 2026 09:17
@roji roji force-pushed the roji/investigate-3831 branch from 89e423a to 567f73e Compare June 12, 2026 09:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 9/9 changed files
  • Comments generated: 5

@roji roji merged commit 1d79a7f into npgsql:main Jun 12, 2026
10 checks passed
@roji roji deleted the roji/investigate-3831 branch June 12, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translation to date_trunc() for e.g. month-based grouping with timezone

2 participants