Skip to content

Fix SQL generation for selective chained searches - #5725

Draft
Mikael Weaver (mikaelweave) wants to merge 5 commits into
mainfrom
fix/selective-first-chained-search
Draft

Fix SQL generation for selective chained searches#5725
Mikael Weaver (mikaelweave) wants to merge 5 commits into
mainfrom
fix/selective-first-chained-search

Conversation

@mikaelweave

@mikaelweave Mikael Weaver (mikaelweave) commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Related work item

Summary

  • generate the selective target reference predicate before traversing the source reference for forward reference-reference-date chained searches
  • preserve the existing CTE query pattern while using local loop joins for reference lookups and a local hash join for the source date filter
  • keep reverse chains, unions, nested chains, non-reference targets, and non-date source filters on the existing generation path

Root cause

The generated query began with the broad source-reference side. Under cached compilation, SQL Server selected nested loops and performed hundreds of thousands of date index probes. Reordering the specialized shape and using the validated local join strategies produces a reusable plan without requiring OPTION (RECOMPILE) or global query hints.

Validation

  • dotnet test src/Microsoft.Health.Fhir.SqlServer.UnitTests/Microsoft.Health.Fhir.SqlServer.UnitTests.csproj --no-restore -- --no-progress
  • 1,044 tests passed

Generate the selective target reference CTE first for the affected forward reference-reference-date chain, then use local loop joins for reference traversal and a hash join for the source date filter. This avoids parameter-sensitive nested-loop date probes while preserving the existing CTE query pattern.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mikaelweave
Mikael Weaver (mikaelweave) requested a review from a team as a code owner August 15, 2026 14:16
@mikaelweave
Mikael Weaver (mikaelweave) marked this pull request as draft August 15, 2026 14:27
@mikaelweave Mikael Weaver (mikaelweave) added Bug Bug bug bug. Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs No-PaaS-breaking-change labels Aug 15, 2026
@mikaelweave Mikael Weaver (mikaelweave) added the No-ADR ADR not needed label Aug 15, 2026
Move the specialized query generation into a focused SqlQueryGenerator partial, consolidate its sequencing state, and fall back for preceding source filters and sorted searches.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add production-driven regression coverage for top, count-only, include persistence, intervening source predicates, multiple target predicates, and chained token targets without source date filters.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Move the specialized regression coverage into a focused partial test class and cover a following source predicate.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@codecov-commenter

Codecov Comments Bot (codecov-commenter) commented Aug 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@1e4cec8). Learn more about missing BASE report.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #5725   +/-   ##
=======================================
  Coverage        ?   77.41%           
=======================================
  Files           ?     1012           
  Lines           ?    37432           
  Branches        ?     5688           
=======================================
  Hits            ?    28978           
  Misses          ?     7083           
  Partials        ?     1371           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

* Support sorting in target-first chained search

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Strengthen target-first sort coverage

Preserve later sorts in the generic pipeline so intervening predicates are applied before sort projection, while making atomic match consumption and CTE identities explicit.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Cover unsupported adjacent numeric sort

Verify the target-first matcher rejects the atomic rewrite and preserve the generic numeric SortValue path for both sort directions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Revert "Cover unsupported adjacent numeric sort"

This reverts commit 3c394b6.

* Retry CI after Cosmos provisioning lock

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure Healthcare APIs Label denotes that the issue or PR is relevant to the FHIR service in the Azure Healthcare APIs Bug Bug bug bug. No-ADR ADR not needed No-PaaS-breaking-change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants