[
https://issues.apache.org/jira/browse/AVRO-2378?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
RAJAT SOMANI updated AVRO-2378:
-------------------------------
Description:
byte[] data type resolves to array type when specified in union as \{ "type":
"null", "bytes" } in the avro schema defined for a byte[] by using
ReflectData.allowNull().get().getSchema(x.class);
and then fails because byte primitive is not supported in array type after
resolveUnion resolves it to array using Reflect data for datum class - class [B
as described in this function below:
protected void writeArray(Schema schema, Object datum, Encoder out)
throws IOException {
if (datum instanceof Collection) {
super.writeArray(schema, datum, out);
return;
}
Class<?> elementClass = datum.getClass().getComponentType();
if (null == elementClass) {
// not a Collection or an Array
throw new AvroRuntimeException("Array data must be a Collection or
Array");
}
Schema element = schema.getElementType();
if (elementClass.isPrimitive()) {
Schema.Type type = element.getType();
out.writeArrayStart();
switch(type) {
case BOOLEAN:
if(elementClass.isPrimitive())
ArrayAccessor.writeArray((boolean[]) datum, out);
break;
case DOUBLE:
ArrayAccessor.writeArray((double[]) datum, out);
break;
case FLOAT:
ArrayAccessor.writeArray((float[]) datum, out);
break;
case INT:
if(elementClass.equals(int.class)) {
ArrayAccessor.writeArray((int[]) datum, out);
} else if(elementClass.equals(char.class)) {
ArrayAccessor.writeArray((char[]) datum, out);
} else if(elementClass.equals(short.class)) {
ArrayAccessor.writeArray((short[]) datum, out);
} else {
arrayError(elementClass, type);
}
break;
case LONG:
ArrayAccessor.writeArray((long[]) datum, out);
break;
default:
arrayError(elementClass, type);
}
out.writeArrayEnd();
} else {
out.writeArrayStart();
writeObjectArray(element, (Object[]) datum, out);
out.writeArrayEnd();
}
}
was:
byte[] data type resolves to array type when specified in union as \{ "type":
"null", "bytes" } in the avro schema defined for a byte[] by using
ReflectData.allowNull().get().getSchema(x.class);
and then fails because byte primitive is not supported in array type after
resolveUnion resolves it to array using Reflect data.
> byte[] is not supported by Reflect data when specified in union as "type":
> "null", "bytes"
> ------------------------------------------------------------------------------------------
>
> Key: AVRO-2378
> URL: https://issues.apache.org/jira/browse/AVRO-2378
> Project: Apache Avro
> Issue Type: Bug
> Affects Versions: 1.8.2
> Reporter: RAJAT SOMANI
> Priority: Major
>
> byte[] data type resolves to array type when specified in union as \{ "type":
> "null", "bytes" } in the avro schema defined for a byte[] by using
> ReflectData.allowNull().get().getSchema(x.class);
> and then fails because byte primitive is not supported in array type after
> resolveUnion resolves it to array using Reflect data for datum class - class
> [B as described in this function below:
>
> protected void writeArray(Schema schema, Object datum, Encoder out)
> throws IOException {
> if (datum instanceof Collection) {
> super.writeArray(schema, datum, out);
> return;
> }
> Class<?> elementClass = datum.getClass().getComponentType();
> if (null == elementClass) {
> // not a Collection or an Array
> throw new AvroRuntimeException("Array data must be a Collection or
> Array");
> }
> Schema element = schema.getElementType();
> if (elementClass.isPrimitive()) {
> Schema.Type type = element.getType();
> out.writeArrayStart();
> switch(type) {
> case BOOLEAN:
> if(elementClass.isPrimitive())
> ArrayAccessor.writeArray((boolean[]) datum, out);
> break;
> case DOUBLE:
> ArrayAccessor.writeArray((double[]) datum, out);
> break;
> case FLOAT:
> ArrayAccessor.writeArray((float[]) datum, out);
> break;
> case INT:
> if(elementClass.equals(int.class)) {
> ArrayAccessor.writeArray((int[]) datum, out);
> } else if(elementClass.equals(char.class)) {
> ArrayAccessor.writeArray((char[]) datum, out);
> } else if(elementClass.equals(short.class)) {
> ArrayAccessor.writeArray((short[]) datum, out);
> } else {
> arrayError(elementClass, type);
> }
> break;
> case LONG:
> ArrayAccessor.writeArray((long[]) datum, out);
> break;
> default:
> arrayError(elementClass, type);
> }
> out.writeArrayEnd();
> } else {
> out.writeArrayStart();
> writeObjectArray(element, (Object[]) datum, out);
> out.writeArrayEnd();
> }
> }
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)