Skip to content

Commit f99c692

Browse files
committed
Support VECTOR for MariaDB 11.7.
Signed-off-by: Bradley Grainger <[email protected]>
1 parent 4ff2620 commit f99c692

File tree

7 files changed

+44
-15
lines changed

7 files changed

+44
-15
lines changed

azure-pipelines.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ jobs:
192192
image: 'mariadb:11.4'
193193
connectionStringExtra: ''
194194
unsupportedFeatures: 'CachingSha2Password,CancelSleepSuccessfully,Json,RoundDateTime,QueryAttributes,Redirection,Sha256Password,Tls11,UuidToBin,Vector'
195-
'MariaDB 11.6':
196-
image: 'mariadb:11.6'
195+
'MariaDB 11.7':
196+
image: 'mariadb:11.7'
197197
connectionStringExtra: ''
198-
unsupportedFeatures: 'CachingSha2Password,CancelSleepSuccessfully,Json,RoundDateTime,QueryAttributes,Redirection,Sha256Password,Tls11,UuidToBin,Vector'
198+
unsupportedFeatures: 'CachingSha2Password,CancelSleepSuccessfully,Json,RoundDateTime,QueryAttributes,Redirection,Sha256Password,Tls11,UuidToBin,VectorType'
199199
steps:
200200
- template: '.ci/integration-tests-steps.yml'
201201
parameters:

src/MySqlConnector/Core/SingleCommandPayloadCreator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ private static void WriteBinaryParameters(ByteBufferWriter writer, MySqlParamete
165165
mySqlDbType = TypeMapper.Instance.GetMySqlDbTypeForDbType(dbType);
166166
}
167167

168+
// HACK: MariaDB doesn't have a dedicated Vector type so mark it as binary data
169+
if (mySqlDbType == MySqlDbType.Vector && command.Connection!.Session.ServerVersion.IsMariaDb)
170+
mySqlDbType = MySqlDbType.LongBlob;
171+
168172
writer.Write(TypeMapper.ConvertToColumnTypeAndFlags(mySqlDbType, command.Connection!.GuidFormat));
169173

170174
if (supportsQueryAttributes)

tests/IntegrationTests/CharacterSetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void CollationConnection(bool reopenConnection)
7979

8080
var collation = connection.Query<string>(@"select @@collation_connection;").Single();
8181
var expected = connection.ServerVersion.Substring(0, 2) is "8." or "9." ? "utf8mb4_0900_ai_ci" :
82-
connection.ServerVersion.StartsWith("11.4.", StringComparison.Ordinal) || connection.ServerVersion.StartsWith("11.6.", StringComparison.Ordinal) ? "utf8mb4_uca1400_ai_ci" :
82+
connection.ServerVersion.StartsWith("11.4.", StringComparison.Ordinal) || connection.ServerVersion.StartsWith("11.7.", StringComparison.Ordinal) ? "utf8mb4_uca1400_ai_ci" :
8383
"utf8mb4_general_ci";
8484
Assert.Equal(expected, collation);
8585
}

tests/IntegrationTests/DataTypes.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,15 @@ private void DoGetSchemaTable(string column, string table, MySqlDbType mySqlDbTy
12051205
if (table == "datatypes_vector" && !AppConfig.SupportedFeatures.HasFlag(ServerFeatures.Vector))
12061206
return;
12071207

1208+
// adjust for databases that don't have a dedicated on-the-wire type for VECTOR(n)
1209+
if (mySqlDbType == MySqlDbType.Vector && !AppConfig.SupportedFeatures.HasFlag(ServerFeatures.VectorType))
1210+
{
1211+
mySqlDbType = MySqlDbType.VarBinary;
1212+
columnSize *= 4;
1213+
dataType = typeof(byte[]);
1214+
scale = 0;
1215+
}
1216+
12081217
var isAutoIncrement = flags.IndexOf('A') != -1;
12091218
var isKey = flags.IndexOf('K') != -1;
12101219
var isLong = flags.IndexOf('L') != -1;
@@ -1609,26 +1618,32 @@ public void QueryJson(string column, string[] expected)
16091618
}
16101619

16111620
[SkippableTheory(ServerFeatures.Vector)]
1612-
[InlineData("value", new[] { null, "0,0,0", "1,1,1", "1,2,3", "3.40282347E+38,3.40282347E+38,3.40282347E+38" })]
1621+
[InlineData("value", new[] { null, "0,0,0", "1,1,1", "1,2,3", "-1,-1,-1" })]
16131622
public void QueryVector(string column, string[] expected)
16141623
{
1615-
string dataTypeName = "VECTOR";
1624+
var hasVectorType = AppConfig.SupportedFeatures.HasFlag(ServerFeatures.VectorType);
1625+
string dataTypeName = hasVectorType ? "VECTOR" : "BLOB";
16161626
DoQuery("vector", column, dataTypeName,
16171627
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())
1628+
#if !MYSQL_DATA
1629+
hasVectorType ? (object) GetFloatArray(x) : GetByteArray(x))
16211630
#else
1622-
x?.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray())
1631+
// Connector/NET returns the float array as a byte[]
1632+
GetByteArray(x))
16231633
#endif
16241634
.ToArray(),
16251635
#if !MYSQL_DATA
1626-
static x => (float[]) x.GetValue(0),
1636+
x => hasVectorType ? (float[]) x.GetValue(0) : (byte[]) x.GetValue(0),
16271637
#else
16281638
// NOTE: Connector/NET returns 'null' for NULL so simulate an exception for the tests
16291639
x => x.IsDBNull(0) ? throw new GetValueWhenNullException() : x.GetValue(0),
16301640
#endif
16311641
omitWhereTest: true);
1642+
1643+
static float[] GetFloatArray(string value) => value?.Split(',').Select(x => float.Parse(x, CultureInfo.InvariantCulture)).ToArray();
1644+
1645+
static byte[] GetByteArray(string value) =>
1646+
GetFloatArray(value) is { } floats ? MemoryMarshal.AsBytes<float>(floats).ToArray() : null;
16321647
}
16331648

16341649
[SkippableTheory(MySqlData = "https://bugs.mysql.com/bug.php?id=97067")]

tests/IntegrationTests/DataTypesFixture.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ insert into datatypes_json_core (value)
245245

246246
if (AppConfig.SupportedFeatures.HasFlag(ServerFeatures.Vector))
247247
{
248+
// create a helper function for MariaDB 11.7+
249+
if (Connection.ServerVersion.StartsWith("11.7.", StringComparison.Ordinal))
250+
Connection.Execute("create function if not exists STRING_TO_VECTOR(s text) returns vector(3) deterministic return Vec_FromText(s);");
251+
248252
Connection.Execute("""
249253
drop table if exists datatypes_vector;
250254
create table datatypes_vector (
@@ -257,7 +261,7 @@ insert into datatypes_vector (value)
257261
(STRING_TO_VECTOR('[0, 0, 0]')),
258262
(STRING_TO_VECTOR('[1, 1, 1]')),
259263
(STRING_TO_VECTOR('[1, 2, 3]')),
260-
(STRING_TO_VECTOR('[3.40282347E+38, 3.40282347E+38, 3.40282347E+38]'));
264+
(STRING_TO_VECTOR('[-1, -1, -1]'));
261265
""");
262266
}
263267
Connection.Close();

tests/IntegrationTests/QueryTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,7 @@ public void QueryVector(bool prepare)
17001700

17011701
connection.Execute("""
17021702
drop table if exists test_vector;
1703-
create table test_vector(id int auto_increment not null primary key, vec vector not null);
1703+
create table test_vector(id int auto_increment not null primary key, vec vector(3) not null);
17041704
""");
17051705

17061706
using var cmd = m_database.Connection.CreateCommand();
@@ -1735,7 +1735,8 @@ public void QueryVector(bool prepare)
17351735
#if MYSQL_DATA
17361736
var result = MemoryMarshal.Cast<byte, float>((byte[]) value).ToArray();
17371737
#else
1738-
var result = (float[]) value;
1738+
var result = AppConfig.SupportedFeatures.HasFlag(ServerFeatures.VectorType) ? (float[]) value :
1739+
MemoryMarshal.Cast<byte, float>((byte[]) value).ToArray();
17391740
#endif
17401741
Assert.Equal(floatArray, result);
17411742
}

tests/IntegrationTests/ServerFeatures.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ public enum ServerFeatures
4747
TlsFingerprintValidation = 0x100_0000,
4848

4949
/// <summary>
50-
/// Server supports the <c>VECTOR</c> data type.
50+
/// Server supports the <c>VECTOR</c> SQL type.
5151
/// </summary>
5252
Vector = 0x200_0000,
53+
54+
/// <summary>
55+
/// Server has a dedicated type on the wire for <c>VECTOR</c>.
56+
/// </summary>
57+
VectorType = 0x400_0000,
5358
}

0 commit comments

Comments
 (0)