Skip to main content

Running SQL on FHIR views

The Pathling library leverages the SQL on FHIR specification to provide a way to project FHIR data into easy-to-use tabular forms.

Once you have transformed your FHIR data into tabular views, you can choose to keep it in a Spark dataframe and continue to work with in Apache Spark, or export it to Python or R dataframes or a variety of different file formats for use in the tool of your choice.

from pathling import PathlingContext

pc = PathlingContext.create()
data = pc.read.ndjson("/some/file/location")

result = data.view(
resource="Patient",
select=[
{"column": [{"path": "getResourceKey()", "name": "patient_id"}]},
{
"forEach": "address",
"column": [
{"path": "line.join('\\n')", "name": "street"},
{"path": "use", "name": "use"},
{"path": "city", "name": "city"},
{"path": "postalCode", "name": "zip"},
],
},
],
)

display(result)

The result of this query would look something like this:

patient_idstreetusecityzip
1398 Kautzer Walk Suite 62homeBarnstable02675
1186 Nitzsche ForgeworkRevere02151
21087 Quitzon ClubhomePlymouthNULL
3442 Bruen ArcadehomeNantucketNULL
4858 Miller Junction Apt 61workBrockton02301

To join the rows of one view to the rows of another, use the getResourceKey() and getReferenceKey() functions. See SQL on FHIR functions for the keys that they produce and the references that they support.

Recursive elements, such as item within a Questionnaire, are only encoded to the configured maximum nesting level. Paths that navigate deeper than this behave as if the data were absent: a forEach produces no rows, a forEachOrNull produces a single row of nulls, and a column is null, or an empty array if it is a collection column. See Navigation beyond the encoded depth for how this applies to FHIRPath expressions in general.

For a more comprehensive example demonstrating SQL on FHIR queries with multiple views, complex transformations and joins, see the SQL on FHIR example.