Skip to content

fix decimal issue#30

Open
liuhhhing wants to merge 2 commits intotrinodb:mainfrom
liuhhhing:fix_decimal
Open

fix decimal issue#30
liuhhhing wants to merge 2 commits intotrinodb:mainfrom
liuhhhing:fix_decimal

Conversation

@liuhhhing
Copy link
Copy Markdown

@liuhhhing liuhhhing commented Oct 31, 2025

Summary by Sourcery

Preserve explicit sign in TrinoBigDecimal to correctly handle negative zero and improve decimal parsing, string conversion, equality, hashing, and conversion to decimal.

New Features:

  • Store an explicit sign field to distinguish negative zero cases
  • Add GetScale, GetPrecision, GetSign, GetIntegerPart, and GetFractionalPart accessors

Bug Fixes:

  • Fix ToString, Equals, and GetHashCode to account for the explicit sign

Enhancements:

  • Uniformly parse and apply sign in both string and BigInteger constructors
  • Unify and simplify ToDecimal implementation with precision validation

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Oct 31, 2025

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to cla@trino.io. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Oct 31, 2025

Reviewer's Guide

This PR refactors the TrinoBigDecimal type to explicitly track and preserve the numeric sign, overhaul the decimal-conversion logic with integrated precision checks, and expose utility getters, while cleaning up legacy conversion code and comments.

Class diagram for updated TrinoBigDecimal structure

classDiagram
    class TrinoBigDecimal {
        - BigInteger integerPart
        - BigInteger fractionalPart
        - int scale
        - int sign
        + TrinoBigDecimal(string value)
        + TrinoBigDecimal(BigInteger integerPart, BigInteger fractionalPart, int scale)
        + decimal ToDecimal()
        + int GetScale()
        + int GetPrecision()
        + int GetSign()
        + BigInteger GetIntegerPart()
        + BigInteger GetFractionalPart()
        + override string ToString()
        + override bool Equals(object obj)
        + override int GetHashCode()
        - void Validate()
        - static void AlignScales(ref TrinoBigDecimal a, ref TrinoBigDecimal b)
    }
Loading

File-Level Changes

Change Details Files
Explicit sign management added to BigDecimal
  • Introduce a sign field to store positive/negative state
  • Detect and strip leading '+'/'-' in string constructor, set sign accordingly
  • Apply sign in ToString, Equals, GetHashCode, and GetIntegerPart
  • Adjust constructors to use absolute integerPart and derive sign
Trino.Client/Types/BigDecimal.cs
Refactored decimal conversion and precision handling
  • Remove obsolete ToDecimal implementation
  • Add a streamlined ToDecimal method that assembles total value then divides by scale
  • Include precision overflow check against decimal’s max precision
  • Implement new getters: GetScale, GetPrecision, GetSign, GetIntegerPart, GetFractionalPart
Trino.Client/Types/BigDecimal.cs
General cleanup and import updates
  • Add System.Globalization import
  • Trim and validate input string in constructor
  • Remove outdated XML comments and redundant code blocks
Trino.Client/Types/BigDecimal.cs

Possibly linked issues

  • #DECIMAL -0.6 can be wrongly transformed to 0.6 in TrinoBigDecimal: PR adds 'sign' member and modifies TrinoBigDecimal logic to preserve negative signs, fixing the decimal transformation issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `trino-csharp/Trino.Client/Types/BigDecimal.cs:22` </location>
<code_context>
+            value = value.Trim();
+
+            // Detect sign explicitly
+            sign = value.StartsWith("-", StringComparison.Ordinal) ? -1 : 1;
+            if (value.StartsWith("+") || value.StartsWith("-"))
+                value = value.Substring(1);
</code_context>

<issue_to_address>
**issue (bug_risk):** Sign detection logic may mishandle strings with multiple sign characters.

Please add validation to ensure only one leading sign character is allowed and the remainder of the string is numeric.
</issue_to_address>

### Comment 2
<location> `trino-csharp/Trino.Client/Types/BigDecimal.cs:27` </location>
<code_context>
+                value = value.Substring(1);
+
             var parts = value.Split('.');
             integerPart = BigInteger.Parse(parts[0]);
             fractionalPart = parts.Length > 1 ? BigInteger.Parse(parts[1]) : BigInteger.Zero;
             scale = parts.Length > 1 ? parts[1].Length : 0;
</code_context>

<issue_to_address>
**issue:** Parsing may throw if integer part is empty after sign removal.

For inputs like '-.123', parts[0] becomes an empty string, which will cause BigInteger.Parse to fail. Please handle cases where the integer part is missing by defaulting it to zero.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@liuhhhing
Copy link
Copy Markdown
Author

I have signed the verification and returned back with ok.

Copy link
Copy Markdown
Member

@stephen-zhao stephen-zhao left a comment

Choose a reason for hiding this comment

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

fyi, the maintainers haven't been active on this repo..

public TrinoBigDecimal(BigInteger integerPart, BigInteger fractionalPart, int scale)
{
this.integerPart = integerPart;
sign = integerPart.Sign < 0 ? -1 : 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
sign = integerPart.Sign < 0 ? -1 : 1;
this.sign = integerPart.Sign < 0 ? -1 : 1;

for style

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Comment on lines -10 to -17

/// <summary>
/// The scale represents the number of digits to the right of the decimal point.
/// It is used to determine the precision of the fractional part of the BigDecimal.
/// </summary>
/// <example>
/// For example, if the BigDecimal is "123.00456", the scale is 5 because there are five digits to the right of the decimal point, including the leading zeros.
/// </example>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add back all these comments? why remove them?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

@stephen-zhao
Copy link
Copy Markdown
Member

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label Nov 22, 2025
@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Nov 22, 2025

The cla-bot has been summoned, and re-checked this pull request!

@cla-bot
Copy link
Copy Markdown

cla-bot bot commented Mar 17, 2026

Thank you for your pull request and welcome to the Trino community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. Continue to work with us on the review and improvements in this PR, and submit the signed CLA to cla@trino.io. Photos, scans, or digitally-signed PDF files are all suitable. Processing may take a few days. The CLA needs to be on file before we merge your changes. For more information, see https://github.com/trinodb/cla

@cla-bot cla-bot bot removed the cla-signed label Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants