diff --git a/csharp/src/Apache.Arrow/Arrays/Date32Array.cs b/csharp/src/Apache.Arrow/Arrays/Date32Array.cs index 35c0065e1190..23ad7356eb32 100644 --- a/csharp/src/Apache.Arrow/Arrays/Date32Array.cs +++ b/csharp/src/Apache.Arrow/Arrays/Date32Array.cs @@ -25,6 +25,9 @@ namespace Apache.Arrow public class Date32Array : PrimitiveArray { private static readonly DateTime _epochDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Unspecified); +#if NET6_0_OR_GREATER + private static readonly int _epochDayNumber = new DateOnly(1970, 1, 1).DayNumber; +#endif /// /// The class can be used to fluently build objects. @@ -57,6 +60,13 @@ protected override int Convert(DateTimeOffset dateTimeOffset) // DateTimeOffset.Date property. return (int)(dateTimeOffset.UtcDateTime.Date - _epochDate).TotalDays; } + +#if NET6_0_OR_GREATER + protected override int Convert(DateOnly date) + { + return (int)(date.DayNumber - _epochDayNumber); + } +#endif } public Date32Array( @@ -108,5 +118,21 @@ public Date32Array(ArrayData data) ? new DateTimeOffset(_epochDate.AddDays(value.Value), TimeSpan.Zero) : default(DateTimeOffset?); } + +#if NET6_0_OR_GREATER + /// + /// Get the date at the specified index + /// + /// Index at which to get the date. + /// Returns a , or null if there is no object at that index. + /// + public DateOnly? GetDateOnly(int index) + { + int? value = GetValue(index); + return value.HasValue + ? DateOnly.FromDayNumber(_epochDayNumber + value.Value) + : default(DateOnly?); + } +#endif } } diff --git a/csharp/src/Apache.Arrow/Arrays/Date64Array.cs b/csharp/src/Apache.Arrow/Arrays/Date64Array.cs index cf977b2e4969..b0d42e27bbd2 100644 --- a/csharp/src/Apache.Arrow/Arrays/Date64Array.cs +++ b/csharp/src/Apache.Arrow/Arrays/Date64Array.cs @@ -69,6 +69,13 @@ protected override long Convert(DateTimeOffset dateTimeOffset) long days = millis / MillisecondsPerDay; return (millis < 0 ? days - 1 : days) * MillisecondsPerDay; } + +#if NET6_0_OR_GREATER + protected override long Convert(DateOnly date) + { + return ((long)date.DayNumber - _epochDayNumber) * MillisecondsPerDay; + } +#endif } public Date64Array(ArrayData data) @@ -113,5 +120,21 @@ public Date64Array(ArrayData data) ? DateTimeOffset.FromUnixTimeMilliseconds(value.Value) : default(DateTimeOffset?); } + +#if NET6_0_OR_GREATER + /// + /// Get the date at the specified index + /// + /// Index at which to get the date. + /// Returns a , or null if there is no object at that index. + /// + public DateOnly? GetDateOnly(int index) + { + long? value = GetValue(index); + return value.HasValue + ? DateOnly.FromDateTime(DateTimeOffset.FromUnixTimeMilliseconds(value.Value).UtcDateTime) + : default(DateOnly?); + } +#endif } } diff --git a/csharp/src/Apache.Arrow/Arrays/DateArrayBuilder.cs b/csharp/src/Apache.Arrow/Arrays/DateArrayBuilder.cs index 4e69f6fe3e7e..dcbb76930b6d 100644 --- a/csharp/src/Apache.Arrow/Arrays/DateArrayBuilder.cs +++ b/csharp/src/Apache.Arrow/Arrays/DateArrayBuilder.cs @@ -28,9 +28,16 @@ public abstract class DateArrayBuilder : DelegatingArrayBuilder, IArrowArrayBuilder, IArrowArrayBuilder +#if NET6_0_OR_GREATER + , IArrowArrayBuilder +#endif where TArray : IArrowArray where TBuilder : class, IArrowArrayBuilder { +#if NET6_0_OR_GREATER + protected static readonly long _epochDayNumber = new DateOnly(1970, 1, 1).DayNumber; +#endif + /// /// Construct a new instance of the class. /// @@ -72,6 +79,20 @@ public TBuilder Append(DateTimeOffset value) return this as TBuilder; } +#if NET6_0_OR_GREATER + /// + /// Append a date from a object to the array. + /// + /// + /// Date to add. + /// Returns the builder (for fluent-style composition). + public TBuilder Append(DateOnly value) + { + InnerBuilder.Append(Convert(value)); + return this as TBuilder; + } +#endif + /// /// Append a span of dates in the form of objects to the array. /// @@ -114,6 +135,24 @@ public TBuilder Append(ReadOnlySpan span) return this as TBuilder; } +#if NET6_0_OR_GREATER + /// + /// Append a span of dates in the form of objects to the array. + /// + /// Span of dates to add. + /// Returns the builder (for fluent-style composition). + public TBuilder Append(ReadOnlySpan span) + { + InnerBuilder.Reserve(span.Length); + foreach (var item in span) + { + InnerBuilder.Append(Convert(item)); + } + + return this as TBuilder; + } +#endif + /// /// Append a null date to the array. /// @@ -156,6 +195,19 @@ public TBuilder AppendRange(IEnumerable values) return this as TBuilder; } +#if NET6_0_OR_GREATER + /// + /// Append a collection of dates in the form of objects to the array. + /// + /// Collection of dates to add. + /// Returns the builder (for fluent-style composition). + public TBuilder AppendRange(IEnumerable values) + { + InnerBuilder.AppendRange(values.Select(Convert)); + return this as TBuilder; + } +#endif + /// /// Set the value of a date in the form of a object at the specified index. /// @@ -190,6 +242,20 @@ public TBuilder Set(int index, DateTimeOffset value) return this as TBuilder; } +#if NET6_0_OR_GREATER + /// + /// Set the value of a date in the form of a object at the specified index. + /// + /// Index at which to set value. + /// Date to set. + /// Returns the builder (for fluent-style composition). + public TBuilder Set(int index, DateOnly value) + { + InnerBuilder.Set(index, Convert(value)); + return this as TBuilder; + } +#endif + /// /// Swap the values of the dates at the specified indices. /// @@ -205,5 +271,9 @@ public TBuilder Swap(int i, int j) protected abstract TUnderlying Convert(DateTime dateTime); protected abstract TUnderlying Convert(DateTimeOffset dateTimeOffset); + +#if NET6_0_OR_GREATER + protected abstract TUnderlying Convert(DateOnly date); +#endif } } diff --git a/csharp/src/Apache.Arrow/Arrays/Time32Array.cs b/csharp/src/Apache.Arrow/Arrays/Time32Array.cs index bdaf64d5561c..31d17d06a1e4 100644 --- a/csharp/src/Apache.Arrow/Arrays/Time32Array.cs +++ b/csharp/src/Apache.Arrow/Arrays/Time32Array.cs @@ -14,6 +14,7 @@ // limitations under the License. using Apache.Arrow.Types; +using System; using System.IO; namespace Apache.Arrow @@ -27,14 +28,19 @@ public class Time32Array : PrimitiveArray /// /// The class can be used to fluently build objects. /// - public class Builder : PrimitiveArrayBuilder + public class Builder : TimeArrayBuilder { - protected override Time32Array Build( - ArrowBuffer valueBuffer, ArrowBuffer nullBitmapBuffer, - int length, int nullCount, int offset) => - new Time32Array(DataType, valueBuffer, nullBitmapBuffer, length, nullCount, offset); - - protected Time32Type DataType { get; } + private class TimeBuilder : PrimitiveArrayBuilder + { + public Time32Type DataType { get; } + + public TimeBuilder(Time32Type dataType) => DataType = dataType; + + protected override Time32Array Build( + ArrowBuffer valueBuffer, ArrowBuffer nullBitmapBuffer, + int length, int nullCount, int offset) => + new Time32Array(DataType, valueBuffer, nullBitmapBuffer, length, nullCount, offset); + } public Builder() : this(Time32Type.Default) { } @@ -46,10 +52,22 @@ public Builder(TimeUnit unit) /// Construct a new instance of the class. /// public Builder(Time32Type type) - : base() + : base(new TimeBuilder(type)) + { + } + +#if NET6_0_OR_GREATER + protected override int Convert(TimeOnly time) { - DataType = type; + var unit = ((TimeBuilder)InnerBuilder).DataType.Unit; + return unit switch + { + TimeUnit.Second => (int)(time.Ticks / TimeSpan.TicksPerSecond), + TimeUnit.Millisecond => (int)(time.Ticks / TimeSpan.TicksPerMillisecond), + _ => throw new InvalidDataException($"Unsupported time unit for Time32Type: {unit}") + }; } +#endif } public Time32Array( @@ -113,5 +131,30 @@ public Time32Array(ArrayData data) _ => throw new InvalidDataException($"Unsupported time unit for Time32Type: {unit}") }; } + +#if NET6_0_OR_GREATER + /// + /// Get the time at the specified index as + /// + /// Index at which to get the time. + /// Returns a , or null if there is no object at that index. + /// + public TimeOnly? GetTime(int index) + { + int? value = GetValue(index); + if (value == null) + { + return null; + } + + var unit = ((Time32Type)Data.DataType).Unit; + return unit switch + { + TimeUnit.Second => new TimeOnly(value.Value * TimeSpan.TicksPerSecond), + TimeUnit.Millisecond => new TimeOnly(value.Value * TimeSpan.TicksPerMillisecond), + _ => throw new InvalidDataException($"Unsupported time unit for Time32Type: {unit}") + }; + } +#endif } } diff --git a/csharp/src/Apache.Arrow/Arrays/Time64Array.cs b/csharp/src/Apache.Arrow/Arrays/Time64Array.cs index 127db63a7e09..95faf18fe9e6 100644 --- a/csharp/src/Apache.Arrow/Arrays/Time64Array.cs +++ b/csharp/src/Apache.Arrow/Arrays/Time64Array.cs @@ -14,6 +14,7 @@ // limitations under the License. using Apache.Arrow.Types; +using System; using System.IO; namespace Apache.Arrow @@ -24,17 +25,25 @@ namespace Apache.Arrow /// public class Time64Array : PrimitiveArray { + private const long TicksPerMicrosecond = 10; + private const long NanosecondsPerTick = 100; + /// /// The class can be used to fluently build objects. /// - public class Builder : PrimitiveArrayBuilder + public class Builder : TimeArrayBuilder { - protected override Time64Array Build( - ArrowBuffer valueBuffer, ArrowBuffer nullBitmapBuffer, - int length, int nullCount, int offset) => - new Time64Array(DataType, valueBuffer, nullBitmapBuffer, length, nullCount, offset); + private class TimeBuilder : PrimitiveArrayBuilder + { + public Time64Type DataType { get; } - protected Time64Type DataType { get; } + public TimeBuilder(Time64Type dataType) => DataType = dataType; + + protected override Time64Array Build( + ArrowBuffer valueBuffer, ArrowBuffer nullBitmapBuffer, + int length, int nullCount, int offset) => + new Time64Array(DataType, valueBuffer, nullBitmapBuffer, length, nullCount, offset); + } public Builder() : this(Time64Type.Default) { } @@ -46,10 +55,22 @@ public Builder(TimeUnit unit) /// Construct a new instance of the class. /// public Builder(Time64Type type) - : base() + : base(new TimeBuilder(type)) + { + } + +#if NET6_0_OR_GREATER + protected override long Convert(TimeOnly time) { - DataType = type; + var unit = ((TimeBuilder)InnerBuilder).DataType.Unit; + return unit switch + { + TimeUnit.Microsecond => (long)(time.Ticks / TicksPerMicrosecond), + TimeUnit.Nanosecond => (long)(time.Ticks * NanosecondsPerTick), + _ => throw new InvalidDataException($"Unsupported time unit for Time32Type: {unit}") + }; } +#endif } public Time64Array( @@ -113,5 +134,33 @@ public Time64Array(ArrayData data) _ => throw new InvalidDataException($"Unsupported time unit for Time64Type: {unit}") }; } + +#if NET6_0_OR_GREATER + /// + /// Get the time at the specified index as + /// + /// + /// This may cause truncation of nanosecond values, as the resolution of TimeOnly is in 100-ns increments. + /// + /// Index at which to get the time. + /// Returns a , or null if there is no object at that index. + /// + public TimeOnly? GetTime(int index) + { + long? value = GetValue(index); + if (value == null) + { + return null; + } + + var unit = ((Time64Type)Data.DataType).Unit; + return unit switch + { + TimeUnit.Microsecond => new TimeOnly(value.Value * TicksPerMicrosecond), + TimeUnit.Nanosecond => new TimeOnly(value.Value / NanosecondsPerTick), + _ => throw new InvalidDataException($"Unsupported time unit for Time64Type: {unit}") + }; + } +#endif } } diff --git a/csharp/src/Apache.Arrow/Arrays/TimeArrayBuilder.cs b/csharp/src/Apache.Arrow/Arrays/TimeArrayBuilder.cs new file mode 100644 index 000000000000..da93db84717d --- /dev/null +++ b/csharp/src/Apache.Arrow/Arrays/TimeArrayBuilder.cs @@ -0,0 +1,152 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Apache.Arrow +{ + public abstract class TimeArrayBuilder : + DelegatingArrayBuilder +#if NET6_0_OR_GREATER + , IArrowArrayBuilder +#endif + where TArray : IArrowArray + where TBuilder : class, IArrowArrayBuilder + { + /// + /// Construct a new instance of the class. + /// + /// Inner builder that will produce arrays of type . + /// + protected TimeArrayBuilder(IArrowArrayBuilder> innerBuilder) + : base(innerBuilder) + { } + +#if NET6_0_OR_GREATER + /// + /// Append a time in the form of a object to the array. + /// + /// Time to add. + /// Returns the builder (for fluent-style composition). + public TBuilder Append(TimeOnly value) + { + InnerBuilder.Append(Convert(value)); + return this as TBuilder; + } +#endif + + /// + /// Append a time + /// + /// + /// + public TBuilder Append(TUnderlying value) + { + InnerBuilder.Append(value); + return this as TBuilder; + } + +#if NET6_0_OR_GREATER + /// + /// Append a span of times in the form of objects to the array. + /// + /// Span of times to add. + /// Returns the builder (for fluent-style composition). + public TBuilder Append(ReadOnlySpan span) + { + InnerBuilder.Reserve(span.Length); + foreach (var item in span) + { + InnerBuilder.Append(Convert(item)); + } + + return this as TBuilder; + } +#endif + + public TBuilder Append(ReadOnlySpan values) + { + InnerBuilder.Append(values); + return this as TBuilder; + } + + /// + /// Append a null time to the array. + /// + /// Returns the builder (for fluent-style composition). + public TBuilder AppendNull() + { + InnerBuilder.AppendNull(); + return this as TBuilder; + } + +#if NET6_0_OR_GREATER + /// + /// Append a collection of times in the form of objects to the array. + /// + /// Collection of times to add. + /// Returns the builder (for fluent-style composition). + public TBuilder AppendRange(IEnumerable values) + { + InnerBuilder.AppendRange(values.Select(Convert)); + return this as TBuilder; + } +#endif + + public TBuilder AppendRange(IEnumerable values) + { + InnerBuilder.AppendRange(values); + return this as TBuilder; + } + +#if NET6_0_OR_GREATER + /// + /// Set the value of a time in the form of a object at the specified index. + /// + /// Index at which to set value. + /// Time to set. + /// Returns the builder (for fluent-style composition). + public TBuilder Set(int index, TimeOnly value) + { + InnerBuilder.Set(index, Convert(value)); + return this as TBuilder; + } +#endif + + public TBuilder Set(int index, TUnderlying value) + { + InnerBuilder.Set(index, value); + return this as TBuilder; + } + + /// + /// Swap the values of the times at the specified indices. + /// + /// First index. + /// Second index. + /// Returns the builder (for fluent-style composition). + public TBuilder Swap(int i, int j) + { + InnerBuilder.Swap(i, j); + return this as TBuilder; + } + +#if NET6_0_OR_GREATER + protected abstract TUnderlying Convert(TimeOnly time); +#endif + } +} diff --git a/csharp/test/Apache.Arrow.Tests/Apache.Arrow.Tests.csproj b/csharp/test/Apache.Arrow.Tests/Apache.Arrow.Tests.csproj index cdbfe479470a..55005a91c74a 100644 --- a/csharp/test/Apache.Arrow.Tests/Apache.Arrow.Tests.csproj +++ b/csharp/test/Apache.Arrow.Tests/Apache.Arrow.Tests.csproj @@ -1,4 +1,4 @@ - + @@ -21,4 +21,8 @@ + + + + \ No newline at end of file diff --git a/csharp/test/Apache.Arrow.Tests/ArrayBuilderTests.cs b/csharp/test/Apache.Arrow.Tests/ArrayBuilderTests.cs index 9ac2f779a6f6..0c40fd82af7c 100644 --- a/csharp/test/Apache.Arrow.Tests/ArrayBuilderTests.cs +++ b/csharp/test/Apache.Arrow.Tests/ArrayBuilderTests.cs @@ -40,8 +40,8 @@ public void PrimitiveArrayBuildersProduceExpectedArray() Test(); Test(); Test(); - Test(); - Test(); + TestArrayBuilder(x => x.Append(10).Append(20).Append(30)); + TestArrayBuilder(x => x.Append(10).Append(20).Append(30)); static void Test() where T : struct, INumber @@ -64,8 +64,8 @@ public void PrimitiveArrayBuildersProduceExpectedArrayWithNulls() Test(); Test(); Test(); - Test(); - Test(); + TestArrayBuilder(x => x.Append(123).AppendNull().AppendNull().Append(127), 4, 2, 0x9); + TestArrayBuilder(x => x.Append(123).AppendNull().AppendNull().Append(127), 4, 2, 0x9); static void Test() where T : struct, INumber diff --git a/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs b/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs index af3e0f80e647..16fca684ff5e 100644 --- a/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs +++ b/csharp/test/Apache.Arrow.Tests/ArrowArrayTests.cs @@ -109,8 +109,8 @@ public void SliceArray() TestNumberSlice(); TestSlice(x => x.Append(new DateTime(2019, 1, 1)).Append(new DateTime(2019, 1, 2)).Append(new DateTime(2019, 1, 3))); TestSlice(x => x.Append(new DateTime(2019, 1, 1)).Append(new DateTime(2019, 1, 2)).Append(new DateTime(2019, 1, 3))); - TestNumberSlice(); - TestNumberSlice(); + TestSlice(x => x.Append(10).Append(20).Append(30)); + TestSlice(x => x.Append(10).Append(20).Append(30)); TestSlice(x => x.Append("10").Append("20").Append("30")); static void TestNumberSlice() @@ -136,8 +136,8 @@ public void SlicePrimitiveArrayWithNulls() TestNumberSlice(); TestSlice(x => x.Append(new DateTime(2019, 1, 1)).Append(new DateTime(2019, 1, 2)).AppendNull().Append(new DateTime(2019, 1, 3))); TestSlice(x => x.Append(new DateTime(2019, 1, 1)).Append(new DateTime(2019, 1, 2)).AppendNull().Append(new DateTime(2019, 1, 3))); - TestNumberSlice(); - TestNumberSlice(); + TestSlice(x => x.Append(10).Append(20).AppendNull().Append(30)); + TestSlice(x => x.Append(10).Append(20).AppendNull().Append(30)); static void TestNumberSlice() where T : struct, INumber diff --git a/csharp/test/Apache.Arrow.Tests/Date32ArrayTests.cs b/csharp/test/Apache.Arrow.Tests/Date32ArrayTests.cs index 0d6aad96e5df..2a674b942c17 100644 --- a/csharp/test/Apache.Arrow.Tests/Date32ArrayTests.cs +++ b/csharp/test/Apache.Arrow.Tests/Date32ArrayTests.cs @@ -31,6 +31,11 @@ public static IEnumerable GetDateTimesData() => public static IEnumerable GetDateTimeOffsetsData() => TestDateAndTimeData.ExampleDateTimeOffsets.Select(dto => new object[] { dto }); +#if NET6_0_OR_GREATER + public static IEnumerable GetDateOnlyData() => + TestDateAndTimeData.ExampleDates.Select(d => new object[] { DateOnly.FromDateTime(d) }); +#endif + public class AppendNull { [Fact] @@ -121,5 +126,32 @@ public void AppendGivesUtcDate(DateTimeOffset dateTimeOffset) Assert.Equal(expectedValue, array.GetValue(0)); } } + +#if NET6_0_OR_GREATER + public class AppendDateOnly + { + [Theory] + [MemberData(nameof(GetDateOnlyData), MemberType = typeof(Date64ArrayTests))] + public void AppendDateGivesSameDate(DateOnly date) + { + // Arrange + var builder = new Date32Array.Builder(); + var expectedDateTime = date.ToDateTime(TimeOnly.MinValue); + var expectedDateTimeOffset = new DateTimeOffset(expectedDateTime, TimeSpan.Zero); + int expectedValue = date.DayNumber - new DateOnly(1970, 1, 1).DayNumber; + + // Act + builder = builder.Append(date); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Equal(date, array.GetDateOnly(0)); + Assert.Equal(expectedDateTime, array.GetDateTime(0)); + Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0)); + Assert.Equal(expectedValue, array.GetValue(0)); + } + } +#endif } } diff --git a/csharp/test/Apache.Arrow.Tests/Date64ArrayTests.cs b/csharp/test/Apache.Arrow.Tests/Date64ArrayTests.cs index 65cffc84e555..22ae08a617c4 100644 --- a/csharp/test/Apache.Arrow.Tests/Date64ArrayTests.cs +++ b/csharp/test/Apache.Arrow.Tests/Date64ArrayTests.cs @@ -33,6 +33,11 @@ public static IEnumerable GetDateTimesData() => public static IEnumerable GetDateTimeOffsetsData() => TestDateAndTimeData.ExampleDateTimeOffsets.Select(dto => new object[] { dto }); +#if NET6_0_OR_GREATER + public static IEnumerable GetDateOnlyData() => + TestDateAndTimeData.ExampleDates.Select(d => new object[] { DateOnly.FromDateTime(d) }); +#endif + public class AppendNull { [Fact] @@ -129,5 +134,33 @@ public void AppendGivesUtcDate(DateTimeOffset dateTimeOffset) Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay); } } + +#if NET6_0_OR_GREATER + public class AppendDateOnly + { + [Theory] + [MemberData(nameof(GetDateOnlyData), MemberType = typeof(Date64ArrayTests))] + public void AppendDateGivesSameDate(DateOnly date) + { + // Arrange + var builder = new Date64Array.Builder(); + var expectedDateTime = date.ToDateTime(TimeOnly.MinValue); + var expectedDateTimeOffset = new DateTimeOffset(expectedDateTime, TimeSpan.Zero); + long expectedValue = (date.DayNumber - new DateOnly(1970, 1, 1).DayNumber) * MillisecondsPerDay; + + // Act + builder = builder.Append(date); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Equal(date, array.GetDateOnly(0)); + Assert.Equal(expectedDateTime, array.GetDateTime(0)); + Assert.Equal(expectedDateTimeOffset, array.GetDateTimeOffset(0)); + Assert.Equal(expectedValue, array.GetValue(0)); + Assert.Equal(0, array.GetValue(0).Value % MillisecondsPerDay); + } + } +#endif } } diff --git a/csharp/test/Apache.Arrow.Tests/TestDateAndTimeData.cs b/csharp/test/Apache.Arrow.Tests/TestDateAndTimeData.cs index 1f2eae45b039..c258fdd2d698 100644 --- a/csharp/test/Apache.Arrow.Tests/TestDateAndTimeData.cs +++ b/csharp/test/Apache.Arrow.Tests/TestDateAndTimeData.cs @@ -59,6 +59,11 @@ from date in _exampleDates from kind in _exampleKinds select DateTime.SpecifyKind(date, kind); + /// + /// Gets a collection of example times + /// + public static IEnumerable ExampleTimes => _exampleTimes; + /// /// Gets a collection of example date/times, of all different kinds. /// diff --git a/csharp/test/Apache.Arrow.Tests/TimeOnlyTests.cs b/csharp/test/Apache.Arrow.Tests/TimeOnlyTests.cs new file mode 100644 index 000000000000..cd66530a0e93 --- /dev/null +++ b/csharp/test/Apache.Arrow.Tests/TimeOnlyTests.cs @@ -0,0 +1,111 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Linq; +using Apache.Arrow.Types; +using Xunit; + +namespace Apache.Arrow.Tests +{ + public class TimeOnlyTests + { + private static IEnumerable GetTimeOnlyData(params TimeUnit[] units) => + from time in TestDateAndTimeData.ExampleTimes + from unit in units + select new object[] { TimeOnly.FromTimeSpan(time), unit }; + + public class Time32 + { + public static IEnumerable GetTestData => GetTimeOnlyData(TimeUnit.Second, TimeUnit.Millisecond); + + [Fact] + public void AppendThenGetGivesNull() + { + // Arrange + var builder = new Time32Array.Builder(); + + // Act + builder = builder.AppendNull(); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Null(array.GetTime(0)); + Assert.Null(array.GetValue(0)); + } + + [Theory] + [MemberData(nameof(GetTestData))] + public void AppendTimeGivesSameTime(TimeOnly time, TimeUnit timeUnit) + { + // Arrange + var builder = new Time32Array.Builder(timeUnit); + var expectedTime = time; + int expectedMilliseconds = (int)(time.Ticks / TimeSpan.TicksPerMillisecond); + + // Act + builder = builder.Append(time); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Equal(expectedTime, array.GetTime(0)); + Assert.Equal(expectedMilliseconds, array.GetMilliSeconds(0)); + } + } + + public class Time64 + { + public static IEnumerable GetTestData => GetTimeOnlyData(TimeUnit.Microsecond, TimeUnit.Nanosecond); + + [Fact] + public void AppendThenGetGivesNull() + { + // Arrange + var builder = new Time64Array.Builder(); + + // Act + builder = builder.AppendNull(); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Null(array.GetTime(0)); + Assert.Null(array.GetValue(0)); + } + + [Theory] + [MemberData(nameof(GetTestData))] + public void AppendTimeGivesSameTime(TimeOnly time, TimeUnit timeUnit) + { + // Arrange + var builder = new Time64Array.Builder(timeUnit); + var expectedTime = time; + long expectedNanoseconds = time.Ticks * TimeSpan.NanosecondsPerTick; + + // Act + builder = builder.Append(time); + + // Assert + var array = builder.Build(); + Assert.Equal(1, array.Length); + Assert.Equal(expectedTime, array.GetTime(0)); + Assert.Equal(expectedNanoseconds, array.GetNanoSeconds(0)); + } + } + } +}