use of org.apache.avro.Schema.Type in project databus by linkedin.
the class OracleAvroGenericEventFactory method put.
private void put(GenericRecord record, Field field, Object databaseFieldValue) throws EventCreationException {
// Get the field name and type from the event schema
String schemaFieldName = field.name();
// == field.schema() if not a union
Schema fieldSchema = SchemaHelper.unwindUnionSchema(field);
Type avroFieldType = fieldSchema.getType();
if (databaseFieldValue == null) {
// The field value was null. If the field is nullable then we do nothing. Otherwise this is an error.
boolean isNullAllowedInSchema = SchemaHelper.isNullable(field);
if (!isNullAllowedInSchema) {
throw new EventCreationException("Null value not allowed for field " + schemaFieldName);
}
} else {
if (_log.isTraceEnabled()) {
_log.trace("record.put(\"" + schemaFieldName + "\", (" + avroFieldType + ") \"" + databaseFieldValue + "\"");
}
try {
switch(avroFieldType) {
case BOOLEAN:
case BYTES:
case DOUBLE:
case FLOAT:
case INT:
case LONG:
case STRING:
case NULL:
putSimpleValue(record, schemaFieldName, avroFieldType, databaseFieldValue);
break;
case RECORD:
addOracleRecordToParent(record, schemaFieldName, fieldSchema, (Struct) databaseFieldValue);
break;
case ARRAY:
putArray(record, schemaFieldName, fieldSchema, (Array) databaseFieldValue);
break;
// exists in some Espresso schemas: don't blindly cut and paste!
case ENUM:
// ditto
case MAP:
case FIXED:
// shouldn't be possible, given unwindUnionSchema() call above
case UNION:
default:
throw new EventCreationException("Don't know how to populate this type of field: " + avroFieldType);
}
} catch (ClassCastException ex) {
throw new EventCreationException("Type conversion error for field name (" + field.name() + ") in source " + _sourceId + ". Value was: " + databaseFieldValue + " avro field was: " + avroFieldType, ex);
}
}
}
use of org.apache.avro.Schema.Type in project pinot by linkedin.
the class SegmentTestUtils method getColumnType.
public static DataType getColumnType(Field field) {
org.apache.avro.Schema fieldSchema = field.schema();
fieldSchema = extractSchemaFromUnionIfNeeded(fieldSchema);
final Type type = fieldSchema.getType();
if (type == Type.ARRAY) {
org.apache.avro.Schema elementSchema = extractSchemaFromUnionIfNeeded(fieldSchema.getElementType());
if (elementSchema.getType() == Type.RECORD) {
if (elementSchema.getFields().size() == 1) {
elementSchema = elementSchema.getFields().get(0).schema();
} else {
throw new RuntimeException("More than one schema in Multi-value column!");
}
elementSchema = extractSchemaFromUnionIfNeeded(elementSchema);
}
return DataType.valueOf(elementSchema.getType());
} else {
return DataType.valueOf(type);
}
}
use of org.apache.avro.Schema.Type in project voldemort by voldemort.
the class SchemaEvolutionValidator method comparePrimitiveTypes.
/* package private */
void comparePrimitiveTypes(Schema oldSchema, Schema newSchema, List<Message> messages, String name) {
if (oldSchema == null || newSchema == null) {
throw new IllegalArgumentException("Old schema must both be a primitive type. Name=" + name + ". Type=" + oldSchema);
}
Schema.Type oldType = oldSchema.getType();
Schema.Type newType = newSchema.getType();
if (oldType != newType) {
if (((oldType == Schema.Type.INT && (newType == Schema.Type.LONG || newType == Schema.Type.FLOAT || newType == Schema.Type.DOUBLE)) || (oldType == Schema.Type.LONG && (newType == Schema.Type.FLOAT || newType == Schema.Type.DOUBLE)) || (oldType == Schema.Type.FLOAT && (newType == Schema.Type.DOUBLE)))) {
messages.add(new Message(Level.INFO, "Type change from " + oldSchema.getType() + " to " + newSchema.getType() + " for field " + name));
} else {
messages.add(new Message(Level.ERROR, "Illegal type change from " + oldSchema.getType() + " to " + newSchema.getType() + " for field " + name));
}
}
}
use of org.apache.avro.Schema.Type in project voldemort by voldemort.
the class SchemaEvolutionValidator method checkDefaultJson.
/**
* Check that the default json node is a valid default value. If not, return
* the expected type as a String.
*/
/* package private */
static String checkDefaultJson(JsonNode defaultJson, Schema schema) {
Schema.Type fieldType = schema.getType();
String expectedVal = null;
switch(fieldType) {
case NULL:
if (!defaultJson.isNull()) {
expectedVal = "null";
}
break;
case BOOLEAN:
if (!defaultJson.isBoolean()) {
expectedVal = "boolean";
}
break;
case INT:
if (!defaultJson.isInt()) {
expectedVal = "int";
}
break;
case LONG:
if (!defaultJson.isInt() && !defaultJson.isLong()) {
expectedVal = "long";
}
break;
case FLOAT:
case DOUBLE:
if (!defaultJson.isNumber()) {
expectedVal = "number";
}
break;
case BYTES:
if (defaultJson.isTextual()) {
break;
}
expectedVal = "bytes (ex. \"\\u00FF\")";
break;
case STRING:
if (!defaultJson.isTextual()) {
expectedVal = "string";
}
break;
case ENUM:
if (defaultJson.isTextual()) {
if (schema.hasEnumSymbol(defaultJson.getTextValue())) {
break;
}
}
expectedVal = "valid enum";
break;
case FIXED:
if (defaultJson.isTextual()) {
byte[] fixed = defaultJson.getValueAsText().getBytes();
if (fixed.length == schema.getFixedSize()) {
break;
}
expectedVal = "fixed size incorrect. Expected size: " + schema.getFixedSize() + " got size " + fixed.length;
break;
}
expectedVal = "fixed (ex. \"\\u00FF\")";
break;
case ARRAY:
if (defaultJson.isArray()) {
// Check all array variables
boolean isGood = true;
for (JsonNode node : defaultJson) {
String val = checkDefaultJson(node, schema.getElementType());
if (val == null) {
continue;
} else {
isGood = false;
break;
}
}
if (isGood) {
break;
}
}
expectedVal = "array of type " + schema.getElementType().toString();
break;
case MAP:
if (defaultJson.isObject()) {
boolean isGood = true;
for (JsonNode node : defaultJson) {
String val = checkDefaultJson(node, schema.getValueType());
if (val == null) {
continue;
} else {
isGood = false;
break;
}
}
if (isGood) {
break;
}
}
expectedVal = "map of type " + schema.getValueType().toString();
break;
case RECORD:
if (defaultJson.isObject()) {
boolean isGood = true;
for (Field field : schema.getFields()) {
JsonNode jsonNode = defaultJson.get(field.name());
if (jsonNode == null) {
jsonNode = field.defaultValue();
if (jsonNode == null) {
isGood = false;
break;
}
}
String val = checkDefaultJson(jsonNode, field.schema());
if (val != null) {
isGood = false;
break;
}
}
if (isGood) {
break;
}
}
expectedVal = "record of type " + schema.toString();
break;
case UNION:
// Avro spec states we only need to match with the first item
expectedVal = checkDefaultJson(defaultJson, schema.getTypes().get(0));
break;
}
return expectedVal;
}
use of org.apache.avro.Schema.Type in project voldemort by voldemort.
the class SchemaEvolutionValidator method compareTypes.
/* package private */
void compareTypes(Schema oldSchema, Schema newSchema, List<Message> messages, String name) {
oldSchema = stripOptionalTypeUnion(oldSchema);
newSchema = stripOptionalTypeUnion(newSchema);
Schema.Type oldType = oldSchema.getType();
Schema.Type newType = newSchema.getType();
if (oldType != Type.UNION && newType == Type.UNION) {
boolean compatibleTypeFound = false;
for (Schema s : newSchema.getTypes()) {
if ((oldType != Type.RECORD && oldType == s.getType()) || (oldType == Type.RECORD && s.getType() == Type.RECORD && oldSchema.getName().equals(s.getName()))) {
compareTypes(oldSchema, s, messages, name);
compatibleTypeFound = true;
break;
}
}
if (compatibleTypeFound) {
messages.add(new Message(Level.INFO, "Type change from " + oldType + " to " + newType + " for field " + name + ". The new union includes the original type."));
} else {
messages.add(new Message(Level.ERROR, "Incompatible type change from " + oldType + " to " + newType + " for field " + name + ". The new union does not include the original type."));
}
} else if (oldType == Type.RECORD) {
if (!_recordStack.contains(oldSchema.getName())) {
_recordStack.add(oldSchema.getName());
compareRecordTypes(oldSchema, newSchema, messages, name);
_recordStack.remove(oldSchema.getName());
}
} else if (oldType == Type.ENUM) {
compareEnumTypes(oldSchema, newSchema, messages, name);
} else if (oldType == Type.ARRAY) {
compareArrayTypes(oldSchema, newSchema, messages, name);
} else if (oldType == Type.MAP) {
compareMapTypes(oldSchema, newSchema, messages, name);
} else if (oldType == Type.UNION) {
compareUnionTypes(oldSchema, newSchema, messages, name);
} else if (oldType == Type.FIXED) {
compareFixedTypes(oldSchema, newSchema, messages, name);
} else {
comparePrimitiveTypes(oldSchema, newSchema, messages, name);
}
}
Aggregations