-
Notifications
You must be signed in to change notification settings - Fork 0
Fix truncate() function #188
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
Changes from 1 commit
2ee64f3
ca87242
10ae053
a9e18e5
ba76b90
d91ec92
21619b4
8599dbd
5622094
3cde7ce
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 |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
||
| package org.opensearch.sql.expression.operator.arthmetic; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
| import lombok.experimental.UtilityClass; | ||
|
|
||
| @UtilityClass | ||
| public class MathUtil { | ||
|
|
||
| /** | ||
| * Truncates a number to required decimal places. | ||
| * | ||
| * @param numberToTruncate number to be truncated | ||
| * @param numberOfDecimals required decimal places | ||
| * @return truncated number as {@link BigDecimal} | ||
| */ | ||
| public static BigDecimal truncateNumber(double numberToTruncate, int numberOfDecimals) { | ||
|
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. Please, add unit tests with different assertEquals(value.toString().subString(...), truncateNumber(value, scale).toString()) |
||
| if (numberToTruncate > 0) { | ||
|
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. Better to use a conditional instead of if...else: or |
||
| return new BigDecimal(String.valueOf(numberToTruncate)) | ||
| .setScale(numberOfDecimals, RoundingMode.FLOOR); | ||
| } else { | ||
| return new BigDecimal(String | ||
| .valueOf(numberToTruncate)).setScale(numberOfDecimals, RoundingMode.CEILING); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -500,26 +500,22 @@ private static DefaultFunctionResolver truncate() { | |
| FunctionDSL.impl( | ||
| FunctionDSL.nullMissingHandling( | ||
| (x, y) -> new ExprLongValue( | ||
| new BigDecimal(x.integerValue()).setScale(y.integerValue(), | ||
| RoundingMode.DOWN).longValue())), | ||
| MathUtil.truncateNumber(x.integerValue(), y.integerValue()).longValue())), | ||
|
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. this is going to convert |
||
| LONG, INTEGER, INTEGER), | ||
| FunctionDSL.impl( | ||
| FunctionDSL.nullMissingHandling( | ||
| (x, y) -> new ExprLongValue( | ||
| new BigDecimal(x.integerValue()).setScale(y.integerValue(), | ||
| RoundingMode.DOWN).longValue())), | ||
| MathUtil.truncateNumber(x.longValue(), y.integerValue()).longValue())), | ||
| LONG, LONG, INTEGER), | ||
| FunctionDSL.impl( | ||
| FunctionDSL.nullMissingHandling( | ||
| (x, y) -> new ExprDoubleValue( | ||
| new BigDecimal(x.floatValue()).setScale(y.integerValue(), | ||
| RoundingMode.DOWN).doubleValue())), | ||
| MathUtil.truncateNumber(x.floatValue(), y.integerValue()).doubleValue())), | ||
| DOUBLE, FLOAT, INTEGER), | ||
| FunctionDSL.impl( | ||
| FunctionDSL.nullMissingHandling( | ||
| (x, y) -> new ExprDoubleValue( | ||
| new BigDecimal(x.doubleValue()).setScale(y.integerValue(), | ||
| RoundingMode.DOWN).doubleValue())), | ||
| MathUtil.truncateNumber(x.doubleValue(), y.integerValue()).doubleValue())), | ||
| DOUBLE, DOUBLE, INTEGER)); | ||
| } | ||
|
|
||
|
|
||
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.
This could be troublesome: opensearch-project#1045 (comment)
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'm not sure if MathUtil is necessary. Let's include
privatefunctiontruncateNumberinMathematicalFunction.java.