FHIR SQL
fyron.fhir.sql queries relational FHIR stores with explicit SQL and returns analysis-ready DataFrames. Use it when your institution exposes FHIR resources through Postgres or a warehouse layer and SQL joins are more appropriate than paginated REST calls.
When To Use It
- cohort-scale extraction from FHIR warehouse tables,
- joins across patients, encounters, observations, conditions, and imaging metadata,
- reproducible SQL queries versioned with the analysis,
- chunked or parallel SQL jobs for large extracts.
Use FHIR REST for live API access, server write-back, or smaller endpoint-driven workflows.
| Input | Needed for | Notes |
|---|
| connection settings | all queries | FHIR_DB_DSN or host/db/user/password variables. |
sql | all query methods | SQL statement with optional placeholders. |
| Input | Use |
|---|
params | safe parameterized queries |
rename | normalize output column names |
chunksize | stream large result sets |
column_map | row-wise SQL from a cohort table |
parallel | split repeated row-wise jobs |
Minimal Example
from fyron import FHIRSQLClient
client = FHIRSQLClient(dsn="postgresql://user:password@localhost/fhir")
patients = client.query_df(
"select id as patient_id, birth_date from patient limit 100"
)
Clinical Example
labs = client.query_df(
"""
select
o.subject_id as patient_id,
o.effective_datetime,
o.value_quantity_value as hemoglobin,
o.value_quantity_unit as unit
from observation o
where o.code_code = %(loinc)s
and o.effective_datetime >= %(start)s
""",
params={"loinc": "718-7", "start": "2020-01-01"},
)
Function Table
| Function | Required | Optional | Returns |
|---|
FHIRSQLClient(...) | DSN or DB env vars | timeout, logging | client |
query(sql, ...) | SQL | params | list of dict rows |
query_df(sql, ...) | SQL | params, rename | DataFrame |
query_df_stream(sql, ...) | SQL | params, rename, chunksize | iterator of DataFrames |
query_df_parallel(jobs, ...) | jobs | workers, merge, unique keys | DataFrame |
query_df_from(df, ...) | input table, SQL, column map | chunking, parallelism | DataFrame |
Common Pitfalls
- SQL table names and JSON paths vary by FHIR warehouse implementation.
- Prefer parameterized SQL over string formatting.
- Keep patient identifiers explicit and avoid accidental cross-patient joins.
- Store the SQL text with analysis outputs for auditability.
- FHIR for the overview.
- FHIR REST for endpoint-driven queries and write-back.
- Cohort Tables for joining extracted features to outcomes.