Skip to content

Correct interval extraction from FHIR Range and Period - #130

Open
bryantaustin13 wants to merge 2 commits into
mainfrom
interval-fhir-type-extraction
Open

bryantaustin13 wants to merge 2 commits into
mainfrom
interval-fhir-type-extraction

Conversation

@bryantaustin13

@bryantaustin13 bryantaustin13 commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

The extractors that map FHIR.Range and FHIR.Period back to CQL intervals — the representation the
"Using CQL with FHIR" IG specifies for Interval in Conformance Requirement 4.3 — had four defects:

  • DateTimeIntervalExtractor hardcoded highClosed: true, so a Period with no end extracted as
    {highClosed: true, high: null}. An absent boundary is unbounded and therefore open;
    NumericIntervalExtractor already treated it that way, so the two disagreed on the same shape.
  • A null valuePeriod, a null valueRange, or a null Range boundary each threw a TypeError out of
    the extractor, which turns a test into an error rather than a comparison.
  • A Range boundary carrying unit but no code lost its unit, silently reducing a dimensioned
    boundary to a unitless one.

Adds tests covering all seven interval point types the IG maps (Integer, Long, Decimal and Quantity
onto Range; Date, DateTime and Time onto Period) plus the boundary and null cases above.

One behaviour is pinned deliberately rather than changed: a Period carrying no cqf-cqlType is read
as Interval<DateTime>. The point type cannot be recovered in that case — a boundary of
2012-01-01T00:00:00-07:00 is indistinguishable from a DateTime interval starting at midnight — and
the IG states that a value without cqf-cqlType is the FHIR type. Inferring a Date would be a
guess, so the test asserts the mismatch instead, keeping the missing extension visible as a server
defect.

Rebased on main (#129 and #135)

This branch was opened before #129 and #135 merged, both of which rewrote
datetime-interval-extractor.ts. main has since been merged in and the resulting conflict
resolved so that all three changes survive:

From Kept
#135 applyDeclaredTimePrecision on the _start / _end companion elements
#129 List<...> unwrapping in declaredPointType
this PR early-return null guards and highClosed: high !== null

Taking either side of the conflict wholesale would have lost work — this branch's copy of the file
predated both merged PRs, so accepting it would have reverted time-precision support and lists of
temporal intervals. Each behaviour was re-checked individually after resolving:

The effective diff against main is now this PR's own work only — two extractors and one new test
file.

Verification

  • tsc --noEmit: no new errors. The two src/server/rest-routes.ts errors it reports are
    pre-existing on main (Express 5 types req.params.id as string | string[] under TypeScript
    7.0.2) and are untouched by this PR.
  • Unit suite: 247 passing (235 on main, plus this PR's 12).
  • Full cql-tests suite against the reference engine (HAPI FHIR 8.10.0 / CQF engine 4.1.0, $cql,
    CQL 1.5, Node 26): 1639 pass / 170 fail / 14 skip / 0 error — identical to main, with zero
    status changes
    across all 1823 records.

That last point is the important one for review: no test changes status on this engine. The four
defects fixed here need inputs this engine does not produce — it never returns a Period without an
end, never a null boundary, and always includes code on Range quantities. The fixes are
demonstrated by the unit tests rather than by the integration run, and the integration run's role is
to confirm the merge resolution introduced no regression.

bryantaustin13 and others added 2 commits August 28, 2026 12:29
The extractors that map FHIR.Range and FHIR.Period back to CQL intervals — the
representation the "Using CQL with FHIR" IG specifies for Interval in
Conformance Requirement 4.3 — had four defects:

- DateTimeIntervalExtractor hardcoded `highClosed: true`, so a Period with no
  `end` extracted as `{highClosed: true, high: null}`. An absent boundary is
  unbounded and therefore open; NumericIntervalExtractor already treated it that
  way, so the two disagreed on the same shape.
- A null `valuePeriod`, a null `valueRange`, or a null Range boundary each threw
  a TypeError out of the extractor, which turns a test into an error rather than
  a comparison.
- A Range boundary carrying `unit` but no `code` lost its unit, silently
  reducing a dimensioned boundary to a unitless one.

Adds tests covering all seven interval point types the IG maps (Integer, Long,
Decimal and Quantity onto Range; Date, DateTime and Time onto Period) plus the
boundary and null cases above.

One behaviour is pinned deliberately rather than changed: a Period carrying no
cqf-cqlType is read as Interval<DateTime>. The point type cannot be recovered in
that case — a boundary of 2012-01-01T00:00:00-07:00 is indistinguishable from a
DateTime interval starting at midnight — and the IG states that a value without
cqf-cqlType is the FHIR type. Inferring a Date would be a guess, so the test
asserts the mismatch instead, keeping the missing extension visible as a server
defect.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Resolves the conflict in datetime-interval-extractor.ts, which arose because this
branch predates #129 and #135 and carried an older copy of the whole file.

Taking either side alone would have lost work:

- main's side drops this branch's null guards and its `highClosed: high !== null`,
  leaving an absent Period boundary reported as closed.
- this branch's side drops #135's applyDeclaredTimePrecision on the `_start`/`_end`
  companion elements and #129's List<...> unwrapping in declaredPointType, which
  would have reverted time-precision support and lists of temporal intervals.

The resolution keeps all of it: this branch's early-return guards and derived
highClosed, wrapping main's precision-aware boundary extraction, with #129's
List unwrapping untouched above.

Verified after resolution: 247 tests pass, and each behaviour checked directly —
a Period with hour time-precision extracts as @T10/@t12, List<Interval<System.Date>>
still resolves to Date literals, an absent end yields highClosed:false, and null
valuePeriod / valueRange / boundary no longer throw. tsc reports only the two
pre-existing rest-routes.ts errors present on main.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

1 participant