forked from opensearch-project/sql
-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2ee64f3
Fix truncate() function
margarit-h ca87242
Address PR review feedback
margarit-h 10ae053
Edit a javadoc
margarit-h a9e18e5
Add/fix tests
margarit-h ba76b90
Address PR review feedback
margarit-h d91ec92
Added more tests
margarit-h 21619b4
Addressed more PR review feedback
margarit-h 8599dbd
minor fix
margarit-h 5622094
Clean up, apdate a unit test
margarit-h 3cde7ce
minor fix
margarit-h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
core/src/main/java/org/opensearch/sql/expression/operator/arthmetic/MathUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
|
|
||
| package org.opensearch.sql.expression.operator.arthmetic; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.RoundingMode; | ||
|
|
||
| public class MathUtil { | ||
|
|
||
| /** | ||
| * Truncates a double number to required decimal places. | ||
| * | ||
| * @param numberToTruncate number to be truncated | ||
| * @param numberOfDecimals required decimal places | ||
| * @return truncated number as double | ||
| */ | ||
| public static double truncateDouble(double numberToTruncate, int numberOfDecimals) { | ||
| return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, | ||
| numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); | ||
| } | ||
|
|
||
| /** | ||
| * Truncates a float number to required decimal places. | ||
| * | ||
| * @param numberToTruncate number to be truncated | ||
| * @param numberOfDecimals required decimal places | ||
| * @return truncated number as double | ||
| */ | ||
| public static double truncateFloat(float numberToTruncate, int numberOfDecimals) { | ||
| return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, | ||
| numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).doubleValue(); | ||
| } | ||
|
|
||
| /** | ||
| * Truncates an int number to required decimal places. | ||
| * | ||
| * @param numberToTruncate number to be truncated | ||
| * @param numberOfDecimals required decimal places | ||
| * @return truncated number as long | ||
| */ | ||
| public static long truncateInt(int numberToTruncate, int numberOfDecimals) { | ||
| return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, | ||
| numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); | ||
| } | ||
|
|
||
| /** | ||
| * Truncates a long number to required decimal places. | ||
| * | ||
| * @param numberToTruncate number to be truncated | ||
| * @param numberOfDecimals required decimal places | ||
| * @return truncated number as long | ||
| */ | ||
| public static long truncateLong(long numberToTruncate, int numberOfDecimals) { | ||
| return new BigDecimal(String.valueOf(numberToTruncate)).setScale(numberOfDecimals, | ||
| numberToTruncate > 0 ? RoundingMode.FLOOR : RoundingMode.CEILING).longValue(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/src/test/java/org/opensearch/sql/expression/operator/arthmetic/MathUtilTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package org.opensearch.sql.expression.operator.arthmetic; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| /** | ||
| * Test class for {@link MathUtil}. | ||
| */ | ||
| class MathUtilTest { | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(doubles = {11.2D, 22.5678D, -1.2D}) | ||
| void testTruncateDouble(final Double value) { | ||
| String result = Double.toString(MathUtil.truncateDouble(value, 1)); | ||
| assertEquals(Double.toString(value).substring(0,4), result); | ||
| } | ||
|
|
||
| @ParameterizedTest(name = "truncate({0}, {1})") | ||
| @ValueSource(floats = {11.2F, 22.5678F, -1.2F}) | ||
| void testTruncateFloat(final Float value) { | ||
| String result = Double.toString(MathUtil.truncateFloat(value, 1)); | ||
| assertEquals(Float.toString(value).substring(0,4), result); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(longs = {2056L, -777L}) | ||
| void testTruncateLong(final Long value) { | ||
| String result = Long.toString(MathUtil.truncateLong(value, 1)); | ||
| assertEquals(Long.toString(value).substring(0,4), result); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(ints = {11, 22, -7}) | ||
| void testTruncateInt(final int value) { | ||
| String result = Long.toString(MathUtil.truncateInt(value, 1)); | ||
| assertEquals(Integer.toString(value).substring(0,2), result); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 it worthwhile to have this class. These are all single-line calls used a single time. I would prefer to remove this file and include the code in the
MathematicalFunction.javacode.Like: