Repository navigation
Python: fix(sql_server): order every distance metric ascending - #14714
Open
哈基米 (BlueX888) wants to merge 1 commit into
Open
哈基米 (BlueX888) wants to merge 1 commit into
哈基米 (BlueX888) wants to merge 1 commit into
Conversation
This branch was successfully deployed
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.
Fixes #14713
Problem
SQL Server vector searches are ordered using the shared
DISTANCE_FUNCTION_DIRECTION_HELPER, which models dot product as a similarity.VECTOR_DISTANCEis a distance for every SQL Server metric — thedotmetric is documented as the negative dot product, with smaller values more similar — so this has two consequences:A vector field left at the default distance function (
VectorStoreField("vector", dimensions=...)→DistanceFunction.DEFAULT) is accepted byDISTANCE_FUNCTION_MAP(mapped tocosine) but has no entry in the direction helper. Every search fails at runtime:A field using
DistanceFunction.DOT_PRODbuildsORDER BY ... DESC, returning the least similar rows first.InMemoryCollectionalready resolvesDEFAULTto cosine; SQL Server's supported metrics are all distances, so the direction is always ascending.Change
Order SQL Server vector results ascending for every supported metric and drop the now-unused direction-helper import. The metric name passed to
VECTOR_DISTANCEis unchanged.Verification
test_build_search_querynow runs withCOSINE_DISTANCE,EUCLIDEAN_DISTANCE,DOT_PROD, andDEFAULT, asserting the matchingVECTOR_DISTANCEmetric andORDER BY ... ASC.SqlServerCollection.searchtest whose model leaves the vector field atDistanceFunction.DEFAULT.DOT_PRODandDEFAULTcases fail (DESC and KeyError respectively); with the change, all 82 tests intests/unit/connectors/memory/test_sql_server.pypass.ruff checkand packagemypy -p semantic_kernelare clean.