Update SQL Server VECTOR_SEARCH() support with WithApproximate LINQ operator#38144
Merged
Update SQL Server VECTOR_SEARCH() support with WithApproximate LINQ operator#38144
Conversation
- Remove deprecated topN parameter from VectorSearch API - Add WithApproximate() LINQ operator for opt-in approximate search - Implicit ORDER BY Distance for VectorSearch results - Update SQL generation: GenerateTop checks WithApproximateExpression - Handle nullability processing for WithApproximateExpression - Remove SetOffset from SelectExpression (no longer needed) - Add comprehensive tests including exact kNN, approximate, subquery, joins, skip/take patterns - Refresh API baselines Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
81bc04c to
06e7ec7
Compare
There was a problem hiding this comment.
Pull request overview
Updates EF Core’s SQL Server provider vector search support to match the latest Azure SQL VECTOR_SEARCH() behavior by separating “exact kNN” vs “approximate ANN” via an explicit LINQ opt-in.
Changes:
- Removes the
topNparameter fromSqlServerQueryableExtensions.VectorSearch()and relies onTake()instead. - Adds
WithApproximate<T>()to renderSELECT TOP(N) WITH APPROXIMATE ...for ANN/vector-index usage. - Wires
WithApproximateExpressionthrough SQL generation and nullability processing; expands/updates SQL Server functional tests and string resources.
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Query/Translations/VectorTranslationsSqlServerTest.cs | Updates/extends Azure SQL-only tests to cover exact vs approximate vector search SQL. |
| src/EFCore.SqlServer/Query/Internal/SqlServerSqlNullabilityProcessor.cs | Treats WithApproximateExpression as pass-through during nullability processing. |
| src/EFCore.SqlServer/Query/Internal/SqlServerQueryableMethodTranslatingExpressionVisitor.cs | Translates WithApproximate() by wrapping SelectExpression.Limit, and validates call shape. |
| src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs | Renders TOP(N) WITH APPROXIMATE by recognizing WithApproximateExpression during SQL generation. |
| src/EFCore.SqlServer/Query/Internal/SqlExpressions/WithApproximateExpression.cs | New internal SQL expression node used to carry the WITH APPROXIMATE modifier. |
| src/EFCore.SqlServer/Properties/SqlServerStrings.resx | Adds new user-facing resource strings for WithApproximate() validation errors. |
| src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs | Adds generated accessors for the new resource strings. |
| src/EFCore.SqlServer/Extensions/SqlServerQueryableExtensions.cs | Updates VectorSearch signature and introduces the WithApproximate() LINQ operator. |
| src/EFCore.SqlServer/EFCore.SqlServer.baseline.json | Updates API baseline to reflect the signature change and the new experimental API. |
Files not reviewed (1)
- src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs: Language not supported
Comments suppressed due to low confidence (1)
src/EFCore.SqlServer/Properties/SqlServerStrings.resx:425
- The guidance in this message is misleading: rewriting
Skip(...).Take(...).WithApproximate()toTake(...).WithApproximate().Skip(...)is not semantically equivalent (it changes the number of returned rows unless theTakeis adjusted to account for the skipped rows). Consider rewording to avoid suggesting an incorrect rewrite, or explicitly describe the correct equivalent rewrite (e.g., taking enough rows to cover the skip) depending on what you intend to support.
<data name="VectorPropertiesNotSupportedInJson" xml:space="preserve">
<value>Vector property '{propertyName}' is on '{structuralType}' which is mapped to JSON. Vector properties are not supported within JSON documents.</value>
</data>
- Remove unused using directive in SqlServerSqlNullabilityProcessor - Add test for Skip().Take().WithApproximate() throwing Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
acebe85 to
d82024e
Compare
Member
Author
|
@AndriySvyryd this should be ready for reviewing - I've added the warning for when EF.Functions.VectorSearch() is used without WithApproximate() on a column with an index, as discussed. |
AndriySvyryd
approved these changes
Apr 24, 2026
Contributor
API review baseline changes for
|
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.
Updates the EF Core SQL Server provider's vector search support to align with the latest Azure SQL
VECTOR_SEARCH()syntax and semantics.Changes
API changes
topNparameter fromEF.Functions.VectorSearch()(deprecated by SQL Server).WithApproximate<T>()LINQ operator — opt-in modifier that addsWITH APPROXIMATEto the SQLTOPclause, instructing SQL Server to use the vector index for ANN search. Without it, an exact kNN search is performed.Design
WithApproximate()is placed afterTake()in the LINQ pipeline, mirroring the T-SQLSELECT TOP(N) WITH APPROXIMATEsyntax.WithApproximateExpressionwraps theLimitin theSelectExpression. It flows transparently throughPushdownIntoSubquery(), preservingWITH APPROXIMATEin inner subqueries (e.g. reranking patterns).ORDER BYis added — users add explicitOrderByas needed, matching T-SQL behavior.InvalidOperationExceptionifWithApproximate()is used without a precedingTake().Rendering
WithApproximateExpressionis identified and rendered inVisitExtensioninSqlServerQuerySqlGenerator, keepingGenerateTopgeneric.WithApproximateExpressionas a pure pass-through.Tests (12 tests, all passing against Azure SQL)
VectorDistance_with_parameter/VectorDistance_with_constantVectorSearch_project_entity_and_distance(approximate)VectorSearch_exact_knn(no WithApproximate)VectorSearch_project_entity_only_with_distance_filterVectorSearch_in_subqueryVectorSearch_with_Where_before_Take/VectorSearch_with_Join_before_TakeVectorSearch_with_Take_and_Skip(pushdown preserves WITH APPROXIMATE)VectorSearch_reranking(approximate inner + exact outer re-rank)WithApproximate_without_Take_throwsLength(VECTORPROPERTY)NativeAOT compatible
Uses
new Func<IQueryable<T>, IQueryable<T>>(WithApproximate).Methodpattern instead ofMakeGenericMethod.All APIs remain
[Experimental("EF9105")].Closes #36384