@Override public Schema field(Types.NestedField field, Schema fieldSchema) { if (field.isOptional()) { return toOption(fieldSchema); } else { return fieldSchema; } }
@Override public Schema union(Schema union, List<Schema> options) { Preconditions.checkState(AvroSchemaUtil.isOptionSchema(union), "Invalid schema: non-option unions are not supported: {}", union); // only unions with null are allowed, and a null schema results in null Schema pruned = null; if (options.get(0) != null) { pruned = options.get(0); } else if (options.get(1) != null) { pruned = options.get(1); } if (pruned != null) { if (pruned != AvroSchemaUtil.fromOption(union)) { return AvroSchemaUtil.toOption(pruned); } return union; } return null; }
@Override public Schema union(Schema union, Iterable<Schema> options) { Preconditions.checkState(isOptionSchema(union), "Invalid schema: non-option unions are not supported: {}", union); Schema nonNullOriginal = fromOption(union); Schema nonNullResult = fromOptions(Lists.newArrayList(options)); if (nonNullOriginal != nonNullResult) { return toOption(nonNullResult); } return union; }
@Override public Schema list(Types.ListType list, Schema elementSchema) { Schema listSchema = results.get(list); if (listSchema != null) { return listSchema; } if (list.isElementOptional()) { listSchema = Schema.createArray(toOption(elementSchema)); } else { listSchema = Schema.createArray(elementSchema); } listSchema.addProp(AvroSchemaUtil.ELEMENT_ID_PROP, list.elementId()); results.put(list, listSchema); return listSchema; }
@Override public Schema map(Types.MapType map, Schema keySchema, Schema valueSchema) { Schema mapSchema = results.get(map); if (mapSchema != null) { return mapSchema; } if (keySchema.getType() == Schema.Type.STRING) { // if the map has string keys, use Avro's map type mapSchema = Schema.createMap( map.isValueOptional() ? toOption(valueSchema) : valueSchema); mapSchema.addProp(AvroSchemaUtil.KEY_ID_PROP, map.keyId()); mapSchema.addProp(AvroSchemaUtil.VALUE_ID_PROP, map.valueId()); } else { mapSchema = AvroSchemaUtil.createMap(map.keyId(), keySchema, map.valueId(), map.isValueOptional() ? toOption(valueSchema) : valueSchema); } results.put(map, mapSchema); return mapSchema; }
field.name(), toOption(convert(field.type())), null, JsonProperties.NULL_VALUE); newField.addProp(AvroSchemaUtil.FIELD_ID_PROP, field.fieldId()); updatedFields.add(newField);
static Schema.Field optionalField(int id, String name, Schema schema) { return addId(id, new Schema.Field(name, toOption(schema), null, JsonProperties.NULL_VALUE)); }