Skip to content

Commit 5cb5245

Browse files
Change API for vector datatype from SqlVectorFloat32 to SqlVector<T> for supporting SqlDbType Vector (#3441)
This commit changes APIs for vector datatype. from SqlType class SqlVectorFloat32 to SqlVector<T> to reduce API surface area for unmanaged types. All the tests have been modified to use SqlVector<T>.
1 parent c576041 commit 5cb5245

32 files changed

+2481
-26
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ No conversions are performed; therefore. the data retrieved must already be a ch
415415
<item><description>SqlMoney</description></item>
416416
<item><description>SqlSingle</description></item>
417417
<item><description>SqlString</description></item>
418+
<item><description>SqlVector</description></item>
418419
<item><description>Stream</description></item>
419420
<item><description>String</description></item>
420421
<item><description>TextReader</description></item>
@@ -489,6 +490,7 @@ No conversions are performed; therefore. the data retrieved must already be a ch
489490
<item><description>SqlMoney</description></item>
490491
<item><description>SqlSingle</description></item>
491492
<item><description>SqlString</description></item>
493+
<item><description>SqlVector</description></item>
492494
<item><description>Stream</description></item>
493495
<item><description>String</description></item>
494496
<item><description>TextReader</description></item>
@@ -958,7 +960,31 @@ The <xref:Microsoft.Data.SqlClient.SqlDataReader.GetSchemaTable%2A> method retur
958960
<param name="i"></param>
959961
<summary>Gets the value of the specified column as a <see cref="T:Microsoft.Data.SqlTypes.SqlJson"/>.</summary>
960962
<returns>A <see cref="T:Microsoft.Data.SqlTypes.SqlJson"/> object representing the column at the given ordinal.</returns>
963+
<remarks>
964+
No conversions are performed; therefore, the data retrieved must already be a JSON string, or an exception is generated.
965+
</remarks>
961966
</GetSqlJson>
967+
<GetSqlVector>
968+
<param name="i"></param>
969+
<summary>
970+
Gets the value of the specified column as a <see cref="T:Microsoft.Data.SqlTypes.SqlVector"/>.
971+
</summary>
972+
<returns>
973+
A <see cref="T:Microsoft.Data.SqlTypes.SqlVector"/> object representing the column at the given ordinal.
974+
</returns>
975+
<exception cref="T:System.ArgumentOutOfRangeException">
976+
The index passed was outside the range of 0 to <see cref="P:System.Data.DataTableReader.FieldCount" /> - 1
977+
</exception>
978+
<exception cref="T:System.InvalidOperationException">
979+
An attempt was made to read or access columns in a closed <see cref="T:Microsoft.Data.SqlClient.SqlDataReader" />.
980+
</exception>
981+
<exception cref="T:System.InvalidCastException">
982+
The retrieved data is not compatible with the <see cref="T:Microsoft.Data.SqlTypes.SqlVector" /> type.
983+
</exception>
984+
<remarks>
985+
No conversions are performed; therefore, the data retrieved must already be a vector value, or an exception is generated.
986+
</remarks>
987+
</GetSqlVector>
962988
<GetSqlMoney>
963989
<param name="i">
964990
The zero-based column ordinal.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0"?>
2+
<docs>
3+
<members name="SqlVector">
4+
<SqlVector>
5+
<summary>Represents a vector value in SQL Server.</summary>
6+
</SqlVector>
7+
<ctor1>
8+
<param name="length"></param>
9+
<summary>
10+
Takes a <see cref="int"/> as input and represents a null vector value.
11+
</summary>
12+
</ctor1>
13+
<ctor2>
14+
<param name="memory"></param>
15+
<summary>
16+
Takes an array of values as input and initializes a new instance of the SqlVector class.
17+
</summary>
18+
</ctor2>
19+
<IsNull>
20+
<inheritdoc/>
21+
</IsNull>
22+
<Null>
23+
<summary>
24+
Represents a null instance without any attributes.
25+
</summary>
26+
<remarks>
27+
This property is provided for compatibility with DataTable.
28+
In most cases, prefer using <c>IsNull</c> to check if a SqlVector instance is a null vector.
29+
This is equivalent to <c>null</c> value.
30+
</remarks>
31+
</Null>
32+
<Length>
33+
<summary>
34+
Indicates the count of the elements in the vector.
35+
</summary>
36+
</Length>
37+
<Size>
38+
<summary>
39+
Indicates the size in bytes for a SqlVector value.
40+
</summary>
41+
</Size>
42+
<Memory>
43+
<summary>Returns the values as a <see cref="T:System.ReadOnlyMemory"/> instance.</summary>
44+
</Memory>
45+
<ToArray>
46+
<summary>Returns an array representing the vector values.</summary>
47+
</ToArray>
48+
<ToString>
49+
<summary>Returns the string representation of a JSON array of the values.</summary>
50+
<returns>A JSON <see cref="T:System.String"/> value.</returns>
51+
</ToString>
52+
</members>
53+
</docs>

doc/snippets/Microsoft.Data/SqlDbTypeExtensions.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,13 @@ The <xref:Microsoft.Data.SqlDbTypeExtensions> class provides <xref:System.Data.S
1818
<see cref="T:System.Data.SqlDbType" /> enum value for JSON datatype.
1919
</returns>
2020
</SqlJson>
21+
<SqlVector name="default">
22+
<summary>
23+
Gets the <see cref="T:System.Data.SqlDbType" /> enum value for the vector datatype.
24+
</summary>
25+
<returns>
26+
<see cref="T:System.Data.SqlDbType" /> enum value for vector datatype.
27+
</returns>
28+
</SqlVector>
2129
</members>
2230
</docs>

src/Microsoft.Data.SqlClient.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlTypes", "
172172
ProjectSection(SolutionItems) = preProject
173173
..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml
174174
..\doc\snippets\Microsoft.Data.SqlTypes\SqlJson.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlJson.xml
175+
..\doc\snippets\Microsoft.Data.SqlTypes\SqlVector.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlVector.xml
175176
EndProjectSection
176177
EndProject
177178
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Data.SqlClient.TestUtilities", "Microsoft.Data.SqlClient\tests\tools\Microsoft.Data.SqlClient.TestUtilities\Microsoft.Data.SqlClient.TestUtilities.csproj", "{89D6D382-9B36-43C9-A912-03802FDA8E36}"

src/Microsoft.Data.SqlClient/netcore/ref/Microsoft.Data.SqlClient.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public static class SqlDbTypeExtensions
1919
{
2020
/// <include file='../../../../doc/snippets/Microsoft.Data/SqlDbTypeExtensions.xml' path='docs/members[@name="SqlDbTypeExtensions"]/SqlJson[@name="default"]' />
2121
public const System.Data.SqlDbType Json = (System.Data.SqlDbType)35;
22+
/// <include file='../../../../doc/snippets/Microsoft.Data/SqlDbTypeExtensions.xml' path='docs/members[@name="SqlDbTypeExtensions"]/SqlVector[@name="default"]' />
23+
public const System.Data.SqlDbType Vector = (System.Data.SqlDbType)36;
2224
}
2325
}
2426

@@ -119,6 +121,30 @@ public SqlJson(System.Text.Json.JsonDocument jsonDoc) { }
119121
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlJson.xml' path='docs/members[@name="SqlJson"]/ToString/*' />
120122
public override string ToString() { throw null; }
121123
}
124+
125+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/SqlVector/*' />
126+
public sealed class SqlVector<T> : System.Data.SqlTypes.INullable
127+
where T : unmanaged
128+
{
129+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/ctor1/*' />
130+
public SqlVector(int length) { }
131+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/ctor2/*' />
132+
public SqlVector(System.ReadOnlyMemory<T> memory) { }
133+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/IsNull/*' />
134+
public bool IsNull => throw null;
135+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/Null/*' />
136+
public static SqlVector<T> Null => throw null;
137+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/Length/*' />
138+
public int Length { get { throw null; } }
139+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/Size/*' />
140+
public int Size { get { throw null; } }
141+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/Memory/*' />
142+
public System.ReadOnlyMemory<T> Memory { get { throw null; } }
143+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/ToString/*' />
144+
public override string ToString() { throw null; }
145+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlTypes/SqlVector.xml' path='docs/members[@name="SqlVector"]/ToArray/*' />
146+
public T[] ToArray() { throw null; }
147+
}
122148
}
123149
namespace Microsoft.Data.SqlClient
124150
{
@@ -1370,6 +1396,8 @@ public override void Close() { }
13701396
public virtual object GetSqlValue(int i) { throw null; }
13711397
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlValues/*'/>
13721398
public virtual int GetSqlValues(object[] values) { throw null; }
1399+
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlVector/*'/>
1400+
public virtual Microsoft.Data.SqlTypes.SqlVector<T> GetSqlVector<T>(int i) where T : unmanaged { throw null; }
13731401
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlXml/*'/>
13741402
public virtual System.Data.SqlTypes.SqlXml GetSqlXml(int i) { throw null; }
13751403
/// <include file='../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSchemaTable/*'/>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@
471471
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs">
472472
<Link>Microsoft\Data\SqlClient\Server\ValueUtilsSmi.cs</Link>
473473
</Compile>
474+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\ISqlVector.cs">
475+
<Link>Microsoft\Data\SqlClient\ISqlVector.cs</Link>
476+
</Compile>
474477
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlClient\SignatureVerificationCache.cs">
475478
<Link>Microsoft\Data\SqlClient\SignatureVerificationCache.cs</Link>
476479
</Compile>
@@ -792,6 +795,9 @@
792795
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlJson.cs">
793796
<Link>Microsoft\Data\SqlTypes\SqlJson.cs</Link>
794797
</Compile>
798+
<Compile Include="$(CommonSourceRoot)Microsoft\Data\SqlTypes\SqlVector.cs">
799+
<Link>Microsoft\Data\SqlTypes\SqlVector.cs</Link>
800+
</Compile>
795801
<Compile Include="$(CommonSourceRoot)Resources\ResCategoryAttribute.cs">
796802
<Link>Resources\ResCategoryAttribute.cs</Link>
797803
</Compile>

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6435,6 +6435,16 @@ internal string BuildParamList(TdsParser parser, SqlParameterCollection paramete
64356435
paramList.Append(scale);
64366436
paramList.Append(')');
64376437
}
6438+
else if (mt.SqlDbType == SqlDbTypeExtensions.Vector)
6439+
{
6440+
// The validate function for SqlParameters would
6441+
// have already thrown InvalidCastException if an incompatible
6442+
// value is specified for SqlDbType Vector.
6443+
var sqlVectorProps = (ISqlVector)sqlParam.Value;
6444+
paramList.Append('(');
6445+
paramList.Append(sqlVectorProps.Length);
6446+
paramList.Append(')');
6447+
}
64386448
else if (!mt.IsFixed && !mt.IsLong && mt.SqlDbType != SqlDbType.Timestamp && mt.SqlDbType != SqlDbType.Udt && SqlDbType.Structured != mt.SqlDbType)
64396449
{
64406450
int size = sqlParam.Size;

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlInternalConnectionTds.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ private void Login(ServerInfo server, TimeoutTimer timeout, string newPassword,
14251425
requestedFeatures |= TdsEnums.FeatureExtension.AzureSQLSupport;
14261426
}
14271427

1428-
// The SQLDNSCaching and JSON features are implicitly set
1428+
// The following features are implicitly set
14291429
requestedFeatures |= TdsEnums.FeatureExtension.SQLDNSCaching;
14301430
requestedFeatures |= TdsEnums.FeatureExtension.JsonSupport;
14311431
requestedFeatures |= TdsEnums.FeatureExtension.VectorSupport;

0 commit comments

Comments
 (0)