You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "ChristophMajcenAtXxxlutz (via GitHub)" <gi...@apache.org> on 2026/04/27 10:25:23 UTC

[PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

ChristophMajcenAtXxxlutz opened a new pull request, #3744:
URL: https://github.com/apache/avro/pull/3744

   …t scale
   
   <!--
   
   *Thank you very much for contributing to Apache Avro - we are happy that you want to help us improve Avro. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Avro a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/AVRO/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "AVRO-XXXX: [component] Title of the pull request", where *AVRO-XXXX* should be replaced by the actual issue number. 
       The *component* is optional, but can help identify the correct reviewers faster: either the language ("java", "python") or subsystem such as "build" or "doc" are good candidates.  
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests. You can [build the entire project](https://github.com/apache/avro/blob/main/BUILD.md) or just the [language-specific SDK](https://avro.apache.org/project/how-to-contribute/#unit-tests).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Every commit message references Jira issues in their subject lines. In addition, commits follow the guidelines from [How to write a good git commit message](https://chris.beams.io/posts/git-commit/)
       1. Subject is separated from body by a blank line
       1. Subject is limited to 50 characters (not including Jira issue reference)
       1. Subject does not end with a period
       1. Subject uses the imperative mood ("add", not "adding")
       1. Body wraps at 72 characters
       1. Body explains "what" and "why", not "how"
   
   -->
   
   ## What is the purpose of the change
   
   Fixes AVRO-4162
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
   - Added tests in AvroDecimalTests that verify correct behaviour
   
   ## Documentation
   
   - Does this pull request introduce a new feature?  no
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "RyanSkraba (via GitHub)" <gi...@apache.org>.
RyanSkraba commented on code in PR #3744:
URL: https://github.com/apache/avro/pull/3744#discussion_r3176352337


##########
lang/csharp/src/apache/main/AvroDecimal.cs:
##########
@@ -1140,20 +1140,14 @@ public int CompareTo(AvroDecimal other)
             var unscaledValueCompare = UnscaledValue.CompareTo(other.UnscaledValue);
             var scaleCompare = Scale.CompareTo(other.Scale);
 
-            // if both are the same value, return the value
-            if (unscaledValueCompare == scaleCompare)
-            {
-                return unscaledValueCompare;
-            }
-
             // if the scales are both the same return unscaled value
             if (scaleCompare == 0)
             {
                 return unscaledValueCompare;
             }
 
-            var scaledValue = BigInteger.Divide(UnscaledValue, BigInteger.Pow(new BigInteger(10), Scale));
-            var otherScaledValue = BigInteger.Divide(other.UnscaledValue, BigInteger.Pow(new BigInteger(10), other.Scale));
+            var scaledValue = (decimal) UnscaledValue / (decimal) Math.Pow(10, Scale);

Review Comment:
   Am I right in thinking the old version *only* compared the two as whole numbers?  As in `2.5` ==`2.51` ?  This looks better to me! 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "ChristophMajcenAtXxxlutz (via GitHub)" <gi...@apache.org>.
ChristophMajcenAtXxxlutz commented on code in PR #3744:
URL: https://github.com/apache/avro/pull/3744#discussion_r3147425266


##########
lang/csharp/src/apache/test/AvroDecimalTest.cs:
##########
@@ -23,17 +23,18 @@ namespace Avro.test
     [TestFixture]
     class AvroDecimalTest
     {
-        [TestCase(1)]
-        [TestCase(1000)]
-        [TestCase(10.10)]
-        [TestCase(0)]
-        [TestCase(0.1)]
-        [TestCase(0.01)]
-        [TestCase(-1)]
-        [TestCase(-1000)]
-        [TestCase(-10.10)]
-        [TestCase(-0.1)]
-        [TestCase(-0.01)]
+        //Use strings as parameters as otherwise doubles will be used intermediately by C# and scale will be lost in this process

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "RyanSkraba (via GitHub)" <gi...@apache.org>.
RyanSkraba merged PR #3744:
URL: https://github.com/apache/avro/pull/3744


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "KalleOlaviNiemitalo (via GitHub)" <gi...@apache.org>.
KalleOlaviNiemitalo commented on code in PR #3744:
URL: https://github.com/apache/avro/pull/3744#discussion_r3147204038


##########
lang/csharp/src/apache/test/AvroDecimalTest.cs:
##########
@@ -23,17 +23,18 @@ namespace Avro.test
     [TestFixture]
     class AvroDecimalTest
     {
-        [TestCase(1)]
-        [TestCase(1000)]
-        [TestCase(10.10)]
-        [TestCase(0)]
-        [TestCase(0.1)]
-        [TestCase(0.01)]
-        [TestCase(-1)]
-        [TestCase(-1000)]
-        [TestCase(-10.10)]
-        [TestCase(-0.1)]
-        [TestCase(-0.01)]
+        //Use strings as parameters as otherwise doubles will be used intermediately by C# and scale will be lost in this process

Review Comment:
   This may make the test culture-sensitive.  I suggest changing the parameters to `string` and explicitly calling `decimal.Parse` with `CultureInfo.InvariantCulture` within the test methods.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "ChristophMajcenAtXxxlutz (via GitHub)" <gi...@apache.org>.
ChristophMajcenAtXxxlutz commented on code in PR #3744:
URL: https://github.com/apache/avro/pull/3744#discussion_r3180105808


##########
lang/csharp/src/apache/main/AvroDecimal.cs:
##########
@@ -1140,20 +1140,14 @@ public int CompareTo(AvroDecimal other)
             var unscaledValueCompare = UnscaledValue.CompareTo(other.UnscaledValue);
             var scaleCompare = Scale.CompareTo(other.Scale);
 
-            // if both are the same value, return the value
-            if (unscaledValueCompare == scaleCompare)
-            {
-                return unscaledValueCompare;
-            }
-
             // if the scales are both the same return unscaled value
             if (scaleCompare == 0)
             {
                 return unscaledValueCompare;
             }
 
-            var scaledValue = BigInteger.Divide(UnscaledValue, BigInteger.Pow(new BigInteger(10), Scale));
-            var otherScaledValue = BigInteger.Divide(other.UnscaledValue, BigInteger.Pow(new BigInteger(10), other.Scale));
+            var scaledValue = (decimal) UnscaledValue / (decimal) Math.Pow(10, Scale);

Review Comment:
   That's one thing it did wrong, yes. Additionally when comparing for example 2.5 vs 2.49 it used to compare the unscaled values (25 vs 249 -> right side bigger) and the scales (1 vs 2 -> right side bigger) and if both comparisons returned the same value would return it as a result therefore returning that 2.49 is bigger than 2.5.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AVRO-4162: [csharp] Fix AvroDecimal.CompareTo for values with different scales [avro]

Posted by "RyanSkraba (via GitHub)" <gi...@apache.org>.
RyanSkraba commented on PR #3744:
URL: https://github.com/apache/avro/pull/3744#issuecomment-4363331778

   Cherry-picked to [branch-1.12](https://github.com/apache/avro/commit/a4124a76e11bb3a1a011bd4b9df379efad0d27c4).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org