Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #2902.
Problem
$sql-runand$sql-exportwith a SQLQuery or SQLView Library read everydepends-onViewDefinition 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 overHFS_SOF_SQLQUERY_MAX_SOURCE_ROWS_PER_VDrows 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:
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.SofRunnergainsnative_sql_dialect()andrun_sql_query(); the REST handlers pick the native path when the runner offers a dialect.Security
The caller's SQL now runs beside the
resourcestable, so the SQL check is a tenant-isolation boundary. Please review it as one.__sof_prefix and the stored table names are reserved.SELECT INTO, locking clauses, non-tableFROMitems andOPERATOR(...)are refused.:nameparameters can be bound.READ ONLYtransaction withSET LOCAL statement_timeout.PRAGMA,ATTACH,sqlite_*reads andload_extension, with a progress-handler timeout.Breaking changes
application/sql;dialect=postgresqlattachment, or a bareapplication/sqlone. 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.:nameplaceholder is now an error on both SQL backends.ORDER BY.HFS_SOF_SQLQUERY_MAX_SOURCE_ROWS_PER_VDapplies only on MongoDB and S3.$sql-exportof a SQL Query is one statement bounded byHFS_SOF_SQLQUERY_TIMEOUT_SECSon these backends.Not changed
Testing
crates/persistence/tests/common/sof_native_sql.rs): joins, typed columns, parameters, tenant isolation, refused SQL, timeout.cargo check --workspace --all-features --tests, clippy with the CI flags andcargo fmt --checkare clean.Not run locally: UI Playwright tests, S3, the PostgreSQL + Elasticsearch composite, and the full workspace test run.