Skip to content

feat: translate the DateRange calendar factories over a column (#191) - #447

Merged
CaffeinatedCoder merged 1 commit into
mainfrom
feat/191-translate-calendar-factories
Oct 25, 2026
Merged

CaffeinatedCoder merged 1 commit into
mainfrom
feat/191-translate-calendar-factories

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #191

The third addition of 10.3.0 (#408). main first, then cherry-picked into release/10.x.

Problem

DateRange.Month, Quarter, Year and IsoWeek failed translation in a Where when their argument was a column (DateRange.Month(b.Day)), and ran in memory in a final Select. The proposal on #191 (date_trunc, make_date, a [) constructor, to_date(…, 'IYYY-IW')) had three silent differences, measured on PostgreSQL 16.15 and 18.1 (measurements):

  • to_date('2025-53', 'IYYY-IW') is 2025-12-29: week 53 of a 52-week year rolls over to week 1 of the next year, where memory throws.
  • make_date(10000, 1, 1) and make_date(-1, 1, 1) succeed, where DateRange throws outside 1 to 9999.
  • date_trunc keeps infinity, so the [) range over a column holding DateOnly.MaxValue or MinValue is empty, where memory gives [9999-12-01,9999-12-31] and [0001-01-01,0001-01-31]. A date argument also resolves to date_trunc's timestamptz overload, which reads the session time zone.

Change

  • ValueRangesMethodCallTranslator translates the eight DateRange overloads (date form and integer form of each factory). Its dispatch is by whole signature, and any other overload of those names throws MissingArm:

    -- DateRange.Month(b."Day")
    daterange(CASE WHEN d <= DATE '0001-01-31' THEN DATE '-infinity'
                   WHEN d >= DATE '9999-12-01' THEN DATE '9999-12-01'
                   ELSE date_trunc('month', d::timestamp)::date END,
              CASE WHEN d >= DATE '9999-12-01' THEN DATE 'infinity'
                   WHEN d <= DATE '0001-01-31' THEN DATE '0001-01-31'
                   ELSE CAST(date_trunc('month', d::timestamp) + '1 month -1 day'::interval AS date) END,
              '[]')
    • First and last periods are constants taken from memory's own factory, written through the date mapping, so they are -infinity and infinity as Npgsql binds DateOnly.MinValue/MaxValue. With Npgsql.DisableDateTimeInfinityConversions set they are the days themselves. A column holding an infinity falls into one of these arms, so date_trunc never sees it.
    • [] over the first and last day, not [). PostgreSQL turns a finite ] into ), and an infinity] stays as Npgsql writes it, so the result equals a stored range of the same period.
    • The integer forms use make_date, which raises for a bad month and for year 0. A guard raises for any other year outside 1 to 9999. IsoWeek(weekYear, week) is the Monday on or before January 4 plus 7 * (week - 1), and it raises for a week outside 1 to date_part('week', make_date(y, 12, 28)). to_date is not used.
    • A raise is a cast of a message, so the message reaches the user: invalid input syntax for type integer: "DateRange.Month: the year 10000 is outside 1 to 9999". Every message holds an argument. A fully constant cast is folded while planning, even inside a CASE arm no row reaches. The first version of the week-past-9999 raise did exactly that and failed every query.
    • NULL argument: the result is NULL (rule (c)), through NullOverNullElement.
  • LocalDateRange and YearMonthRange stay in memory. A LocalDate column at -infinity stands for -9998-01-01, and neither its month nor any year before 4713 BC has a PostgreSQL date. So the database would fail where memory returns a range. That cell can't match, so per Epic: 10.3.0 #408 the member stays in memory on those types. The reason is in docs/efcore.md#what-runs-where, translation-support.json and NodaCalendarFactoryClientSideTests.

  • Docs: docs/efcore.md#what-runs-where moves DateRange's forms to "Runs as SQL" and adds two "Good to know" bullets (claim E76). The in-memory row now names the NodaTime forms and gives their reason. docs/ranges.md and the EF Core README follow.

  • Inventory and claims: translation-support.json makes *Range.Month/Quarter/Year/IsoWeek(* client with DateRange: server, in place of planned Translate the calendar factories when they read a column #191. Claim E46 is held on the NodaTime types now. Claim E76 is new.

  • Changelogs: ### Added under [11.0.0-preview.1], in the root and EF Core changelogs.

Tests

  • CalendarFactoryTranslationTests (no database): the SQL for the date form, the week-past-9999 raise, the integer form with its year guard and a NULL argument, and the week check (no to_date).
  • CalendarFactoryParityTests (integration; registered as an oracle with the seed calendar-last-period-ends-on-a-day-not-infinity.patch) covers Epic: 10.3.0 #408's parity list against memory over the loaded rows:
    • Shapes and extremes: 35 days around every month, quarter, year and ISO-week boundary, a leap day, and ISO weeks of the neighbouring year. DateOnly.MinValue and MaxValue are stored as -infinity/infinity. The integer forms cover years 1 and 9999 with their first and last month, quarter and week, plus 52- and 53-week years.
    • Nullable operands: a nullable date and year column read with .Value, a missing outer-join child, and a constant beside a column argument.
    • Composition: each range in Where(== null), Where(!= null) and a final Select. Each of 16 predicates in Where, Where(!) and Select, through CompositionRunner.Judge. The predicates are Contains of a column and of the extreme days, equality with a stored range, Overlaps, IsContainedBy, bounds, and compound &&/||.
    • Ordering: by the range and by its bounds, ascending and descending.
    • Arguments memory rejects: 13 cases (week 53 of 2025, week 0, week 52 of 9999, the week of 9999-12-27 and of MaxValue, month 13, years 0, -1 and 10000, quarters 0 and 5). Each throws ArgumentOutOfRangeException in memory and raises a PostgresException in a Select and a Where.
    • The composition sweeps discover RangeExtensions and RangeSet members, not a range type's static factories, so they don't reach these. That is why the parity test carries the list itself.
  • CalendarFactoryExtremesTests (the infinity-disabled process): with the switch set, the first and last periods equal the stored ranges memory writes, as days rather than infinities.
  • DeclaredNullabilityTests now includes the four factories over the nullable date column and their integer forms. date_trunc, make_date and date_part are checked against pg_proc.
  • Proofs:
    • With the DateRange arm removed, every parity call is "Untranslated", and the ordering and rejection tests fail too.
    • The seed (the last period's end written as the day 9999-12-31 rather than infinity) fails parity on every row in 9999-12 and 9999. Equality with the stored range and Contains(DateOnly.MaxValue) disagree, and the server's [9999-12-01,10000-01-01) doesn't load. dotnet run tools/oracles.cs seeds calendar: CAUGHT.
  • Inventory: TranslationSupportProbeTests passes with DateRange server and the NodaTime types client. TranslatorDispatchSweepTests passes.
  • Full local runs on the .NET 11 RC2 SDK: integration 317 and infinity-disabled 5, each on PostgreSQL 16 and 18; EF translation 324; NodaTime EF translation 94; conventions 417. All passed. The packages outside the EF Core one are unchanged.

🤖 Generated with Claude Code

DateRange.Month, Quarter, Year and IsoWeek failed translation in a Where
when their argument was a column. They now run as SQL with memory's
answers:

  daterange(<first day>, <last day>, '[]')

The first and last periods of DateOnly are constants taken from memory's
own factory and written through the date mapping, so they are -infinity
and infinity as Npgsql binds DateOnly.MinValue and MaxValue (the days
themselves with DisableDateTimeInfinityConversions). A date column at or
past them takes them too, so date_trunc never sees an infinity. Otherwise
date_trunc(unit, d::timestamp) and that plus '<n> <unit> -1 day'.

Measured on PostgreSQL 16 and 18 first (#191): to_date(..., 'IYYY-IW')
rolls week 53 of a 52-week year over, make_date accepts the years 10000
and -1, and the proposed [) form over date_trunc(infinity) is empty. So
the integer forms use make_date with a guard for the year, IsoWeek counts
from January 4 and checks the week against date_part('week', Dec 28),
and every argument memory rejects raises: a message cast to the type it
stands in for, which always holds an argument, since PostgreSQL folds a
constant cast while planning, even in a CASE arm no row reaches.

LocalDateRange and YearMonthRange stay in memory: a LocalDate at
-infinity (-9998-01-01) and the years before 4713 BC have no PostgreSQL
date, so the server would fail where memory answers.

CalendarFactoryParityTests holds every condition on #408's parity list;
CalendarFactoryExtremesTests the extremes with the infinity conversions
off. translation-support.json classifies DateRange server, and the docs
move the row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.3.0 milestone Oct 25, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request area/ranges Range unions, variants and the shape engines package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime labels Oct 25, 2026
@CaffeinatedCoder
CaffeinatedCoder merged commit 7503c18 into main Oct 25, 2026
14 of 16 checks passed
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/191-translate-calendar-factories branch October 25, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines enhancement New feature or request package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/efcore-nodatime Affects CodoMetis.ValueRanges.EFCore.PostgreSQL.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translate the calendar factories when they read a column

1 participant