Query sort compares DateTime keys with "<" (as text), so values in different offsets sort out of instant order

Open
#390 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

The issue points to ByDirection.exec and ByExpression.exec in src/elm/query.ts; start by reading their sort comparators and how DateTime comparison methods handle offsets. Use the supplied CQL repro to check both input orders. Done means values sort by instant across offsets and the repro returns the newer value in both cases.

Written by the indexing model from the issue text.

Description

ByDirection.exec and ByExpression.exec (src/elm/query.ts, on master and in 4.0.0-beta.1) compare sort keys with a < b. DateTime has no valueOf, so < compares the toString() output, which ends in the timezone offset. Two DateTimes in different offsets therefore sort by text, not by instant.

Repro, evaluated at offset 0:

library SortCheck version '1.0.0'
using FHIR version '4.0.1'
context Unfiltered

define "Newest":
  Last(({ Tuple { id: 'older 05:00Z', d: @2025-01-02T05:00:00.000Z },
          Tuple { id: 'newer 06:00Z', d: @2025-01-01T20:00:00.000-10:00 } }) X sort by d).id

define "Newest, reversed input":
  Last(({ Tuple { id: 'newer 06:00Z', d: @2025-01-01T20:00:00.000-10:00 },
          Tuple { id: 'older 05:00Z', d: @2025-01-02T05:00:00.000Z } }) X sort by d).id

@2025-01-01T20:00:00.000-10:00 is 06:00Z on 2 January, an hour after the other value.

Engine Newest Newest, reversed input
cql-execution 4.0.0-beta.1 older 05:00Z older 05:00Z
cqf-fhir-cr ($cql, HAPI FHIR 8.12.0 / clinical-reasoning 4.13.0) newer 06:00Z newer 06:00Z

Where it matters. CMS's FHIR measures take:

  • Last(... sort by start of effective.toInterval()) for CMS2's depression screening;
  • Last(... sort by end of effective.toInterval() asc) for AdvancedIllnessandFrailty's housing status, used by CMS122, CMS125, CMS130 and CMS165.

Patient data from different sources, or from either side of a daylight-saving change, can mix offsets.

A possible fix: compare DateTime keys as instants, converting one into the other's offset as sameAs/before/after already do.

A note only, not a request: two keys at the same instant never compare as 0 (the comparator returns high_order both ways), so asyncMergeSort reverses tied elements, while cqf-fhir-cr keeps them in input order. The spec leaves the order of duplicates unspecified.

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.