public static Writable convert()

in odps-sdk-impl/odps-common-local/src/main/java/com/aliyun/odps/local/common/utils/LocalWritableUtils.java [79:182]


  public static Writable convert(Object arg0, TypeInfo typeInfo) {
    if (arg0 == null) {
      return null;
    }
    if (typeInfo == TypeInfoFactory.STRING || arg0 instanceof String) {
      if (arg0 instanceof String) {
        return new Text((String) arg0);
      } else {
        byte[] bytes = (byte[]) arg0;
        return new Text(bytes);
      }
    }
    if (typeInfo == TypeInfoFactory.TINYINT || arg0 instanceof Byte) {
      return new ByteWritable((Byte) arg0);
    }
    if (typeInfo == TypeInfoFactory.SMALLINT || arg0 instanceof Short) {
      return new ShortWritable((Short) arg0);
    }
    if (typeInfo == TypeInfoFactory.INT || arg0 instanceof Integer) {
      return new IntWritable((Integer) arg0);
    }
    if (typeInfo == TypeInfoFactory.BIGINT || arg0 instanceof Long) {
      return new LongWritable((Long) arg0);
    }
    if (typeInfo == TypeInfoFactory.BOOLEAN || arg0 instanceof Boolean) {
      return new BooleanWritable((Boolean) arg0);
    }
    if (typeInfo == TypeInfoFactory.FLOAT || arg0 instanceof Float) {
      return new FloatWritable((Float) arg0);
    }
    if (typeInfo == TypeInfoFactory.DOUBLE || arg0 instanceof Double) {
      return new DoubleWritable((Double) arg0);
    }
    if (typeInfo == TypeInfoFactory.DECIMAL || arg0 instanceof BigDecimal) {
      return new BigDecimalWritable((BigDecimal) arg0);
    }
    if (typeInfo == TypeInfoFactory.DATETIME || arg0 instanceof java.util.Date) {
      return new DatetimeWritable(((java.util.Date)arg0).getTime());
    }
    if (typeInfo == TypeInfoFactory.TIMESTAMP || arg0 instanceof Timestamp) {
      TimestampWritable temp = new TimestampWritable();
      temp.setTimestamp((Timestamp) arg0);
      return temp;
    }
    if (typeInfo == TypeInfoFactory.DATE || arg0 instanceof java.sql.Date) {
      return new DateWritable(((java.sql.Date) arg0).getTime()/1000);
    }
    if (typeInfo instanceof CharTypeInfo || arg0 instanceof Char) {
      return new CharWritable((Char) arg0);
    }
    if (typeInfo instanceof VarcharTypeInfo || arg0 instanceof Varchar) {
      return new VarcharWritable((Varchar) arg0);
    }
    if (typeInfo == TypeInfoFactory.BINARY || arg0 instanceof Binary) {
      return new BytesWritable(((Binary) arg0).data());
    }
    if (typeInfo == TypeInfoFactory.INTERVAL_DAY_TIME || arg0 instanceof IntervalDayTime) {
      return new IntervalDayTimeWritable((IntervalDayTime) arg0);
    }
    if (typeInfo == TypeInfoFactory.INTERVAL_YEAR_MONTH || arg0 instanceof IntervalYearMonth) {
      return new IntervalYearMonthWritable((IntervalYearMonth) arg0);
    }
    if (typeInfo instanceof ArrayTypeInfo) {
      TypeInfo subType = ((ArrayTypeInfo) typeInfo).getElementTypeInfo();
      List list = (List) arg0;
      Writable[] writables = new Writable[list.size()];
      for (int i = 0; i < writables.length; i++) {
        Object ele = list.get(i);
        writables[i] = convert(ele, subType);
      }
      Class subClazz = getWritableClass(subType);
      return new ArrayWritable(subClazz, writables);
    }
    if (typeInfo instanceof MapTypeInfo) {
      TypeInfo keyType = ((MapTypeInfo) typeInfo).getKeyTypeInfo();
      TypeInfo valueType = ((MapTypeInfo) typeInfo).getValueTypeInfo();
      Map map = (Map) arg0;
      MapWritable result = new MapWritable();
      for (Object entry : map.entrySet()) {
        Map.Entry mapEntry = (Map.Entry) entry;
        Writable key = convert(mapEntry.getKey(), keyType);
        Writable value = null;
        if (mapEntry.getValue() != null) {
          value = convert(mapEntry.getValue(), valueType);
        }
        result.put(key, value);
      }
      return result;
    }
    if (typeInfo instanceof StructTypeInfo) {
      List<TypeInfo> fieldTypes = ((StructTypeInfo)typeInfo).getFieldTypeInfos();
      List<Object> fieldValues = ((SimpleStruct) arg0).getFieldValues();
      assert fieldTypes.size() == fieldValues.size();
      List<Writable> writables = new ArrayList<>();
      for (int i = 0; i < fieldValues.size(); i++) {
        Object val = fieldValues.get(i);
        TypeInfo type = fieldTypes.get(i);
        writables.add(convert(val, type));
      }
      return new StructWritable(writables);
    }

    throw new IllegalArgumentException("unsupported data type:" + arg0.getClass().getName());
  }