Skip to content

Commit

Permalink
apacheGH-39223: [C#] Support IReadOnlyList<T?> on remaining scalar ty…
Browse files Browse the repository at this point in the history
…pes (apache#39224)

### What changes are included in this PR?

Decimal128Array implements IReadOnlyList<SqlDecimal?> and IReadOnlyList<byte[]>.
Decimal256Array implements IReadOnlyList<SqlDecimal?>, IReadOnlyList<string> and IReadOnlyList<byte[]>.
FixedLengthBinaryArray implements IReadOnlyList<byte[]>.
DurationArray implements IReadOnlyList<TimeSpan?>.

Also removes #ifs which are no longer relevant now that netstandard13 isn't being built any more.

### Are these changes tested?

Yes.
* Closes: apache#39223

Authored-by: Curt Hagenlocher <curt@hagenlocher.org>
Signed-off-by: Curt Hagenlocher <curt@hagenlocher.org>
  • Loading branch information
CurtHagenlocher authored Dec 14, 2023
1 parent 4e58f7c commit 431c4ea
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 65 deletions.
23 changes: 15 additions & 8 deletions csharp/src/Apache.Arrow/Arrays/Decimal128Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
#if !NETSTANDARD1_3
using System.Data.SqlTypes;
#endif
using System.Diagnostics;
using System.Numerics;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public class Decimal128Array : FixedSizeBinaryArray
public class Decimal128Array : FixedSizeBinaryArray, IReadOnlyList<SqlDecimal?>
{
public class Builder : BuilderBase<Decimal128Array, Builder>
{
Expand Down Expand Up @@ -95,7 +93,6 @@ public Builder AppendRange(IEnumerable<string> values)
return Instance;
}

#if !NETSTANDARD1_3
public Builder Append(SqlDecimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
Expand All @@ -118,7 +115,6 @@ public Builder AppendRange(IEnumerable<SqlDecimal> values)

return Instance;
}
#endif

public Builder Set(int index, decimal value)
{
Expand Down Expand Up @@ -184,7 +180,6 @@ public string GetString(int index)
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
}

#if !NETSTANDARD1_3
public SqlDecimal? GetSqlDecimal(int index)
{
if (IsNull(index))
Expand All @@ -194,6 +189,18 @@ public string GetString(int index)

return DecimalUtility.GetSqlDecimal128(ValueBuffer, index, Precision, Scale);
}
#endif

int IReadOnlyCollection<SqlDecimal?>.Count => Length;
SqlDecimal? IReadOnlyList<SqlDecimal?>.this[int index] => GetSqlDecimal(index);

IEnumerator<SqlDecimal?> IEnumerable<SqlDecimal?>.GetEnumerator()
{
for (int index = 0; index < Length; index++)
{
yield return GetSqlDecimal(index);
}
}

IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<SqlDecimal>)this).GetEnumerator();
}
}
44 changes: 37 additions & 7 deletions csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
#if !NETSTANDARD1_3
using System.Data.SqlTypes;
#endif
using System.Diagnostics;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public class Decimal256Array : FixedSizeBinaryArray
public class Decimal256Array : FixedSizeBinaryArray, IReadOnlyList<SqlDecimal?>, IReadOnlyList<string>
{
public class Builder : BuilderBase<Decimal256Array, Builder>
{
Expand Down Expand Up @@ -94,7 +93,6 @@ public Builder AppendRange(IEnumerable<string> values)
return Instance;
}

#if !NETSTANDARD1_3
public Builder Append(SqlDecimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
Expand Down Expand Up @@ -123,7 +121,6 @@ public Builder AppendRange(IEnumerable<SqlDecimal> values)

return Instance;
}
#endif

public Builder Set(int index, decimal value)
{
Expand Down Expand Up @@ -190,7 +187,6 @@ public string GetString(int index)
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
}

#if !NETSTANDARD1_3
public bool TryGetSqlDecimal(int index, out SqlDecimal? value)
{
if (IsNull(index))
Expand All @@ -211,6 +207,40 @@ public bool TryGetSqlDecimal(int index, out SqlDecimal? value)
value = null;
return false;
}
#endif

private SqlDecimal? GetSqlDecimal(int index)
{
SqlDecimal? value;
if (TryGetSqlDecimal(index, out value))
{
return value;
}

throw new OverflowException("decimal256 value out of range of SqlDecimal");
}

int IReadOnlyCollection<SqlDecimal?>.Count => Length;
SqlDecimal? IReadOnlyList<SqlDecimal?>.this[int index] => GetSqlDecimal(index);

IEnumerator<SqlDecimal?> IEnumerable<SqlDecimal?>.GetEnumerator()
{
for (int index = 0; index < Length; index++)
{
yield return GetSqlDecimal(index);
}
}

int IReadOnlyCollection<string>.Count => Length;
string? IReadOnlyList<string>.this[int index] => GetString(index);

IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
for (int index = 0; index < Length; index++)
{
yield return GetString(index);
}
}

IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<string>)this).GetEnumerator();
}
}
17 changes: 16 additions & 1 deletion csharp/src/Apache.Arrow/Arrays/DurationArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public class DurationArray : PrimitiveArray<long>
public class DurationArray : PrimitiveArray<long>, IReadOnlyList<TimeSpan?>
{
public class Builder : PrimitiveArrayBuilder<long, DurationArray, Builder>
{
Expand Down Expand Up @@ -80,5 +82,18 @@ public DurationArray(ArrayData data)
}

public override void Accept(IArrowArrayVisitor visitor) => Accept(this, visitor);

int IReadOnlyCollection<TimeSpan?>.Count => Length;
TimeSpan? IReadOnlyList<TimeSpan?>.this[int index] => GetTimeSpan(index);

IEnumerator<TimeSpan?> IEnumerable<TimeSpan?>.GetEnumerator()
{
for (int index = 0; index < Length; index++)
{
yield return GetTimeSpan(index);
}
}

IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<TimeSpan?>)this).GetEnumerator();
}
}
17 changes: 15 additions & 2 deletions csharp/src/Apache.Arrow/Arrays/FixedSizeBinaryArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
// limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using Apache.Arrow.Memory;
using Apache.Arrow.Types;

namespace Apache.Arrow.Arrays
{
public class FixedSizeBinaryArray : Array
public class FixedSizeBinaryArray : Array, IReadOnlyList<byte[]>
{
public FixedSizeBinaryArray(ArrayData data)
: base(data)
Expand Down Expand Up @@ -70,6 +71,19 @@ public ReadOnlySpan<byte> GetBytes(int index)
return ValueBuffer.Span.Slice(index * size, size);
}

int IReadOnlyCollection<byte[]>.Count => Length;
byte[] IReadOnlyList<byte[]>.this[int index] => GetBytes(index).ToArray();

IEnumerator<byte[]> IEnumerable<byte[]>.GetEnumerator()
{
for (int index = 0; index < Length; index++)
{
yield return GetBytes(index).ToArray();
}
}

IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<byte[]>)this).GetEnumerator();

public abstract class BuilderBase<TArray, TBuilder> : IArrowArrayBuilder<byte[], TArray, TBuilder>
where TArray : IArrowArray
where TBuilder : class, IArrowArrayBuilder<byte[], TArray, TBuilder>
Expand Down Expand Up @@ -220,7 +234,6 @@ public TBuilder SetNull(int index)
ValidityBuffer.Set(index, false);
return Instance;
}

}
}
}
6 changes: 0 additions & 6 deletions csharp/src/Apache.Arrow/DecimalUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
// limitations under the License.

using System;
#if !NETSTANDARD1_3
using System.Data.SqlTypes;
#endif
using System.Numerics;

namespace Apache.Arrow
Expand Down Expand Up @@ -183,7 +181,6 @@ internal unsafe static string GetString(in ArrowBuffer valueBuffer, int index, i
}
#endif

#if !NETSTANDARD1_3
internal static SqlDecimal GetSqlDecimal128(in ArrowBuffer valueBuffer, int index, int precision, int scale)
{
const int byteWidth = 16;
Expand All @@ -207,7 +204,6 @@ internal static SqlDecimal GetSqlDecimal128(in ArrowBuffer valueBuffer, int inde
return new SqlDecimal((byte)precision, (byte)scale, false, (int)(data1 & 0xffffffff), (int)(data1 >> 32), (int)(data2 & 0xffffffff), (int)(data2 >> 32));
}
}
#endif

private static decimal DivideByScale(BigInteger integerValue, int scale)
{
Expand Down Expand Up @@ -428,7 +424,6 @@ internal static void GetBytes(string value, int precision, int scale, int byteWi
}
}

#if !NETSTANDARD1_3
internal static void GetBytes(SqlDecimal value, int precision, int scale, Span<byte> bytes)
{
if (value.Precision != precision || value.Scale != scale)
Expand All @@ -446,6 +441,5 @@ internal static void GetBytes(SqlDecimal value, int precision, int scale, Span<b
longSpan[1] = (longSpan[0] == 0) ? -longSpan[1] : ~longSpan[1];
}
}
#endif
}
}
25 changes: 7 additions & 18 deletions csharp/test/Apache.Arrow.Tests/Decimal128ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@
// limitations under the License.

using System;
#if !NETSTANDARD1_3
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
#endif
using Apache.Arrow.Types;
using Xunit;

namespace Apache.Arrow.Tests
{
public class Decimal128ArrayTests
{
#if !NETSTANDARD1_3
static SqlDecimal? Convert(decimal? value)
{
return value == null ? null : new SqlDecimal(value.Value);
Expand All @@ -35,7 +33,6 @@ public class Decimal128ArrayTests
{
return value == null ? null : value.Value.Value;
}
#endif

public class Builder
{
Expand All @@ -61,11 +58,9 @@ public void AppendThenGetGivesNull()
Assert.Null(array.GetValue(1));
Assert.Null(array.GetValue(2));

#if !NETSTANDARD1_3
Assert.Null(array.GetSqlDecimal(0));
Assert.Null(array.GetSqlDecimal(1));
Assert.Null(array.GetSqlDecimal(2));
#endif
}
}

Expand Down Expand Up @@ -99,9 +94,7 @@ public void AppendDecimal(int count)
for (int i = 0; i < count; i++)
{
Assert.Equal(testData[i], array.GetValue(i));
#if !NETSTANDARD1_3
Assert.Equal(Convert(testData[i]), array.GetSqlDecimal(i));
#endif
}
}

Expand All @@ -120,10 +113,8 @@ public void AppendLargeDecimal()
Assert.Equal(large, array.GetValue(0));
Assert.Equal(-large, array.GetValue(1));

#if !NETSTANDARD1_3
Assert.Equal(Convert(large), array.GetSqlDecimal(0));
Assert.Equal(Convert(-large), array.GetSqlDecimal(1));
#endif
}

[Fact]
Expand All @@ -145,12 +136,10 @@ public void AppendMaxAndMinDecimal()
Assert.Equal(Decimal.MaxValue - 10, array.GetValue(2));
Assert.Equal(Decimal.MinValue + 10, array.GetValue(3));

#if !NETSTANDARD1_3
Assert.Equal(Convert(Decimal.MaxValue), array.GetSqlDecimal(0));
Assert.Equal(Convert(Decimal.MinValue), array.GetSqlDecimal(1));
Assert.Equal(Convert(Decimal.MaxValue) - 10, array.GetSqlDecimal(2));
Assert.Equal(Convert(Decimal.MinValue) + 10, array.GetSqlDecimal(3));
#endif
}

[Fact]
Expand All @@ -168,10 +157,8 @@ public void AppendFractionalDecimal()
Assert.Equal(fraction, array.GetValue(0));
Assert.Equal(-fraction, array.GetValue(1));

#if !NETSTANDARD1_3
Assert.Equal(Convert(fraction), array.GetSqlDecimal(0));
Assert.Equal(Convert(-fraction), array.GetSqlDecimal(1));
#endif
}

[Fact]
Expand All @@ -190,9 +177,7 @@ public void AppendRangeDecimal()
for (int i = 0; i < range.Length; i++)
{
Assert.Equal(range[i], array.GetValue(i));
#if !NETSTANDARD1_3
Assert.Equal(Convert(range[i]), array.GetSqlDecimal(i));
#endif
}

Assert.Null(array.GetValue(range.Length));
Expand Down Expand Up @@ -301,7 +286,6 @@ public void SwapNull()
}
}

#if !NETSTANDARD1_3
public class SqlDecimals
{
[Theory]
Expand Down Expand Up @@ -335,6 +319,12 @@ public void AppendSqlDecimal(int count)
Assert.Equal(testData[i], array.GetSqlDecimal(i));
Assert.Equal(Convert(testData[i]), array.GetValue(i));
}

IReadOnlyList<SqlDecimal?> asList = array;
for (int i = 0; i < asList.Count; i++)
{
Assert.Equal(testData[i], asList[i]);
}
}

[Fact]
Expand Down Expand Up @@ -467,7 +457,6 @@ public void AppendRangeSqlDecimal()
Assert.Null(array.GetValue(range.Length));
}
}
#endif
}
}
}
Loading

0 comments on commit 431c4ea

Please sign in to comment.