Using CQL With FHIR
2.0.0 - STU 2 International flag

This page is part of the Using CQL with FHIR Implementation Guide (v2.0.0: STU2) based on FHIR (HL7® FHIR® Standard) R4. This is the current published version. For a full list of available versions, see the Directory of published versions

Library: Type Mapping Example (Experimental)

Official URL: http://hl7.org/fhir/uv/cql/Library/TypeMappingExample Version: 2.0.0
Standards status: Informative Computable Name: TypeMappingExample
Other Identifiers: OID:2.16.840.1.113883.4.642.40.37.28.6

Example CQL library providing a complete set of expressions that cover all possible CQL and FHIR Type Mapping, for both input and output. See the Type Mapping Evaluation Result example for an illustration of the type mapping in a Parameters resource.

Title: Type Mapping Example
Id: TypeMappingExample
Version: 2.0.0
Url: Type Mapping Example

urn:oid:2.16.840.1.113883.4.642.40.37.28.6

Experimental: true
Type:

system: http://terminology.hl7.org/CodeSystem/library-type

code: logic-library

Date: 2025-07-08 14:56:17+0000
Publisher: HL7 International / Clinical Decision Support
Description:

Example CQL library providing a complete set of expressions that cover all possible CQL and FHIR Type Mapping, for both input and output. See the Type Mapping Evaluation Result example for an illustration of the type mapping in a Parameters resource.

Jurisdiction: 001
Related Artifacts:

Dependencies

Content: text/cql
/*
@description: An example library illustrating the FHIR Type mapping for CQL
*/
library TypeMappingExample

using FHIR version '4.0.1'

include FHIRHelpers version '4.0.1'

codesystem ExampleCodeSystem: 'http://hl7.org/fhir/uv/cql/CodeSystem/example'
codesystem LOINC: 'http://loinc.org'
valueset ExampleValueSet: 'http://hl7.org/fhir/uv/cql/ValueSet/shareable-example'
code ExampleCode: 'example-code' from ExampleCodeSystem
code BloodGlucose: '2339-0' from LOINC display 'Glucose Bld-mCnc'
concept ExampleConcept: { ExampleCode }

parameter ExampleVocabulary Vocabulary

context Patient

// Example values as output

// CQL Values
// Boolean
define CQLBooleanExample: true
// Code
define CQLCodeExample: ExampleCode
// CodeSystem
define CQLCodeSystemExample: ExampleCodeSystem
// Concept
define CQLConceptExample: ExampleConcept
// Date
define CQLDateExample: @2024-01-01
// DateTime
define CQLDateTimeExample: @2024-01-01T10:30:00Z
define CQLPartialDateTimeExample: @2024-01-01TZ
define CQLPartialDateTimeMinutesExample: @2024-01-01T10:30Z
// Decimal
define CQLDecimalExample: 10.0
define CQLDecimalPrecisionExample: 10.0000
// Long
define CQLLongExample: 10L
// Integer
define CQLIntegerExample: 10
// Quantity
define CQLQuantityExample: 10 'mg'
// Ratio
define CQLRatioExample: 5 'mg' : 10 'mg'
// String
define CQLStringExample: 'John'
// Time
define CQLTimeExample: @T10:30:00
define CQLPartialTimeExample: @T10:30
// ValueSet
define CQLValueSetExample: ExampleValueSet
// Vocabulary
define CQLVocabularyExample: ExampleVocabulary
// Interval<Date>
define CQLDateIntervalExample: Interval[@2024-01-01, @2024-01-31]
// Interval<DateTime>
define CQLDateTimeIntervalExample: Interval[@2024-01-01T10:30:00Z, @2024-01-31T10:30:00Z]
// Interval<Time>
define CQLTimeIntervalExample: Interval[@T10:30:00, @T11:30:00]
// Interval<Integer>
define CQLIntegerIntervalExample: Interval[5, 10]
// Interval<Quantity>
define CQLQuantityIntervalExample: Interval[5 'mg', 10 'mg']
// List
define CQLListExample: { 1, 2, 3, 4, 5 }
// Tuple
define CQLTupleExample: { X: 1, Y: 1 }
// ChoiceList
define CQLChoiceListExample: List<Choice<Integer, Decimal>> { 1, 1.0 }
// TupleList
define CQLTupleListExample: { { X: 1, Y: 1 }, { X: 1, Y: 2 }, { X: 1, Y: 3 } }
// ComplexTuple
define CQLComplexTupleExample: { id: 1, name: 'Patrick', address: { { street: '123 Spinning Ave', city: 'Dayton', state: 'OH' } } }
// ComplexTupleList
define CQLComplexTupleListExample: { CQLComplexTupleExample }
// EmptyList
define CQLEmptyListExample: CQLComplexTupleListExample E where false
// ListList
define CQLListListExample: { CQLListExample, CQLListExample }
// EmptyTuple
define CQLEmptyTupleExample: { : }

// FHIR Values
define private FHIRObservations: [Observation]
define private FHIRBloodGlucoseObservation: (FHIRObservations O where O.code ~ BloodGlucose).single()

// boolean
define FHIRBooleanExample: Patient.active
// string
define FHIRStringExample: Patient.name.first().family
// Quantity
define FHIRQuantityExample: FHIRBloodGlucoseObservation.value as FHIR.Quantity
// Period
define FHIRPeriodExample: Patient.address.first().period
// Range
define FHIRRangeExample: FHIRBloodGlucoseObservation O return FHIR.Range { low: O.referenceRange.first().low, high: O.referenceRange.first().high }
// Observation
define FHIRObservationExample: FHIRBloodGlucoseObservation
// List<Observation>
define FHIRObservationListExample: FHIRObservations
// EmptyList<Observation>
define FHIREmptyObservationListExample: FHIRObservations O where false
// BackboneElement
define FHIRBackboneElementExample: Patient.contact.first()
// Extension (simple)
define FHIRSimpleExtensionExample: Patient.birthDate.extension.where(url = 'http://hl7.org/fhir/StructureDefinition/patient-birthTime').first()
// Extension (complex)
define FHIRComplexExtensionExample: Patient.extension.where(url = 'http://hl7.org/fhir/us/core/StructureDefinition/us-core-ethnicity').first()
Content: application/elm+xml
Encoded data (32944 characters)
Content: application/elm+json
Encoded data (76932 characters)