Skip to content

Commit 8d7409a

Browse files
authored
Correcting display of Vector dimension in data type label, adding test (#2483)
* Correcting display of Vector dimension in data type label, adding test * fixing linter error
1 parent 04c8c5f commit 8d7409a

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/Microsoft.SqlTools.SqlCore/ObjectExplorer/SmoModel/SmoColumnCustomNode.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static string GetCustomizedLabel(Column column, UserDefinedDataTypeColle
156156
return string.Empty;
157157
}
158158

159-
private static string GetTypeSpecifierLabel(DataType dataType, UserDefinedDataTypeCollection uddts)
159+
internal static string GetTypeSpecifierLabel(DataType dataType, UserDefinedDataTypeCollection uddts)
160160
{
161161
string typeName = string.Empty;
162162
if (dataType != null)
@@ -195,7 +195,6 @@ private static string GetTypeSpecifierLabel(DataType dataType, UserDefinedDataTy
195195
case SqlDataType.VarChar:
196196
case SqlDataType.NVarChar:
197197
case SqlDataType.VarBinary:
198-
case SqlDataType.Vector:
199198
typeName += $"({dataType.MaximumLength})";
200199
break;
201200
case SqlDataType.Numeric:
@@ -212,6 +211,14 @@ private static string GetTypeSpecifierLabel(DataType dataType, UserDefinedDataTy
212211
case SqlDataType.VarCharMax:
213212
typeName += "(max)";
214213
break;
214+
case SqlDataType.Vector:
215+
// Temporary workaround to convert the maxLength to dimensions for vector types
216+
// until SMO is updated to store the actual dimensions of the vector type.
217+
// https://msdata.visualstudio.com/SQLToolsAndLibraries/_workitems/edit/3906463
218+
// dimensions = (length - 8) / 4
219+
// https://learn.microsoft.com/sql/t-sql/data-types/vector-data-type
220+
typeName += $"({(dataType.MaximumLength - 8) / 4})";
221+
break;
215222
}
216223
}
217224
return typeName;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using Microsoft.SqlServer.Management.Smo;
7+
using Microsoft.SqlTools.SqlCore.ObjectExplorer.SmoModel;
8+
using NUnit.Framework;
9+
10+
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.ObjectExplorer
11+
{
12+
public class SmoColumnCustomNodeHelperTests
13+
{
14+
[Test]
15+
public void ShouldCalculateTypeLabels()
16+
{
17+
var cases = new[]
18+
{
19+
// Numbers
20+
new { Type = DataType.Int, Label = "int" },
21+
new { Type = DataType.BigInt, Label = "bigint" },
22+
new { Type = DataType.SmallInt, Label = "smallint" },
23+
new { Type = DataType.TinyInt, Label = "tinyint" },
24+
new { Type = DataType.Bit, Label = "bit" },
25+
new { Type = DataType.Decimal(10, 2), Label = "decimal(2,10)" }, // precision then scale is standard, opposite order from the constructor
26+
new { Type = DataType.Float, Label = "float" },
27+
new { Type = DataType.Real, Label = "real" },
28+
new { Type = DataType.Money, Label = "money" },
29+
new { Type = DataType.SmallMoney, Label = "smallmoney" },
30+
31+
// Binary types
32+
new { Type = DataType.Binary(10), Label = "binary(10)" },
33+
new { Type = DataType.VarBinary(10), Label = "varbinary(10)" },
34+
new { Type = DataType.VarBinaryMax, Label = "varbinary(max)" },
35+
36+
// String types
37+
new { Type = DataType.Char(10), Label = "char(10)" },
38+
new { Type = DataType.NChar(10), Label = "nchar(10)" },
39+
new { Type = DataType.NVarChar(10), Label = "nvarchar(10)" },
40+
new { Type = DataType.NVarCharMax, Label = "nvarchar(max)" },
41+
new { Type = DataType.VarChar(20), Label = "varchar(20)" },
42+
new { Type = DataType.VarCharMax, Label = "varchar(max)" },
43+
44+
// Date Types
45+
new { Type = DataType.DateTime, Label = "datetime" },
46+
new { Type = DataType.Date, Label = "date" },
47+
new { Type = DataType.DateTime2(7), Label = "datetime2(7)" },
48+
new { Type = DataType.DateTimeOffset(5), Label = "datetimeoffset(5)" },
49+
new { Type = DataType.Time(3), Label = "time(3)" },
50+
51+
// Specialty types
52+
new { Type = DataType.Vector(17), Label = "vector(17)" },
53+
};
54+
55+
foreach (var testCase in cases)
56+
{
57+
string label = SmoColumnCustomNodeHelper.GetTypeSpecifierLabel(testCase.Type, uddts: null);
58+
Assert.That(label, Is.EqualTo(testCase.Label), $"Expected label to be {testCase.Label}");
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)