Skip to content

Implement full UCUM unit conversion for toQuantity(unit) and convertsToQuantity(unit) #2504

@piotrszul

Description

@piotrszul

Overview

The toQuantity(unit) and convertsToQuantity(unit) functions currently support the optional unit parameter using exact string matching only. This issue tracks the implementation of full UCUM unit conversion capabilities.

Current Implementation

The current implementation:

  • Accepts an optional unit parameter for both functions
  • Performs exact string matching on unit codes (e.g., '10 'mg''.toQuantity('mg') works)
  • Returns empty when units don't match (e.g., '10 'mg''.toQuantity('g') returns empty)
  • This is compliant with FHIRPath spec which states: "Implementations are not required to support a complete UCUM implementation, and may return empty when the unit argument is used and it is different than the input quantity unit."

Requested Enhancement

Implement full UCUM unit conversion so that:

  • toQuantity(unit) converts the quantity value to the target unit (e.g., '1 'wk''.toQuantity('d') returns 7 'd')
  • convertsToQuantity(unit) returns true if conversion is possible between compatible units
  • Support common UCUM conversions (length, mass, volume, time, etc.)

Examples

These test cases are currently excluded in config.yaml:

'1 'wk''.toQuantity('d').toString() = '7 'd''  // Should convert weeks to days
'1 'cm''.toQuantity('mm').value = 10           // Should convert cm to mm

Implementation Notes

  • Implement test cases in au.csiro.pathling.fhirpath.dsl.ConversionFunctionsDslTest - extract toQuantity(unit) tests for UCUM units to a separate method.
  • Implement in au.csiro.pathling.fhirpath.column.QuantityValue
  • Leveraging existing UCUM integration (au.csiro.pathling.encoders.terminology.ucum.Ucum)
  • Ensure conversion accuracy and precision handling
  • Document which UCUM conversions are supported
  • Update test exclusions in config.yaml once implemented

Related

Metadata

Metadata

Assignees

Labels

fhirpathRelated to fhirpath reference implementationnew featureNew feature or request

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions