Search in sources :

Example 11 with DecimalType

use of com.facebook.presto.spi.type.DecimalType in project presto by prestodb.

the class ParquetShortDecimalColumnReader method readValue.

@Override
protected void readValue(BlockBuilder blockBuilder, Type type) {
    if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) {
        long decimalValue;
        if (columnDescriptor.getType().equals(INT32)) {
            HiveDecimalWritable hiveDecimalWritable = new HiveDecimalWritable(HiveDecimal.create(valuesReader.readInteger()));
            decimalValue = getShortDecimalValue(hiveDecimalWritable, ((DecimalType) type).getScale());
        } else if (columnDescriptor.getType().equals(INT64)) {
            HiveDecimalWritable hiveDecimalWritable = new HiveDecimalWritable(HiveDecimal.create(valuesReader.readLong()));
            decimalValue = getShortDecimalValue(hiveDecimalWritable, ((DecimalType) type).getScale());
        } else {
            decimalValue = getShortDecimalValue(valuesReader.readBytes().getBytes());
        }
        type.writeLong(blockBuilder, decimalValue);
    } else {
        blockBuilder.appendNull();
    }
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) DecimalType(com.facebook.presto.spi.type.DecimalType)

Example 12 with DecimalType

use of com.facebook.presto.spi.type.DecimalType in project presto by prestodb.

the class HiveTypeTranslator method translate.

@Override
public TypeInfo translate(Type type) {
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharLength);
        } else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return HIVE_STRING.getTypeInfo();
        } else {
            throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
        }
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            fieldNames.add(namedTypeSignature.getName());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType)

Example 13 with DecimalType

use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.

the class PrestoFilterUtil method ConvertDataByType.

private static Object ConvertDataByType(Object rawdata, Type type) {
    if (type.equals(IntegerType.INTEGER) || type.equals(SmallintType.SMALLINT))
        return Integer.valueOf(rawdata.toString());
    else // new Integer((rawdata.toString()));
    if (type.equals(BigintType.BIGINT))
        return rawdata;
    else if (type.equals(VarcharType.VARCHAR)) {
        if (rawdata instanceof Slice) {
            return ((Slice) rawdata).toStringUtf8();
        } else {
            return rawdata;
        }
    } else if (type.equals(BooleanType.BOOLEAN))
        return rawdata;
    else if (type.equals(DateType.DATE)) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(0));
        c.add(Calendar.DAY_OF_YEAR, ((Long) rawdata).intValue());
        Date date = c.getTime();
        return date.getTime() * 1000;
    } else if (type instanceof DecimalType) {
        if (rawdata instanceof Double) {
            return new BigDecimal((Double) rawdata);
        } else if (rawdata instanceof Long) {
            return new BigDecimal(new BigInteger(String.valueOf(rawdata)), ((DecimalType) type).getScale());
        } else if (rawdata instanceof Slice) {
            return new BigDecimal(Decimals.decodeUnscaledValue((Slice) rawdata), ((DecimalType) type).getScale());
        }
    } else if (type.equals(TimestampType.TIMESTAMP)) {
        return (Long) rawdata * 1000;
    }
    return rawdata;
}
Also used : Slice(io.airlift.slice.Slice) Calendar(java.util.Calendar) DecimalType(com.facebook.presto.spi.type.DecimalType) BigInteger(java.math.BigInteger) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Example 14 with DecimalType

use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.

the class DecimalSliceStreamReader method populateShortDecimalVector.

private void populateShortDecimalVector(Type type, int numberOfRows, BlockBuilder builder) {
    DecimalType decimalType = (DecimalType) type;
    if (isDictionary) {
        for (int i = 0; i < numberOfRows; i++) {
            int value = (int) columnVector.getData(i);
            Object data = DataTypeUtil.getDataBasedOnDataType(dictionary.getDictionaryValueForKey(value), DataTypes.createDecimalType(decimalType.getPrecision(), decimalType.getScale()));
            if (Objects.isNull(data)) {
                builder.appendNull();
            } else {
                BigDecimal decimalValue = (BigDecimal) data;
                long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), decimalType.getScale());
                type.writeLong(builder, rescaledDecimal);
            }
        }
    } else {
        for (int i = 0; i < numberOfRows; i++) {
            BigDecimal decimalValue = (BigDecimal) columnVector.getData(i);
            long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), decimalType.getScale());
            type.writeLong(builder, rescaledDecimal);
        }
    }
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) BigDecimal(java.math.BigDecimal)

Example 15 with DecimalType

use of com.facebook.presto.spi.type.DecimalType in project carbondata by apache.

the class DecimalSliceStreamReader method populateLongDecimalVector.

private void populateLongDecimalVector(Type type, int numberOfRows, BlockBuilder builder) {
    if (isDictionary) {
        for (int i = 0; i < numberOfRows; i++) {
            int value = (int) columnVector.getData(i);
            DecimalType decimalType = (DecimalType) type;
            Object data = DataTypeUtil.getDataBasedOnDataType(dictionary.getDictionaryValueForKey(value), DataTypes.createDecimalType(decimalType.getPrecision(), decimalType.getScale()));
            if (Objects.isNull(data)) {
                builder.appendNull();
            } else {
                BigDecimal decimalValue = (BigDecimal) data;
                Slice slice = getSlice(decimalValue, type);
                type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
            }
        }
    } else {
        for (int i = 0; i < numberOfRows; i++) {
            Slice slice = getSlice((columnVector.getData(i)), type);
            type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
        }
    }
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(com.facebook.presto.spi.type.DecimalType) 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