This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/main by this push:
new 0997c339b3 AVRO-4161: [C#] Fix System.Decimal to Avro decimal
conversion (#3423)
0997c339b3 is described below
commit 0997c339b3c08ada6442e8bbeb1a402c348de7c4
Author: Robert Yokota <[email protected]>
AuthorDate: Fri Jul 11 15:10:27 2025 -0700
AVRO-4161: [C#] Fix System.Decimal to Avro decimal conversion (#3423)
* AVRO-4161: [C#] Fix System.Decimal to Avro decimal conversion
* Fix test
* Add test for negative high precision value
---
lang/csharp/src/apache/main/AvroDecimal.cs | 7 +++++--
lang/csharp/src/apache/test/AvroDecimalTest.cs | 22 +++++++++++++++++++++-
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/lang/csharp/src/apache/main/AvroDecimal.cs
b/lang/csharp/src/apache/main/AvroDecimal.cs
index 98de9b1b13..794617c497 100644
--- a/lang/csharp/src/apache/main/AvroDecimal.cs
+++ b/lang/csharp/src/apache/main/AvroDecimal.cs
@@ -53,8 +53,11 @@ namespace Avro
{
var bytes = GetBytesFromDecimal(value);
- var unscaledValueBytes = new byte[12];
- Array.Copy(bytes, unscaledValueBytes, unscaledValueBytes.Length);
+ // Copy the first 12 bytes of the decimal into an array of size 13
+ // so that the last byte is 0, which is required by the
+ // BigInteger constructor to ensure the unscaled value is positive.
+ var unscaledValueBytes = new byte[13];
+ Array.Copy(bytes, unscaledValueBytes, 12);
var unscaledValue = new BigInteger(unscaledValueBytes);
var scale = bytes[14];
diff --git a/lang/csharp/src/apache/test/AvroDecimalTest.cs
b/lang/csharp/src/apache/test/AvroDecimalTest.cs
index 4a8654aa8a..f90b11a324 100644
--- a/lang/csharp/src/apache/test/AvroDecimalTest.cs
+++ b/lang/csharp/src/apache/test/AvroDecimalTest.cs
@@ -38,10 +38,30 @@ namespace Avro.test
{
var valueString = value.ToString();
- var avroDecimal = new AvroDecimal(value);
+ var avroDecimal = new AvroDecimal(value);
var avroDecimalString = avroDecimal.ToString();
Assert.AreEqual(valueString, avroDecimalString);
}
+
+ [Test]
+ public void TestHighPrecisionAvroDecimalToString()
+ {
+ var value = 4.1748330066797328106875724512m; // High precision
decimal value
+ var valueString = value.ToString();
+
+ var avroDecimal = new AvroDecimal(value);
+ var avroDecimalString = avroDecimal.ToString();
+
+ Assert.AreEqual(valueString, avroDecimalString);
+
+ value = -4.1748330066797328106875724512m; // High precision
decimal value
+ valueString = value.ToString();
+
+ avroDecimal = new AvroDecimal(value);
+ avroDecimalString = avroDecimal.ToString();
+
+ Assert.AreEqual(valueString, avroDecimalString);
+ }
}
}