Skip to content

Commit 4ff2620

Browse files
committed
Add more tests and test with MySql.Data.
MySql.Data 8.4.0 already supports the VECTOR data type. Signed-off-by: Bradley Grainger <[email protected]>
1 parent 6f6210e commit 4ff2620

File tree

3 files changed

+110
-5
lines changed

3 files changed

+110
-5
lines changed

tests/IntegrationTests/DataTypes.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Globalization;
2+
using System.Runtime.InteropServices;
3+
24
#if MYSQL_DATA
35
using MySql.Data.Types;
46
#endif
@@ -1143,7 +1145,9 @@ private static object CreateGeometry(byte[] data)
11431145
[InlineData("Int64", "datatypes_integers", MySqlDbType.Int64, 20, typeof(long), "N", 0, 0)]
11441146
[InlineData("UInt64", "datatypes_integers", MySqlDbType.UInt64, 20, typeof(ulong), "N", 0, 0)]
11451147
[InlineData("value", "datatypes_json_core", MySqlDbType.JSON, int.MaxValue, typeof(string), "LN", 0, 0)]
1146-
#if !MYSQL_DATA
1148+
#if MYSQL_DATA
1149+
[InlineData("value", "datatypes_vector", MySqlDbType.Vector, 12, typeof(byte[]), "N", 0, 31)]
1150+
#else
11471151
[InlineData("value", "datatypes_vector", MySqlDbType.Vector, 3, typeof(float[]), "N", 0, 31)]
11481152
#endif
11491153
[InlineData("Single", "datatypes_reals", MySqlDbType.Float, 12, typeof(float), "N", 0, 31)]
@@ -1604,17 +1608,28 @@ public void QueryJson(string column, string[] expected)
16041608
DoQuery("json_core", column, dataTypeName, expected, reader => reader.GetString(0), omitWhereTest: true);
16051609
}
16061610

1607-
#if !MYSQL_DATA
16081611
[SkippableTheory(ServerFeatures.Vector)]
16091612
[InlineData("value", new[] { null, "0,0,0", "1,1,1", "1,2,3", "3.40282347E+38,3.40282347E+38,3.40282347E+38" })]
16101613
public void QueryVector(string column, string[] expected)
16111614
{
16121615
string dataTypeName = "VECTOR";
16131616
DoQuery("vector", column, dataTypeName,
1614-
expected.Select(x => x?.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray()).ToArray(),
1615-
static x => (float[]) x.GetValue(0), omitWhereTest: true);
1616-
}
1617+
expected.Select(x =>
1618+
#if MYSQL_DATA
1619+
// Connector/NET returns the float array as a byte[]
1620+
x is null ? null : MemoryMarshal.AsBytes<float>(x.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray()).ToArray())
1621+
#else
1622+
x?.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray())
1623+
#endif
1624+
.ToArray(),
1625+
#if !MYSQL_DATA
1626+
static x => (float[]) x.GetValue(0),
1627+
#else
1628+
// NOTE: Connector/NET returns 'null' for NULL so simulate an exception for the tests
1629+
x => x.IsDBNull(0) ? throw new GetValueWhenNullException() : x.GetValue(0),
16171630
#endif
1631+
omitWhereTest: true);
1632+
}
16181633

16191634
[SkippableTheory(MySqlData = "https://bugs.mysql.com/bug.php?id=97067")]
16201635
[InlineData(false, "MIN", 0)]

tests/IntegrationTests/QueryTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Runtime.InteropServices;
2+
13
namespace IntegrationTests;
24

35
public class QueryTests : IClassFixture<DatabaseFixture>, IDisposable
@@ -1688,6 +1690,56 @@ public void GetBytesByName()
16881690
}
16891691
#endif
16901692

1693+
[SkippableTheory(ServerFeatures.Vector)]
1694+
[InlineData(false)]
1695+
[InlineData(true)]
1696+
public void QueryVector(bool prepare)
1697+
{
1698+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
1699+
connection.Open();
1700+
1701+
connection.Execute("""
1702+
drop table if exists test_vector;
1703+
create table test_vector(id int auto_increment not null primary key, vec vector not null);
1704+
""");
1705+
1706+
using var cmd = m_database.Connection.CreateCommand();
1707+
cmd.CommandText = "INSERT INTO test_vector(vec) VALUES(@vec)";
1708+
cmd.Parameters.Add(new MySqlParameter
1709+
{
1710+
ParameterName = "@vec",
1711+
MySqlDbType = MySqlDbType.Vector,
1712+
});
1713+
1714+
var floatArray = new[] { 1.2f, 3.4f, 5.6f };
1715+
#if MYSQL_DATA
1716+
// Connector/NET requires the float vector to be passed as a byte array
1717+
cmd.Parameters[0].Value = MemoryMarshal.AsBytes<float>(floatArray).ToArray();
1718+
#else
1719+
cmd.Parameters[0].Value = floatArray;
1720+
#endif
1721+
1722+
if (prepare)
1723+
cmd.Prepare();
1724+
cmd.ExecuteNonQuery();
1725+
1726+
// Select and verify the value
1727+
cmd.CommandText = "SELECT vec FROM test_vector";
1728+
if (prepare)
1729+
cmd.Prepare();
1730+
1731+
using var reader = cmd.ExecuteReader();
1732+
Assert.True(reader.Read());
1733+
var value = reader.GetValue(0);
1734+
1735+
#if MYSQL_DATA
1736+
var result = MemoryMarshal.Cast<byte, float>((byte[]) value).ToArray();
1737+
#else
1738+
var result = (float[]) value;
1739+
#endif
1740+
Assert.Equal(floatArray, result);
1741+
}
1742+
16911743
private class BoolTest
16921744
{
16931745
public int Id { get; set; }

tests/IntegrationTests/StoredProcedureTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,44 @@ public void PassJsonParameter()
761761
Assert.False(reader.Read());
762762
}
763763

764+
#if false
765+
[SkippableTheory(ServerFeatures.Vector)]
766+
[InlineData(false)]
767+
[InlineData(true)]
768+
public void VectorOutputParameter(bool prepare)
769+
{
770+
using var cmd = m_database.Connection.CreateCommand();
771+
cmd.CommandText = """
772+
DROP PROCEDURE IF EXISTS sp_vector_out;
773+
CREATE PROCEDURE sp_vector_out (OUT vec VECTOR)
774+
BEGIN
775+
SELECT STRING_TO_VECTOR('[1.2, 3.4, 5.6]') INTO vec;
776+
END;
777+
""";
778+
cmd.ExecuteNonQuery();
779+
780+
cmd.CommandText = "sp_vector_out";
781+
cmd.CommandType = CommandType.StoredProcedure;
782+
cmd.Parameters.Add(new MySqlParameter
783+
{
784+
ParameterName = "@vec",
785+
MySqlDbType = MySqlDbType.Vector,
786+
Direction = ParameterDirection.Output,
787+
});
788+
789+
if (prepare)
790+
cmd.Prepare();
791+
cmd.ExecuteNonQuery();
792+
793+
var value = cmd.Parameters[0].Value;
794+
Assert.IsType<float[]>(value);
795+
796+
var result = (float[]) value;
797+
798+
Assert.Equal(new float[] { 1.2f, 3.4f, 5.6f }, result);
799+
}
800+
#endif
801+
764802
private static Action<MySqlParameter> AssertParameter(string name, ParameterDirection direction, MySqlDbType mySqlDbType)
765803
{
766804
return x =>

0 commit comments

Comments
 (0)