Skip to content

Support quantity search against Range values (context-quantity) #2971

Backlog

Description

@piotrszul

Summary

Quantity search parameters match Quantity values but not Range values. In the bundled R4 registry this affects context-quantity on 29 resource types, whose expression is (X.useContext.value as Quantity) | (X.useContext.value as Range). R4 defines how a quantity search matches a Range, so a resource whose context is a Range should be found. Today it is silently left out.

This is a documented limitation, not a defect: #2953 (Skip search expressions of a type the parameter does not allow) made these parameters usable by skipping the Range expression, and the library and server search docs say that context-quantity "matches Quantity values but not Range values". Before #2953, a search on these parameters failed with an error (#2913, search_to_column rejects Observation?value-quantity with InvalidSearchParameterException (SampledData)).

Current behaviour (observed)

On 11.0.0-SNAPSHOT, built from release/11.0.0 at 4de8ad6 (Python 3.11.13, Java 21.0.3, Spark 4.0.5, FHIR R4):

import json

from pathling import PathlingContext

pc = PathlingContext.create(enable_terminology=False)

focus = {"system": "http://terminology.hl7.org/CodeSystem/usage-context-type", "code": "focus"}
mg = {"system": "http://unitsofmeasure.org", "code": "mg"}


def value_set(id, value):
    return {"resourceType": "ValueSet", "id": id, "status": "active",
            "useContext": [dict({"code": focus}, **value)]}


resources = [
    value_set("quantity-5.4", {"valueQuantity": dict({"value": 5.4}, **mg)}),
    value_set("range-5-6", {"valueRange": {"low": dict({"value": 5}, **mg), "high": dict({"value": 6}, **mg)}}),
    value_set("range-5.0-5.2", {"valueRange": {"low": dict({"value": 5.0}, **mg), "high": dict({"value": 5.2}, **mg)}}),
]
value_sets = pc.encode(pc.spark.createDataFrame([(json.dumps(r),) for r in resources], ["value"]), "ValueSet")
for query in ["context-quantity=5.4", "context-quantity=5", "context-quantity=gt4", "context-quantity=lt7"]:
    ids = sorted(r.id for r in value_sets.filter(pc.search_to_column("ValueSet", query)).select("id").collect())
    print(f"{query} -> {ids}")

Output:

context-quantity=5.4 -> ['quantity-5.4']
context-quantity=5 -> ['quantity-5.4']
context-quantity=gt4 -> ['quantity-5.4']
context-quantity=lt7 -> ['quantity-5.4']

Neither Range ValueSet is returned by any query.

Requested behaviour (from the specification, not verified end to end)

R4 lists Range as a type that quantity parameters search (the cross-map of search parameter types and data types on the R4 search page), and treats a Range target as an explicit range when applying a prefix (prefixes): "If the target value is a Range, a Period, or a Timing, then the target is explicitly a range." A search value is itself the implicit range set by its significant figures, so 5 is [4.5, 5.5) and 5.4 is [5.35, 5.45). Applying the R4 prefix rules to the snippet above:

Query R4 rule for a range target Expected
context-quantity=5.4 eq: the search range fully contains the target range quantity-5.4
context-quantity=5 as above; [4.5, 5.5) contains 5.0–5.2 but not 5–6 quantity-5.4, range-5.0-5.2
context-quantity=gt4 gt: the range above the search value intersects the target range all three
context-quantity=lt7 lt: the range below the search value intersects the target range all three

So the change is visible for =5, gt4 and lt7; =5.4 returns the same result as today.

Notes

  • Open questions for the design: a Range with only low or only high (an unbounded range), Ranges whose low and high units differ or need UCUM conversion, and which other prefixes (ne, ge, le, sa, eb, ap) Pathling should support for Range targets.
  • Scope: in the bundled R4 registry, the parameters whose expression includes a Range are context-quantity (29 resource types, all with the same expression shape; only ValueSet was run) and, on Condition, onset-age and abatement-age (Condition.onset.as(Age) | Condition.onset.as(Range), and the same for abatement). The Condition parameters were not run here. Custom search parameters over a Range would need the same support.
  • When this is implemented, the sentence about Range in the library and server search docs needs to be removed.

Activity

  1. added
    PoF_line
    and removed
    PoF_line
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions