Stream: fhirpath
Topic: should all functions work on repeating fields?
Bryn Rhodes (Mar 16 2022 at 23:52):
Yes, open JIRAs please, that clarification is supposed to be on each function, but apparently we missed some.
Bryn Rhodes (Mar 16 2022 at 23:53):
Actually one JIRA would be fine with all of them listed
Bryn Rhodes (Mar 16 2022 at 23:54):
But to answer the question, length() expects a singleton. Basically, unless the function is something that will by it's nature iterate over the items in the collection (like .where, .select, etc), then it's a singleton. Another way to think about it is if you would have to supply collection semantics, then it's a singleton. For example, in .length(), what should it do if it gets multiple items? Compute the total length or return the length of each?
Lee Surprenant (Mar 17 2022 at 14:24):
I think i'll do 2 issues: one for fhirpath and one for fhir. its actually the latter one that defines memberOf
which was the original function that raised this question
Lee Surprenant (Mar 17 2022 at 14:25):
if you would have to supply collection semantics, then it's a singleton...
For example, in .length(), what should it do if it gets multiple items?
That matches our implementation, but the verbiage in the spec about all functions operating over collections got me worried that maybe we had it wrong.
Lee Surprenant (Mar 17 2022 at 14:30):
I think the alternative would be for any function that operates over singletons to instead apply the function for each input element and produce a 1-to-1 mapping to the output collection.
For example, for length()
it could execute the function against each string in the input collection and return a collection of integers.
Personally, I think that interpretation would have some nice properties.
Because the current FHIRPath specification requires FHIRPath authors to be super-aware of any repeating elements in the model.
This is probably the number 1 mistake I've seen with FHIRPath expressions "in the wild".
Lee Surprenant (Mar 17 2022 at 14:30):
Has anything like that ever been considered?
Lee Surprenant (Mar 17 2022 at 14:37):
This is probably the number 1 mistake I've seen with FHIRPath expressions "in the wild".
One thing that contributes to that issue is that I think most engines will happily execute the function if you apply it to a resource such that only a single element is present.
That leads to issues like https://jira.hl7.org/browse/FHIR-36328 where the expression seems to work for some input (e.g. when the repeating element is only there once) but then fails when you try applying it to a resource that has more than a single instance of the repeating element.
Lee Surprenant (Mar 17 2022 at 14:38):
Do any of the engines / other tools warn users when they use singleton functions against a model that could lead to multiple inputs?
Paul Lynch (Mar 17 2022 at 14:40):
Yes. https://hl7.github.io/fhirpath.js/. ('aaa'| 'zzz').length() => Error: Unexpected collection["aaa","zzz"]; expected singleton of type String
Lee Surprenant (Mar 17 2022 at 14:44):
I mean if you have an expression like Observation.component.value is string
(just an example off the top of my head)
Lee Surprenant (Mar 17 2022 at 14:44):
that will execute on any Observations with 0 or 1 components
Lee Surprenant (Mar 17 2022 at 14:44):
but if you run that against an Observation with 2 components... kablooey
Lee Surprenant (Mar 17 2022 at 14:45):
it would be interesting to use model-awareness to warn FHIRPath authors about that
Paul Lynch (Mar 17 2022 at 14:50):
Yes, a warning in such cases could be helpful.
Bryn Rhodes (Mar 17 2022 at 15:09):
When you evaluate FHIRPath expressions in CQL, the translator does this with a "list demotion" and will issue a warning whenever it happens, yes.
Lee Surprenant (Mar 17 2022 at 18:32):
Lee Surprenant said:
I think i'll do 2 issues: one for fhirpath and one for fhir. its actually the latter one that defines
memberOf
which was the original function that raised this question
FHIR-36335 (fhirpath) and FHIR-36334 (fhir)
Last updated: Apr 12 2022 at 19:14 UTC