-
Notifications
You must be signed in to change notification settings - Fork 3k
NIFI-16069 - PutIcebergRecord fails with ClassCastException when writing complex types (arrays, maps, nested records) #11391
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
Open
maltesander
wants to merge
4
commits into
apache:main
Choose a base branch
from
maltesander:NIFI-16069
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d834a3
NIFI-16069 - PutIcebergRecord fails with ClassCastException when writ…
maltesander d174fd3
refactor: extract nested complex conditionals to convertComplexValue …
maltesander ef3e162
tests: use static constants for field names and values
maltesander c9b79b4
Merge remote-tracking branch 'origin/main' into NIFI-16069
maltesander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,8 @@ | |
| */ | ||
| package org.apache.nifi.processors.iceberg.record; | ||
|
|
||
| import org.apache.nifi.serialization.record.DataType; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.nifi.serialization.record.MapRecord; | ||
| import org.apache.nifi.serialization.record.Record; | ||
| import org.apache.nifi.serialization.record.RecordField; | ||
|
|
@@ -26,6 +27,9 @@ | |
| import java.sql.Date; | ||
| import java.sql.Time; | ||
| import java.sql.Timestamp; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -39,63 +43,107 @@ class RecordConverter { | |
| private static final Set<RecordFieldType> CONVERSION_REQUIRED_FIELD_TYPES = Set.of( | ||
| RecordFieldType.TIMESTAMP, | ||
| RecordFieldType.DATE, | ||
| RecordFieldType.TIME | ||
| RecordFieldType.TIME, | ||
| RecordFieldType.ARRAY, | ||
| RecordFieldType.RECORD, | ||
| RecordFieldType.MAP | ||
| ); | ||
|
|
||
| /** | ||
| * Get Converted Record with conditional handling for field values requiring translation | ||
| * Get Converted Record with recursive, schema-aware handling for field values requiring translation | ||
| * | ||
| * @param inputRecord Input Record to be converted | ||
| * @param struct Iceberg Struct Type describing the target field types (may be null for scalar-only conversion) | ||
| * @return Input Record or new Record with converted field values | ||
| */ | ||
| static Record getConvertedRecord(final Record inputRecord) { | ||
| final Record convertedRecord; | ||
|
|
||
| static Record getConvertedRecord(final Record inputRecord, final Types.StructType struct) { | ||
| final RecordSchema recordSchema = inputRecord.getSchema(); | ||
| if (isConversionRequired(recordSchema)) { | ||
| final Map<String, Object> values = inputRecord.toMap(); | ||
| convertedRecord = getConvertedRecord(recordSchema, values); | ||
| } else { | ||
| convertedRecord = inputRecord; | ||
| if (!isConversionRequired(recordSchema)) { | ||
| return inputRecord; | ||
| } | ||
|
|
||
| return convertedRecord; | ||
| } | ||
|
|
||
| private static Record getConvertedRecord(final RecordSchema recordSchema, final Map<String, Object> values) { | ||
| final Map<String, Object> values = inputRecord.toMap(); | ||
| final Map<String, Object> convertedValues = new LinkedHashMap<>(); | ||
|
|
||
| for (final Map.Entry<String, Object> entry : values.entrySet()) { | ||
| final String field = entry.getKey(); | ||
| final Object value = entry.getValue(); | ||
| final Object converted = getConvertedValue(value); | ||
| convertedValues.put(field, converted); | ||
| final Type fieldType = fieldType(struct, field); | ||
| convertedValues.put(field, convertValue(entry.getValue(), fieldType)); | ||
| } | ||
|
|
||
| return new MapRecord(recordSchema, convertedValues); | ||
| } | ||
|
|
||
| private static Object getConvertedValue(final Object value) { | ||
| static Object convertValue(final Object value, final Type icebergType) { | ||
| return switch (value) { | ||
| // Convert java.sql types to corresponding java.time types for Apache Iceberg | ||
| case Timestamp timestamp -> timestamp.toLocalDateTime(); | ||
| case Date date -> date.toLocalDate(); | ||
| case Time time -> time.toLocalTime(); | ||
| case null, default -> value; | ||
| // Recursively convert complex types against the matching Iceberg type | ||
| case null, default -> convertComplexValue(value, icebergType); | ||
| }; | ||
| } | ||
|
|
||
| private static boolean isConversionRequired(final RecordSchema recordSchema) { | ||
| final List<RecordField> fields = recordSchema.getFields(); | ||
| /** | ||
| * Recursively convert array, collection, nested record, and map values against the matching Iceberg type. | ||
| * The value is returned unchanged when the target Iceberg type is unknown or does not describe a complex type | ||
| * matching the value. | ||
| */ | ||
| private static Object convertComplexValue(final Object value, final Type icebergType) { | ||
| if (icebergType == null) { | ||
| return value; | ||
| } | ||
|
|
||
| if (icebergType.isListType()) { | ||
| final Type elementType = icebergType.asListType().elementType(); | ||
| if (value instanceof Object[] array) { | ||
| return convertList(Arrays.asList(array), elementType); | ||
| } | ||
| if (value instanceof Collection<?> collection) { | ||
| return convertList(collection, elementType); | ||
| } | ||
| } else if (icebergType.isStructType() && value instanceof Record nestedRecord) { | ||
| return new DelegatedRecord(nestedRecord, icebergType.asStructType()); | ||
| } else if (icebergType.isMapType() && value instanceof Map<?, ?> map) { | ||
| return convertMap(map, icebergType.asMapType()); | ||
| } | ||
|
|
||
| for (final RecordField field : fields) { | ||
| final DataType dataType = field.getDataType(); | ||
| final RecordFieldType recordFieldType = dataType.getFieldType(); | ||
| return value; | ||
| } | ||
|
|
||
| private static List<Object> convertList(final Collection<?> collection, final Type elementType) { | ||
| final List<Object> converted = new ArrayList<>(collection.size()); | ||
| for (final Object element : collection) { | ||
| converted.add(convertValue(element, elementType)); | ||
| } | ||
| return converted; | ||
| } | ||
|
|
||
| private static Map<Object, Object> convertMap(final Map<?, ?> map, final Types.MapType mapType) { | ||
| final Map<Object, Object> converted = new LinkedHashMap<>(); | ||
|
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. LinkedHashMap vs HashMap might be debateable? |
||
| for (final Map.Entry<?, ?> entry : map.entrySet()) { | ||
| final Object key = convertValue(entry.getKey(), mapType.keyType()); | ||
| final Object mappedValue = convertValue(entry.getValue(), mapType.valueType()); | ||
| converted.put(key, mappedValue); | ||
| } | ||
| return converted; | ||
| } | ||
|
|
||
| private static Type fieldType(final Types.StructType struct, final String fieldName) { | ||
| if (struct == null) { | ||
| return null; | ||
| } | ||
| final Types.NestedField nestedField = struct.field(fieldName); | ||
| return nestedField == null ? null : nestedField.type(); | ||
| } | ||
|
|
||
| private static boolean isConversionRequired(final RecordSchema recordSchema) { | ||
| for (final RecordField field : recordSchema.getFields()) { | ||
| final RecordFieldType recordFieldType = field.getDataType().getFieldType(); | ||
| if (CONVERSION_REQUIRED_FIELD_TYPES.contains(recordFieldType)) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The field names should be declared statically and reused in both the input Schema and output row.
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.
Moved hardcoded constants to static ones: ef3e162