Skip to content

Commit 8c73c0b

Browse files
committed
Add CHAR and VARCHAR length support
1 parent c9fa98f commit 8c73c0b

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

csharp/src/Drivers/Databricks/Result/DescTableExtendedResult.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public int? ColumnSize
150150
ColumnTypeId.SMALLINT => 2,
151151
ColumnTypeId.INTEGER or ColumnTypeId.FLOAT or ColumnTypeId.DATE => 4,
152152
ColumnTypeId.BIGINT or ColumnTypeId.DECIMAL or ColumnTypeId.DOUBLE or ColumnTypeId.TIMESTAMP or ColumnTypeId.TIMESTAMP_WITH_TIMEZONE => 8,
153+
ColumnTypeId.CHAR => Type.Length,
154+
ColumnTypeId.VARCHAR => Type.Name.Trim().ToUpper() == "STRING" ? null: Type.Length,
153155
_ => null
154156
};
155157
}
@@ -171,30 +173,60 @@ internal class ColumnType
171173
[JsonPropertyName("name")]
172174
public string Name { get; set; } = String.Empty;
173175

176+
/// <summary>
177+
/// Precision for DECIMAL type, only for DECIMAL and NUMERIC types
178+
/// </summary>
174179
[JsonPropertyName("precision")]
175180
public int? Precision { get; set; }
176181

182+
/// <summary>
183+
/// Scale for DECIMAL type, only for DECIMAL and NUMERIC types
184+
/// </summary>
177185
[JsonPropertyName("scale")]
178186
public int? Scale { get; set; }
179187

188+
/// <summary>
189+
/// Element type for ARRAY type, only for ARRAY type
190+
/// </summary>
180191
[JsonPropertyName("element_type")]
181192
public ColumnType? ElementType { get; set; }
182193

194+
/// <summary>
195+
/// Key and value types for MAP type, only for MAP type
196+
/// </summary>
183197
[JsonPropertyName("key_type")]
184198
public ColumnType? KeyType { get; set; }
185199

200+
/// <summary>
201+
/// Value type for MAP type, only for MAP type
202+
/// </summary>
186203
[JsonPropertyName("value_type")]
187204
public ColumnType? ValueType { get; set; }
188205

206+
/// <summary>
207+
/// Fields for STRUCT type, only for STRUCT type
208+
/// </summary>
189209
[JsonPropertyName("fields")]
190210
public List<ColumnInfo>? Fields { get; set; }
191211

212+
/// <summary>
213+
/// Interval start and end units, only for INTERVAL type
214+
/// </summary>
192215
[JsonPropertyName("start_unit")]
193216
public string? StartUnit { get; set; }
194217

218+
/// <summary>
219+
/// Interval start and end units, only for INTERVAL type
220+
/// </summary>
195221
[JsonPropertyName("end_unit")]
196222
public string? EndUnit { get; set; }
197223

224+
/// <summary>
225+
/// Length of characters, only for character types (CHAR, VARCHAR)
226+
/// </summary>
227+
[JsonPropertyName("length")]
228+
public int? Length {get; set; }
229+
198230
/// <summary>
199231
/// Get the full type name e.g. DECIMAL(10,2), map<string,int>
200232
/// </summary>
@@ -207,6 +239,7 @@ public string FullTypeName
207239

208240
return normalizedTypeName switch
209241
{
242+
"CHAR" or "VARCHAR" => $"{normalizedTypeName}({Length ?? 1})",
210243
"DECIMAL" or "NUMERIC" => Precision != null ? $"{normalizedTypeName}({Precision},{Scale ?? 0})" : normalizedTypeName,
211244
"ARRAY" => ElementType != null ? $"ARRAY<{ElementType.FullTypeName}>" : "ARRAY<>",
212245
"MAP" => (KeyType != null && ValueType != null) ? $"MAP<{KeyType!.FullTypeName},{ValueType!.FullTypeName}>":"Map<>",

csharp/test/Drivers/Databricks/Result/DescTableExtendedResultTest.cs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,29 @@ public void TestTableWithoutConstraints()
5252
""nullable"": false
5353
},
5454
{
55-
""name"": ""string_col"",
55+
""name"": ""string_col"",
5656
""type"": {
5757
""name"": ""string"",
5858
""collation"": ""UTF8_BINARY""
5959
},
6060
""nullable"": false
6161
},
62+
{
63+
""name"": ""varchar_col"",
64+
""type"": {
65+
""name"": ""varchar"",
66+
""length"": 20
67+
},
68+
""nullable"": false
69+
},
70+
{
71+
""name"": ""char_col"",
72+
""type"": {
73+
""name"": ""char"",
74+
""length"": 20
75+
},
76+
""nullable"": false
77+
},
6278
{
6379
""name"": ""array_col"",
6480
""type"": {
@@ -149,7 +165,7 @@ public void TestTableWithoutConstraints()
149165
Assert.Empty(result.PrimaryKeys);
150166
Assert.Empty(result.ForeignKeys);
151167

152-
Assert.Equal(7, result.Columns.Count);
168+
Assert.Equal(9, result.Columns.Count);
153169

154170
var column = result.Columns.Find(c => c.Name == "big_number");
155171
Assert.NotNull(column);
@@ -171,6 +187,18 @@ public void TestTableWithoutConstraints()
171187
Assert.Equal("string", column.Type.Name);
172188
Assert.Equal("STRING", column.Type.FullTypeName);
173189

190+
column = result.Columns.Find(c => c.Name == "varchar_col");
191+
Assert.NotNull(column);
192+
Assert.Equal("varchar", column.Type.Name);
193+
Assert.Equal(20, column.Type.Length);
194+
Assert.Equal("VARCHAR(20)", column.Type.FullTypeName);
195+
196+
column = result.Columns.Find(c => c.Name == "char_col");
197+
Assert.NotNull(column);
198+
Assert.Equal("char", column.Type.Name);
199+
Assert.Equal(20, column.Type.Length);
200+
Assert.Equal("CHAR(20)", column.Type.FullTypeName);
201+
174202
column = result.Columns.Find(c => c.Name == "array_col");
175203
Assert.NotNull(column);
176204
Assert.Equal("array", column.Type.Name);
@@ -309,8 +337,8 @@ public void TestTableWithConstraints(string constaints, string[] primaryKeys, st
309337
[InlineData("REAL", ColumnTypeId.FLOAT, true, 4)]
310338
[InlineData("DOUBLE", ColumnTypeId.DOUBLE, true, 8)]
311339
[InlineData("DECIMAL", ColumnTypeId.DECIMAL, true, 8)]
312-
[InlineData("CHAR", ColumnTypeId.CHAR, false, null)]
313-
[InlineData("VARCHAR", ColumnTypeId.VARCHAR, false, null)]
340+
[InlineData("CHAR", ColumnTypeId.CHAR, false, 20)]
341+
[InlineData("VARCHAR", ColumnTypeId.VARCHAR, false, 20)]
314342
[InlineData("STRING", ColumnTypeId.VARCHAR, false, null)]
315343
[InlineData("BINARY", ColumnTypeId.BINARY,false, null )]
316344
[InlineData("DATE", ColumnTypeId.DATE, false, 4)]
@@ -334,7 +362,8 @@ internal void TestCalculatedTypeProperties(string baseType, ColumnTypeId dataTyp
334362
{
335363
""name"": ""col"",
336364
""type"": {
337-
""name"": ""<col_type>""
365+
""name"": ""<col_type>"",
366+
""length"": 20
338367
},
339368
""nullable"": false
340369
}

0 commit comments

Comments
 (0)