Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-41199: [C#] Fix accessing values of a sliced decimal array #41200

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions csharp/src/Apache.Arrow/Arrays/Decimal128Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Decimal128Array(ArrayData data)
{
return null;
}
return DecimalUtility.GetDecimal(ValueBuffer, index, Scale, ByteWidth);
return DecimalUtility.GetDecimal(ValueBuffer, Offset + index, Scale, ByteWidth);
}

public IList<decimal?> ToList(bool includeNulls = false)
Expand Down Expand Up @@ -177,7 +177,7 @@ public string GetString(int index)
{
return null;
}
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
return DecimalUtility.GetString(ValueBuffer, Offset + index, Precision, Scale, ByteWidth);
}

public SqlDecimal? GetSqlDecimal(int index)
Expand All @@ -187,7 +187,7 @@ public string GetString(int index)
return null;
}

return DecimalUtility.GetSqlDecimal128(ValueBuffer, index, Precision, Scale);
return DecimalUtility.GetSqlDecimal128(ValueBuffer, Offset + index, Precision, Scale);
}

int IReadOnlyCollection<SqlDecimal?>.Count => Length;
Expand Down
8 changes: 4 additions & 4 deletions csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
return null;
}

return DecimalUtility.GetDecimal(ValueBuffer, index, Scale, ByteWidth);
return DecimalUtility.GetDecimal(ValueBuffer, Offset + index, Scale, ByteWidth);
}

public IList<decimal?> ToList(bool includeNulls = false)
Expand Down Expand Up @@ -184,7 +184,7 @@
{
return null;
}
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
return DecimalUtility.GetString(ValueBuffer, Offset + index, Precision, Scale, ByteWidth);
}

public bool TryGetSqlDecimal(int index, out SqlDecimal? value)
Expand All @@ -196,11 +196,11 @@
}

const int longWidth = 4;
var span = ValueBuffer.Span.CastTo<long>().Slice(index * longWidth);
var span = ValueBuffer.Span.CastTo<long>().Slice((Offset + index) * longWidth);
if ((span[2] == 0 && span[3] == 0) ||
(span[2] == -1 && span[3] == -1))
{
value = DecimalUtility.GetSqlDecimal128(ValueBuffer, 2 * index, Precision, Scale);
value = DecimalUtility.GetSqlDecimal128(ValueBuffer, 2 * (Offset + index), Precision, Scale);
return true;
}

Expand Down Expand Up @@ -231,7 +231,7 @@
}

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

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 macOS 11 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 macOS 11 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 Ubuntu 18.04 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 Ubuntu 18.04 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 Windows 2019 18.04 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 234 in csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs

View workflow job for this annotation

GitHub Actions / AMD64 Windows 2019 18.04 C# 7.0.x

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
Expand Down
43 changes: 43 additions & 0 deletions csharp/test/Apache.Arrow.Tests/Decimal128ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,48 @@ public void AppendRangeSqlDecimal()
}
}
}

[Fact]
public void SliceDecimal128Array()
{
// Arrange
const int originalLength = 50;
const int offset = 3;
const int sliceLength = 32;

var builder = new Decimal128Array.Builder(new Decimal128Type(14, 10));
var random = new Random();

for (int i = 0; i < originalLength; i++)
{
if (random.NextDouble() < 0.2)
{
builder.AppendNull();
}
else
{
builder.Append(i * (decimal)Math.Round(random.NextDouble(), 10));
}
}

var array = builder.Build();

// Act
var slice = (Decimal128Array)array.Slice(offset, sliceLength);

// Assert
Assert.NotNull(slice);
Assert.Equal(sliceLength, slice.Length);
for (int i = 0; i < sliceLength; ++i)
{
Assert.Equal(array.GetValue(offset + i), slice.GetValue(i));
Assert.Equal(array.GetSqlDecimal(offset + i), slice.GetSqlDecimal(i));
Assert.Equal(array.GetString(offset + i), slice.GetString(i));
}

Assert.Equal(
array.ToList(includeNulls: true).Skip(offset).Take(sliceLength).ToList(),
slice.ToList(includeNulls: true));
}
}
}
51 changes: 51 additions & 0 deletions csharp/test/Apache.Arrow.Tests/Decimal256ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,5 +476,56 @@ public void AppendRangeSqlDecimal()
}
}
}

[Fact]
public void SliceDecimal256Array()
{
// Arrange
const int originalLength = 50;
const int offset = 3;
const int sliceLength = 32;

var builder = new Decimal256Array.Builder(new Decimal256Type(14, 10));
var random = new Random();

for (int i = 0; i < originalLength; i++)
{
if (random.NextDouble() < 0.2)
{
builder.AppendNull();
}
else
{
builder.Append(i * (decimal)Math.Round(random.NextDouble(), 10));
}
}

var array = builder.Build();

// Act
var slice = (Decimal256Array)array.Slice(offset, sliceLength);

// Assert
Assert.NotNull(slice);
Assert.Equal(sliceLength, slice.Length);
for (int i = 0; i < sliceLength; ++i)
{
Assert.Equal(array.GetValue(offset + i), slice.GetValue(i));
if (array.TryGetSqlDecimal(offset + i, out var expectedSqlDecimal))
{
Assert.True(slice.TryGetSqlDecimal(i, out var actualSqlDecimal));
Assert.Equal(expectedSqlDecimal, actualSqlDecimal);
}
else
{
Assert.False(slice.TryGetSqlDecimal(i, out _));
}
Assert.Equal(array.GetString(offset + i), slice.GetString(i));
}

Assert.Equal(
array.ToList(includeNulls: true).Skip(offset).Take(sliceLength).ToList(),
slice.ToList(includeNulls: true));
}
}
}
Loading