Date/DateTime arithmetic outside 0001-9999 returns null instead of throwing

Open
#413 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript
Domain
backend

Research direction

Read src/elm/arithmetic.ts, especially Add, Subtract, and finalizeArithmeticResult, then inspect the Date and DateTime range checks in the datatypes. Reproduce the listed date arithmetic cases and compare them with the numeric overflow behavior. Done means out-of-range Date and DateTime results throw while numeric overflow still returns null, with the existing tests updated or added to cover both.

Written by the indexing model from the issue text.

Description

Adding a quantity to (or subtracting it from) a Date or DateTime returns null when the result falls outside the supported range (0001-01-01 to 9999-12-31). The specification and the official tests expect a run-time error, at least for years.

library DateArithmeticOutOfRange version '1.0.0'

define AddYearsDateTime: DateTime(2005, 10, 10) + 8000 years       // null  (cql-tests DateTimeAddInvalidYears)
define SubtractYearsDateTime: DateTime(2005, 10, 10) - 2005 years  // null  (cql-tests DateTimeSubtractInvalidYears)
define AddYearsDate: @2005-10-10 + 8000 years                      // null
define AddMonthDateTime: @9999-12-15T00:00:00.000Z + 1 month       // null
define AddDayDate: @9999-12-31 + 1 day                             // null
define SubtractDayDate: @0001-01-01 - 1 day                        // null
define AddDayDateIsNull: (@9999-12-31 + 1 day) is null             // true

Expected: a run-time error.

  • Author's Guide, Date/Time arithmetic, calendar semantics for year: "If the resulting year is out of range, an error is thrown."
  • cql-tests marks DateTimeAddInvalidYears and DateTimeSubtractInvalidYears as invalid="true" ("The date time addition results in a year greater than the accepted range."). The spec-test runner currently treats such tests as no-ops (#351), so the difference does not show up there.
  • The reference Java/Kotlin engine throws for all six expressions ("The year: 10005 falls above the accepted bounds of 0001-9999.").

For months and days the text does not repeat the rule. We assume it applies too, since the result is not a representable Date or DateTime. The specification returns null only for numeric overflow and for successor/predecessor at the bounds of the type.

A null here changes the logic silently: the last line shows that the overflowed value is indistinguishable from a missing one.

Cause: Add and Subtract pass every result through finalizeArithmeticResult, which returns null when overflowsOrUnderflows is true:

https://github.com/cqframework/cql-execution/blob/e87e3fa8a3802c69a760b2583d2306d7c66d9899/src/elm/arithmetic.ts#L28-L34

This is correct for Integer, Long, Decimal and Quantity (Appendix B, Add: "If the result of the addition cannot be represented (i.e. arithmetic overflow), the result is null."), but it also applies to Date and DateTime operands. A possible fix is to throw for Date/DateTime results outside the range and keep null for the numeric types.

Side note: DateTime.add has a misplaced parenthesis, result.after(MAX_DATETIME_VALUE || result.before(MIN_DATETIME_VALUE)), so its own underflow check never runs:

https://github.com/cqframework/cql-execution/blob/e87e3fa8a3802c69a760b2583d2306d7c66d9899/src/datatypes/datetime.ts#L250

It has no visible effect today, because Add/Subtract check the result again.

Environment:

  • cql-execution 3.3.2 and 4.0.0-beta.1 (same results);
  • ELM from @cqframework/cql 5.8.0 (npm);
  • Node 24, Windows 11, TZ=UTC.

Found while differential testing date operators across engines.

Dominant language
JavaScript
Stars
86
Forks
40
PR merge metrics
No merged PRs in 30d

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from cqframework/cql-execution

All issues in cqframework/cql-execution

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.