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/03/12 18:48:57 UTC

[GitHub] [nifi] mattyb149 opened a new pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

mattyb149 opened a new pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139
 
 
   Thank you for submitting a contribution to Apache NiFi.
   
   Please provide a short description of the PR here:
   
   #### Description of PR
   
   Avro maps have Utf8 keys, but DataTypeUtil wants String keys in a map, so when inferring the type of a map, the keys need to be coerced to Strings.
   
   In order to streamline the review of the contribution we ask you
   to ensure the following steps have been taken:
   
   ### For all changes:
   - [x] Is there a JIRA ticket associated with this PR? Is it referenced 
        in the commit message?
   
   - [x] Does your PR title start with **NIFI-XXXX** where XXXX is the JIRA number you are trying to resolve? Pay particular attention to the hyphen "-" character.
   
   - [x] Has your PR been rebased against the latest commit within the target branch (typically `master`)?
   
   - [x] Is your initial contribution a single, squashed commit? _Additional commits in response to PR reviewer feedback should be made on this branch and pushed to allow change tracking. Do not `squash` or use `--force` when pushing to allow for clean monitoring of changes._
   
   ### For code changes:
   - [ ] Have you ensured that the full suite of tests is executed via `mvn -Pcontrib-check clean install` at the root `nifi` folder?
   - [x] Have you written or updated unit tests to verify your changes?
   - [ ] Have you verified that the full build is successful on both JDK 8 and JDK 11?
   - [ ] If adding new dependencies to the code, are these dependencies licensed in a way that is compatible for inclusion under [ASF 2.0](http://www.apache.org/legal/resolved.html#category-a)? 
   - [ ] If applicable, have you updated the `LICENSE` file, including the main `LICENSE` file under `nifi-assembly`?
   - [ ] If applicable, have you updated the `NOTICE` file, including the main `NOTICE` file found under `nifi-assembly`?
   - [ ] If adding new Properties, have you added `.displayName` in addition to .name (programmatic access) for each of the new properties?
   
   ### For documentation related changes:
   - [ ] Have you ensured that format looks appropriate for the output in which it is rendered?
   
   ### Note:
   Please ensure that once the PR is submitted, you check travis-ci for build issues and submit an update to your PR as soon as possible.
   

----------------------------------------------------------------
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

[GitHub] [nifi] mattyb149 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
mattyb149 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#discussion_r391894216
 
 

 ##########
 File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
 ##########
 @@ -442,6 +442,14 @@ private void testChooseDataType(List<DataType> dataTypes, Object value, DataType
         assertEquals(expected, actual);
     }
 
+    @Test
+    public void testInferTypeWithMapNonStringKeys() {
+        Map<Integer, String> map = new HashMap<>();
+        map.put(1, "Hello");
+        map.put(2, "World");
+        DataTypeUtils.inferDataType(map, RecordFieldType.MAP.getMapDataType(RecordFieldType.STRING.getDataType()));
 
 Review comment:
   Good suggestion, thanks! Done.

----------------------------------------------------------------
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

[GitHub] [nifi] rumbin commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
rumbin commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#issuecomment-598813865
 
 
   You guys are simply amazing!

----------------------------------------------------------------
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

[GitHub] [nifi] markap14 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
markap14 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#discussion_r392214919
 
 

 ##########
 File path: nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
 ##########
 @@ -474,7 +474,9 @@ public static DataType inferDataType(final Object value, final DataType defaultT
 
         // A value of a Map could be either a Record or a Map type. In either case, it must have Strings as keys.
         if (value instanceof Map) {
-            final Map<String, ?> map = (Map<String, ?>) value;
+            Map<?, ?> m = (Map<?, ?>) value;
 
 Review comment:
   In the majority of cases, the Map will already have Strings for keys. Might recommend first checking this, in order to avoid the garbage collection & related resource consumption when it's usually not needed. Something along the lines of:
   ```
   final Map<String, Object> map;
   boolean allStrings = true;
   for (final Object key : ((Map<?, ?) value).keySet()) {
     if (!(key instanceof String)) {
       allStrings = false;
       break;
     }
   }
   
   if (allKeyStrings) {
     map = (Map<String, Object>) value;
   } else {
     final Map<?, ?> m = (Map<?, ?>) value;
     map = new HashMap<>(m.size());
     m.forEach((k, v) -> map.put(k == null ? null : k.toString(), v));
   }
   ```
   
   Is also worth noting that Map does allow for null keys. I don't believe in our uses that we'd ever encounter this to this point, but it does make sense to check for it and avoid an NPE if it does occur.

----------------------------------------------------------------
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

[GitHub] [nifi] tpalfy commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
tpalfy commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#discussion_r391848338
 
 

 ##########
 File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
 ##########
 @@ -442,6 +442,14 @@ private void testChooseDataType(List<DataType> dataTypes, Object value, DataType
         assertEquals(expected, actual);
     }
 
+    @Test
+    public void testInferTypeWithMapNonStringKeys() {
+        Map<Integer, String> map = new HashMap<>();
+        map.put(1, "Hello");
+        map.put(2, "World");
+        DataTypeUtils.inferDataType(map, RecordFieldType.MAP.getMapDataType(RecordFieldType.STRING.getDataType()));
 
 Review comment:
   An `expected` could be useful as this doesn't test the string keys only the lack of exception. (Later a bug could make it return an empty map or null for example that this test would not catch.)
   ```java
           RecordDataType expected = (RecordDataType)RecordFieldType.RECORD.getRecordDataType(new SimpleRecordSchema(Arrays.asList(
               new RecordField("1", RecordFieldType.STRING.getDataType()),
               new RecordField("2", RecordFieldType.STRING.getDataType())
           )));
   
   ...
   
           DataType actual = DataTypeUtils.inferDataType(map, null);
   
           assertEquals(expected, actual);
   ```
   
   Also a minor suggestion: could simply be
   ```java
           DataTypeUtils.inferDataType(map, null);
   ```
   (The second parameter is just a default type and the fact that it's correctly constructed kind of suggests that it's relevant for the test.)

----------------------------------------------------------------
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

[GitHub] [nifi] asfgit closed pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
asfgit closed pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139
 
 
   

----------------------------------------------------------------
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

[GitHub] [nifi] tpalfy commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
tpalfy commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#discussion_r391830893
 
 

 ##########
 File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
 ##########
 @@ -442,6 +442,14 @@ private void testChooseDataType(List<DataType> dataTypes, Object value, DataType
         assertEquals(expected, actual);
     }
 
+    @Test
 
 Review comment:
   Can we also please add a test to `TestAvroTypeUtil`:
   ```java
       @Test
       public void testConvertAvroMap() {
          // GIVEN
           Map<?, ?> expected = new HashMap<String, Object>() {{
               put(
                   "nullableMapField",
                   new HashMap<String, Object>() {{
                       put("key1", "value1");
                       put("key2", "value2");
                   }}
               );
           }};
   
           Schema nullableMapFieldAvroSchema = Schema.createUnion(
               Schema.create(Type.NULL),
               Schema.create(Type.INT),
               Schema.createMap(Schema.create(Type.STRING))
           );
   
           Schema avroRecordSchema = Schema.createRecord(
               "record", "doc", "namespace", false,
               Arrays.asList(
                   new Field("nullableMapField", nullableMapFieldAvroSchema, "nullable map field", (Object)null)
               )
           );
   
           Map<?, ?> value = new HashMap<Utf8, Object>(){{
               put(new Utf8("key1"), "value1");
               put(new Utf8("key2"), "value2");
           }};
   
           Record avroRecord = new GenericRecordBuilder(avroRecordSchema)
               .set("nullableMapField", value)
               .build();
   
           RecordSchema nifiRecordSchema = new SimpleRecordSchema(
               Arrays.asList(
                   new RecordField("nullableMapField", RecordFieldType.CHOICE.getChoiceDataType(
                       RecordFieldType.MAP.getMapDataType(RecordFieldType.STRING.getDataType())
                   ))
               )
           );
   
           // WHEN
           Object actual = AvroTypeUtil.convertAvroRecordToMap(avroRecord, nifiRecordSchema);
   
           // THEN
           assertEquals(expected, actual);
       }
   ```

----------------------------------------------------------------
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

[GitHub] [nifi] markap14 commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
markap14 commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#issuecomment-598802344
 
 
   Thanks @mattyb149 looks good to me, +1

----------------------------------------------------------------
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

[GitHub] [nifi] pvillard31 commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
pvillard31 commented on issue #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#issuecomment-598804025
 
 
   Merged to master, thank you all!

----------------------------------------------------------------
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

[GitHub] [nifi] mattyb149 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()

Posted by GitBox <gi...@apache.org>.
mattyb149 commented on a change in pull request #4139: NIFI-7249: Force String keys in maps in DataTypeUtils.inferDataType()
URL: https://github.com/apache/nifi/pull/4139#discussion_r391839818
 
 

 ##########
 File path: nifi-commons/nifi-record/src/test/java/org/apache/nifi/serialization/record/TestDataTypeUtils.java
 ##########
 @@ -442,6 +442,14 @@ private void testChooseDataType(List<DataType> dataTypes, Object value, DataType
         assertEquals(expected, actual);
     }
 
+    @Test
 
 Review comment:
   Good idea, done!

----------------------------------------------------------------
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