Skip to content

%context.getResourceKey() fails with type mismatch inside forEach within unionAll #2569

@johngrimes

Description

@johngrimes

When using %context.getResourceKey() inside a forEach branch within a unionAll, Pathling throws an InvalidUserInputError:

InvalidUserInputError: Function 'getResourceKey', input: Type mismatch: expected ResourceCollection but got Collection

Per the SQL on FHIR spec, %context within a forEach inside a unionAll should refer to the resource root. Pathling appears to resolve %context as a generic Collection rather than a ResourceCollection, which causes getResourceKey() to reject the input.

Minimal reproduction

from pathling import PathlingContext

pc = PathlingContext.create(enable_extensions=True)
ds = pc.read.ndjson("/path/to/ndjson")

view_json = """
{
  "resourceType": "ViewDefinition",
  "resource": "Observation",
  "status": "draft",
  "name": "test",
  "where": [
    {"path": "category.coding.where(system = 'http://terminology.hl7.org/CodeSystem/observation-category' and code = 'vital-signs').exists()"}
  ],
  "select": [
    {
      "unionAll": [
        {
          "column": [
            {"name": "id", "path": "getResourceKey()", "type": "string"},
            {"name": "code_value", "path": "code.coding.first().code", "type": "code"},
            {"name": "value_number", "path": "value.ofType(Quantity).value", "type": "decimal"}
          ]
        },
        {
          "forEach": "component",
          "column": [
            {"name": "id", "path": "%context.getResourceKey()", "type": "string"},
            {"name": "code_value", "path": "code.coding.first().code", "type": "code"},
            {"name": "value_number", "path": "value.ofType(Quantity).value", "type": "decimal"}
          ]
        }
      ]
    }
  ]
}
"""

df = ds.view(json=view_json)  # Throws InvalidUserInputError

Workaround

Move resource-level columns (those using getResourceKey(), subject.getReferenceKey(), etc.) into a separate top-level select outside the unionAll, and keep only the differing columns inside the unionAll branches:

{
  "select": [
    {
      "column": [
        {"name": "id", "path": "getResourceKey()", "type": "string"}
      ]
    },
    {
      "unionAll": [
        {
          "column": [
            {"name": "code_value", "path": "code.coding.first().code", "type": "code"}
          ]
        },
        {
          "forEach": "component",
          "column": [
            {"name": "code_value", "path": "code.coding.first().code", "type": "code"}
          ]
        }
      ]
    }
  ]
}

Environment

  • pathling 9.4.0
  • PySpark 4.0.2
  • Python 3.14.2
  • macOS (Darwin 25.3.0, arm64)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions