in xtable-hudi-support/xtable-hudi-support-utils/src/main/java/org/apache/xtable/hudi/idtracking/IdTracker.java [131:180]
private static List<IdMapping> generateIdMappings(
Schema schema, AtomicInteger lastFieldId, List<IdMapping> existingMappings) {
Map<String, IdMapping> fieldNameToExistingMapping =
existingMappings == null
? new HashMap<>()
: existingMappings.stream()
.collect(Collectors.toMap(IdMapping::getName, Function.identity()));
List<IdMapping> mappings = new ArrayList<>();
List<Pair<IdMapping, Schema>> nested = new ArrayList<>();
if (schema.getType() == Schema.Type.ARRAY) {
// add a single field "element"
IdMapping fieldMapping =
fieldNameToExistingMapping.computeIfAbsent(
ARRAY_FIELD, key -> new IdMapping(key, lastFieldId.incrementAndGet()));
mappings.add(fieldMapping);
nested.add(Pair.of(fieldMapping, getFieldSchema(schema.getElementType())));
} else if (schema.getType() == Schema.Type.MAP) {
// add a field for the key and value
IdMapping keyFieldMapping =
fieldNameToExistingMapping.computeIfAbsent(
KEY_FIELD, key -> new IdMapping(key, lastFieldId.incrementAndGet()));
IdMapping valueFieldMapping =
fieldNameToExistingMapping.computeIfAbsent(
VALUE_FIELD, key -> new IdMapping(key, lastFieldId.incrementAndGet()));
mappings.add(keyFieldMapping);
mappings.add(valueFieldMapping);
nested.add(Pair.of(valueFieldMapping, getFieldSchema(schema.getValueType())));
} else if (schema.getType() == Schema.Type.RECORD) {
for (Schema.Field field : schema.getFields()) {
Schema fieldSchema = getFieldSchema(field.schema());
IdMapping fieldMapping =
fieldNameToExistingMapping.computeIfAbsent(
field.name(), key -> new IdMapping(key, lastFieldId.incrementAndGet()));
mappings.add(fieldMapping);
if (fieldSchema.getType() == Schema.Type.RECORD
|| fieldSchema.getType() == Schema.Type.ARRAY
|| fieldSchema.getType() == Schema.Type.MAP) {
nested.add(Pair.of(fieldMapping, fieldSchema));
}
}
}
nested.forEach(
pair ->
pair.getLeft()
.setFields(
generateIdMappings(pair.getRight(), lastFieldId, pair.getLeft().getFields())));
return mappings.stream()
.sorted(Comparator.comparing(IdMapping::getId))
.collect(Collectors.toList());
}