You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ma...@apache.org on 2019/03/26 14:40:18 UTC
[nifi] branch master updated (ead6a3b -> cae01d5)
This is an automated email from the ASF dual-hosted git repository.
mattyb149 pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git.
from ead6a3b NIFI-6144 - Enable EL on listening port in HandleHttpRequest
new c2dc091 NIFI-6117: Fix BIGINT handling in DataTypeUtils
new cae01d5 NIFI-6117: Fix BIGINT handling in DataTypeUtils
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../serialization/record/util/DataTypeUtils.java | 34 +++++++++++++++++---
.../serialization/record/TestDataTypeUtils.java | 37 ++++++++++++++++++++++
2 files changed, 66 insertions(+), 5 deletions(-)
[nifi] 01/02: NIFI-6117: Fix BIGINT handling in DataTypeUtils
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
mattyb149 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git
commit c2dc0910b76565817e95fa349e56aca5d8f2328e
Author: Matthew Burgess <ma...@apache.org>
AuthorDate: Wed Mar 13 18:35:38 2019 -0400
NIFI-6117: Fix BIGINT handling in DataTypeUtils
---
.../serialization/record/util/DataTypeUtils.java | 20 +++++++++++++-
.../serialization/record/TestDataTypeUtils.java | 31 ++++++++++++++++++++++
2 files changed, 50 insertions(+), 1 deletion(-)
diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
index 63db142..c6dcb26 100644
--- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
+++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
@@ -1083,12 +1083,30 @@ public class DataTypeUtils {
if (value instanceof Long) {
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);
}
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) {
diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
index cef0eec..c64ed37 100644
--- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
+++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
@@ -19,8 +19,10 @@ package org.apache.nifi.serialization.record;
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;
@@ -287,4 +289,33 @@ public class TestDataTypeUtils {
testMap.put("Hello", "World");
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);
+ }
}
[nifi] 02/02: NIFI-6117: Fix BIGINT handling in DataTypeUtils
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
mattyb149 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi.git
commit cae01d555cc2f30b2a302b9dc8ec84db2a04469c
Author: Koji Kawamura <ij...@apache.org>
AuthorDate: Tue Mar 26 08:37:20 2019 +0900
NIFI-6117: Fix BIGINT handling in DataTypeUtils
Follow numeric type conversion convention used for other integral types.
Signed-off-by: Matthew Burgess <ma...@apache.org>
This closes #3371
---
.../serialization/record/util/DataTypeUtils.java | 36 +++++++++++++---------
.../serialization/record/TestDataTypeUtils.java | 36 +++++++++++++---------
2 files changed, 42 insertions(+), 30 deletions(-)
diff --git a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
index c6dcb26..3411b13 100644
--- a/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
+++ b/nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
@@ -1080,15 +1080,11 @@ public class DataTypeUtils {
if (value instanceof BigInteger) {
return (BigInteger) value;
}
- if (value instanceof Long) {
- 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 Number) {
+ return BigInteger.valueOf(((Number) value).longValue());
}
+
if (value instanceof String) {
try {
return new BigInteger((String) value);
@@ -1102,11 +1098,7 @@ public class DataTypeUtils {
}
public static boolean isBigIntTypeCompatible(final Object value) {
- return value instanceof BigInteger
- || value instanceof Long
- || value instanceof Integer
- || value instanceof Short
- || value instanceof String;
+ return isNumberTypeCompatible(value, DataTypeUtils::isIntegral);
}
public static Boolean toBoolean(final Object value, final String fieldName) {
@@ -1277,7 +1269,10 @@ public class DataTypeUtils {
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;
}
@@ -1298,6 +1293,18 @@ public class DataTypeUtils {
}
}
+ 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;
@@ -1307,7 +1314,6 @@ public class DataTypeUtils {
}
}
-
public static Integer toInteger(final Object value, final String fieldName) {
if (value == null) {
return null;
diff --git a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
index c64ed37..89a0490 100644
--- a/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
+++ b/nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
@@ -30,6 +30,7 @@ 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;
@@ -292,27 +293,32 @@ public class TestDataTypeUtils {
@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()));
+ 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() {
- 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);
+ 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 {
- DataTypeUtils.convertType("1234567XYZ", RecordFieldType.BIGINT.getDataType(), "field");
+ toBigInteger.apply("1234567XYZ");
} catch (IllegalTypeConversionException itce) {
e = itce;
}