Skip to content

Commit 57deb3d

Browse files
committed
Handling mysql decimal data types with precision 19 or higher
Signed-off-by: Divyansh Bokadia <dbokadia@amazon.com>
1 parent 72a85f5 commit 57deb3d

3 files changed

Lines changed: 41 additions & 2 deletions

File tree

data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/mysql/MySQLDataType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public boolean isBigIntUnsigned() {
129129
return this == MySQLDataType.BIGINT_UNSIGNED;
130130
}
131131

132+
public boolean isDecimal() { return this == MySQLDataType.DECIMAL; }
133+
132134
public boolean isBit() {
133135
return this == MySQLDataType.BIT;
134136
}

data-prepper-plugins/rds-source/src/main/java/org/opensearch/dataprepper/plugins/source/rds/datatype/mysql/handler/NumericTypeHandler.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.opensearch.dataprepper.plugins.source.rds.datatype.mysql.MySQLDataType;
55
import org.opensearch.dataprepper.plugins.source.rds.model.TableMetadata;
66

7+
import java.math.BigDecimal;
78
import java.math.BigInteger;
89
import java.util.ArrayList;
910
import java.util.BitSet;
@@ -39,7 +40,13 @@ private Number handleNumericType(final MySQLDataType columnType, final Object va
3940
}
4041

4142
if (value instanceof Number) {
42-
return (Number)value;
43+
return (Number) value;
44+
}
45+
46+
if (columnType.isDecimal()) {
47+
if (value instanceof byte[] || value instanceof Map || value instanceof ArrayList<?>) {
48+
return handleByteArray(value);
49+
}
4350
}
4451

4552
throw new IllegalArgumentException("Unsupported value type. The value is of type: " + value.getClass());
@@ -107,4 +114,29 @@ private static BigInteger bitSetToBigInteger(BitSet bitSet) {
107114
}
108115
return result;
109116
}
117+
118+
private Number handleByteArray(final Object value) {
119+
if (value instanceof byte[]) {
120+
return new BigDecimal(new BigInteger((byte[]) value));
121+
}
122+
123+
if (value instanceof Map) {
124+
Object data = ((Map<?, ?>)value).get(BYTES_KEY);
125+
if (data instanceof byte[]) {
126+
return new BigDecimal(new BigInteger((byte[]) data));
127+
}
128+
}
129+
130+
if (value instanceof ArrayList<?>) {
131+
ArrayList<?> list = (ArrayList<?>) value;
132+
byte[] bytes = new byte[list.size()];
133+
for (int i = 0; i < list.size(); i++) {
134+
bytes[i] = ((Number) list.get(i)).byteValue();
135+
}
136+
return new BigDecimal(new BigInteger(bytes));
137+
}
138+
139+
throw new IllegalArgumentException("Unsupported byte array value type: " + value.getClass());
140+
}
141+
110142
}

data-prepper-plugins/rds-source/src/test/java/org/opensearch/dataprepper/plugins/source/rds/datatype/mysql/handler/NumericTypeHandlerTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,12 @@ private static Stream<Arguments> provideNumericTypeData() {
113113

114114
// BIT tests
115115
Arguments.of(MySQLDataType.BIT, "bit_col", BitSet.valueOf(new byte[]{ 4, 3, 2, 1 }), new BigInteger("16909060")), // BitSet interprets the bytes in little-endian order
116-
Arguments.of(MySQLDataType.BIT, "bit_col", Map.of("bytes", new byte[]{ 1, 2, 3, 4 }), new BigInteger("16909060")) // Direct BigInteger interprets the bytes in big-endian order.
116+
Arguments.of(MySQLDataType.BIT, "bit_col", Map.of("bytes", new byte[]{ 1, 2, 3, 4 }), new BigInteger("16909060")), // Direct BigInteger interprets the bytes in big-endian order.
117+
118+
//DECIMAL tests represented as byte arrays
119+
Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new ArrayList<>(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)), new BigDecimal("1339673755198158349044581307228491536")),
120+
Arguments.of(MySQLDataType.DECIMAL, "decimal_col", Map.of("bytes", new byte[]{1, 2, 3, 4}), new BigDecimal("16909060")),
121+
Arguments.of(MySQLDataType.DECIMAL, "decimal_col", new byte[]{1, 35, 69, 103, -119, -85, -51, -17}, new BigDecimal("81985529216486895"))
117122
);
118123
}
119124

0 commit comments

Comments
 (0)