Skip to content

Commit 771f01d

Browse files
committed
Merge branch 'DvDateTime3' of https://github.com/codemzs/machinelearning into typesystem
# Conflicts: # src/Microsoft.ML.Data/Data/Conversion.cs # src/Microsoft.ML.Data/DataLoadSave/Binary/CodecFactory.cs # src/Microsoft.ML.Data/DataLoadSave/Binary/Codecs.cs # src/Microsoft.ML.Data/DataLoadSave/Binary/UnsafeTypeOps.cs # src/Microsoft.ML.Transforms/NAReplaceUtils.cs # test/BaselineOutput/SingleDebug/Command/Datatypes-datatypes.txt # test/BaselineOutput/SingleRelease/Command/Datatypes-datatypes.txt
2 parents dca6f24 + 67f96a7 commit 771f01d

File tree

14 files changed

+147
-209
lines changed

14 files changed

+147
-209
lines changed

src/Microsoft.ML.Api/ApiUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static OpCode GetAssignmentOpCode(Type t)
2222
if (t == typeof(ReadOnlyMemory<char>) || t == typeof(string) || t.IsArray ||
2323
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(VBuffer<>)) ||
2424
(t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>)) ||
25-
t == typeof(DvDateTime) || t == typeof(DvDateTimeZone) || t == typeof(DvTimeSpan) || t == typeof(UInt128))
25+
t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(TimeSpan) || t == typeof(UInt128))
2626
{
2727
return OpCodes.Stobj;
2828
}

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

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,47 +120,38 @@ public bool IsBool
120120
}
121121

122122
/// <summary>
123-
/// Whether this type is the standard timespan type.
123+
/// Whether this type is the standard <see cref="TimeSpan"/> type.
124124
/// </summary>
125125
public bool IsTimeSpan
126126
{
127127
get
128128
{
129-
if (!(this is TimeSpanType))
130-
return false;
131-
// TimeSpanType is a singleton.
132-
Contracts.Assert(this == TimeSpanType.Instance);
133-
return true;
129+
Contracts.Assert((this == TimeSpanType.Instance) == (this is TimeSpanType));
130+
return this is TimeSpanType;
134131
}
135132
}
136133

137134
/// <summary>
138-
/// Whether this type is a DvDateTime.
135+
/// Whether this type is a <see cref="DateTime"/>.
139136
/// </summary>
140137
public bool IsDateTime
141138
{
142139
get
143140
{
144-
if (!(this is DateTimeType))
145-
return false;
146-
// DateTimeType is a singleton.
147-
Contracts.Assert(this == DateTimeType.Instance);
148-
return true;
141+
Contracts.Assert((this == DateTimeType.Instance) == (this is DateTimeType));
142+
return this is DateTimeType;
149143
}
150144
}
151145

152146
/// <summary>
153-
/// Whether this type is a DvDateTimeZone.
147+
/// Whether this type is a <see cref="DateTimeOffset"/>
154148
/// </summary>
155149
public bool IsDateTimeZone
156150
{
157151
get
158152
{
159-
if (!(this is DateTimeZoneType))
160-
return false;
161-
// DateTimeZoneType is a singleton.
162-
Contracts.Assert(this == DateTimeZoneType.Instance);
163-
return true;
153+
Contracts.Assert((this == DateTimeOffsetType.Instance) == (this is DateTimeOffsetType));
154+
return this is DateTimeOffsetType;
164155
}
165156
}
166157

@@ -319,7 +310,7 @@ public static PrimitiveType FromKind(DataKind kind)
319310
if (kind == DataKind.DT)
320311
return DateTimeType.Instance;
321312
if (kind == DataKind.DZ)
322-
return DateTimeZoneType.Instance;
313+
return DateTimeOffsetType.Instance;
323314
return NumberType.FromKind(kind);
324315
}
325316
}
@@ -605,7 +596,7 @@ public static DateTimeType Instance
605596
}
606597

607598
private DateTimeType()
608-
: base(typeof(DvDateTime), DataKind.DT)
599+
: base(typeof(DateTime), DataKind.DT)
609600
{
610601
}
611602

@@ -623,29 +614,29 @@ public override string ToString()
623614
}
624615
}
625616

626-
public sealed class DateTimeZoneType : PrimitiveType
617+
public sealed class DateTimeOffsetType : PrimitiveType
627618
{
628-
private static volatile DateTimeZoneType _instance;
629-
public static DateTimeZoneType Instance
619+
private static volatile DateTimeOffsetType _instance;
620+
public static DateTimeOffsetType Instance
630621
{
631622
get
632623
{
633624
if (_instance == null)
634-
Interlocked.CompareExchange(ref _instance, new DateTimeZoneType(), null);
625+
Interlocked.CompareExchange(ref _instance, new DateTimeOffsetType(), null);
635626
return _instance;
636627
}
637628
}
638629

639-
private DateTimeZoneType()
640-
: base(typeof(DvDateTimeZone), DataKind.DZ)
630+
private DateTimeOffsetType()
631+
: base(typeof(DateTimeOffset), DataKind.DZ)
641632
{
642633
}
643634

644635
public override bool Equals(ColumnType other)
645636
{
646637
if (other == this)
647638
return true;
648-
Contracts.Assert(!(other is DateTimeZoneType));
639+
Contracts.Assert(!(other is DateTimeOffsetType));
649640
return false;
650641
}
651642

@@ -672,7 +663,7 @@ public static TimeSpanType Instance
672663
}
673664

674665
private TimeSpanType()
675-
: base(typeof(DvTimeSpan), DataKind.TS)
666+
: base(typeof(TimeSpan), DataKind.TS)
676667
{
677668
}
678669

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public static Type ToType(this DataKind kind)
165165
case DataKind.BL:
166166
return typeof(bool);
167167
case DataKind.TS:
168-
return typeof(DvTimeSpan);
168+
return typeof(TimeSpan);
169169
case DataKind.DT:
170-
return typeof(DvDateTime);
170+
return typeof(DateTime);
171171
case DataKind.DZ:
172-
return typeof(DvDateTimeZone);
172+
return typeof(DateTimeOffset);
173173
case DataKind.UG:
174174
return typeof(UInt128);
175175
}
@@ -209,11 +209,11 @@ public static bool TryGetDataKind(this Type type, out DataKind kind)
209209
kind = DataKind.TX;
210210
else if (type == typeof(bool))
211211
kind = DataKind.BL;
212-
else if (type == typeof(DvTimeSpan))
212+
else if (type == typeof(TimeSpan))
213213
kind = DataKind.TS;
214-
else if (type == typeof(DvDateTime))
214+
else if (type == typeof(DateTime))
215215
kind = DataKind.DT;
216-
else if (type == typeof(DvDateTimeZone))
216+
else if (type == typeof(DateTimeOffset))
217217
kind = DataKind.DZ;
218218
else if (type == typeof(UInt128))
219219
kind = DataKind.UG;

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

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
namespace Microsoft.ML.Runtime.Data.Conversion
1616
{
17-
using BL = Boolean;
18-
using DT = DvDateTime;
19-
using DZ = DvDateTimeZone;
17+
using BL = Boolean;;
18+
using DT = DateTime;
19+
using DZ = DateTimeOffset;
2020
using R4 = Single;
2121
using R8 = Double;
2222
using I1 = SByte;
2323
using I2 = Int16;
2424
using I4 = Int32;
2525
using I8 = Int64;
2626
using SB = StringBuilder;
27-
using TS = DvTimeSpan;
2827
using TX = ReadOnlyMemory<char>;
28+
using TS = TimeSpan;
2929
using U1 = Byte;
3030
using U2 = UInt16;
3131
using U4 = UInt32;
@@ -242,21 +242,12 @@ private Conversions()
242242

243243
AddIsNA<R4>(IsNA);
244244
AddIsNA<R8>(IsNA);
245-
AddIsNA<TS>(IsNA);
246-
AddIsNA<DT>(IsNA);
247-
AddIsNA<DZ>(IsNA);
248245

249246
AddGetNA<R4>(GetNA);
250247
AddGetNA<R8>(GetNA);
251-
AddGetNA<TS>(GetNA);
252-
AddGetNA<DT>(GetNA);
253-
AddGetNA<DZ>(GetNA);
254248

255249
AddHasNA<R4>(HasNA);
256250
AddHasNA<R8>(HasNA);
257-
AddHasNA<TS>(HasNA);
258-
AddHasNA<DT>(HasNA);
259-
AddHasNA<DZ>(HasNA);
260251

261252
AddIsDef<I1>(IsDefault);
262253
AddIsDef<I2>(IsDefault);
@@ -826,17 +817,11 @@ public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
826817
#region IsNA
827818
private bool IsNA(ref R4 src) => src.IsNA();
828819
private bool IsNA(ref R8 src) => src.IsNA();
829-
private bool IsNA(ref TS src) => src.IsNA;
830-
private bool IsNA(ref DT src) => src.IsNA;
831-
private bool IsNA(ref DZ src) => src.IsNA;
832820
#endregion IsNA
833821

834822
#region HasNA
835823
private bool HasNA(ref VBuffer<R4> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA()) return true; } return false; }
836824
private bool HasNA(ref VBuffer<R8> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA()) return true; } return false; }
837-
private bool HasNA(ref VBuffer<TS> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
838-
private bool HasNA(ref VBuffer<DT> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
839-
private bool HasNA(ref VBuffer<DZ> src) { for (int i = 0; i < src.Count; i++) { if (src.Values[i].IsNA) return true; } return false; }
840825
#endregion HasNA
841826

842827
#region IsDefault
@@ -868,9 +853,6 @@ public ValueGetter<T> GetNAOrDefaultGetter<T>(ColumnType type)
868853
#region GetNA
869854
private void GetNA(ref R4 value) => value = R4.NaN;
870855
private void GetNA(ref R8 value) => value = R8.NaN;
871-
private void GetNA(ref TS value) => value = TS.NA;
872-
private void GetNA(ref DT value) => value = DT.NA;
873-
private void GetNA(ref DZ value) => value = DZ.NA;
874856
#endregion GetNA
875857

876858
#region ToI1
@@ -1001,9 +983,9 @@ public void Convert(ref BL src, ref SB dst)
1001983
else
1002984
dst.Append("1");
1003985
}
1004-
public void Convert(ref TS src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:c}", (TimeSpan)src); }
1005-
public void Convert(ref DT src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:o}", (DateTime)src); }
1006-
public void Convert(ref DZ src, ref SB dst) { ClearDst(ref dst); if (!src.IsNA) dst.AppendFormat("{0:o}", (DateTimeOffset)src); }
986+
public void Convert(ref TS src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:c}", src); }
987+
public void Convert(ref DT src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
988+
public void Convert(ref DZ src, ref SB dst) { ClearDst(ref dst); dst.AppendFormat("{0:o}", src); }
1007989
#endregion ToStringBuilder
1008990

1009991
#region FromR4
@@ -1424,14 +1406,12 @@ public bool TryParse(ref TX src, out TS dst)
14241406
dst = default;
14251407
return true;
14261408
}
1427-
TimeSpan res;
1428-
if (TimeSpan.TryParse(src.ToString(), CultureInfo.InvariantCulture, out res))
1429-
{
1430-
dst = new TS(res);
1409+
1410+
if (TimeSpan.TryParse(src.ToString(), CultureInfo.InvariantCulture, out dst))
14311411
return true;
1432-
}
1433-
dst = TS.NA;
1434-
return IsStdMissing(ref src);
1412+
1413+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1414+
return true;
14351415
}
14361416

14371417
public bool TryParse(ref TX src, out DT dst)
@@ -1441,14 +1421,12 @@ public bool TryParse(ref TX src, out DT dst)
14411421
dst = default;
14421422
return true;
14431423
}
1444-
DateTime res;
1445-
if (DateTime.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out res))
1446-
{
1447-
dst = new DT(res);
1424+
1425+
if (DateTime.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out dst))
14481426
return true;
1449-
}
1450-
dst = DvDateTime.NA;
1451-
return IsStdMissing(ref src);
1427+
1428+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1429+
return true;
14521430
}
14531431

14541432
public bool TryParse(ref TX src, out DZ dst)
@@ -1458,14 +1436,12 @@ public bool TryParse(ref TX src, out DZ dst)
14581436
dst = default;
14591437
return true;
14601438
}
1461-
DateTimeOffset res;
1462-
if (DateTimeOffset.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out res))
1463-
{
1464-
dst = new DZ(res);
1439+
1440+
if (DateTimeOffset.TryParse(src.ToString(), CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dst))
14651441
return true;
1466-
}
1467-
dst = DvDateTimeZone.NA;
1468-
return IsStdMissing(ref src);
1442+
1443+
Contracts.Check(!IsStdMissing(ref src), "Missing values cannot be converted to boolean value.");
1444+
return true;
14691445
}
14701446

14711447
// These throw an exception for unparsable and overflow values.
@@ -1731,21 +1707,10 @@ public void Convert(ref TX src, ref SB dst)
17311707
ReadOnlyMemoryUtils.AddToStringBuilder(dst, src);
17321708
}
17331709

1734-
public void Convert(ref TX span, ref TS value)
1735-
{
1736-
if (!TryParse(ref span, out value))
1737-
Contracts.Assert(value.IsNA);
1738-
}
1739-
public void Convert(ref TX span, ref DT value)
1740-
{
1741-
if (!TryParse(ref span, out value))
1742-
Contracts.Assert(value.IsNA);
1743-
}
1744-
public void Convert(ref TX span, ref DZ value)
1745-
{
1746-
if (!TryParse(ref span, out value))
1747-
Contracts.Assert(value.IsNA);
1748-
}
1710+
public void Convert(ref TX span, ref TS value) => TryParse(ref span, out value);
1711+
public void Convert(ref TX span, ref DT value) => TryParse(ref span, out value);
1712+
public void Convert(ref TX span, ref DZ value) => TryParse(ref span, out value);
1713+
17491714
#endregion FromTX
17501715

17511716
#region FromBL

0 commit comments

Comments
 (0)