Repository navigation
Kafka Connect: Pre-size collections in RecordConverter list/map conversion #16657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,7 @@ | |
| import org.apache.iceberg.mapping.NameMapping; | ||
| import org.apache.iceberg.mapping.NameMappingParser; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.types.Type; | ||
|
|
@@ -300,28 +301,29 @@ protected List<Object> convertListValue( | |
| Object value, ListType type, SchemaUpdate.Consumer schemaUpdateConsumer) { | ||
| Preconditions.checkArgument(value instanceof List); | ||
| List<?> list = (List<?>) value; | ||
| return list.stream() | ||
| .map( | ||
| element -> { | ||
| int fieldId = type.fields().get(0).fieldId(); | ||
| return convertValue(element, type.elementType(), fieldId, schemaUpdateConsumer); | ||
| }) | ||
| .collect(Collectors.toList()); | ||
| int elementFieldId = type.fields().get(0).fieldId(); | ||
| Type elementType = type.elementType(); | ||
| List<Object> result = Lists.newArrayListWithCapacity(list.size()); | ||
| for (Object element : list) { | ||
| result.add(convertValue(element, elementType, elementFieldId, schemaUpdateConsumer)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| protected Map<Object, Object> convertMapValue( | ||
| Object value, MapType type, SchemaUpdate.Consumer schemaUpdateConsumer) { | ||
| Preconditions.checkArgument(value instanceof Map); | ||
| Map<?, ?> map = (Map<?, ?>) value; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor semantic shift:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The empty-map test added in c0c3390 runs this method with size 0, so the now-eager get(1) is exercised on the empty path you flagged. |
||
| Map<Object, Object> result = Maps.newHashMap(); | ||
| int keyFieldId = type.fields().get(0).fieldId(); | ||
| int valueFieldId = type.fields().get(1).fieldId(); | ||
| Type keyType = type.keyType(); | ||
| Type valueType = type.valueType(); | ||
| Map<Object, Object> result = Maps.newHashMapWithExpectedSize(map.size()); | ||
| map.forEach( | ||
| (k, v) -> { | ||
| int keyFieldId = type.fields().get(0).fieldId(); | ||
| int valueFieldId = type.fields().get(1).fieldId(); | ||
| result.put( | ||
| convertValue(k, type.keyType(), keyFieldId, schemaUpdateConsumer), | ||
| convertValue(v, type.valueType(), valueFieldId, schemaUpdateConsumer)); | ||
| }); | ||
| (k, v) -> | ||
| result.put( | ||
| convertValue(k, keyType, keyFieldId, schemaUpdateConsumer), | ||
| convertValue(v, valueType, valueFieldId, schemaUpdateConsumer))); | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we're here —
TestRecordConverteronly ever runs these through 2-element lists/maps, so the new zero-capacity path (newArrayListWithCapacity(0)/newHashMapWithExpectedSize(0)) isn't exercised at all.I'd add one test that converts a record with an empty list field and an empty map field and asserts empty results. Cheap, and it closes a pre-existing gap rather than just papering over this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done c0c3390