Skip to content

feat(sof)!: run SQL Query Libraries in the primary database - #2958

Open
smunini wants to merge 1 commit into
mainfrom
feat/2902-sof-native-sql-query
Open

smunini wants to merge 1 commit into
mainfrom
feat/2902-sof-native-sql-query

Conversation

@smunini

@smunini smunini commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Refs #2902.

Problem

$sql-run and $sql-export with a SQLQuery or SQLView Library read every depends-on ViewDefinition out of storage in full, inserted the rows into an in-memory SQLite created for that request, and ran the Library's SQL there. This happened on every backend, including PostgreSQL. Cost grew with the size of every view rather than the result, and a view over HFS_SOF_SQLQUERY_MAX_SOURCE_ROWS_PER_VD rows failed outright (#1473).

The embedded engine came in with dbdd863aa (2026-05-17), which replaced the raw-SQL runner with the SQL-on-FHIR v2 Library model and used a per-request rusqlite database as the place to join the views.

Change

On SQLite and PostgreSQL the whole request is now one read-only statement in the primary database:

WITH "__sof_raw_N"(...) AS (<compiled view SQL>),
     "__sof_node_N"     AS (SELECT <typed columns> ...),
     "<label>"          AS (SELECT * FROM "__sof_node_N"),
     <SQL View CTEs>,
     <the Library's own CTEs>
<the Library's SELECT>

Nothing is materialized, so the per-dependency row cap no longer applies on these backends. MongoDB and S3 have no SQL engine and keep the embedded engine.

  • crates/sof/src/sqlquery/native.rs: parses, checks and rebuilds the caller's SQL.
  • crates/persistence/src/sof/native_query.rs: composes the statement.
  • crates/persistence/src/sof/{sqlite,postgres}_native.rs: execute it.
  • SofRunner gains native_sql_dialect() and run_sql_query(); the REST handlers pick the native path when the runner offers a dialect.

Security

The caller's SQL now runs beside the resources table, so the SQL check is a tenant-isolation boundary. Please review it as one.

  • The SQL is parsed with sqlparser and the re-serialized AST is what runs, never the original text.
  • Only declared labels and the query's own CTEs can be read; names are single-part; the __sof_ prefix and the stored table names are reserved.
  • SELECT INTO, locking clauses, non-table FROM items and OPERATOR(...) are refused.
  • PostgreSQL functions are allow-listed.
  • Only declared :name parameters can be bound.
  • PostgreSQL runs the statement in a READ ONLY transaction with SET LOCAL statement_timeout.
  • SQLite runs it under an authorizer that denies writes, PRAGMA, ATTACH, sqlite_* reads and load_extension, with a progress-handler timeout.

Breaking changes

  • PostgreSQL runs the Library's SQL as PostgreSQL. It reads the application/sql;dialect=postgresql attachment, or a bare application/sql one. A Library offering only another dialect gets a 422 naming the dialect to add. SQL written for SQLite must be valid PostgreSQL, and mixed-case column names need double quotes.
  • An undeclared :name placeholder is now an error on both SQL backends.
  • Rows have no order without ORDER BY.
  • On PostgreSQL, array or composite result columns must be cast.
  • A PostgreSQL function outside the allow-list is refused, which may reject legitimate queries.
  • HFS_SOF_SQLQUERY_MAX_SOURCE_ROWS_PER_VD applies only on MongoDB and S3.
  • $sql-export of a SQL Query is one statement bounded by HFS_SOF_SQLQUERY_TIMEOUT_SECS on these backends.

Not changed

  • The query still scans every resource of the view's type; it no longer ships or stores the rows.
  • ValueSet and ConceptMap dependencies are bound into the statement as a JSON parameter rather than read from their stored tables.

Testing

  • New shared suite on SQLite and PostgreSQL (crates/persistence/tests/common/sof_native_sql.rs): joins, typed columns, parameters, tenant isolation, refused SQL, timeout.
  • Existing SQL-on-FHIR REST suites pass on SQLite, PostgreSQL and MongoDB. Two row-cap tests were rewritten because the cap no longer applies natively.
  • cargo check --workspace --all-features --tests, clippy with the CI flags and cargo fmt --check are clean.
  • One measurement on PostgreSQL (50,000 observations, selective filter, release build, runner level): reading the whole view took 90–143 ms, the native query 24–49 ms. The old path also inserted those rows into SQLite and queried them there.

Not run locally: UI Playwright tests, S3, the PostgreSQL + Elasticsearch composite, and the full workspace test run.

On SQLite and PostgreSQL, $sql-run and $sql-export with a SQLQuery or
SQLView Library now run as one read-only statement in the primary
database. Each depends-on ViewDefinition becomes a CTE holding its
compiled SQL, each SQL View a CTE, and the Library's SQL reads them by
label. Nothing is copied into a per-request in-memory SQLite, so cost no
longer grows with the size of every view and the per-dependency row cap
no longer applies on these backends.

MongoDB and S3 have no SQL engine and keep the embedded engine.

The caller's SQL now runs beside the stored tables, so it is parsed,
checked and re-serialized first: only declared labels and the query's
own CTEs can be read, PostgreSQL functions are allow-listed, and only
declared parameters can be bound. PostgreSQL runs it in a READ ONLY
transaction with a statement timeout; SQLite under a read-only
authorizer with a progress-handler timeout.

BREAKING CHANGE: a PostgreSQL server runs the Library's SQL as
PostgreSQL. It reads the application/sql;dialect=postgresql attachment,
or a bare application/sql one, and rejects a Library that offers only
another dialect with a 422. On both SQL backends an undeclared :name
placeholder is now an error, and rows have no order without ORDER BY.

Refs #2902

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant