markap14 commented on a change in pull request #3724: NIFI-6640 - UNION/CHOICE
types not handled correctly
URL: https://github.com/apache/nifi/pull/3724#discussion_r324303061
##########
File path:
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java
##########
@@ -225,17 +232,109 @@ public static boolean isCompatibleDataType(final Object
value, final DataType da
}
public static DataType chooseDataType(final Object value, final
ChoiceDataType choiceType) {
- for (final DataType subType : choiceType.getPossibleSubTypes()) {
- if (isCompatibleDataType(value, subType)) {
- if (subType.getFieldType() == RecordFieldType.CHOICE) {
- return chooseDataType(value, (ChoiceDataType) subType);
- }
+ Queue<DataType> possibleSubTypes = new
LinkedList<>(choiceType.getPossibleSubTypes());
+ Set<DataType> possibleSimpleSubTypes = new HashSet<>();
- return subType;
+ while (possibleSubTypes.peek() != null) {
+ DataType subType = possibleSubTypes.poll();
+ if (subType instanceof ChoiceDataType) {
+ possibleSubTypes.addAll(((ChoiceDataType)
subType).getPossibleSubTypes());
+ } else {
+ possibleSimpleSubTypes.add(subType);
}
}
- return null;
+ List<DataType> compatibleSimpleSubTypes =
possibleSimpleSubTypes.stream()
Review comment:
This method is invoked quite a lot, by many different writers, which means
that performance is quite a large concern here. We need to avoid the use of
Streams, as creation of streams is quite expensive. To demonstrate, I created a
simple unit test that creates a schema containing 3 fields. Each is a CHOICE
between int, float, string. Then I used JSON Writer to write a Record 1M times.
It took about 1500 milliseconds on my laptop (on average, after letting the JVM
warm up). Then I wrote another test that did the same thing but for the fields
made one an INT, one a FLOAT, and one a String. It took only 400 milliseconds
(on average, after letting the JVM warm up). A quick profiling of the
application does indeed show that the majority of the time spent was in calls
to `stream()`, `ReferencePipeline.collect()`, `ReferencePipeline.findFirst()`
and `HashSet.add()`. We may not be able to eliminate the calls to
`HashSet.add()` but we can eliminate the use of Streams (ReferencePipeline).
----------------------------------------------------------------
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:
[email protected]
With regards,
Apache Git Services