mirror of https://github.com/apache/nifi.git
NIFI-6117: Fix BIGINT handling in DataTypeUtils
This commit is contained in:
parent
ead6a3b842
commit
c2dc0910b7
|
@ -1083,12 +1083,30 @@ public class DataTypeUtils {
|
||||||
if (value instanceof Long) {
|
if (value instanceof Long) {
|
||||||
return BigInteger.valueOf((Long) value);
|
return BigInteger.valueOf((Long) value);
|
||||||
}
|
}
|
||||||
|
if (value instanceof Integer) {
|
||||||
|
return BigInteger.valueOf(((Integer) value).longValue());
|
||||||
|
}
|
||||||
|
if (value instanceof Short) {
|
||||||
|
return BigInteger.valueOf(((Short) value).longValue());
|
||||||
|
}
|
||||||
|
if (value instanceof String) {
|
||||||
|
try {
|
||||||
|
return new BigInteger((String) value);
|
||||||
|
} catch (NumberFormatException nfe) {
|
||||||
|
throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " to BigInteger for field " + fieldName
|
||||||
|
+ ", value is not a valid representation of BigInteger", nfe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " to BigInteger for field " + fieldName);
|
throw new IllegalTypeConversionException("Cannot convert value [" + value + "] of type " + value.getClass() + " to BigInteger for field " + fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isBigIntTypeCompatible(final Object value) {
|
public static boolean isBigIntTypeCompatible(final Object value) {
|
||||||
return value == null && (value instanceof BigInteger || value instanceof Long);
|
return value instanceof BigInteger
|
||||||
|
|| value instanceof Long
|
||||||
|
|| value instanceof Integer
|
||||||
|
|| value instanceof Short
|
||||||
|
|| value instanceof String;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Boolean toBoolean(final Object value, final String fieldName) {
|
public static Boolean toBoolean(final Object value, final String fieldName) {
|
||||||
|
|
|
@ -19,8 +19,10 @@ package org.apache.nifi.serialization.record;
|
||||||
|
|
||||||
import org.apache.nifi.serialization.SimpleRecordSchema;
|
import org.apache.nifi.serialization.SimpleRecordSchema;
|
||||||
import org.apache.nifi.serialization.record.util.DataTypeUtils;
|
import org.apache.nifi.serialization.record.util.DataTypeUtils;
|
||||||
|
import org.apache.nifi.serialization.record.util.IllegalTypeConversionException;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -287,4 +289,33 @@ public class TestDataTypeUtils {
|
||||||
testMap.put("Hello", "World");
|
testMap.put("Hello", "World");
|
||||||
assertTrue(DataTypeUtils.isCompatibleDataType(testMap, RecordFieldType.RECORD.getDataType()));
|
assertTrue(DataTypeUtils.isCompatibleDataType(testMap, RecordFieldType.RECORD.getDataType()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsCompatibleDataTypeBigint() {
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType(new BigInteger("12345678901234567890"), RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType(1234567890123456789L, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType(1, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType((short) 1, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType("12345678901234567890", RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertTrue(DataTypeUtils.isCompatibleDataType("1234567XYZ", RecordFieldType.BIGINT.getDataType())); // Compatible but the value might not be a valid BigInteger
|
||||||
|
assertFalse(DataTypeUtils.isCompatibleDataType(3.0f, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertFalse(DataTypeUtils.isCompatibleDataType(3.0, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
assertFalse(DataTypeUtils.isCompatibleDataType(new Long[]{1L, 2L}, RecordFieldType.BIGINT.getDataType()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConvertDataTypeBigint() {
|
||||||
|
assertTrue(DataTypeUtils.convertType(new BigInteger("12345678901234567890"), RecordFieldType.BIGINT.getDataType(), "field") instanceof BigInteger);
|
||||||
|
assertTrue(DataTypeUtils.convertType(1234567890123456789L, RecordFieldType.BIGINT.getDataType(), "field") instanceof BigInteger);
|
||||||
|
assertTrue(DataTypeUtils.convertType(1, RecordFieldType.BIGINT.getDataType(), "field") instanceof BigInteger);
|
||||||
|
assertTrue(DataTypeUtils.convertType((short) 1, RecordFieldType.BIGINT.getDataType(), "field") instanceof BigInteger);
|
||||||
|
assertTrue(DataTypeUtils.convertType("12345678901234567890", RecordFieldType.BIGINT.getDataType(), "field") instanceof BigInteger);
|
||||||
|
Exception e = null;
|
||||||
|
try {
|
||||||
|
DataTypeUtils.convertType("1234567XYZ", RecordFieldType.BIGINT.getDataType(), "field");
|
||||||
|
} catch (IllegalTypeConversionException itce) {
|
||||||
|
e = itce;
|
||||||
|
}
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue