Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,20 @@ static Object standardize(String column, @Nullable Object value, boolean isSingl
}
List<Object> standardizedValues = new ArrayList<>(numValues);
for (Object singleValue : values) {
Object standardizedValue = standardize(column, singleValue, true);
Object standardizedValue;

// Check if the value itself is multivalued.
if (singleValue instanceof Object[] || singleValue instanceof List) {
standardizedValue = standardize(column, singleValue, false);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. This can result in nested arrays being generated, and break the following part.

What you can do is to generate all standardized values for every elements, and continue when:

  • There is no Object[]
  • There is only one Object[], no other value (all nulls)

The same logic also applies to collections and map. But before getting into that, I'd invest why are we getting multiple nested arrays

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. On further inspection ComplexTypeConfig should be used to decipher such objects. Closing the PR since by the time the data comes to DataTypeTransformer it shouldn't be in this state (nested array, multi key multivalue map etc)

} else if (singleValue instanceof Map) {
standardizedValue =
standardize(column, singleValue, areCollectionValuesSingleValued(((Map) singleValue).values()));
} else if (singleValue instanceof Collection) {
standardizedValue =
standardize(column, singleValue, areCollectionValuesSingleValued((Collection) singleValue));
} else {
standardizedValue = standardize(column, singleValue, true);
}
if (standardizedValue != null) {
standardizedValues.add(standardizedValue);
}
Expand All @@ -172,6 +185,21 @@ static Object standardize(String column, @Nullable Object value, boolean isSingl
return value;
}

private static boolean areCollectionValuesSingleValued(Collection values) {
for (Object value : values) {
if (value instanceof List) {
if (((List<?>) value).size() > 1) {
return false;
}
} else if (value instanceof Object[]) {
if (((Object[]) value).length > 1) {
return false;
}
}
}
return true;
}

private static Object standardizeCollection(String column, Collection collection, boolean isSingleValue) {
int numValues = collection.size();
if (numValues == 0) {
Expand All @@ -182,7 +210,19 @@ private static Object standardizeCollection(String column, Collection collection
}
List<Object> standardizedValues = new ArrayList<>(numValues);
for (Object singleValue : collection) {
Object standardizedValue = standardize(column, singleValue, true);
Object standardizedValue;

// Check if the value itself is multivalued.
if (singleValue instanceof Object[] || singleValue instanceof List) {
standardizedValue = standardize(column, singleValue, false);
} else if (singleValue instanceof Map) {
standardizedValue =
standardize(column, singleValue, areCollectionValuesSingleValued(((Map) singleValue).values()));
} else if (singleValue instanceof Collection) {
standardizedValue = standardize(column, singleValue, areCollectionValuesSingleValued((Collection) singleValue));
} else {
standardizedValue = standardize(column, singleValue, true);
}
if (standardizedValue != null) {
standardizedValues.add(standardizedValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,5 +200,44 @@ public void testStandardize() {
// Expected
}
assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues);

// Tests for Map with multi-entry List as values.
Map<String, List<String>> testMapWithMultiValueList = new HashMap<>();
testMapWithMultiValueList.put("testKey1", Arrays.asList("testValue1", "testValue2"));
values = new Object[]{
new Object[0], testMapWithMultiValueList
};
assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues);

// Tests for Map with multi-entry Object[] as values.
Map<String, Object[]> testMapWithObjectArray = new HashMap<>();
testMapWithObjectArray.put("testKey1", new Object[]{"testValue1", "testValue2"});
values = new Object[]{
new Object[0], testMapWithObjectArray
};
assertEqualsNoOrder((Object[]) DataTypeTransformer.standardize(COLUMN, values, false), expectedValues);

// Test for Map with multi key and multi-entry List as values.

Map<String, List<String>> testMapWithMultiKeyMultiValueList = new HashMap<>();
testMapWithMultiKeyMultiValueList.put("testKey1", Arrays.asList("testValue1", "testValue2"));
testMapWithMultiKeyMultiValueList.put("testKey2", Arrays.asList("testValue3", "testValue4"));
values = new Object[]{
new Object[0], testMapWithMultiKeyMultiValueList
};
Object[] expectedValuesNew =
new Object[]{new Object[]{"testValue1", "testValue2"}, new Object[]{"testValue3", "testValue4"}};
Object[] result = (Object[]) DataTypeTransformer.standardize(COLUMN, values, false);
Arrays.deepEquals(expectedValuesNew, result);

// Test for Map with multi key and multi-entry Object[] as values.
Map<String, Object[]> testMapWithMultiKeyMultiValueObjectArray = new HashMap<>();
testMapWithMultiKeyMultiValueObjectArray.put("testKey1", new Object[]{"testValue1", "testValue2"});
testMapWithMultiKeyMultiValueObjectArray.put("testKey2", new Object[]{"testValue3", "testValue4"});
values = new Object[]{
new Object[0], testMapWithMultiKeyMultiValueObjectArray
};
result = (Object[]) DataTypeTransformer.standardize(COLUMN, values, false);
Arrays.deepEquals(expectedValuesNew, result);
}
}