-
Notifications
You must be signed in to change notification settings - Fork 213
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
Introduced BigDecimalConverter #4557
Merged
kkondaka
merged 15 commits into
opensearch-project:main
from
san81:dynamodb-scientific-notation-fix
Jun 4, 2024
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9a6a439
Introduced BigDecimalConverter that users can use as part of convert_…
san81 00f654a
Added Test case for the newly introduced class. Removed * imports as …
san81 a46bdbe
Avoiding using a deprecated method. Added additional test cases
san81 81fed09
Additional tests to increase the coverage
san81 5e8b811
Merge branch 'main' of github.com:san81/data-prepper into dynamodb-sc…
san81 e10a5c5
removed "scale" being the state of BigDecimal converter. We are now p…
san81 a4d60b4
test case fix to be inline with the previous commit
san81 45d1c0b
test case fix to be inline with the previous commit
san81 7ce8334
addressing review comments
san81 761dd6f
renaming bigdecimal to big_decimal
san81 72e2eff
Introduced ConverterArguments as a way to pass additional arguments t…
san81 eab7236
Merge branch 'opensearch-project:main' into dynamodb-scientific-notat…
san81 aed99af
Added additional override convert method to reduce the changes across…
san81 19d82b4
additional Test cases to increase the coverage
san81 c399c06
added additional tests for converter cases
san81 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
This file contains 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 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
54 changes: 54 additions & 0 deletions
54
...epper-api/src/main/java/org/opensearch/dataprepper/typeconverter/BigDecimalConverter.java
This file contains 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,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.typeconverter; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
/** | ||
* Converter class for BigDecimal data type. By default, it applies zero scaling keeping the original value as it is. | ||
* If required, the scale can be set using the setScale method. | ||
*/ | ||
public class BigDecimalConverter implements TypeConverter<BigDecimal> { | ||
|
||
public BigDecimal convert(Object source, ConverterArguments arguments) throws IllegalArgumentException { | ||
return this.convert(source, arguments.getScale()); | ||
} | ||
|
||
public BigDecimal convert(Object source, int scale) throws IllegalArgumentException { | ||
BigDecimal result = null; | ||
if (source instanceof String) { | ||
result = new BigDecimal((String)source); | ||
} | ||
else if (source instanceof Float) { | ||
result = BigDecimal.valueOf((Float)source); | ||
} | ||
else if (source instanceof Double) { | ||
result = BigDecimal.valueOf((Double)source); | ||
} | ||
else if (source instanceof Boolean) { | ||
result = ((Boolean)source) ? BigDecimal.valueOf(1L) : BigDecimal.valueOf(0L); | ||
} | ||
else if (source instanceof Integer) { | ||
result = BigDecimal.valueOf((Integer)source); | ||
} | ||
else if (source instanceof Long) { | ||
result = BigDecimal.valueOf((Long)source); | ||
} | ||
else if (source instanceof BigDecimal) { | ||
result = ((BigDecimal)source); | ||
} | ||
|
||
if(result!=null) { | ||
if(scale!=0) { | ||
result = result.setScale(scale, RoundingMode.HALF_EVEN); | ||
} | ||
return result; | ||
} | ||
throw new IllegalArgumentException("Unsupported type conversion. From Source class: " + source.getClass() + " to BigDecimal"); | ||
} | ||
} | ||
|
This file contains 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
10 changes: 10 additions & 0 deletions
10
...repper-api/src/main/java/org/opensearch/dataprepper/typeconverter/ConverterArguments.java
This file contains 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,10 @@ | ||
package org.opensearch.dataprepper.typeconverter; | ||
|
||
/** | ||
* Interface for arguments passed to the {@link TypeConverter} | ||
* | ||
* @since 1.2 | ||
*/ | ||
public interface ConverterArguments { | ||
int getScale(); | ||
} |
This file contains 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 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 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 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 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 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 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
102 changes: 102 additions & 0 deletions
102
...-api/src/test/java/org/opensearch/dataprepper/typeconverter/BigDecimalConverterTests.java
This file contains 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,102 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.typeconverter; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
public class BigDecimalConverterTests { | ||
@Test | ||
void testStringToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final String stringConstant = "12345678912.12345"; | ||
assertThat(converter.convert(stringConstant), equalTo(new BigDecimal(stringConstant))); | ||
} | ||
|
||
@Test | ||
void testIntegerToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final int intConstant = 12345; | ||
assertThat(converter.convert(intConstant), equalTo(BigDecimal.valueOf(intConstant))); | ||
} | ||
|
||
@Test | ||
void testLongToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final long longConstant = 123456789012L; | ||
assertThat(converter.convert(longConstant).longValue(), equalTo(longConstant)); | ||
} | ||
|
||
@Test | ||
void testBooleanToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final Boolean boolFalseConstant = false; | ||
assertThat(converter.convert(boolFalseConstant), equalTo(BigDecimal.valueOf(0))); | ||
final Boolean boolTrueConstant = true; | ||
assertThat(converter.convert(boolTrueConstant), equalTo(BigDecimal.valueOf(1))); | ||
} | ||
|
||
@Test | ||
void testFloatToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final float fval = 12345.6789f; | ||
assertThat(converter.convert(fval).floatValue(), equalTo(fval)); | ||
} | ||
|
||
@Test | ||
void testBigDecimalToBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
BigDecimal bigDecimal = new BigDecimal("12345.6789"); | ||
assertThat(converter.convert(bigDecimal), equalTo(bigDecimal)); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("decimalToBigDecimalValueProvider") | ||
void testDoubleToBigDecimalConversion(BigDecimal expectedBigDecimal, double actualValue, int scale) { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
if(scale!=0) { | ||
expectedBigDecimal = expectedBigDecimal.setScale(scale, RoundingMode.HALF_EVEN); | ||
} | ||
assertThat(converter.convert(actualValue, scale), equalTo(expectedBigDecimal)); | ||
} | ||
|
||
private static Stream<Arguments> decimalToBigDecimalValueProvider() { | ||
return Stream.of( | ||
Arguments.of(new BigDecimal ("0.0"), 0, 1), | ||
Arguments.of(new BigDecimal ("0.0"), 0.0, 1), | ||
Arguments.of(new BigDecimal ("0.00000000000000000000000"), 0.00000000000000000000000, 1), | ||
Arguments.of(BigDecimal.ZERO, BigDecimal.ZERO.doubleValue(), 1), | ||
Arguments.of(new BigDecimal ("1"), (double)1, 1), | ||
Arguments.of(new BigDecimal ("1703908514.045833"), 1703908514.045833, 6), | ||
Arguments.of(new BigDecimal ("1.00000000000000000000000"), 1.00000000000000000000000, 1), | ||
Arguments.of(new BigDecimal ("-12345678912.12345"), -12345678912.12345, 1), | ||
Arguments.of(BigDecimal.ONE, BigDecimal.ONE.doubleValue(), 1), | ||
Arguments.of(new BigDecimal("1.7976931348623157E+308"), 1.7976931348623157E+308, 0), | ||
Arguments.of(new BigDecimal("1702062202420"), 1.70206220242E+12, 12), | ||
Arguments.of(BigDecimal.valueOf(Double.MAX_VALUE), Double.MAX_VALUE, 0), | ||
Arguments.of(BigDecimal.valueOf(Double.MIN_VALUE), Double.MIN_VALUE, 0) | ||
); | ||
} | ||
|
||
@Test | ||
void testInvalidBigDecimalConversion() { | ||
BigDecimalConverter converter = new BigDecimalConverter(); | ||
final Map<Object, Object> map = Collections.emptyMap(); | ||
assertThrows(IllegalArgumentException.class, () -> converter.convert(map)); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
Can we add another test case that covers the conversion described in the issue here (#3840) where we can convert
1.70206220242E+12
to1702062202420
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.
Added additional scenario for this case