Skip to content

Commit 312f9e4

Browse files
authored
Remove ColumnType "Is" properties (except IsKey and IsVector) (#2023)
* Remove ColumnType.IsBool * Remove ColumnType.IsPrimitive * Remove ColumnType.IsNumber * Remove ColumnType.IsText * Move ColumnType.IsStandardScalar to an extension method.
1 parent 1319e2c commit 312f9e4

File tree

63 files changed

+183
-221
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+183
-221
lines changed

src/Microsoft.ML.Core/Data/ColumnType.cs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public abstract class ColumnType : IEquatable<ColumnType>
2121
// This private constructor sets all the IsXxx flags. It is invoked by other ctors.
2222
private ColumnType()
2323
{
24-
IsPrimitive = this is PrimitiveType;
2524
IsVector = this is VectorType;
26-
IsNumber = this is NumberType;
2725
IsKey = this is KeyType;
2826
}
2927

@@ -73,58 +71,6 @@ private protected ColumnType(Type rawType, DataKind rawKind)
7371
[BestFriend]
7472
internal DataKind RawKind { get; }
7573

76-
/// <summary>
77-
/// Whether this is a primitive type. External code should use <c>is <see cref="PrimitiveType"/></c>.
78-
/// </summary>
79-
[BestFriend]
80-
internal bool IsPrimitive { get; }
81-
82-
/// <summary>
83-
/// Whether this type is a standard numeric type. External code should use <c>is <see cref="NumberType"/></c>.
84-
/// </summary>
85-
[BestFriend]
86-
internal bool IsNumber { get; }
87-
88-
/// <summary>
89-
/// Whether this type is the standard text type. External code should use <c>is <see cref="TextType"/></c>.
90-
/// </summary>
91-
[BestFriend]
92-
internal bool IsText
93-
{
94-
get
95-
{
96-
if (!(this is TextType))
97-
return false;
98-
// TextType is a singleton.
99-
Contracts.Assert(this == TextType.Instance);
100-
return true;
101-
}
102-
}
103-
104-
/// <summary>
105-
/// Whether this type is the standard boolean type. External code should use <c>is <see cref="BoolType"/></c>.
106-
/// </summary>
107-
[BestFriend]
108-
internal bool IsBool
109-
{
110-
get
111-
{
112-
if (!(this is BoolType))
113-
return false;
114-
// BoolType is a singleton.
115-
Contracts.Assert(this == BoolType.Instance);
116-
return true;
117-
}
118-
}
119-
120-
/// <summary>
121-
/// Whether this type is a standard scalar type completely determined by its <see cref="RawType"/>
122-
/// (not a <see cref="KeyType"/> or <see cref="StructuredType"/>, etc).
123-
/// </summary>
124-
[BestFriend]
125-
internal bool IsStandardScalar => IsNumber || IsText || IsBool ||
126-
(this is TimeSpanType) || (this is DateTimeType) || (this is DateTimeOffsetType);
127-
12874
/// <summary>
12975
/// Whether this type is a key type, which implies that the order of values is not significant,
13076
/// and arithmetic is non-sensical. A key type can define a cardinality.
@@ -230,13 +176,11 @@ public abstract class StructuredType : ColumnType
230176
protected StructuredType(Type rawType)
231177
: base(rawType)
232178
{
233-
Contracts.Assert(!IsPrimitive);
234179
}
235180

236181
private protected StructuredType(Type rawType, DataKind rawKind)
237182
: base(rawType, rawKind)
238183
{
239-
Contracts.Assert(!IsPrimitive);
240184
}
241185
}
242186

@@ -249,15 +193,13 @@ public abstract class PrimitiveType : ColumnType
249193
protected PrimitiveType(Type rawType)
250194
: base(rawType)
251195
{
252-
Contracts.Assert(IsPrimitive);
253196
Contracts.CheckParam(!typeof(IDisposable).IsAssignableFrom(RawType), nameof(rawType),
254197
"A " + nameof(PrimitiveType) + " cannot have a disposable " + nameof(RawType));
255198
}
256199

257200
private protected PrimitiveType(Type rawType, DataKind rawKind)
258201
: base(rawType, rawKind)
259202
{
260-
Contracts.Assert(IsPrimitive);
261203
Contracts.Assert(!typeof(IDisposable).IsAssignableFrom(RawType));
262204
}
263205

@@ -322,7 +264,6 @@ private NumberType(DataKind kind, string name)
322264
{
323265
Contracts.AssertNonEmpty(name);
324266
_name = name;
325-
Contracts.Assert(IsNumber);
326267
}
327268

328269
private static volatile NumberType _instI1;
@@ -496,7 +437,7 @@ public override bool Equals(ColumnType other)
496437
{
497438
if (other == this)
498439
return true;
499-
Contracts.Assert(other == null || !other.IsNumber || other.RawKind != RawKind);
440+
Contracts.Assert(other == null || !(other is NumberType) || other.RawKind != RawKind);
500441
return false;
501442
}
502443

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.ML.Data
6+
{
7+
/// <summary>
8+
/// Extension methods related to the ColumnType class.
9+
/// </summary>
10+
[BestFriend]
11+
internal static class ColumnTypeExtensions
12+
{
13+
/// <summary>
14+
/// Whether this type is a standard scalar type completely determined by its <see cref="ColumnType.RawType"/>
15+
/// (not a <see cref="KeyType"/> or <see cref="StructuredType"/>, etc).
16+
/// </summary>
17+
public static bool IsStandardScalar(this ColumnType columnType) =>
18+
(columnType is NumberType) || (columnType is TextType) || (columnType is BoolType) ||
19+
(columnType is TimeSpanType) || (columnType is DateTimeType) || (columnType is DateTimeOffsetType);
20+
}
21+
}

src/Microsoft.ML.Core/Data/MetadataUtils.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ internal static IEnumerable<int> GetColumnSet(this Schema schema, string metadat
283283
for (int col = 0; col < schema.Count; col++)
284284
{
285285
var columnType = schema[col].Metadata.Schema.GetColumnOrNull(metadataKind)?.Type;
286-
if (columnType != null && columnType.IsText)
286+
if (columnType != null && columnType is TextType)
287287
{
288288
ReadOnlyMemory<char> val = default;
289289
schema[col].Metadata.GetValue(metadataKind, ref val);
@@ -318,7 +318,7 @@ internal static bool HasSlotNames(this Schema.Column column, int vectorSize)
318318
metaColumn != null
319319
&& metaColumn.Value.Type.IsVector
320320
&& metaColumn.Value.Type.VectorSize == vectorSize
321-
&& metaColumn.Value.Type.ItemType.IsText;
321+
&& metaColumn.Value.Type.ItemType is TextType;
322322
}
323323

324324
public static void GetSlotNames(this Schema.Column column, ref VBuffer<ReadOnlyMemory<char>> slotNames)
@@ -348,15 +348,15 @@ internal static bool HasKeyValues(this Schema.Column column, int keyCount)
348348
metaColumn != null
349349
&& metaColumn.Value.Type.IsVector
350350
&& metaColumn.Value.Type.VectorSize == keyCount
351-
&& metaColumn.Value.Type.ItemType.IsText;
351+
&& metaColumn.Value.Type.ItemType is TextType;
352352
}
353353

354354
[BestFriend]
355355
internal static bool HasKeyValues(this SchemaShape.Column col)
356356
{
357357
return col.Metadata.TryFindColumn(Kinds.KeyValues, out var metaCol)
358358
&& metaCol.Kind == SchemaShape.Column.VectorKind.Vector
359-
&& metaCol.ItemType.IsText;
359+
&& metaCol.ItemType is TextType;
360360
}
361361

362362
/// <summary>
@@ -365,7 +365,7 @@ internal static bool HasKeyValues(this SchemaShape.Column col)
365365
public static bool IsNormalized(this Schema.Column column)
366366
{
367367
var metaColumn = column.Metadata.Schema.GetColumnOrNull((Kinds.IsNormalized));
368-
if (metaColumn == null || !metaColumn.Value.Type.IsBool)
368+
if (metaColumn == null || !(metaColumn.Value.Type is BoolType))
369369
return false;
370370

371371
bool value = default;

src/Microsoft.ML.Data/Commands/DataCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ protected void SendTelemetryMetric(Dictionary<string, IDataView>[] metricValues)
167167
{
168168
var nameOfMetric = "TLC_" + cursor.Schema[currentIndex].Name;
169169
var type = cursor.Schema[currentIndex].Type;
170-
if (type.IsNumber)
170+
if (type is NumberType)
171171
{
172172
var getter = RowCursorUtils.GetGetterAs<double>(NumberType.R8, cursor, currentIndex);
173173
double metricValue = 0;

src/Microsoft.ML.Data/Commands/ShowSchemaCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static void PrintSchema(TextWriter writer, Arguments args, Schema schema
154154
ColumnType typeNames;
155155
if ((typeNames = schema[col].Metadata.Schema.GetColumnOrNull(MetadataUtils.Kinds.SlotNames)?.Type) == null)
156156
continue;
157-
if (typeNames.VectorSize != type.VectorSize || !typeNames.ItemType.IsText)
157+
if (typeNames.VectorSize != type.VectorSize || !(typeNames.ItemType is TextType))
158158
{
159159
Contracts.Assert(false, "Unexpected slot names type");
160160
continue;
@@ -212,7 +212,7 @@ private static void ShowMetadataValue(IndentedTextWriter itw, Schema schema, int
212212
Contracts.AssertValue(type);
213213
Contracts.Assert(!type.IsVector);
214214

215-
if (!type.IsStandardScalar && !type.IsKey)
215+
if (!type.IsStandardScalar() && !type.IsKey)
216216
{
217217
itw.Write(": Can't display value of this type");
218218
return;
@@ -252,7 +252,7 @@ private static void ShowMetadataValueVec(IndentedTextWriter itw, Schema schema,
252252
Contracts.AssertValue(type);
253253
Contracts.Assert(type.IsVector);
254254

255-
if (!type.ItemType.IsStandardScalar && !type.ItemType.IsKey)
255+
if (!type.ItemType.IsStandardScalar() && !type.ItemType.IsKey)
256256
{
257257
itw.Write(": Can't display value of this type");
258258
return;

src/Microsoft.ML.Data/Commands/TypeInfoCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private TypeNaInfo KindReport<T>(IChannel ch, PrimitiveType type)
137137
{
138138
Contracts.AssertValue(ch);
139139
ch.AssertValue(type);
140-
ch.Assert(type.IsStandardScalar);
140+
ch.Assert(type.IsStandardScalar());
141141

142142
var conv = Conversions.Instance;
143143
InPredicate<T> isNaDel;

src/Microsoft.ML.Data/Data/Conversion.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,12 @@ public bool TryGetStandardConversion(ColumnType typeSrc, ColumnType typeDst,
452452
}
453453
else if (typeDst is KeyType keyDst)
454454
{
455-
if (!typeSrc.IsText)
455+
if (!(typeSrc is TextType))
456456
return false;
457457
conv = GetKeyParse(keyDst);
458458
return true;
459459
}
460-
else if (!typeDst.IsStandardScalar)
460+
else if (!typeDst.IsStandardScalar())
461461
return false;
462462

463463
Contracts.Assert(typeSrc.RawKind != 0);
@@ -567,7 +567,7 @@ public ValueMapper<TSrc, SB> GetKeyStringConversion<TSrc>(KeyType key)
567567
public TryParseMapper<TDst> GetTryParseConversion<TDst>(ColumnType typeDst)
568568
{
569569
Contracts.CheckValue(typeDst, nameof(typeDst));
570-
Contracts.CheckParam(typeDst.IsStandardScalar || typeDst.IsKey, nameof(typeDst),
570+
Contracts.CheckParam(typeDst.IsStandardScalar() || typeDst.IsKey, nameof(typeDst),
571571
"Parse conversion only supported for standard types");
572572
Contracts.Check(typeDst.RawType == typeof(TDst), "Wrong TDst type parameter");
573573

@@ -676,7 +676,7 @@ public InPredicate<T> GetIsDefaultPredicate<T>(ColumnType type)
676676

677677
var t = type;
678678
Delegate del;
679-
if (!t.IsStandardScalar && !t.IsKey || !_isDefaultDelegates.TryGetValue(t.RawKind, out del))
679+
if (!t.IsStandardScalar() && !t.IsKey || !_isDefaultDelegates.TryGetValue(t.RawKind, out del))
680680
throw Contracts.Except("No IsDefault predicate for '{0}'", type);
681681

682682
return (InPredicate<T>)del;
@@ -719,7 +719,7 @@ public bool TryGetIsNAPredicate(ColumnType type, out Delegate del)
719719
Contracts.Assert(_isDefaultDelegates.ContainsKey(t.RawKind));
720720
del = _isDefaultDelegates[t.RawKind];
721721
}
722-
else if (!t.IsStandardScalar || !_isNADelegates.TryGetValue(t.RawKind, out del))
722+
else if (!t.IsStandardScalar() || !_isNADelegates.TryGetValue(t.RawKind, out del))
723723
{
724724
del = null;
725725
return false;
@@ -742,7 +742,7 @@ public InPredicate<VBuffer<T>> GetHasMissingPredicate<T>(VectorType type)
742742
Contracts.Assert(_hasZeroDelegates.ContainsKey(t.RawKind));
743743
del = _hasZeroDelegates[t.RawKind];
744744
}
745-
else if (!t.IsStandardScalar || !_hasNADelegates.TryGetValue(t.RawKind, out del))
745+
else if (!t.IsStandardScalar() || !_hasNADelegates.TryGetValue(t.RawKind, out del))
746746
throw Contracts.Except("No HasMissing predicate for '{0}'", type);
747747

748748
return (InPredicate<VBuffer<T>>)del;

src/Microsoft.ML.Data/Data/DataViewUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public static bool AllCachable(Schema schema, Func<int, bool> predicate)
199199
/// </summary>
200200
public static bool IsCachable(this ColumnType type)
201201
{
202-
return type != null && (type.IsPrimitive || type.IsVector);
202+
return type != null && (type is PrimitiveType || type.IsVector);
203203
}
204204

205205
/// <summary>
@@ -859,7 +859,7 @@ public static OutPipe Create(ColumnType type, object pool)
859859
pipeType = typeof(ImplVec<>).MakeGenericType(type.ItemType.RawType);
860860
else
861861
{
862-
Contracts.Assert(type.IsPrimitive);
862+
Contracts.Assert(type is PrimitiveType);
863863
pipeType = typeof(ImplOne<>).MakeGenericType(type.RawType);
864864
}
865865
var constructor = pipeType.GetConstructor(new Type[] { typeof(object) });

src/Microsoft.ML.Data/Data/RowCursorUtils.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ private static Delegate GetGetterAsDelegateCore<TValue>(Row row, int col)
4646
public static Delegate GetGetterAs(ColumnType typeDst, Row row, int col)
4747
{
4848
Contracts.CheckValue(typeDst, nameof(typeDst));
49-
Contracts.CheckParam(typeDst.IsPrimitive, nameof(typeDst));
49+
Contracts.CheckParam(typeDst is PrimitiveType, nameof(typeDst));
5050
Contracts.CheckValue(row, nameof(row));
5151
Contracts.CheckParam(0 <= col && col < row.Schema.Count, nameof(col));
5252
Contracts.CheckParam(row.IsColumnActive(col), nameof(col), "column was not active");
5353

5454
var typeSrc = row.Schema[col].Type;
55-
Contracts.Check(typeSrc.IsPrimitive, "Source column type must be primitive");
55+
Contracts.Check(typeSrc is PrimitiveType, "Source column type must be primitive");
5656

5757
Func<ColumnType, ColumnType, Row, int, ValueGetter<int>> del = GetGetterAsCore<int, int>;
5858
var methodInfo = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(typeSrc.RawType, typeDst.RawType);
@@ -66,14 +66,14 @@ public static Delegate GetGetterAs(ColumnType typeDst, Row row, int col)
6666
public static ValueGetter<TDst> GetGetterAs<TDst>(ColumnType typeDst, Row row, int col)
6767
{
6868
Contracts.CheckValue(typeDst, nameof(typeDst));
69-
Contracts.CheckParam(typeDst.IsPrimitive, nameof(typeDst));
69+
Contracts.CheckParam(typeDst is PrimitiveType, nameof(typeDst));
7070
Contracts.CheckParam(typeDst.RawType == typeof(TDst), nameof(typeDst));
7171
Contracts.CheckValue(row, nameof(row));
7272
Contracts.CheckParam(0 <= col && col < row.Schema.Count, nameof(col));
7373
Contracts.CheckParam(row.IsColumnActive(col), nameof(col), "column was not active");
7474

7575
var typeSrc = row.Schema[col].Type;
76-
Contracts.Check(typeSrc.IsPrimitive, "Source column type must be primitive");
76+
Contracts.Check(typeSrc is PrimitiveType, "Source column type must be primitive");
7777

7878
Func<ColumnType, ColumnType, Row, int, ValueGetter<TDst>> del = GetGetterAsCore<int, TDst>;
7979
var methodInfo = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(typeSrc.RawType, typeof(TDst));
@@ -118,7 +118,7 @@ public static ValueGetter<StringBuilder> GetGetterAsStringBuilder(Row row, int c
118118
Contracts.CheckParam(row.IsColumnActive(col), nameof(col), "column was not active");
119119

120120
var typeSrc = row.Schema[col].Type;
121-
Contracts.Check(typeSrc.IsPrimitive, "Source column type must be primitive");
121+
Contracts.Check(typeSrc is PrimitiveType, "Source column type must be primitive");
122122
return Utils.MarshalInvoke(GetGetterAsStringBuilderCore<int>, typeSrc.RawType, typeSrc, row, col);
123123
}
124124

@@ -356,7 +356,7 @@ public static string TestGetLabelGetter(ColumnType type)
356356

357357
public static string TestGetLabelGetter(ColumnType type, bool allowKeys)
358358
{
359-
if (type == NumberType.R4 || type == NumberType.R8 || type.IsBool)
359+
if (type == NumberType.R4 || type == NumberType.R8 || type is BoolType)
360360
return null;
361361

362362
if (allowKeys && type.IsKey)
@@ -394,7 +394,7 @@ private static ValueGetter<Single> GetLabelGetterNotFloat(Row cursor, int labelI
394394
Contracts.Assert(type != NumberType.R4 && type != NumberType.R8);
395395

396396
// boolean type label mapping: True -> 1, False -> 0.
397-
if (type.IsBool)
397+
if (type is BoolType)
398398
{
399399
var getBoolSrc = cursor.GetGetter<bool>(labelIndex);
400400
return
@@ -429,7 +429,7 @@ public static ValueGetter<VBuffer<Single>> GetLabelGetter(SlotCursor cursor)
429429
var type = cursor.GetSlotType().ItemType;
430430
if (type == NumberType.R4)
431431
return cursor.GetGetter<Single>();
432-
if (type == NumberType.R8 || type.IsBool)
432+
if (type == NumberType.R8 || type is BoolType)
433433
return GetVecGetterAs<Single>(NumberType.R4, cursor);
434434
Contracts.Check(type.IsKey, "Only floating point number, boolean, and key type values can be used as label.");
435435
Contracts.Assert(TestGetLabelGetter(type) == null);

src/Microsoft.ML.Data/DataLoadSave/DataOperations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public IDataView FilterByColumn(IDataView input, string columnName, double lower
6161
Environment.CheckParam(lowerBound <= upperBound, nameof(upperBound), "Must be no less than lowerBound");
6262

6363
var type = input.Schema[columnName].Type;
64-
if (!type.IsNumber)
64+
if (!(type is NumberType))
6565
throw Environment.ExceptSchemaMismatch(nameof(columnName), "filter", columnName, "number", type.ToString());
6666
return new RangeFilter(Environment, input, columnName, lowerBound, upperBound, false);
6767
}

src/Microsoft.ML.Data/DataLoadSave/Text/TextLoaderParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private Func<RowSet, ColumnPipe> GetCreatorOneCore(PrimitiveType type)
7070

7171
private Func<RowSet, ColumnPipe> GetCreatorOneCore<T>(PrimitiveType type)
7272
{
73-
Contracts.Assert(type.IsStandardScalar || type.IsKey);
73+
Contracts.Assert(type.IsStandardScalar() || type.IsKey);
7474
Contracts.Assert(typeof(T) == type.RawType);
7575
var fn = _conv.GetTryParseConversion<T>(type);
7676
return rows => new PrimitivePipe<T>(rows, type, fn);
@@ -84,7 +84,7 @@ private Func<RowSet, ColumnPipe> GetCreatorVecCore(PrimitiveType type)
8484

8585
private Func<RowSet, ColumnPipe> GetCreatorVecCore<T>(PrimitiveType type)
8686
{
87-
Contracts.Assert(type.IsStandardScalar || type.IsKey);
87+
Contracts.Assert(type.IsStandardScalar() || type.IsKey);
8888
Contracts.Assert(typeof(T) == type.RawType);
8989
var fn = _conv.GetTryParseConversion<T>(type);
9090
return rows => new VectorPipe<T>(rows, type, fn);

0 commit comments

Comments
 (0)