Add implicit ORDER BY Distance for VectorSearch translation#38086
Open
Add implicit ORDER BY Distance for VectorSearch translation#38086
Conversation
VECTOR_SEARCH() results are inherently ordered by distance ascending. Add this ordering implicitly during translation so users can compose with Take() without needing an explicit OrderBy(r => r.Distance). Also remove the unnecessary forDml guard from GenerateTop() since VectorSearch() requires DbSet<T> and cannot appear in DELETE/UPDATE table lists through LINQ. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates EFCore.SqlServer’s VectorSearch() translation to automatically apply ORDER BY Distance ASC, reflecting SQL Server’s VECTOR_SEARCH() natural ordering so that users can compose with Take() without adding boilerplate OrderBy(r => r.Distance).
Changes:
- Add an implicit
ORDER BY Distance ASCto the translatedVECTOR_SEARCH()query. - Update SQL Server functional tests to remove redundant explicit
OrderBy(Distance)calls. - Update
VectorSearch()XML docs to describe the new implicit ordering behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/Translations/VectorTranslationsSqlServerTest.cs | Removes explicit OrderBy(Distance) from existing VectorSearch tests while keeping ORDER BY in generated SQL baselines. |
| src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs | Introduces a shared Distance column projection and appends an implicit ordering by that column. |
| src/EFCore.SqlServer/Extensions/SqlServerQueryableExtensions.cs | Updates API remarks to state results are implicitly ordered by distance ascending. |
test/EFCore.SqlServer.FunctionalTests/Query/Translations/VectorTranslationsSqlServerTest.cs
Show resolved
Hide resolved
src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs
Show resolved
Hide resolved
src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs
Show resolved
Hide resolved
Member
Author
|
@copilot address the above unresolved comments. |
…for VECTOR_SEARCH Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/e4de5685-2516-4055-9e2a-b8f8216313a9 Co-authored-by: roji <1862641+roji@users.noreply.github.com>
Contributor
Done in e8af99c:
|
roji
commented
Apr 11, 2026
src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs
Outdated
Show resolved
Hide resolved
Agent-Logs-Url: https://github.com/dotnet/efcore/sessions/89b8b8d6-f330-4dee-b9c9-66fdff8f250b Co-authored-by: roji <1862641+roji@users.noreply.github.com>
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.
Tiny follow-up to #38075.
VECTOR_SEARCH() results are inherently ordered by distance ascending. This adds the ordering implicitly during translation so users can compose with
Take()without needing an explicitOrderBy(r => r.Distance).Before:
After:
An explicit
.OrderBy()or.OrderByDescending()still overrides the implicit ordering.We could adopt a more low-level/conservative approach, saying that since SQL Server requires the explicit ORDER BY at the SQL level, we should continue requiring it at the LINQ level. However, we already add implicit orderings elsewhere (especially when transforming JSON arrays to resultsets with OPENJSON), and I don't see the point of having a mandatory, explicit gesture where we can save users the trouble.