use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class DecimalSliceStreamReader method getSlice.
/**
* Function to getSlice from Decimal Object
* @param value
* @param type
* @return
*/
private Slice getSlice(Object value, Type type) {
if (type instanceof DecimalType) {
DecimalType actual = (DecimalType) type;
BigDecimal bigDecimalValue = (BigDecimal) value;
if (isShortDecimal(type)) {
return utf8Slice(value.toString());
} else {
if (bigDecimalValue.scale() > actual.getScale()) {
BigInteger unscaledDecimal = rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(), bigDecimalValue.scale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
} else {
BigInteger unscaledDecimal = rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(), actual.getScale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
}
}
} else {
return utf8Slice(value.toString());
}
}
use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.
the class CarbondataRecordCursor method getSlice.
@Override
public Slice getSlice(int field) {
Type decimalType = getType(field);
if (decimalType instanceof DecimalType) {
DecimalType actual = (DecimalType) decimalType;
CarbondataColumnHandle carbondataColumnHandle = columnHandles.get(field);
if (carbondataColumnHandle.getPrecision() > 0) {
checkFieldType(field, DecimalType.createDecimalType(carbondataColumnHandle.getPrecision(), carbondataColumnHandle.getScale()));
} else {
checkFieldType(field, DecimalType.createDecimalType());
}
Object fieldValue = getFieldValue(field);
BigDecimal bigDecimalValue = new BigDecimal(fieldValue.toString());
if (isShortDecimal(decimalType)) {
return utf8Slice(Decimals.toString(bigDecimalValue.longValue(), actual.getScale()));
} else {
if (bigDecimalValue.scale() > actual.getScale()) {
BigInteger unscaledDecimal = rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(), bigDecimalValue.scale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
// return decimalSlice;
} else {
BigInteger unscaledDecimal = rescale(bigDecimalValue.unscaledValue(), bigDecimalValue.scale(), actual.getScale());
Slice decimalSlice = Decimals.encodeUnscaledValue(unscaledDecimal);
return utf8Slice(Decimals.toString(decimalSlice, actual.getScale()));
// return decimalSlice;
}
}
} else {
checkFieldType(field, VARCHAR);
return utf8Slice(getFieldValue(field).toString());
}
}
Aggregations