public static Object fromString()

in odps-sdk-impl/odps-common-local/src/main/java/com/aliyun/odps/local/common/utils/TypeConvertUtils.java [71:141]


  public static Object fromString(TypeInfo typeInfo, String token, boolean toBinary) {
    if (token == null || Constants.NULL_TOKEN.equals(token)) {
      return null;
    }
    switch (typeInfo.getOdpsType()) {
      case BIGINT:
        return Long.parseLong(token);
      case DOUBLE:
        return Double.parseDouble(token);
      case BOOLEAN:
        return Boolean.parseBoolean(token);
      case DATETIME:
        try {
          return DATE_FORMAT.parse(token);
        } catch (ParseException e) {
          throw new RuntimeException(" parse date failed:" + token, e);
        }
      case STRING:
        try {
          return toBinary ? token.getBytes(UTF8) : token;
        } catch (Exception e) {
          throw new RuntimeException(" from string failed!", e);
        }
      case DECIMAL:
        return new BigDecimal(token);
      case TINYINT:
        return Byte.parseByte(token);
      case SMALLINT:
        return Short.parseShort(token);
      case INT:
        return Integer.parseInt(token);
      case FLOAT:
        return Float.parseFloat(token);
      case CHAR:
        return new Char(token, ((CharTypeInfo)typeInfo).getLength());
      case VARCHAR:
        return new Varchar(token, ((VarcharTypeInfo)typeInfo).getLength());
      case DATE:
        return java.sql.Date.valueOf(token);
      case TIMESTAMP:
        return Timestamp.valueOf(token);
      case BINARY:
        try {
          return new Binary(LocalRunUtils.fromReadableString(token));
        } catch (Exception e) {
          throw new RuntimeException(" from readable string failed!" + e);
        }
      case INTERVAL_DAY_TIME: {
        JsonObject json = new JsonParser().parse(token).getAsJsonObject();
        return new IntervalDayTime(json.get("totalSeconds").getAsInt(), json.get("nanos").getAsInt());
      }
      case INTERVAL_YEAR_MONTH: {
        JsonObject json = new JsonParser().parse(token).getAsJsonObject();
        return new IntervalYearMonth(json.get("years").getAsInt(), json.get("months").getAsInt());
      }
      case STRUCT: {
        JsonObject json = new JsonParser().parse(token).getAsJsonObject();
        return parseStruct(json, (StructTypeInfo) typeInfo);
      }
      case MAP: {
        JsonObject json = new JsonParser().parse(token).getAsJsonObject();
        return parseMap(json, (MapTypeInfo) typeInfo);
      }
      case ARRAY: {
        JsonArray json = new JsonParser().parse(token).getAsJsonArray();
        return parseArray(json, (ArrayTypeInfo) typeInfo);
      }
      default:
        throw new RuntimeException("Unknown column type: " + typeInfo.getOdpsType());
    }
  }