Skip to content

Commit 7dd38f9

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 7dd38f9

2 files changed

Lines changed: 35 additions & 1 deletion

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
}

0 commit comments

Comments
 (0)