-
Notifications
You must be signed in to change notification settings - Fork 1k
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: standardize KSQL up-casting #3516
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,4 +105,75 @@ public String toString() { | |
public String toString(final FormatOptions formatOptions) { | ||
return toString(); | ||
} | ||
|
||
/** | ||
* Determine the decimal type should two decimals be added together. | ||
* | ||
* @param left the left side decimal. | ||
* @param right the right side decimal. | ||
* @return the resulting decimal type. | ||
*/ | ||
public static SqlDecimal add(final SqlDecimal left, final SqlDecimal right) { | ||
final int precision = Math.max(left.scale, right.scale) | ||
+ Math.max(left.precision - left.scale, right.precision - right.scale) | ||
+ 1; | ||
|
||
final int scale = Math.max(left.scale, right.scale); | ||
return SqlDecimal.of(precision, scale); | ||
} | ||
|
||
/** | ||
* Determine the decimal type should one decimal be subtracted from another. | ||
* | ||
* @param left the left side decimal. | ||
* @param right the right side decimal. | ||
* @return the resulting decimal type. | ||
*/ | ||
public static SqlDecimal subtract(final SqlDecimal left, final SqlDecimal right) { | ||
return add(left, right); | ||
} | ||
|
||
/** | ||
* Determine the decimal type should one decimal be multiplied by another. | ||
* | ||
* @param left the left side decimal. | ||
* @param right the right side decimal. | ||
* @return the resulting decimal type. | ||
*/ | ||
public static SqlDecimal multiply(final SqlDecimal left, final SqlDecimal right) { | ||
final int precision = left.precision + right.precision + 1; | ||
final int scale = left.scale + right.scale; | ||
return SqlDecimal.of(precision, scale); | ||
} | ||
|
||
/** | ||
* Determine the decimal type should one decimal be divided by another. | ||
* | ||
* @param left the left side decimal. | ||
* @param right the right side decimal. | ||
* @return the resulting decimal type. | ||
*/ | ||
public static SqlDecimal divide(final SqlDecimal left, final SqlDecimal right) { | ||
final int precision = left.precision - left.scale + right.scale | ||
+ Math.max(6, left.scale + right.precision + 1); | ||
|
||
final int scale = Math.max(6, left.scale + right.precision + 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we limit to 6 decimal places? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My gut feeling would be to leave all the scale and precision calculations up to BigDecimal and only readjust the scale/precision at the end of the calculation if too many trailing zeros aren't desired. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I understand this code is trying to calculate the required scale and precision of a decimal for a particular operation given the scale and precision of the operands. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment below your review. |
||
return SqlDecimal.of(precision, scale); | ||
} | ||
|
||
/** | ||
* Determine the decimal result type when calculating the remainder of dividing one decimal by | ||
* another. | ||
* | ||
* @param left the left side decimal. | ||
* @param right the right side decimal. | ||
* @return the resulting decimal type. | ||
*/ | ||
public static SqlDecimal modulus(final SqlDecimal left, final SqlDecimal right) { | ||
final int precision = Math.min(left.precision - left.scale, right.precision - right.scale) | ||
+ Math.max(left.scale, right.scale); | ||
|
||
final int scale = Math.max(left.scale, right.scale); | ||
return SqlDecimal.of(precision, scale); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we don't leave it up to BigDecimal to figure out the scale and precision?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment below your review.