Query sort compares DateTime keys with "<" (as text), so values in different offsets sort out of instant order
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
- Domain
- backend-api-design
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cqframework/cql-execution
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cqframework/cql-execution#410 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
cqframework/cql-execution#407 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
cqframework/cql-execution#406 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
cqframework/cql-execution#414 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
cqframework/cql-execution#413 ·
All issues in cqframework/cql-execution
Similar issues
-
Mend: dependency security vulnerability
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
ManageIQ/manageiq-ui-service#2259 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
sagemathinc/sagebrush-agent#230 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
electerious/Ackee#411 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
babalae/bettergi-scripts-list#3905 · 1 comment ·
Maintainers usually reply within 1 day