Skip to content

Commit

Permalink
feat(csharp): Translate time to either TimeSpan or TimeOnly (#1293)
Browse files Browse the repository at this point in the history
  • Loading branch information
CurtHagenlocher committed Nov 14, 2023
1 parent da364d6 commit 4b9eec4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
27 changes: 25 additions & 2 deletions csharp/src/Apache.Arrow.Adbc/AdbcStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/

using System;
using System.IO;
using System.Threading.Tasks;
using Apache.Arrow.Ipc;
using Apache.Arrow.Types;

namespace Apache.Arrow.Adbc
{
Expand Down Expand Up @@ -181,10 +183,31 @@ public virtual object GetValue(IArrowArray arrowArray, Field field, int index)
return int64Array.GetValue(index);
case StringArray stringArray:
return stringArray.GetString(index);
#if NET6_0_OR_GREATER
case Time32Array time32Array:
return time32Array.GetValue(index);
return time32Array.GetTime(index);
case Time64Array time64Array:
return time64Array.GetValue(index);
return time64Array.GetTime(index);
#else
case Time32Array time32Array:
int? time32 = time32Array.GetValue(index);
if (time32 == null) { return null; }
return ((Time32Type)time32Array.Data.DataType).Unit switch
{
TimeUnit.Second => TimeSpan.FromSeconds(time32.Value),
TimeUnit.Millisecond => TimeSpan.FromMilliseconds(time32.Value),
_ => throw new InvalidDataException("Unsupported time unit for Time32Type")
};
case Time64Array time64Array:
long? time64 = time64Array.GetValue(index);
if (time64 == null) { return null; }
return ((Time64Type)time64Array.Data.DataType).Unit switch
{
TimeUnit.Microsecond => TimeSpan.FromTicks(time64.Value * 10),
TimeUnit.Nanosecond => TimeSpan.FromTicks(time64.Value / 100),
_ => throw new InvalidDataException("Unsupported time unit for Time64Type")
};
#endif
case TimestampArray timestampArray:
return timestampArray.GetTimestamp(index);
case UInt8Array uInt8Array:
Expand Down
8 changes: 7 additions & 1 deletion csharp/src/Client/SchemaConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ public static Type ConvertArrowType(Field f, DecimalBehavior decimalBehavior)
case ArrowTypeId.Decimal256:
return typeof(string);

#if NET6_0_OR_GREATER
case ArrowTypeId.Time32:
case ArrowTypeId.Time64:
return typeof(long);
return typeof(TimeOnly);
#else
case ArrowTypeId.Time32:
case ArrowTypeId.Time64:
return typeof(TimeSpan);
#endif

case ArrowTypeId.Date32:
case ArrowTypeId.Date64:
Expand Down
12 changes: 10 additions & 2 deletions csharp/test/Drivers/BigQuery/BigQueryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public static SampleDataBuilder GetSampleData()
new ColumnNetTypeArrowTypeValue("name", typeof(string), typeof(StringType), "John Doe"),
new ColumnNetTypeArrowTypeValue("data", typeof(byte[]), typeof(BinaryType), UTF8Encoding.UTF8.GetBytes("abc123")),
new ColumnNetTypeArrowTypeValue("date", typeof(DateTime), typeof(Date64Type), new DateTime(2023, 9, 8)),
new ColumnNetTypeArrowTypeValue("time", typeof(long), typeof(Time64Type), 45296000000L), //'12:34:56'
#if NET6_0_OR_GREATER
new ColumnNetTypeArrowTypeValue("time", typeof(TimeOnly), typeof(Time64Type), new TimeOnly(12, 34, 56)), //'12:34:56'
#else
new ColumnNetTypeArrowTypeValue("time", typeof(TimeSpan), typeof(Time64Type), new TimeSpan(12, 34, 56)),
#endif
new ColumnNetTypeArrowTypeValue("datetime", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023, 9, 8, 12, 34, 56), TimeSpan.Zero)),
new ColumnNetTypeArrowTypeValue("timestamp", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023, 9, 8, 12, 34, 56), TimeSpan.Zero)),
new ColumnNetTypeArrowTypeValue("point", typeof(string), typeof(StringType), "POINT(1 2)"),
Expand Down Expand Up @@ -124,7 +128,11 @@ public static SampleDataBuilder GetSampleData()
new ColumnNetTypeArrowTypeValue("name", typeof(string), typeof(StringType), null),
new ColumnNetTypeArrowTypeValue("data", typeof(byte[]), typeof(BinaryType), null),
new ColumnNetTypeArrowTypeValue("date", typeof(DateTime), typeof(Date64Type), null),
new ColumnNetTypeArrowTypeValue("time", typeof(long), typeof(Time64Type), null),
#if NET6_0_OR_GREATER
new ColumnNetTypeArrowTypeValue("time", typeof(TimeOnly), typeof(Time64Type), null),
#else
new ColumnNetTypeArrowTypeValue("time", typeof(TimeSpan), typeof(Time64Type), null),
#endif
new ColumnNetTypeArrowTypeValue("datetime", typeof(DateTimeOffset), typeof(TimestampType), null),
new ColumnNetTypeArrowTypeValue("timestamp", typeof(DateTimeOffset), typeof(TimestampType), null),
new ColumnNetTypeArrowTypeValue("point", typeof(string), typeof(StringType), null),
Expand Down
12 changes: 10 additions & 2 deletions csharp/test/Drivers/Snowflake/SnowflakeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ public static SampleDataBuilder GetSampleData()
new ColumnNetTypeArrowTypeValue("BOOLEANTYPE", typeof(bool), typeof(BooleanType), true),
new ColumnNetTypeArrowTypeValue("DATETYPE", typeof(DateTime), typeof(Date32Type), new DateTime(2023, 7, 28)),
new ColumnNetTypeArrowTypeValue("DATETIMETYPE", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023,7,28, 12,34,56), TimeSpan.Zero)),
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(long), typeof(Time64Type), 45296000000000L),
#if NET6_0_OR_GREATER
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(TimeOnly), typeof(Time64Type), new TimeOnly(12, 34, 56)),
#else
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(TimeSpan), typeof(Time64Type), new TimeSpan(12, 34, 56)),
#endif
new ColumnNetTypeArrowTypeValue("TIMESTAMPTYPE", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023,7,28,12,34,56), TimeSpan.Zero)),
new ColumnNetTypeArrowTypeValue("TIMESTAMPLTZTYPE", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023,7,28,19,34,56), TimeSpan.Zero)),
new ColumnNetTypeArrowTypeValue("TIMESTAMPNTZTYPE", typeof(DateTimeOffset), typeof(TimestampType), new DateTimeOffset(new DateTime(2023,7,28, 12,34,56), TimeSpan.Zero)),
Expand Down Expand Up @@ -137,7 +141,11 @@ public static SampleDataBuilder GetSampleData()
new ColumnNetTypeArrowTypeValue("BINARYTYPE", typeof(byte[]), typeof(BinaryType), null),
new ColumnNetTypeArrowTypeValue("BOOLEANTYPE", typeof(bool), typeof(BooleanType), null),
new ColumnNetTypeArrowTypeValue("DATETYPE", typeof(DateTime), typeof(Date32Type), null),
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(long), typeof(Time64Type), null),
#if NET6_0_OR_GREATER
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(TimeOnly), typeof(Time64Type), null),
#else
new ColumnNetTypeArrowTypeValue("TIMETYPE", typeof(TimeSpan), typeof(Time64Type), null),
#endif
new ColumnNetTypeArrowTypeValue("TIMESTAMPTYPE", typeof(DateTimeOffset), typeof(TimestampType), null),
new ColumnNetTypeArrowTypeValue("TIMESTAMPLTZTYPE", typeof(DateTimeOffset), typeof(TimestampType), null),
new ColumnNetTypeArrowTypeValue("TIMESTAMPNTZTYPE", typeof(DateTimeOffset), typeof(TimestampType), null),
Expand Down

0 comments on commit 4b9eec4

Please sign in to comment.