Skip to content

Implement support for SQL Server vector search #36383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.51.0" />

<!-- SQL Server dependencies -->
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.0.2" />
<PackageVersion Include="Microsoft.SqlServer.Types" Version="160.1000.6" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.1.0-preview2.25178.5" />
<PackageVersion Include="Microsoft.SqlServer.Types" Version="170.700.9-ctp2p0" />

<!-- external dependencies -->
<PackageVersion Include="Castle.Core" Version="5.2.1" />
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.SqlServer/EFCore.SqlServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.SqlServer.Types" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// ReSharper disable once CheckNamespace

using Microsoft.Data.SqlTypes;

namespace Microsoft.EntityFrameworkCore;

/// <summary>
Expand Down Expand Up @@ -2452,4 +2454,30 @@ public static long PatIndex(
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VariancePopulation)));

#endregion Population variance

#region Vector functions

/// <summary>
/// Calculates the distance between two vectors using a specified distance metric.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="distanceMetric">
/// A string with the name of the distance metric to use to calculate the distance between the two given vectors. The following distance metrics are supported: <c>cosine</c>, <c>euclidean</c> or <c>dot</c>.
/// </param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <remarks>
/// Vector distance is always exact and doesn't use any vector index, even if available.
/// </remarks>
/// <seealso href="https://learn.microsoft.com/sql/t-sql/functions/vector-distance-transact-sql">SQL Server documentation for <c>VECTOR_DISTANCE</c>.</seealso>
/// <seealso href="https://learn.microsoft.com/sql/relational-databases/vectors/vectors-sql-server">Vectors in the SQL Database Engine.</seealso>
public static double VectorDistance<T>(
this DbFunctions _,
[NotParameterized] string distanceMetric,
SqlVector<T> vector1,
SqlVector<T> vector2)
where T : unmanaged
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));

#endregion Vector functions
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text;
using Microsoft.Data.SqlTypes;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Extensions.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;

namespace Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;

Expand Down Expand Up @@ -43,6 +45,7 @@ public override void Validate(IModel model, IDiagnosticsLogger<DbLoggerCategory.
base.Validate(model, logger);

ValidateDecimalColumns(model, logger);
ValidateVectorColumns(model, logger);
ValidateByteIdentityMapping(model, logger);
ValidateTemporalTables(model, logger);
ValidateUseOfJsonType(model, logger);
Expand Down Expand Up @@ -110,6 +113,32 @@ protected virtual void ValidateDecimalColumns(
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected virtual void ValidateVectorColumns(
IModel model,
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
foreach (IConventionProperty property in model.GetEntityTypes()
.SelectMany(t => t.GetDeclaredProperties())
.Where(p => p.ClrType.UnwrapNullableType() == typeof(SqlVector<float>)))
{
if (property.GetTypeMapping() is not SqlServerVectorTypeMapping { Size: not null } vectorTypeMapping)
{
throw new InvalidOperationException(SqlServerStrings.VectorDimensionsMissing(property.DeclaringType.DisplayName(), property.Name));
}

if (property.DeclaringType.IsMappedToJson())
{
throw new InvalidOperationException(SqlServerStrings.VectorPropertiesNotSupportedInJson(property.DeclaringType.DisplayName(), property.Name));
}
}
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down
22 changes: 22 additions & 0 deletions src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/EFCore.SqlServer/Properties/SqlServerStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -369,4 +369,13 @@
<data name="TransientExceptionDetected" xml:space="preserve">
<value>An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure' to the 'UseSqlServer' call.</value>
</data>
<data name="VectorDimensionsInvalid" xml:space="preserve">
<value>Vector properties require a positive size (number of dimensions).</value>
</data>
<data name="VectorDimensionsMissing" xml:space="preserve">
<value>Vector property '{structuralType}.{propertyName}' was not configured with the number of dimensions. Set the column type to 'vector(x)' with the desired number of dimensions, or use the 'MaxLength' APIs.</value>
</data>
<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>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public SqlServerMemberTranslatorProvider(
new SqlServerDateTimeMemberTranslator(sqlExpressionFactory, typeMappingSource),
new SqlServerStringMemberTranslator(sqlExpressionFactory),
new SqlServerTimeSpanMemberTranslator(sqlExpressionFactory),
new SqlServerTimeOnlyMemberTranslator(sqlExpressionFactory)
new SqlServerTimeOnlyMemberTranslator(sqlExpressionFactory),
new SqlServerVectorTranslator(sqlExpressionFactory, typeMappingSource)
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public SqlServerMethodCallTranslatorProvider(
new SqlServerNewGuidTranslator(sqlExpressionFactory),
new SqlServerObjectToStringTranslator(sqlExpressionFactory, typeMappingSource),
new SqlServerStringMethodTranslator(sqlExpressionFactory, sqlServerSingletonOptions),
new SqlServerTimeOnlyMethodTranslator(sqlExpressionFactory)
new SqlServerTimeOnlyMethodTranslator(sqlExpressionFactory),
new SqlServerVectorTranslator(sqlExpressionFactory, typeMappingSource)
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Data.SqlTypes;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class SqlServerVectorTranslator(
ISqlExpressionFactory sqlExpressionFactory,
IRelationalTypeMappingSource typeMappingSource)
: IMethodCallTranslator, IMemberTranslator
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlExpression? Translate(
SqlExpression? instance,
MethodInfo method,
IReadOnlyList<SqlExpression> arguments,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (method.DeclaringType == typeof(SqlServerDbFunctionsExtensions))
{
switch (method.Name)
{
case nameof(SqlServerDbFunctionsExtensions.VectorDistance)
when arguments is [_, var distanceMetric, var vector1, var vector2]:
{
var vectorTypeMapping = vector1.TypeMapping ?? vector2.TypeMapping
?? throw new InvalidOperationException(
"One of the arguments to EF.Functions.VectorDistance must be a vector column.");

return sqlExpressionFactory.Function(
"VECTOR_DISTANCE",
[
sqlExpressionFactory.ApplyTypeMapping(distanceMetric, typeMappingSource.FindMapping("varchar(max)")),
sqlExpressionFactory.ApplyTypeMapping(vector1, vectorTypeMapping),
sqlExpressionFactory.ApplyTypeMapping(vector2, vectorTypeMapping)
],
nullable: true,
argumentsPropagateNullability: [true, true, true],
typeof(double),
typeMappingSource.FindMapping(typeof(double)));
}
}
}

return null;
}

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public SqlExpression? Translate(
SqlExpression? instance,
MemberInfo member,
Type returnType,
IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
if (member.DeclaringType == typeof(SqlVector<float>))
{
switch (member.Name)
{
case nameof(SqlVector<>.Length) when instance is not null:
{
return sqlExpressionFactory.Function(
"VECTORPROPERTY",
[
instance,
sqlExpressionFactory.Constant("Dimensions", typeMappingSource.FindMapping("varchar(max)"))
],
nullable: true,
argumentsPropagateNullability: [true, true],
typeof(int),
typeMappingSource.FindMapping(typeof(int)));
}
}
}

return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ FROM [sys].[types] AS [t]
var precision = reader.GetValueOrDefault<int>("precision");
var scale = reader.GetValueOrDefault<int>("scale");

var storeType = GetStoreType(systemType, maxLength, precision, scale);
var storeType = GetStoreType(systemType, maxLength, precision, scale, vectorDimensions: 0);

_logger.TypeAliasFound(DisplayName(schema, userType), storeType);

Expand Down Expand Up @@ -472,7 +472,7 @@ FROM [sys].[sequences] AS [s]
storeType = value.storeType;
}

storeType = GetStoreType(storeType, maxLength: 0, precision: precision, scale: scale);
storeType = GetStoreType(storeType, maxLength: 0, precision, scale, vectorDimensions: 0);

_logger.SequenceFound(DisplayName(schema, name), storeType, cyclic, incrementBy, startValue, minValue, maxValue);

Expand Down Expand Up @@ -730,6 +730,7 @@ private void GetColumns(
CAST([c].[max_length] AS int) AS [max_length],
CAST([c].[precision] AS int) AS [precision],
CAST([c].[scale] AS int) AS [scale],
{(_compatibilityLevel is >= 170 ? "[c].[vector_dimensions]" : "NULL as [vector_dimensions]")},
[c].[is_nullable],
[c].[is_identity],
[dc].[definition] AS [default_sql],
Expand Down Expand Up @@ -801,6 +802,7 @@ FROM [sys].[views] v
var maxLength = dataRecord.GetValueOrDefault<int>("max_length");
var precision = dataRecord.GetValueOrDefault<int>("precision");
var scale = dataRecord.GetValueOrDefault<int>("scale");
var vectorDimensions = dataRecord.GetValueOrDefault<int>("vector_dimensions");
var nullable = dataRecord.GetValueOrDefault<bool>("is_nullable");
var isIdentity = dataRecord.GetValueOrDefault<bool>("is_identity");
var defaultValueSql = dataRecord.GetValueOrDefault<string>("default_sql");
Expand Down Expand Up @@ -835,15 +837,19 @@ FROM [sys].[views] v
string storeType;
string systemTypeName;

// Swap store type if type alias is used
if (typeAliases.TryGetValue($"[{dataTypeSchemaName}].[{dataTypeName}]", out var value))
// If the store type is in our loaded aliases dictionary, resolve to the canonical type.
// Note that the vector type is implemented as an alias for varbinary, but we do not want
// to scaffold vectors as varbinary.
var fullQualifiedTypeName = $"[{dataTypeSchemaName}].[{dataTypeName}]";
if (fullQualifiedTypeName is not "[sys].[vector]"
&& typeAliases.TryGetValue(fullQualifiedTypeName, out var value))
{
storeType = value.storeType;
systemTypeName = value.typeName;
}
else
{
storeType = GetStoreType(dataTypeName, maxLength, precision, scale);
storeType = GetStoreType(dataTypeName, maxLength, precision, scale, vectorDimensions);
systemTypeName = dataTypeName;
}

Expand Down Expand Up @@ -995,16 +1001,16 @@ void Unwrap()
}
}

private static string GetStoreType(string dataTypeName, int maxLength, int precision, int scale)
private static string GetStoreType(string dataTypeName, int maxLength, int precision, int scale, int vectorDimensions)
{
if (dataTypeName == "timestamp")
switch (dataTypeName)
{
return "rowversion";
}

if (dataTypeName is "decimal" or "numeric")
{
return $"{dataTypeName}({precision}, {scale})";
case "timestamp":
return "rowversion";
case "decimal" or "numeric":
return $"{dataTypeName}({precision}, {scale})";
case "vector":
return $"vector({vectorDimensions})";
}

if (DateTimePrecisionTypes.Contains(dataTypeName)
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy