use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.
the class RowEncoderV2 method encodeBit.
private void encodeBit(CodecDataOutput cdo, Object value) {
long s = 0;
if (value instanceof Long) {
s = (long) value;
} else if (value instanceof byte[]) {
for (byte b : (byte[]) value) {
s <<= 8;
s |= b;
}
} else {
throw new CodecException("invalid bytes type " + value.getClass());
}
encodeInt(cdo, s);
}
use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.
the class RowEncoderV2 method encodeTimestamp.
private void encodeTimestamp(CodecDataOutput cdo, Object value, DateTimeZone tz) {
if (value instanceof Timestamp) {
ExtendedDateTime extendedDateTime = Converter.convertToDateTime(value);
long t = DateTimeCodec.toPackedLong(extendedDateTime, tz);
encodeInt(cdo, t);
} else if (value instanceof Date) {
ExtendedDateTime extendedDateTime = Converter.convertToDateTime(value);
long t = DateTimeCodec.toPackedLong(extendedDateTime, tz);
encodeInt(cdo, t);
} else {
throw new CodecException("invalid timestamp type " + value.getClass());
}
}
use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.
the class RowEncoderV2 method encodeValue.
private void encodeValue(CodecDataOutput cdo, Object value, DataType tp) {
switch(tp.getType()) {
case TypeLonglong:
case TypeLong:
case TypeInt24:
case TypeShort:
case TypeTiny:
// TODO: encode consider unsigned
encodeInt(cdo, (long) value);
break;
case TypeFloat:
case TypeDouble:
if (value instanceof Double) {
encodeDouble(cdo, value);
} else if (value instanceof Float) {
encodeFloat(cdo, value);
} else {
throw new TypeException("type does not match in encoding, should be float/double");
}
break;
case TypeString:
case TypeVarString:
case TypeVarchar:
case TypeBlob:
case TypeTinyBlob:
case TypeMediumBlob:
case TypeLongBlob:
encodeString(cdo, value);
break;
case TypeNewDecimal:
DecimalType decimalType = (DecimalType) tp;
encodeDecimal(cdo, value, (int) decimalType.getLength(), decimalType.getDecimal());
break;
case TypeBit:
encodeBit(cdo, value);
break;
case TypeTimestamp:
encodeTimestamp(cdo, value, DateTimeZone.UTC);
break;
case TypeDate:
case TypeDatetime:
encodeTimestamp(cdo, value, Converter.getLocalTimezone());
break;
case TypeDuration:
case TypeYear:
encodeInt(cdo, (long) value);
break;
case TypeEnum:
encodeEnum(cdo, value, tp.getElems());
break;
case TypeSet:
encodeSet(cdo, value, tp.getElems());
break;
case TypeJSON:
encodeJson(cdo, value);
break;
case TypeNull:
// ??
case TypeDecimal:
case TypeGeometry:
case TypeNewDate:
throw new CodecException("type should not appear in encoding");
default:
throw new CodecException("invalid data type: " + tp.getType().name());
}
}
use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.
the class Codec method decodeOne.
public static Object decodeOne(byte[] colData) {
if (colData.length <= 1) {
throw new CodecException("invalid encoded column data, length <=1");
}
int flag = colData[0];
DataType tp;
switch(flag) {
case INT_FLAG:
case UINT_FLAG:
case VARINT_FLAG:
case UVARINT_FLAG:
tp = IntegerType.BIGINT;
break;
case FLOATING_FLAG:
tp = RealType.DOUBLE;
break;
case BYTES_FLAG:
case COMPACT_BYTES_FLAG:
tp = BytesType.TEXT;
break;
case DECIMAL_FLAG:
tp = DecimalType.DECIMAL;
break;
case DURATION_FLAG:
tp = TimeType.TIME;
break;
case JSON_FLAG:
tp = JsonType.JSON;
break;
default:
throw new CodecException("Unknown type");
}
return tp.decode(new CodecDataInput(colData));
}
use of com.pingcap.tikv.exception.CodecException in project tispark by pingcap.
the class RowEncoderV2 method encodeDecimal.
private void encodeDecimal(CodecDataOutput cdo, Object value, int precision, int frac) {
if (value instanceof MyDecimal) {
MyDecimal dec = (MyDecimal) value;
DecimalCodec.writeDecimal(cdo, dec, precision, frac);
} else if (value instanceof BigDecimal) {
MyDecimal dec = new MyDecimal();
dec.fromString(((BigDecimal) value).toPlainString());
DecimalCodec.writeDecimal(cdo, dec, precision, frac);
} else {
throw new CodecException("invalid decimal type " + value.getClass());
}
}
Aggregations