FHIR Chat · Tuple Creation Map. Missing Tuples in return? · cql

Stream: cql

Topic: Tuple Creation Map. Missing Tuples in return?


view this post on Zulip Michael Riley (Aug 27 2021 at 18:42):

I am crafting a FHIR-CQL script to not only capture source FHIR resources, but also specifically derived elements as tuples with extra metadata within the tuple to help with downstream processing. The script is a little big so rather than show the entire script, I'm going to describe the workflow and provide a few snippets here. The basic flow of my script is:

  • Capture a set of conditions
  • Capture a set medicationstatements occurring 2-4 weeks after the conditions
  • For each medication, create 3 tuples
  • A 'Code' Tuple which captures the actual code captured from the medicationstatement
  • A 'Dosage' Tuple which captures the dosage component from the medicationstatement
  • A 'Onset' Tuple which captures the starting datetime of the medicationstatement

In my testing FHIR server I loaded a Patient with 2 matching conditions, and then assigned a medicationstatement within the timeframe for each condition.

From my script output I was able to see

  • 2 source conditions (good)
  • 2 source medicationstatements (good)
  • 1 'Code' Tuple (bad)
  • 2 'Dosage' Tuple (good)
  • 2 'Onset' Tuple (good)

Which surprised me, as 2 of the tuple sets we're 1-to-1 on the Dosage and Onset components, but the 'Code' Tuple was missing a tuple. I'm trying to figure out why that might be.

I wrote my code tuple definition as:

define "Penicillin_MSTuple": from Penicillin_MS target //FhirBundleCursor
    return Tuple {
        questionConcept: '20000005',
        sourceValue: target.medication.coding[0].code+'^'+target.medication.coding[0].system,
        answerValue: 'http://www.nlm.nih.gov/research/umls/rxnorm^7980^penicillin G',
        resultType: 'Drug'
    }

and my dosage tuple as:

define "Penicillin_MS_DosageTuple": from Penicillin_MS target //FhirBundleCursor
    return Tuple {
        fhirResourceId: target.id,
        questionConcept: '20000005',
        sourceValue: ToString(FHIRHelpers.ToQuantity(("target".dosage[0].dose as FHIR.Quantity))),
        answerValue: 'target.dosage[0].dose',
        resultType: 'Drug',
        field: 'target.dosage[0].dose'
    }

and my onset/effective tuple as

define "Penicillin_MS_Date_Medication_StartedTuple": from Penicillin_MS target //FhirBundleCursor
    return Tuple {
        fhirResourceId: target.id,
        questionConcept: '20000005',
        sourceValue: target.effective as FHIR.dateTime,
        answerValue: 'target.effective as FHIR.dateTime',
        resultType: 'Drug',
        field: 'target.effective'
    }

All defines returned as the "List" type, with the list of Tuples, but the Code one only contained one. I replicated the issue with another set of medicationstatements too, so I know it's not the source data itself, it's the cql definition causing an issue here. Is there something about tuple rendering that could cause issues?
I can share test data too if that would be helpful

view this post on Zulip Bryn Rhodes (Aug 27 2021 at 19:04):

I'm just guessing here, but the default for _projection_ in a CQL query is distinct, so if all the tuples in the code query are the same, only one will be returned. You can use the all keyword (return all) to preserve duplicates in the result.

view this post on Zulip Michael Riley (Aug 30 2021 at 14:48):

That is what's happening Byrn, thank you! The interesting thing is that we have the same dosage information for each MedicationStatement, but in the backend the dosage component is a different pointer in memory, so they appear distinct. If keeping distinctness in tuples is important to the language, it might be good if the fhir data provider could provide some hashcode or id based on the fields in the component itself.


Last updated: Apr 12 2022 at 19:14 UTC