/** * Validates and returns the underlying {@link LogicalType} of the given {@link Schema.Field}. * * @param field field with logical type * @param allowedTypes acceptable logical types * @return the underlying {@link LogicalType} for the field * @throws UnexpectedFormatException if the field provided does not have a {@link LogicalType} that is * one of the acceptable types */ @Nullable private static LogicalType validateAndGetLogicalType(Schema.Field field, Set<LogicalType> allowedTypes) { if (field == null) { return null; } String fieldName = field.getName(); LogicalType logicalType = getLogicalType(field.getSchema(), allowedTypes); if (logicalType == null) { throw new UnexpectedFormatException(String.format("Field %s does not have a logical type.", fieldName)); } if (!allowedTypes.contains(logicalType)) { throw new UnexpectedFormatException(String.format("Field %s must be of logical type %s, instead it is of type %s", fieldName, allowedTypes, logicalType)); } return logicalType; }
/** * Gets the {@link LogicalType} defined in the given {@link Schema}. * If the given {@link Schema} is a {@link Schema.Type#UNION}, this method will find * the {@link LogicalType} recursively and return the first {@link LogicalType} it encountered. * * @param schema the {@link Schema} for finding the {@link LogicalType} * @param allowedTypes acceptable logical types * @return the {@link LogicalType} defined in the given schema or {@code null} if no {@link LogicalType} was found */ @Nullable private static LogicalType getLogicalType(Schema schema, Set<LogicalType> allowedTypes) { LogicalType logicalType = schema.getLogicalType(); if (logicalType != null && allowedTypes.contains(logicalType)) { return logicalType; } if (schema.getType() == Schema.Type.UNION) { for (Schema unionSchema : schema.getUnionSchemas()) { logicalType = getLogicalType(unionSchema, allowedTypes); if (logicalType != null) { return logicalType; } } } return null; }
/** * Validates and returns the underlying {@link LogicalType} of the given {@link Schema.Field}. * * @param field field with logical type * @param allowedTypes acceptable logical types * @return the underlying {@link LogicalType} for the field * @throws UnexpectedFormatException if the field provided does not have a {@link LogicalType} that is * one of the acceptable types */ @Nullable private static LogicalType validateAndGetLogicalType(Schema.Field field, Set<LogicalType> allowedTypes) { if (field == null) { return null; } String fieldName = field.getName(); LogicalType logicalType = getLogicalType(field.getSchema(), allowedTypes); if (logicalType == null) { throw new UnexpectedFormatException(String.format("Field %s does not have a logical type.", fieldName)); } if (!allowedTypes.contains(logicalType)) { throw new UnexpectedFormatException(String.format("Field %s must be of logical type %s, instead it is of type %s", fieldName, allowedTypes, logicalType)); } return logicalType; }
/** * Gets the {@link LogicalType} defined in the given {@link Schema}. * If the given {@link Schema} is a {@link Schema.Type#UNION}, this method will find * the {@link LogicalType} recursively and return the first {@link LogicalType} it encountered. * * @param schema the {@link Schema} for finding the {@link LogicalType} * @param allowedTypes acceptable logical types * @return the {@link LogicalType} defined in the given schema or {@code null} if no {@link LogicalType} was found */ @Nullable private static LogicalType getLogicalType(Schema schema, Set<LogicalType> allowedTypes) { LogicalType logicalType = schema.getLogicalType(); if (logicalType != null && allowedTypes.contains(logicalType)) { return logicalType; } if (schema.getType() == Schema.Type.UNION) { for (Schema unionSchema : schema.getUnionSchemas()) { logicalType = getLogicalType(unionSchema, allowedTypes); if (logicalType != null) { return logicalType; } } } return null; }