Tests | Refactor NativeVectorFloat32Tests for future Half support#4347
Tests | Refactor NativeVectorFloat32Tests for future Half support#4347edwardneal wants to merge 28 commits into
Conversation
Removed use of var, ensured that all variables are disposed of where trivial
|
/azp run |
|
Azure Pipelines successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Refactors the native SqlVector<T> manual test suite to use a generic test-data + shared test-logic base, so adding future element types (e.g., Half) becomes mostly a matter of adding a new *TestData + thin derived test class.
Changes:
- Adds
NativeVectorTestDataBase<TElement>to centralize per-element sample data and parameter-pattern cases. - Adds
NativeVectorTestsBase<TElement, TTestData>to host the shared insertion/read, stored-proc, bulk-copy, and prepare/execute test logic using RAII DB objects. - Refactors the float32 suite into
VectorFloat32TestData+NativeVectorFloat32Tests : NativeVectorTestsBase<...>.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/VectorTest/NativeVectorTestsBase.cs |
New generic base classes for vector manual tests + shared test logic and RAII-managed DB objects. |
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/VectorTest/NativeVectorFloat32Tests.cs |
Refactors float32 tests into new test-data class + thin derived test class using the new base. |
| int vectorDimensions = TestDataInstance.ValidSampleScalarDataLength; | ||
| string tableDefinition = $@"(Id INT PRIMARY KEY IDENTITY, {VectorColumnName} vector({vectorDimensions}, {TestDataInstance.SqlServerTypeName}) NULL)"; | ||
|
|
||
| _connectionString = DataTestUtility.TCPConnectionString; | ||
| _managementConnection = new SqlConnection(_connectionString); | ||
| _vectorTable = new Table(_managementConnection, "VectorTestTable", tableDefinition); | ||
| _bulkCopySourceTable = new Table(_managementConnection, "VectorBulkCopyTestTable", tableDefinition); | ||
| _vectorProcedure = new StoredProcedure(_managementConnection, | ||
| prefix: "VectorsAsVarcharSp", | ||
| definition: $@" | ||
| {VectorParameterName} vector({vectorDimensions}, {TestDataInstance.SqlServerTypeName}), -- Input: Serialized TElement[] as JSON string | ||
| {VectorOutputParameterName} vector({vectorDimensions}, {TestDataInstance.SqlServerTypeName}) OUTPUT -- Output: Echoed back from latest inserted row | ||
| AS |
There was a problem hiding this comment.
The default vector base type is float32, so vector(N, float32) is a more explicit name for vector(N). We explicitly specify the base type rather than implementing a special case.
| public override bool IsSupported => DataTestUtility.IsSqlVectorSupported; | ||
|
|
||
| // Verify GetFieldValue<SqlVector<float>> returns the correct typed value | ||
| SqlVector<float> typedValue = reader.GetFieldValue<SqlVector<float>>(0); | ||
| Assert.IsType<SqlVector<float>>(typedValue); | ||
| Assert.Equal(VectorFloat32TestData.testData, typedValue.Memory.ToArray()); | ||
| } | ||
| public override string SqlServerTypeName => "float32"; | ||
| } |
There was a problem hiding this comment.
The default vector base type is float32, so vector(N, float32) is a more explicit name for vector(N). We explicitly specify the base type rather than implementing a special case.
| case 1: | ||
| bulkCopy.WriteToServer(reader); | ||
| break; | ||
| case 2: | ||
| bulkCopy.WriteToServer(table); | ||
| break; |
| case 1: | ||
| await bulkCopy.WriteToServerAsync(reader); | ||
| break; | ||
| case 2: | ||
| await bulkCopy.WriteToServerAsync(table); | ||
| break; |
Description
This is somewhat inspired by #4234. We don't yet have support for
SqlVector<Half>and other types, but this should hopefully make the test logic trivial to add.I'd recommend moving commit by commit - the introduction of a base class has resulted in the PR looking much larger than it truly is.
The overall design is now more generic. We have:
NativeVectorTestDataBase<TElement>, which contains test data forSqlVector<TElement>NativeVectorTestsBase<TElement, TTestData>, which contains the test logic and links it to the test dataVectorFloat32TestData: derived from NativeVectorTestDataBase which containsfloat-based sample dataNativeVectorFloat32Tests: the same tests and the same logic as in main, but derived from NativeVectorTestsBaseAdding future support for
Halfshould mean simply introducingVectorFloat16TestDataandNativeVectorFloat16Testsclasses:I've also made a handful of hygiene improvements, but none are particularly controversial:
Assert.Fail, performingAssert.True(!verifyReader.IsDBNull(0), ...), etc.)Issues
In lieu of any more specific issue for Half/float16 support for SqlVector, this contributes to #3444.
Testing
All automated tests continue to pass.