Skip to content

Implement %rowIndex environment variable#12

Open
johngrimes wants to merge 6 commits into
mainfrom
issue/11
Open

Implement %rowIndex environment variable#12
johngrimes wants to merge 6 commits into
mainfrom
issue/11

Conversation

@johngrimes
Copy link
Copy Markdown
Member

Implements the %rowIndex environment variable from the SQL on FHIR v2 spec, which holds the 0-based index of the current element within the collection being iterated by forEach, forEachOrNull, or repeat. This lets columns capture position for ordering or surrogate keys.

The nine %rowIndex tests in sqlonfhir/tests/row_index.json (pulled in by the submodule bump in #9) were previously failing against the MSSQL implementation and are now passing.

Spec reference: https://build.fhir.org/ig/FHIR/sql-on-fhir-v2/functional-model.html#the-foreach-and-foreachornull-functions

Work was planned and tracked via an OpenSpec change, now archived.

Fixes #11

Scaffolds the implement-row-index change with proposal, spec, design,
and tasks for resolving the SQL on FHIR %rowIndex environment variable
to a 0-based T-SQL integer across forEach, forEachOrNull, repeat, and
unionAll, so the nine in-scope row_index tests can pass.

Re #11
Recognise the SQL on FHIR %rowIndex environment variable in column path
expressions and resolve it to the 0-based position of the current element
within the active iteration.

The visitor special-cases the reserved name before the user-constant
lookup, returning a per-iteration index expression supplied via a new
TranspilerContext.rowIndexExpr field, or 0 at the resource root. forEach
and forEachOrNull set it from the OPENJSON [key] column (with a COALESCE
so the null-padded forEachOrNull row reports 0). repeat sets it to a
ROW_NUMBER window ordered by a zero-padded __order accumulator added to
the recursive CTE, giving the depth-first pre-order position. unionAll
needs no change as branches inherit or override the expression through
the shared context.

This makes the nine in-scope row_index.json conformance tests pass.

Fixes #11
Sync the row-index-variable capability into the main specs and move the
completed change to the archive.
@johngrimes johngrimes marked this pull request as ready for review May 28, 2026 19:12
@johngrimes johngrimes requested a review from piotrszul May 28, 2026 19:12
@johngrimes johngrimes moved this from Backlog to In progress in Pathling May 28, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in Pathling May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

Implement %rowIndex environment variable

1 participant