ByteArrayToFloatArrayPropertyHandler

A property handler that maps a binary-encoded vector column, such as the MySQL VECTOR type, into a float array property.

A property handler that maps a binary-encoded vector column (for example, the MySQL VECTOR type, which is transferred as little-endian IEEE 754 single-precision values) into a float array property.

Overview

Name Description
Class ByteArrayToFloatArrayPropertyHandler
Namespace RepoDb.PropertyHandlers
Package RepoDb
Implements IPropertyHandler<byte[], float[]>
Column type A binary-encoded vector column (for example, the MySQL VECTOR type)
Property type float[]

Conversions

Method Direction Description
Get Database to property Converts the binary vector value into a float[], reading 4 bytes per element as little-endian IEEE 754 single-precision values. A null value is returned as null.
Set Property to database Converts the float[] into the binary vector value, writing 4 bytes per element in little-endian order. A null array is written as null.

Exceptions

Exception Thrown When
ArgumentException The length of the value returned by the database is not a multiple of 4 bytes.

Usability

Bind the handler to the property that maps to a VECTOR column.

Attribute

public class Embedding
{
    public int Id { get; set; }

    [Map("vector"), PropertyHandler(typeof(ByteArrayToFloatArrayPropertyHandler))]
    public float[] Vector { get; set; }
}

Once bound, the library invokes the handler automatically when the property is read or written.

using (var connection = new MySqlConnection(connectionString))
{
    connection.Insert(new Embedding { Id = 1, Vector = new float[] { 0.1f, 0.2f, 0.3f } });
    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<ByteArrayToFloatArrayPropertyHandler>(e => e.Vector);

Property Level Mapping

To configure via PropertyHandlerMapper:

PropertyHandlerMapper.Add<Embedding, ByteArrayToFloatArrayPropertyHandler>(e => e.Vector, new ByteArrayToFloatArrayPropertyHandler(), true);

Type Level Mapping

To apply the handler to every property of the type float[]:

PropertyHandlerMapper.Add<float[], ByteArrayToFloatArrayPropertyHandler>(new ByteArrayToFloatArrayPropertyHandler(), 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 every float[] property maps to a binary-encoded vector column.

See Also