in odps-console-dship/src/main/java/com/aliyun/odps/ship/common/RecordConverter.java [446:571]
private Object parseValue(TypeInfo typeInfo, byte[] v)
throws UnsupportedEncodingException, ParseException {
if (Arrays.equals(v, nullBytes)) {
return null;
}
boolean isIgnoreCharset = Util.isIgnoreCharset(charset);
OdpsType type = typeInfo.getOdpsType();
String value = getTrimmedString(v, defaultCharset);
switch (type) {
case BIGINT: {
return Long.valueOf(value);
}
case DOUBLE: {
return Double.valueOf(value);
}
case DATETIME: {
try {
// support Date format yyyy-MM-dd for compatible
TemporalAccessor accessor = timestampFormatter.parseBest(value, ZonedDateTime::from, LocalDate::from);
if (accessor instanceof LocalDate) {
accessor = ((LocalDate) accessor).atStartOfDay(zoneId);
}
return accessor;
} catch (RuntimeException e) {
throw new ParseException(e.getMessage());
}
}
case BOOLEAN: {
String vStr = value.toLowerCase();
if ("true".equals(vStr) || "false".equals(vStr)) {
return "true".equals(vStr);
} else if ("0".equals(vStr) || "1".equals(vStr)) {
return "1".equals(vStr);
} else {
throw new IllegalArgumentException(
"Invalid boolean value, expect: 'true'|'false'|'0'|'1'");
}
}
case STRING: {
try {
if (isIgnoreCharset) {
return v;
} else {
return getTrimmedString(v, charset);
}
} catch (IllegalArgumentException e) {
// for big than 8M
throw new IllegalArgumentException("String value bigger than 8M");
}
}
case CHAR: {
return new Char(value);
}
case VARCHAR: {
return new Varchar(value);
}
case DECIMAL: {
return new BigDecimal(value);
}
case INT: {
return Integer.valueOf(value);
}
case TINYINT: {
return Byte.valueOf(value);
}
case SMALLINT: {
return Short.valueOf(value);
}
case FLOAT: {
return Float.valueOf(value);
}
case BINARY: {
return new Binary(v);
}
case DATE: {
return LocalDate.parse(value, dateFormatter);
}
case TIMESTAMP: {
try {
// 不兼容 nano > 9 位的情况
// 1. 用户不指定 format,用标准的 pattern + 0-9 nano
// 2. 指定 format, 按用户 format 走
ZonedDateTime dateTime = ZonedDateTime.parse(value, timestampFormatter);
return dateTime.toInstant();
} catch (RuntimeException e) {
throw new ParseException(e.getMessage());
}
}
case TIMESTAMP_NTZ: {
try {
return LocalDateTime.parse(value, timestampFormatter);
} catch (RuntimeException e) {
throw new ParseException(e.getMessage());
}
}
case MAP: {
JsonElement map = jsonParser.parse(value);
// 当数据为null(字符串)时,会被jsonParser解析为JsonNull类型:
// 1. 当nullTag为NULL/null/"",会在函数入口处返回null。
// 2. 当nullTag不为NULL/null/"",需要在此处提前中止,返回null。
// 以下同理
if (map.isJsonNull()) {
return null;
}
return transformMap(map.getAsJsonObject(), (MapTypeInfo) typeInfo);
}
case STRUCT: {
JsonElement struct = jsonParser.parse(value);
if (struct.isJsonNull()) {
return null;
}
return transformStruct(struct.getAsJsonObject(), (StructTypeInfo) typeInfo);
}
case ARRAY: {
JsonElement array = jsonParser.parse(value);
if (array.isJsonNull()) {
return null;
}
return transformArray(array.getAsJsonArray(), (ArrayTypeInfo) typeInfo);
}
default:
throw new IllegalArgumentException("Unknown column type: " + typeInfo);
}
}