vgi-fhir

module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: MIT

README

Vector Gateway Interface (VGI)

A Query.Farm VGI worker for DuckDB.

Query HL7 FHIR R4 Resources as Tables in DuckDB

vgi-fhir · a Query.Farm VGI worker

CI

A VGI worker, written in Go, that queries HL7 FHIR REST servers and returns resources to DuckDB/SQL as rows. FHIR is plain REST/JSON, so the worker uses only the Go standard library (net/http + encoding/json) for transport; no FHIR SDK is required. Multi-page searchset Bundles are followed automatically via their link[].relation == "next" links, up to a bounded max_results.

Built on the vgi-go SDK; it speaks the VGI protocol over stdio. Catalog name: fhir. Targets FHIR R4 (the common version, 4.0.1).

INSTALL vgi FROM community; LOAD vgi;

ATTACH 'fhir' AS fhir (TYPE vgi, LOCATION 'github-auto://Query-farm/vgi-fhir@v0.4.1');

-- Patients (core fields flattened, full JSON in `raw`). Every search-backed
-- function STREAMS: it fetches one Bundle page per batch, so `LIMIT` stops the
-- scan after a single HTTP round-trip instead of crawling every page first.
SELECT id, family, given, gender, birth_date, active
FROM fhir.fhir_patients('https://r4.smarthealthit.org');

-- All observations (valueQuantity flattened; non-numeric values are NULL).
SELECT code, code_display, value, unit, subject
FROM fhir.fhir_observations('https://r4.smarthealthit.org');

-- Generic search of any resource type (id + full resource JSON).
SELECT id, resource
FROM fhir.fhir_search('https://r4.smarthealthit.org', 'Condition');

-- A raw search query string + a custom page size.
SELECT id
FROM fhir.fhir_search(
  'https://r4.smarthealthit.org', 'Patient',
  query := 'family=smith&gender=female',
  count := 50);

-- One resource by id.
SELECT resource
FROM fhir.fhir_read('https://r4.smarthealthit.org', 'Patient',
                    '45a69bf0-7116-4f9f-a1d9-29700439b4a4');

-- What the server supports.
SELECT resource_type, interactions
FROM fhir.fhir_capabilities('https://r4.smarthealthit.org');

-- Combine functions: one patient's Body Weight trend (fhir_observations),
-- labelled with their name (fhir_patients) by joining on the subject
-- reference. Table-function arguments must be literals, so both sides are
-- filtered to the same patient id.
SELECT p.given, p.family, o.effective[:10] AS measured_on,
       round(o.value, 1) AS weight_kg,
       round(o.value - lag(o.value) OVER (ORDER BY o.effective), 1) AS change_kg
FROM fhir.fhir_observations('https://r4.smarthealthit.org',
       query := 'patient=45a69bf0-7116-4f9f-a1d9-29700439b4a4&code=29463-7') AS o
JOIN fhir.fhir_patients('https://r4.smarthealthit.org',
       query := '_id=45a69bf0-7116-4f9f-a1d9-29700439b4a4') AS p
  ON o.subject = 'Patient/' || p.id
ORDER BY o.effective;

-- A bearer token for protected servers (named option on every function).
SELECT id FROM fhir.fhir_patients('https://server/fhir', token := 'BEARER_TOKEN');

Quick start

Every GitHub release ships a prebuilt worker binary. The vgi extension downloads the one for your platform, checks its SHA-256 and caches it, so there is nothing to install besides DuckDB:

INSTALL vgi FROM community;
LOAD vgi;

ATTACH 'fhir' AS fhir (TYPE vgi, LOCATION 'github-auto://Query-farm/vgi-fhir@v0.4.1');

-- List the curated public FHIR R4 test servers and whether each needs authentication.
SELECT name, base_url, requires_auth FROM fhir.main.fhir_servers ORDER BY name;

-- List Patients with their core demographic fields flattened into columns.
SELECT id, family, given, gender, birth_date FROM fhir.main.fhir_patients('https://r4.smarthealthit.org') LIMIT 20;

Every function fetches from the FHIR server URL you pass. Many public test servers are open; for a protected server pass an OAuth bearer token with token := '…' (see Named options and Behaviour).

Prebuilt binaries cover macOS (Apple silicon), Linux (x86-64 and arm64) and Windows (x86-64). github-auto:// needs an explicit release tag; change @v0.4.1 to move to a newer release.

Other ways to run it
  • Docker (the image bundles the worker; needs a container runtime):

    ATTACH 'fhir' AS fhir (TYPE vgi, LOCATION 'docker://ghcr.io/query-farm/vgi-fhir:0.4.1');
    
  • Manual download. Download the archive for your platform from the releases page, verify it against its .sha256 file (a cosign .cosign.bundle is published alongside), unpack it, and set LOCATION to the path of the extracted worker executable.

Build from source

To build the worker yourself, see Building.

Functions

All five are table functions (each makes one or more FHIR HTTP requests and returns rows). The first argument is always base_url (the FHIR R4 service base).

Function Returns Description
fhir_search(base_url, resource_type) id, resource VARCHAR One row per resource from a search of {base}/{resourceType}; resource is the full JSON. Streams page by page, following next links up to max_results (1000) or until a page comes back empty.
fhir_read(base_url, resource_type, id) resource VARCHAR A single resource read from {base}/{resourceType}/{id} (one row).
fhir_patients(base_url) id, family, given, gender, birth_date VARCHAR, active BOOLEAN, raw VARCHAR All Patients, core fields flattened; raw is the full Patient JSON.
fhir_observations(base_url) id, status, code, code_display VARCHAR, value DOUBLE, unit, effective, subject, raw VARCHAR All Observations; valueQuantity flattened (non-numeric → NULL value); raw is the full JSON.
fhir_capabilities(base_url) resource_type VARCHAR, interactions VARCHAR[] The resource types and REST interactions from the server's CapabilityStatement ({base}/metadata).
Named options

These optional named arguments use DuckDB name := value syntax:

Option Applies to Default Meaning
token all none OAuth bearer token, sent as Authorization: Bearer <token>. Omit for open servers.
query fhir_search, fhir_patients, fhir_observations none Raw FHIR search query string (e.g. family=smith&_count=50), appended to the search URL verbatim.
count fhir_search, fhir_patients, fhir_observations 50 Page size, sent as the _count search parameter. All pages are still followed (up to max_results = 1000).
Behaviour
  • Pagination — searchset Bundles are followed page by page via their link[].relation == "next" links, collecting up to max_results (1000) resources, then stopping.
  • Auth — when a token is supplied it is sent as Authorization: Bearer <token>; otherwise no auth header is sent (many public FHIR servers are open).
  • Robustness — non-2xx HTTP responses, FHIR OperationOutcome error bodies, and malformed JSON all become clear DuckDB errors with a short excerpt / diagnostics; the worker never crashes or hangs (every request is timeout bounded).
  • Flattening — fhir_patients/fhir_observations lift the common fields; everything else stays available in raw. Observations without a numeric valueQuantity surface a NULL value.

Building

make build        # builds vgi-fhir-worker + mockserver
make test-unit    # Go unit/integration tests (in-process httptest FHIR server)
make test-sql     # haybarn SQL end-to-end against a local mock FHIR server
make test         # both

Licensing

This worker is licensed MIT (see LICENSE).

Dependencies: the Go standard library only for FHIR transport (net/http, encoding/json) plus the VGI SDK stack — vgi-go, vgi-rpc-go, and arrow-go (Apache-2.0). No third-party FHIR client library is used. Scope is FHIR R4 (4.0.1); other versions are not specifically supported.


Authorship & License

Written by Query.Farm.

Copyright 2026 Query Farm LLC - https://query.farm

Notebooks

Executed FHIR notebooks use synthetic resources to audit pagination, reference/quantity policies and raw-resource reads, with separate parameter experiments.

Directories

Path Synopsis
cmd
mockserver command
Command mockserver runs a standalone in-memory HL7 FHIR R4 server exposing /Patient and /Observation (searchset Bundles), /Patient/{id}, and /metadata (CapabilityStatement).
Command mockserver runs a standalone in-memory HL7 FHIR R4 server exposing /Patient and /Observation (searchset Bundles), /Patient/{id}, and /metadata (CapabilityStatement).
vgi-fhir-worker command
Command vgi-fhir-worker is a VGI worker that queries HL7 FHIR R4 REST servers over plain REST/JSON and returns resources as DuckDB rows.
Command vgi-fhir-worker is a VGI worker that queries HL7 FHIR R4 REST servers over plain REST/JSON and returns resources as DuckDB rows.
internal
mockfhir
Package mockfhir provides an in-memory HL7 FHIR R4 server used by both the Go unit tests (as an httptest.Server) and the standalone mockserver binary (for the haybarn SQL E2E).
Package mockfhir provides an in-memory HL7 FHIR R4 server used by both the Go unit tests (as an httptest.Server) and the standalone mockserver binary (for the haybarn SQL E2E).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL