Skip to content

feat: Implement %rowIndex environment variable#2573

Open
piotrszul wants to merge 1 commit intomainfrom
issue/2560
Open

feat: Implement %rowIndex environment variable#2573
piotrszul wants to merge 1 commit intomainfrom
issue/2560

Conversation

@piotrszul
Copy link
Collaborator

Summary

  • Implement %rowIndex environment variable for ViewDefinition forEach and forEachOrNull iterations, as defined in the SQL on FHIR ViewDefinition spec.
  • %rowIndex provides the 0-based index of the current element within the iterated collection. At the top level (no iteration), it evaluates to 0. Each nesting level maintains independent values.
  • Uses Spark's indexed transform(array, (element, index) -> ...) to track element positions during unnesting, threading the index through ProjectionContext into FHIRPath evaluation as a supplied variable.

Key changes

File Change
ColumnRepresentation Added transformWithIndex() using Spark's indexed transform
SingleResourceEvaluator Added withVariable() for creating evaluators with additional variables
ProjectionContext Added rowIndex field, withRowIndex(), and %rowIndex injection in evalExpression()
UnnestingSelection Rewrote evaluate() to use indexed transform, passing element index as %rowIndex

Test coverage (8 test cases in viewTests/rowindex.json)

  • 3 spec examples: element position, nested iteration with independent indices, unionAll with inherited/independent indices
  • Top-level %rowIndex defaults to 0
  • forEach and forEachOrNull with %rowIndex
  • forEachOrNull with empty collection (null row)
  • Arithmetic expression (%rowIndex + 1)

Test plan

  • All 8 new %rowIndex tests pass via FhirViewExtraTest
  • All 18 existing view tests pass (no regressions)
  • All 41 existing FhirViewExecutorTest + FhirViewBuilderUnitTest tests pass
  • Pre-existing compliance test failures unchanged (2 failures unrelated to this change)

Closes #2560

🤖 Generated with Claude Code

…Each/forEachOrNull

Add support for the %rowIndex environment variable as defined in the
SQL on FHIR ViewDefinition spec. Within forEach and forEachOrNull
iterations, %rowIndex resolves to the 0-based index of the current
element. At the top level (no iteration), it evaluates to 0. Each
nesting level maintains independent %rowIndex values.

The implementation uses Spark's indexed transform(array, (elem, idx) ->)
to track element positions during unnesting, threading the index through
ProjectionContext into the FHIRPath evaluation as a supplied variable.

Closes #2560

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@piotrszul piotrszul added new feature New feature or request fhirpath Related to fhirpath reference implementation labels Mar 18, 2026
@piotrszul piotrszul moved this to In progress in Pathling Mar 18, 2026
@sonarqubecloud
Copy link

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

Labels

fhirpath Related to fhirpath reference implementation new feature New feature or request

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

Implement %rowIndex environment variable

1 participant