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

fix / and div operations on value of decimal type column #1622

Merged
merged 2 commits into from
Feb 28, 2023

Conversation

jennifersp
Copy link
Contributor

The cases fixed are:

  • the scale of decimal type column is bigger than the value stored. For example, the decimal column type is DECIMAL(25,10) and the value stored is 4990. This value is evaluated with 0 scale, whereas this should be evaluated as value with scale of 10.
  • the IntPart() function of decimal package returns 0 as undefined value for out of range values. This causes it hard to differentiate between cases where the final result is valid value of 0 and the final result is out of range value.

@jennifersp jennifersp linked an issue Feb 27, 2023 that may be closed by this pull request
@fulghum fulghum self-requested a review February 28, 2023 20:01
Copy link
Contributor

@fulghum fulghum left a comment

Choose a reason for hiding this comment

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

Looks good! Just one comment about a very small possible simplification.

Nice comments in several places. The arithmetic code is quite tricky in some places and those good comments really help a lot to explain the intent of the code. 🙌

@@ -717,9 +742,11 @@ func intDiv(ctx *sql.Context, lval, rval interface{}) (interface{}, error) {

// intDiv operation gets the integer part of the divided value
divRes := l.DivRound(r, 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we pass in 0 here as the precision and avoid the call to divRes.Truncate(0) on line 748? Seems like that might be a little more direct, since we don't ever need those decimal digits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah DivRound rounds the result value with defined precision, but int division only takes int part without rounding the result, which is why I have it do this weird steps 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just made it more clear and added comment for this behavior.

Comment on lines +178 to +180
// this operation is only done on the left value as the scale/fraction part of the leftmost value
// is used to calculate the scale of the final result. If the value is GetField of decimal type column
// the decimal value evaluated does not always match the scale of column type definition
Copy link
Contributor

Choose a reason for hiding this comment

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

Fantastic comment! ✨

@jennifersp jennifersp merged commit 56a0659 into main Feb 28, 2023
@jennifersp jennifersp deleted the jennifer/divs branch February 28, 2023 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unexpected error when using div operation
2 participants