CQL
The $cql operation evaluates CQL against the server’s data, as an expression or
as a whole library. It is registered when cql.enabled is on.
[cql]enabled = truemax_retrieve_size = 10000 # resources per retrievecache_capacity = 1000 # compiled libraries kept in memorycompile_timeout_ms = 5000evaluation_timeout_ms = 30000Retrieves run as searches of the caller, under the caller’s grants.
POST /fhir/$cqlPOST /fhir/{type}/$cqlPOST /fhir/{type}/{id}/$cql| Parameter | Meaning |
|---|---|
expression |
One CQL expression |
library |
Full CQL source. Every define is evaluated, and library wins over expression. |
context |
The context type, such as Patient |
contextValue |
The context resource, inline. Without context, its resourceType gives the type. |
validate |
true parses the source and reports problems, without touching data |
| any other name | A CQL parameter, as valueString, valueInteger, valueBoolean, valueDecimal, valueDate, valueDateTime or resource |
At instance level the resource is the context. At type level the type is the default context type.
An expression
Section titled “An expression”POST /fhir/$cqlContent-Type: application/fhir+json
{ "resourceType": "Parameters", "parameter": [{"name": "expression", "valueString": "1 + 1"}]}{ "resourceType": "Parameters", "id": "cql", "parameter": [ {"name": "expression", "valueString": "1 + 1"}, {"name": "return", "valueString": "2"} ]}return holds the result serialized as JSON.
A library
Section titled “A library”POST /fhir/Patient/123/$cqlContent-Type: application/fhir+json
{ "resourceType": "Parameters", "parameter": [{ "name": "library", "valueString": "library Example version '1.0.0'\ndefine Sum: 1 + 1\ndefine Greeting: 'hello'" }]}The response has one part per define, in source order, each with the value serialized as JSON:
{ "resourceType": "Parameters", "id": "cql", "parameter": [{ "name": "result", "part": [ {"name": "Sum", "valueString": "2"}, {"name": "Greeting", "valueString": "\"hello\""} ] }]}Validation
Section titled “Validation”With validate set to true the response has one issue per problem, with the parts severity,
message, and line and column when known. An empty parameter list means the source is valid.
Errors in the source or in evaluation are 400, with the diagnostic in the OperationOutcome.
CQL console
Section titled “CQL console”The CQL page (g q) of the web console is built on $cql.
- It treats a source with
libraryordefineas a library and evaluates every define. Anything else is a single expression. - It validates as you type, so errors show before you run.
- The context tab takes a resource as JSON, with samples to start from, and the page shows the context type it found. The params tab takes CQL parameters.
- A palette lists functions to insert.
Measures
Section titled “Measures”$evaluate-measure scores a Measure whose logic is a CQL Library. On R6 there is also
$evaluate for several measures at once:
GET /fhir/Measure/screening/$evaluate-measure?periodStart=2024-01-01&periodEnd=2024-12-31&subject=Patient/123| Parameter | Meaning |
|---|---|
periodStart, periodEnd |
Required. Passed to the library as the Measurement Period parameter. A date covers the whole day |
subject |
Patient/id for an individual report, Group/id for its members. Without it, every Patient the token can read |
reportType |
subject-list adds a contained List of subjects per population. Otherwise the report is individual or a summary |
measure |
The Measure’s canonical URL, at Measure/$evaluate-measure |
A population criterion names a define of the library: true, or a list with something in it,
puts the subject in that population. With a populationBasis extension naming a resource type, such
as Encounter, the defines return resources and each one is counted.
Proportion, ratio, cohort and continuous variable scoring are supported, with denominator-exclusion,
denominator-exception, numerator-exclusion and measure-population-exclusion. group.stratifier
produces one stratum per value of its define.
For continuous variable, the measure-observation criterion names a CQL function. It takes one member of
the measure-population, or no argument in a subject-based measure, and returns a number or a Quantity.
The cqfm-aggregateMethod extension on that population (sum, average, median, minimum, maximum,
count) turns the values into measureScore.
A ratio can observe its numerator and denominator: two measure-observation populations, each pointing
at its population with the cqfm-criteriaReference extension and carrying an aggregate method. Excluded
members are not observed, and the score is the numerator aggregate over the denominator aggregate.
Measure.supplementalData names defines of the library. In an individual report a resource is
referenced and any other value goes into a contained Observation. In a summary each element is one
contained Observation with a component per value, counting the members of the initial population
that had it. The references are in MeasureReport.supplementalData; R4 has no such element and uses
the extension http://hl7.org/fhir/5.0/StructureDefinition/extension-MeasureReport.supplementalData.
A composite Measure has scoring composite, a compositeScoring, and its components as composed-of
entries of relatedArtifact. A component is a subject-based proportion measure; cqfm-groupId picks one
of its groups and cqfm-weight gives its weight.
compositeScoring |
Score |
|---|---|
all-or-nothing |
A proportion: a subject is in the numerator when in the numerator of every component whose denominator holds them |
opportunity |
A proportion over cases: each component a subject is in counts as one |
linear |
The average, over subjects, of the numerators reached out of the denominators entered |
weighted |
The weighted average of the component scores; a component with no score is left out |
A component whose improvementNotation differs from the composite’s counts the other way: the absence
from its numerator in the first three, one minus its score in weighted.
The library is compiled once for the run and all data is read under the caller’s grants. The result
is a MeasureReport, which is not stored.
Debug trace
Section titled “Debug trace”$cql with a debug parameter set to true adds a debug trace to the response, in the format the
FHIR Mapping Language IG proposes for all evaluation languages: traceStatus, and a trace per event.
The units of CQL are its definitions and function calls: each has an enter, with the operands of a
function, and an exit with the value, and Message() adds a step. A definition already evaluated is
not entered again. expressionPosition holds the name of the unit and source its library. A library
that fails while evaluating answers with its trace, status stopped, and an outcome. The console has
a Trace switch that replays it.
Data requirements
Section titled “Data requirements”$data-requirements says what a Measure or Library reads before anything runs:
GET /fhir/Measure/screening/$data-requirements?periodStart=2024-01-01&periodEnd=2024-12-31GET /fhir/Library/screening/$data-requirementsperiodStart and periodEnd are required on a Measure, as the operation defines them. The result is a
module-definition Library:
| Element | Holds |
|---|---|
dataRequirement |
One entry per distinct retrieve in the library and everything it includes, with codeFilter for the value set or codes of the retrieve |
relatedArtifact |
depends-on entries for the Measure’s library, included libraries, value sets and code systems |
parameter |
The library’s parameters, such as Measurement Period as Period |
Filters come from the retrieve and from the where clause of a query over that one retrieve. There a
conjunction of =, ~ or in against a string, code, list of strings or value set adds a codeFilter,
and a comparison with an interval parameter, such as E.period.start during "Measurement Period", a
dateFilter naming the element. Anything else, such as or or not, leaves the retrieve as it is. Both
operations work on an instance; the system-level Library form with target is
not implemented.