Skip to content

Commit c106b0b

Browse files
committed
Use ReadOnlyMemory<float> as the type for VECTOR.
Based on these comments: dotnet/runtime#115148 (comment). Signed-off-by: Bradley Grainger <bgrainger@gmail.com>
1 parent c27edde commit c106b0b

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/MySqlConnector/ColumnReaders/VectorColumnReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ internal sealed class VectorColumnReader : ColumnReader
88
public static VectorColumnReader Instance { get; } = new();
99

1010
public override object ReadValue(ReadOnlySpan<byte> data, ColumnDefinitionPayload columnDefinition) =>
11-
MemoryMarshal.Cast<byte, float>(data).ToArray();
11+
new ReadOnlyMemory<float>(MemoryMarshal.Cast<byte, float>(data).ToArray());
1212
}

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ private TypeMapper()
5656
AddColumnTypeMetadata(new("FLOAT", typeFloat, MySqlDbType.Float));
5757

5858
// vector
59-
var typeFloatArray = AddDbTypeMapping(new(typeof(float[]), [DbType.Object]));
60-
AddColumnTypeMetadata(new("VECTOR", typeFloatArray, MySqlDbType.Vector, binary: true, simpleDataTypeName: "VECTOR", createFormat: "VECTOR({0})"));
59+
var typeFloatReadOnlyMemory = AddDbTypeMapping(new(typeof(ReadOnlyMemory<float>), [DbType.Object]));
60+
AddColumnTypeMetadata(new("VECTOR", typeFloatReadOnlyMemory, MySqlDbType.Vector, binary: true, simpleDataTypeName: "VECTOR", createFormat: "VECTOR({0})"));
6161

6262
// string
6363
var typeFixedString = AddDbTypeMapping(new(typeof(string), [DbType.StringFixedLength, DbType.AnsiStringFixedLength], convert: Convert.ToString!));

tests/IntegrationTests/DataTypes.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ private static object CreateGeometry(byte[] data)
11481148
#if MYSQL_DATA
11491149
[InlineData("value", "datatypes_vector", MySqlDbType.Vector, 12, typeof(byte[]), "N", 0, 31)]
11501150
#else
1151-
[InlineData("value", "datatypes_vector", MySqlDbType.Vector, 3, typeof(float[]), "N", 0, 31)]
1151+
[InlineData("value", "datatypes_vector", MySqlDbType.Vector, 3, typeof(ReadOnlyMemory<float>), "N", 0, 31)]
11521152
#endif
11531153
[InlineData("Single", "datatypes_reals", MySqlDbType.Float, 12, typeof(float), "N", 0, 31)]
11541154
[InlineData("Double", "datatypes_reals", MySqlDbType.Double, 22, typeof(double), "N", 0, 31)]
@@ -1626,18 +1626,26 @@ public void QueryVector(string column, string[] expected)
16261626
DoQuery("vector", column, dataTypeName,
16271627
expected.Select(x =>
16281628
#if !MYSQL_DATA
1629-
hasVectorType ? (object) GetFloatArray(x) : GetByteArray(x))
1629+
hasVectorType ? (GetFloatArray(x) is float[] a ? (object) new ReadOnlyMemory<float>(a) : null) : GetByteArray(x))
16301630
#else
16311631
// Connector/NET returns the float array as a byte[]
16321632
GetByteArray(x))
16331633
#endif
16341634
.ToArray(),
16351635
#if !MYSQL_DATA
1636-
x => hasVectorType ? (float[]) x.GetValue(0) : (byte[]) x.GetValue(0),
1636+
x => hasVectorType ? (ReadOnlyMemory<float>) x.GetValue(0) : (byte[]) x.GetValue(0),
16371637
#else
16381638
// NOTE: Connector/NET returns 'null' for NULL so simulate an exception for the tests
16391639
x => x.IsDBNull(0) ? throw new GetValueWhenNullException() : x.GetValue(0),
16401640
#endif
1641+
assertEqual: (l, r) =>
1642+
{
1643+
if (l is ReadOnlyMemory<float> roml)
1644+
l = roml.ToArray();
1645+
if (r is ReadOnlyMemory<float> romr)
1646+
r = romr.ToArray();
1647+
Assert.Equal(l, r);
1648+
},
16411649
omitWhereTest: true);
16421650

16431651
static float[] GetFloatArray(string value) => value?.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray();

tests/IntegrationTests/QueryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,7 +1735,7 @@ public void QueryVector(bool prepare)
17351735
#if MYSQL_DATA
17361736
var result = MemoryMarshal.Cast<byte, float>((byte[]) value).ToArray();
17371737
#else
1738-
var result = AppConfig.SupportedFeatures.HasFlag(ServerFeatures.VectorType) ? (float[]) value :
1738+
var result = AppConfig.SupportedFeatures.HasFlag(ServerFeatures.VectorType) ? (ReadOnlyMemory<float>) value :
17391739
MemoryMarshal.Cast<byte, float>((byte[]) value).ToArray();
17401740
#endif
17411741
Assert.Equal(floatArray, result);

0 commit comments

Comments
 (0)