NIFI-5141: Updated regex for doubles to allow for numbers that have no decimal

NIFI-5141: Loosened regex for floating-point numbers to account for decimal place followed by 0 digits, such as '13.' and also added unit tests

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #2679
This commit is contained in:
Mark Payne 2018-05-04 15:35:00 -04:00 committed by Matthew Burgess
parent caa71fce92
commit 06d1276f09
2 changed files with 35 additions and 5 deletions

View File

@ -63,11 +63,11 @@ public class DataTypeUtils {
private static final String Infinity = "(Infinity)";
private static final String NotANumber = "(NaN)";
private static final String Base10Digits = "\\d+";
private static final String Base10Decimal = "\\." + Base10Digits;
private static final String OptionalBase10Decimal = Base10Decimal + "?";
private static final String Base10Digits = "\\d+";
private static final String Base10Decimal = "\\." + Base10Digits;
private static final String OptionalBase10Decimal = "(\\.\\d*)?";
private static final String Base10Exponent = "[eE]" + OptionalSign + Base10Digits;
private static final String Base10Exponent = "[eE]" + OptionalSign + Base10Digits;
private static final String OptionalBase10Exponent = "(" + Base10Exponent + ")?";
private static final String doubleRegex =
@ -75,7 +75,7 @@ public class DataTypeUtils {
"(" +
Infinity + "|" +
NotANumber + "|"+
"(" + Base10Digits + Base10Decimal + ")" + "|" +
"(" + Base10Digits + OptionalBase10Decimal + ")" + "|" +
"(" + Base10Digits + OptionalBase10Decimal + Base10Exponent + ")" + "|" +
"(" + Base10Decimal + OptionalBase10Exponent + ")" +
")";
@ -408,6 +408,7 @@ public class DataTypeUtils {
* @param dataType The type of the provided object
* @return An object representing a native Java conversion of the given input object
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public static Object convertRecordFieldtoObject(final Object value, final DataType dataType) {
if (value == null) {

View File

@ -92,6 +92,7 @@ public class TestDataTypeUtils {
}
@Test
@SuppressWarnings("unchecked")
public void testConvertRecordFieldToObject() {
assertNull(DataTypeUtils.convertRecordFieldtoObject(null, null));
assertNull(DataTypeUtils.convertRecordFieldtoObject(null, RecordFieldType.MAP.getDataType()));
@ -195,4 +196,32 @@ public class TestDataTypeUtils {
assertTrue(b instanceof Byte[]);
assertEquals("Conversion from byte[] to String failed at char 0", (Object) "Hello".getBytes(StandardCharsets.UTF_16)[0], ((Byte[]) b)[0]);
}
@Test
public void testFloatingPointCompatibility() {
final String[] prefixes = new String[] {"", "-", "+"};
final String[] exponents = new String[] {"e0", "e1", "e-1", "E0", "E1", "E-1"};
final String[] decimals = new String[] {"", ".0", ".1", "."};
for (final String prefix : prefixes) {
for (final String decimal : decimals) {
for (final String exp : exponents) {
String toTest = prefix + "100" + decimal + exp;
assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest));
assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest));
Double.parseDouble(toTest); // ensure we can actually parse it
Float.parseFloat(toTest);
if (decimal.length() > 1) {
toTest = prefix + decimal + exp;
assertTrue(toTest + " not valid float", DataTypeUtils.isFloatTypeCompatible(toTest));
assertTrue(toTest + " not valid double", DataTypeUtils.isDoubleTypeCompatible(toTest));
Double.parseDouble(toTest); // ensure we can actually parse it
Float.parseFloat(toTest);
}
}
}
}
}
}