Search in sources :

Example 16 with DecimalType

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());
    }
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(com.facebook.presto.spi.type.DecimalType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 17 with DecimalType

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());
    }
}
Also used : DataType(org.apache.carbondata.core.metadata.datatype.DataType) DecimalType(com.facebook.presto.spi.type.DecimalType) Type(com.facebook.presto.spi.type.Type) TimestampType(com.facebook.presto.spi.type.TimestampType) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(com.facebook.presto.spi.type.DecimalType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Aggregations

DecimalType (com.facebook.presto.spi.type.DecimalType)17 Slice (io.airlift.slice.Slice)7 BigDecimal (java.math.BigDecimal)7 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)5 Type (com.facebook.presto.spi.type.Type)4 BigInteger (java.math.BigInteger)4 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)3 CharType (com.facebook.presto.spi.type.CharType)3 TimestampType (com.facebook.presto.spi.type.TimestampType)3 VarcharType (com.facebook.presto.spi.type.VarcharType)3 PrestoException (com.facebook.presto.spi.PrestoException)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 DataType (org.apache.carbondata.core.metadata.datatype.DataType)2 HiveUtil.isArrayType (com.facebook.presto.hive.HiveUtil.isArrayType)1 HiveUtil.isMapType (com.facebook.presto.hive.HiveUtil.isMapType)1 HiveUtil.isRowType (com.facebook.presto.hive.HiveUtil.isRowType)1 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)1 Block (com.facebook.presto.spi.block.Block)1