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

NIFI-6117: Fix BIGINT handling in DataTypeUtils #3371

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1080,15 +1080,25 @@ public static BigInteger toBigInt(final Object value, final String fieldName) {
if (value instanceof BigInteger) {
return (BigInteger) value;
}
if (value instanceof Long) {
return BigInteger.valueOf((Long) value);

if (value instanceof Number) {
return BigInteger.valueOf(((Number) 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);
}

public static boolean isBigIntTypeCompatible(final Object value) {
return value == null && (value instanceof BigInteger || value instanceof Long);
return isNumberTypeCompatible(value, DataTypeUtils::isIntegral);
}

public static Boolean toBoolean(final Object value, final String fieldName) {
Expand Down Expand Up @@ -1259,7 +1269,10 @@ public static boolean isLongTypeCompatible(final Object value) {
return false;
}

private static boolean isIntegral(final String value, final long minValue, final long maxValue) {
/**
* Check if the value is an integral.
*/
private static boolean isIntegral(final String value) {
if (value == null || value.isEmpty()) {
return false;
}
Expand All @@ -1280,6 +1293,18 @@ private static boolean isIntegral(final String value, final long minValue, final
}
}

return true;
}

/**
* Check if the value is an integral within a value range.
*/
private static boolean isIntegral(final String value, final long minValue, final long maxValue) {

if (!isIntegral(value)) {
return false;
}

try {
final long longValue = Long.parseLong(value);
return longValue >= minValue && longValue <= maxValue;
Expand All @@ -1289,7 +1314,6 @@ private static boolean isIntegral(final String value, final long minValue, final
}
}


public static Integer toInteger(final Object value, final String fieldName) {
if (value == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@

import org.apache.nifi.serialization.SimpleRecordSchema;
import org.apache.nifi.serialization.record.util.DataTypeUtils;
import org.apache.nifi.serialization.record.util.IllegalTypeConversionException;
import org.junit.Test;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -287,4 +290,38 @@ public void testIsCompatibleDataTypeMap() {
testMap.put("Hello", "World");
assertTrue(DataTypeUtils.isCompatibleDataType(testMap, RecordFieldType.RECORD.getDataType()));
}

@Test
public void testIsCompatibleDataTypeBigint() {
final DataType dataType = RecordFieldType.BIGINT.getDataType();
assertTrue(DataTypeUtils.isCompatibleDataType(new BigInteger("12345678901234567890"), dataType));
assertTrue(DataTypeUtils.isCompatibleDataType(1234567890123456789L, dataType));
assertTrue(DataTypeUtils.isCompatibleDataType(1, dataType));
assertTrue(DataTypeUtils.isCompatibleDataType((short) 1, dataType));
assertTrue(DataTypeUtils.isCompatibleDataType("12345678901234567890", dataType));
assertTrue(DataTypeUtils.isCompatibleDataType(3.1f, dataType));
assertTrue(DataTypeUtils.isCompatibleDataType(3.0, dataType));
assertFalse(DataTypeUtils.isCompatibleDataType("1234567XYZ", dataType));
assertFalse(DataTypeUtils.isCompatibleDataType(new Long[]{1L, 2L}, dataType));
}

@Test
public void testConvertDataTypeBigint() {
final Function<Object, BigInteger> toBigInteger = v -> (BigInteger) DataTypeUtils.convertType(v, RecordFieldType.BIGINT.getDataType(), "field");
assertEquals(new BigInteger("12345678901234567890"), toBigInteger.apply(new BigInteger("12345678901234567890")));
assertEquals(new BigInteger("1234567890123456789"), toBigInteger.apply(1234567890123456789L));
assertEquals(new BigInteger("1"), toBigInteger.apply(1));
assertEquals(new BigInteger("1"), toBigInteger.apply((short) 1));
// Decimals are truncated.
assertEquals(new BigInteger("3"), toBigInteger.apply(3.4f));
assertEquals(new BigInteger("3"), toBigInteger.apply(3.9f));
assertEquals(new BigInteger("12345678901234567890"), toBigInteger.apply("12345678901234567890"));
Exception e = null;
try {
toBigInteger.apply("1234567XYZ");
} catch (IllegalTypeConversionException itce) {
e = itce;
}
assertNotNull(e);
}
}