westonpace commented on code in PR #36125:
URL: https://github.com/apache/arrow/pull/36125#discussion_r1265754312
##########
csharp/src/Apache.Arrow/Arrays/Date32Array.cs:
##########
@@ -108,5 +118,21 @@ public Date32Array(ArrayData data)
? new DateTimeOffset(_epochDate.AddDays(value.Value),
TimeSpan.Zero)
: default(DateTimeOffset?);
}
+
+#if NET6_0_OR_GREATER
+ /// <summary>
+ /// Get the date at the specified index
+ /// </summary>
+ /// <param name="index">Index at which to get the date.</param>
+ /// <returns>Returns a <see cref="DateOnly" />, or <c>null</c> if
there is no object at that index.
+ /// </returns>
+ public DateOnly? GetDateOnly(int index)
+ {
+ int? value = GetValue(index);
+ return value.HasValue
+ ? DateOnly.FromDayNumber(_epochDayNumber + value.Value)
Review Comment:
What happens if `value.Value` is out of range of `DateOnly`? I assume some
kind of exception?
##########
csharp/src/Apache.Arrow/Arrays/Time64Array.cs:
##########
@@ -24,17 +25,25 @@ namespace Apache.Arrow
/// </summary>
public class Time64Array : PrimitiveArray<long>
{
+ private const long TicksPerMicrosecond = 1000;
+ private const long NanosecondsPerTick = 100;
Review Comment:
I'm not sure I follow. If 1 tick = 100 nanoseconds then wouldn't there only
be 10 ticks in 1 microsecond (1 microsecond is 1000 nanoseconds)?
##########
csharp/src/Apache.Arrow/Arrays/Time64Array.cs:
##########
@@ -113,5 +134,33 @@ public Time64Array(ArrayData data)
_ => throw new InvalidDataException($"Unsupported time unit
for Time64Type: {unit}")
};
}
+
+#if NET6_0_OR_GREATER
+ /// <summary>
+ /// Get the time at the specified index as <see cref="TimeOnly"/>
+ /// </summary>
+ /// <remarks>
+ /// This may cause truncation of nanosecond values, as the resolution
of TimeOnly is in 100-ns increments.
+ /// </remarks>
+ /// <param name="index">Index at which to get the time.</param>
+ /// <returns>Returns a <see cref="TimeOnly" />, or <c>null</c> if
there is no object at that index.
+ /// </returns>
+ 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),
Review Comment:
Should this be rounded? Although I guess you mention it might be truncated
above.
##########
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<object[]> 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<object[]> 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<object[]> 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)
Review Comment:
Can you clean up these lint checks (possibly just remove `timeUnit`)?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]