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

Support BigDecimal data type in expressions #4930

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ EscapeSequence
DataTypes
: INTEGER
| BOOLEAN
| BIG_DECIMAL
| LONG
| MAP
| ARRAY
Expand Down Expand Up @@ -339,6 +340,7 @@ EXPONENTLETTER
;

INTEGER: 'integer';
BIG_DECIMAL: 'big_decimal';
Copy link
Member

Choose a reason for hiding this comment

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

Didn't we use the term decimal to be consistent with OpenSearch?

Copy link
Collaborator Author

@kkondaka kkondaka Sep 11, 2024

Choose a reason for hiding this comment

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

BOOLEAN: 'boolean';
LONG : 'long';
DOUBLE : 'double';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import javax.inject.Named;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -26,6 +27,7 @@ public Map<Class<? extends Serializable>, Function<Object, Object>> literalTypeC
Long.class, Function.identity(),
ArrayList.class, Function.identity(),
LinkedHashMap.class, Function.identity(),
BigDecimal.class, Function.identity(),
Double.class, o -> ((Double) o).floatValue()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.opensearch.dataprepper.expression.util.TestObject;
import org.opensearch.dataprepper.model.event.Event;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -113,6 +114,19 @@ void testCoerceTerminalNodeFloatType() {
assertThat(result, equalTo(testFloat));
}

@Test
void testCoerceTerminalNodeBigDecimalType() {
when(token.getType()).thenReturn(DataPrepperExpressionParser.JsonPointer);
final BigDecimal testBigDecimal = new BigDecimal(new Random().nextFloat());
when(terminalNode.getSymbol()).thenReturn(token);
when(terminalNode.getText()).thenReturn("/key");

final Event testEvent = createTestEvent(Map.of("key", testBigDecimal));
final Object result = objectUnderTest.coercePrimaryTerminalNode(terminalNode, testEvent);
assertThat(result, instanceOf(BigDecimal.class));
assertThat(result, equalTo(testBigDecimal));
}

@ParameterizedTest
@ValueSource(strings={"integer", "boolean", "long", "string", "double", "map", "array"})
void testCoerceTerminalNodeDataTypesType(String testString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -71,6 +72,7 @@ private static Stream<Arguments> getTypeOfTestData() {
List<Integer> testArrayList = new ArrayList<Integer>();
testArrayList.add(1);
testArrayList.add(2);
BigDecimal bigDecimalValue = new BigDecimal(1.222);
return Stream.of(
Arguments.of(2, "integer", true),
Arguments.of("testString", "string", true),
Expand All @@ -86,6 +88,8 @@ private static Stream<Arguments> getTypeOfTestData() {
Arguments.of("testString", "double", false),
Arguments.of(2, "boolean", false),
Arguments.of(2L, "map", false),
Arguments.of(2L, "big_decimal", false),
Arguments.of(bigDecimalValue, "big_decimal", true),
Arguments.of(2, "array", false)
);
}
Expand Down
Loading