📢 Proposal: Introducing SqlType SqlVectorFloat32
for SQL Server's New VECTOR Data Type
#3382
Replies: 3 comments 7 replies
-
@roji Your inputs would be helpful. We would like to understand if there are any concerns from EF side in consuming the proposed API. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the discussion - I've been looking forward to seeing how vector support pans out in SqlClient. I can't speak much about the EF side, but have a few thoughts. Top-level type definitionI'd suggest sealing the class by default. We can always unseal it later, and it means we avoid virtual method invocations. I'm not sure what the definition of ISqlVector is - I imagine it's internal. Do you have any opinions on implementing Round-trippingSQL Server returns a vector as JSON to an unenlightened client, and I assume this is what ToString returns. Since that's a canonical format, it feels natural to have Parse and TryParse methods, and for the return values of Parse and ToString to be able to round-trip: public SqlVectorFloat32 Parse(string? s);
public SqlVectorFloat32 Parse(ReadOnlySpan<char> s);
public bool TryParse(string? s, out SqlVectorFloat32? result);
public bool TryParse(ReadOnlySpan<char> s, out SqlVectorFloat32? result); On a slight tangent: I assume that vector probably uses the invariant culture when parsing and outputting strings - is that correct, or would a SQL Server with a different decimal separator use that separator? Additional uses
Is vector supported in user-defined table types? If so, it'll also need support in public virtual SqlVectorFloat32 GetSqlVectorFloat32(int ordinal);
public virtual void SetSqlVectorFloat32(int ordinal, SqlVectorFloat32 value); Interoperability.NET already has a The copy constructor accepts a public SqlVectorFloat32(params ReadOnlySpan<float> values);
public ReadOnlySpan<float> AsSpan(); Collection expressionsAlthough this is purely cosmetic, we could potentially use a collection expression to build the type, as below. This needs the type to implement IEnumerable. [CollectionBuilder(typeof(SqlVectorFloat32), nameof(FromSpan))]
public class SqlVectorFloat32 : INullable, IEnumerable<float>
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static SqlVectorFloat32 FromSpan(ReadOnlySpan<float> values) => new SqlVectorFloat32(values);
}
SqlVectorFloat32 vectorValue = [0.03f, 0.1f, 0.8f];
SqlDataRecord record = new(/*...*/);
record.SetSqlVectorFloat32(0, [0.03f, 0.1f, 0.8f]); |
Beta Was this translation helpful? Give feedback.
-
@roji @edwardneal Thank you for your valuable inputs. Really appreciate it! We should be able to incorporate some of your suggestions. I will discuss the feedback internally within team and update this discussion thread. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Status: Proposed
Related Documentation: VECTOR (Transact-SQL)
🎯 Summary
With SQL Server's new support for the
VECTOR
data type, we propose adding first-class support for interacting with it through a new public API:SqlVectorFloat32
. This type will be included in theMicrosoft.Data.SqlTypes
namespace in SqlClient.This proposal introduces:
SqlVectorFloat32
class that provides a managed representation of theVECTOR
SQL type.SqlParameter
,SqlDataReader
andSqlBulkCopy
.VECTOR
.💡 Motivation
The new
VECTOR
type in SQL Server enables machine learning, embedding, and AI scenarios by providing optimized storage for fixed-length float vectors. However, the vector format requires that evenNULL
values maintain metadata such as:Length
ElementType
Due to these requirements, there is no direct CLR equivalent for the SQL Server
VECTOR
type (i.e., nofloat[]
mapping). As such, we need to introduce a new structured type—SqlVectorFloat32
—to handle this correctly.✅ Proposed API Surface
🧪 Sample Usage of
SqlVectorFloat32
Below are sample code snippets that illustrate how to use
SqlVectorFloat32
with various ADO.NET operations.📥 Inserting Values into a Table
Insert non-null vector (explicit
SqlDbType
):Or using
Or using AddWithValue
Inserting Null values with preserved metadata:
Reading Vector Values From Sql Server Table With SqlDataReader.GetSqlVectorFloat32()
Or using DbDataReader.GetValue()
Using Vectors in StoredProcedure
Beta Was this translation helpful? Give feedback.
All reactions