You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2020/04/08 10:48:06 UTC

[GitHub] [nifi] tpalfy commented on a change in pull request #4190: NIFI-7300 Allowing narrow numeric types to fit againt schema check with wider type;

tpalfy commented on a change in pull request #4190: NIFI-7300 Allowing narrow numeric types to fit againt schema check with wider type;
URL: https://github.com/apache/nifi/pull/4190#discussion_r405426808
 
 

 ##########
 File path: nifi-nar-bundles/nifi-extension-utils/nifi-record-utils/nifi-standard-record-utils/src/main/java/org/apache/nifi/schema/validation/StandardSchemaValidator.java
 ##########
 @@ -233,41 +247,57 @@ private boolean isTypeCorrect(final Object value, final DataType dataType) {
                 }
 
                 return false;
-            case BIGINT:
-                return value instanceof BigInteger;
             case BOOLEAN:
                 return value instanceof Boolean;
-            case BYTE:
-                return value instanceof Byte;
             case CHAR:
                 return value instanceof Character;
             case DATE:
                 return value instanceof java.sql.Date;
-            case DOUBLE:
-                return value instanceof Double;
-            case FLOAT:
-                // Some readers do not provide float vs. double.
-                // We should consider if it makes sense to allow either a Float or a Double here or have
-                // a Reader indicate whether or not it supports higher precision, etc.
-                // Same goes for Short/Integer
-                return value instanceof Float;
-            case INT:
-                return value instanceof Integer;
-            case LONG:
-                return value instanceof Long;
-            case SHORT:
-                return value instanceof Short;
             case STRING:
                 return value instanceof String;
             case TIME:
                 return value instanceof java.sql.Time;
             case TIMESTAMP:
                 return value instanceof java.sql.Timestamp;
+
+            // Numeric data types
+            case BIGINT:
+            case LONG:
+            case INT:
+            case SHORT:
+            case BYTE:
+            case DOUBLE:
+                return isFittingNumberType(value, dataType.getFieldType());
+            case FLOAT:
+                // Some readers do not provide float vs. double.
+                // We should consider if it makes sense to allow either a Float or a Double here or have
+                // a Reader indicate whether or not it supports higher precision, etc.
+                // Same goes for Short/Integer
+                return isFittingNumberType(value, dataType.getFieldType()) || isDoubleWithinFloatInterval(value);
         }
 
         return false;
     }
 
+    /**
+     * Checks if an incoming value satisfies the requirements of a given (numeric) type or any of it's narrow data type.
+     *
+     * @param value Incoming value.
+     * @param fieldType The expected field type.
+     *
+     * @return Returns true if the incoming value satisfies the data type of any of it's narrow data types. Otherwise returns false. Only numeric data types are supported.
+     */
+    private boolean isFittingNumberType(final Object value, final RecordFieldType fieldType) {
+        return NUMERIC_VALIDATORS.containsKey(fieldType)
+            && (NUMERIC_VALIDATORS.get(fieldType).test(value)
+                || fieldType.getNarrowDataTypes().stream().map(type -> NUMERIC_VALIDATORS.get(type)).anyMatch(validator -> validator.test(value))
+        );
+    }
+
+    private boolean isDoubleWithinFloatInterval(final Object value) {
 
 Review comment:
   `Double`-`Float` comparison is tricky. This method returns `false` for `0.1` (as `0.1` cannot be represented as `Float` precisely).
   Not sure if that is good for us.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services