SqlServerVectorToFloatArrayPropertyHandler
A property handler that maps the SQL Server vector type into a float array property.
A property handler that maps the SQL Server vector type into a float array property.
Reading requires a version of
Microsoft.Data.SqlClientthat returns thevectortype as aSqlVector<float>. A value that is returned in any other form is read asnull.
Overview
| Name | Description |
|---|---|
| Class | SqlServerVectorToFloatArrayPropertyHandler |
| Namespace | RepoDb.PropertyHandlers.SqlServer |
| Package | RepoDb.SqlServer |
| Implements | IPropertyHandler<object, float[]> |
| Column type | The SQL Server vector type |
| Property type | float[] |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Converts the SqlVector<float> returned by the driver into a float[]. Any other value, including a null vector, is returned as null. |
| Set | Property to database | Converts the float[] into its JSON array text (for example [1.5,2.5,3.5], using the invariant culture), which SQL Server converts implicitly when written into a vector column. A null array is written as null. |
Usability
Bind the handler to the property that maps to a vector(n) column.
Attribute
public class Embedding
{
public int Id { get; set; }
[Map("vector"), PropertyHandler(typeof(SqlServerVectorToFloatArrayPropertyHandler))]
public float[] Vector { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new SqlConnection(connectionString))
{
connection.Insert(new Embedding { Id = 1, Vector = new float[] { 1.5f, 2.5f, 3.5f } });
var embedding = connection.Query<Embedding>(e => e.Id == 1).First();
// embedding.Vector is a float[] with 3 elements
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Embedding>()
.PropertyHandler<SqlServerVectorToFloatArrayPropertyHandler>(e => e.Vector);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Embedding, SqlServerVectorToFloatArrayPropertyHandler>(e => e.Vector, new SqlServerVectorToFloatArrayPropertyHandler(), true);
Type Level Mapping
To apply the handler to every property of the type float[]:
PropertyHandlerMapper.Add<float[], SqlServerVectorToFloatArrayPropertyHandler>(new SqlServerVectorToFloatArrayPropertyHandler(), true);
A type level mapping is process-wide: it is applied to every
float[]property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless everyfloat[]property maps to avectorcolumn.