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-37730: [C#] throw OverflowException in DecimalUtility if fractionalPart is too large #37731

Merged
merged 7 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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: 5 additions & 1 deletion csharp/src/Apache.Arrow/DecimalUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ internal static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int sc
BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart);
if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal
{
throw new OverflowException($"Value: {integerPart} too big or too small to be represented as a decimal");
throw new OverflowException($"Value: {integerValue} is too big or too small to be represented as a decimal");
}
if (fractionalPart > _maxDecimal || fractionalPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal
{
throw new OverflowException($"Value: {integerValue} is too big or too small to be represented as a decimal");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases confused me, but looking at it, it's not necessarily about too big or too small, but rather too precise?

Arrow represents 7.89 in decimal256(76,38) as 789000000000000000000000000000000000000
integerPart is then 7
fractionalPart is 89000000000000000000000000000000000000
and that's just too many digits of precision for decimal?

Which, it is literally too big, but that's a bit confusing unless you are thinking about the representation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separately, I think it would be useful to differentiate the error messages here and possibly include the problematic value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated in latest push

}
return (decimal)integerPart + DivideByScale(fractionalPart, scale);
}
Expand Down
19 changes: 19 additions & 0 deletions csharp/test/Apache.Arrow.Tests/DecimalUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ public void HasExpectedResultOrThrows(decimal d, int precision , int scale, bool
Assert.Equal(d, result.GetValue(0));
}
}

[Theory]
[InlineData(4.56, 38, 9, false)]
[InlineData(7.89, 76, 38, true)]
public void Decimal256HasExpectedResultOrThrows(decimal d, int precision, int scale, bool shouldThrow)
{
var builder = new Decimal256Array.Builder(new Decimal256Type(precision, scale));
builder.Append(d);
Decimal256Array result = builder.Build(new TestMemoryAllocator()); ;

if (shouldThrow)
{
Assert.Throws<OverflowException>(() => result.GetValue(0));
}
else
{
Assert.Equal(d, result.GetValue(0));
}
}
}
}
}
Loading