in odps-sdk-impl/odps-common-local/src/main/java/com/aliyun/odps/local/common/utils/LocalWritableUtils.java [184:275]
public static Object convert(Writable arg0, TypeInfo typeInfo) {
if (arg0 == null) {
return null;
}
if (typeInfo == TypeInfoFactory.STRING || arg0 instanceof Text) {
Text text = (Text) arg0;
return new String(text.getBytes(), 0, text.getLength(), TypeConvertUtils.UTF8);
}
if (typeInfo == TypeInfoFactory.TINYINT || arg0 instanceof ByteWritable) {
return ((ByteWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.SMALLINT || arg0 instanceof ShortWritable) {
return ((ShortWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.INT || arg0 instanceof IntWritable) {
return ((IntWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.BIGINT || arg0 instanceof LongWritable) {
return ((LongWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.BOOLEAN || arg0 instanceof BooleanWritable) {
return ((BooleanWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.FLOAT || arg0 instanceof FloatWritable) {
return ((FloatWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.DOUBLE || arg0 instanceof DoubleWritable) {
return ((DoubleWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.DECIMAL || arg0 instanceof BigDecimalWritable) {
return ((BigDecimalWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.DATETIME || arg0 instanceof DatetimeWritable) {
return ((DatetimeWritable) arg0).getDatetime();
}
if (typeInfo == TypeInfoFactory.TIMESTAMP || arg0 instanceof TimestampWritable) {
return ((TimestampWritable)arg0).getTimestamp();
}
if (typeInfo == TypeInfoFactory.DATE || arg0 instanceof DateWritable) {
return ((DateWritable)arg0).getDate();
}
if (typeInfo instanceof CharTypeInfo || arg0 instanceof CharWritable) {
return ((CharWritable) arg0).get();
}
if (typeInfo instanceof VarcharTypeInfo || arg0 instanceof VarcharWritable) {
return ((VarcharWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.BINARY || arg0 instanceof BytesWritable) {
BytesWritable tmp = (BytesWritable) arg0;
byte[] bytes = Arrays.copyOfRange(tmp.getBytes(), 0, tmp.getLength());
return new Binary(bytes);
}
if (typeInfo == TypeInfoFactory.INTERVAL_DAY_TIME || arg0 instanceof IntervalDayTimeWritable) {
return ((IntervalDayTimeWritable) arg0).get();
}
if (typeInfo == TypeInfoFactory.INTERVAL_YEAR_MONTH || arg0 instanceof IntervalYearMonthWritable) {
return ((IntervalYearMonthWritable) arg0).get();
}
if (typeInfo instanceof ArrayTypeInfo) {
TypeInfo subType = ((ArrayTypeInfo) typeInfo).getElementTypeInfo();
List<Object> res = new ArrayList<>();
for (Writable writable : ((ArrayWritable) arg0).get()) {
res.add(convert(writable, subType));
}
return res;
}
if (typeInfo instanceof MapTypeInfo) {
TypeInfo keyType = ((MapTypeInfo) typeInfo).getKeyTypeInfo();
TypeInfo valueType = ((MapTypeInfo) typeInfo).getValueTypeInfo();
Map map = new HashMap();
for (Map.Entry<Writable, Writable> entry : ((MapWritable) arg0).entrySet()) {
Object key = convert(entry.getKey(), keyType);
Object value = convert(entry.getValue(), valueType);
map.put(key, value);
}
return map;
}
if (typeInfo instanceof StructTypeInfo) {
List<TypeInfo> fieldTypes = ((StructTypeInfo)typeInfo).getFieldTypeInfos();
List<Writable> writables = ((StructWritable) arg0).get();
assert fieldTypes.size() == writables.size();
List<Object> elements = new ArrayList<>(writables.size());
for (int i = 0; i < writables.size(); i++) {
Writable val = writables.get(i);
TypeInfo type = fieldTypes.get(i);
elements.add(convert(val, type));
}
return new SimpleStruct((StructTypeInfo)typeInfo, elements);
}
throw new IllegalArgumentException("unsupported data type:" + arg0.getClass().getName());
}