Search in sources :

Example 26 with DecimalType

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

the class MongoPageSink method getObjectValue.

private Object getObjectValue(Type type, Block block, int position) {
    if (block.isNull(position)) {
        if (type.equals(OBJECT_ID)) {
            return new ObjectId();
        }
        return null;
    }
    if (type.equals(OBJECT_ID)) {
        return new ObjectId(block.getSlice(position, 0, block.getSliceLength(position)).getBytes());
    }
    if (type.equals(BooleanType.BOOLEAN)) {
        return type.getBoolean(block, position);
    }
    if (type.equals(BigintType.BIGINT)) {
        return type.getLong(block, position);
    }
    if (type.equals(IntegerType.INTEGER)) {
        return (int) type.getLong(block, position);
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return (short) type.getLong(block, position);
    }
    if (type.equals(TinyintType.TINYINT)) {
        return (byte) type.getLong(block, position);
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return type.getDouble(block, position);
    }
    if (isVarcharType(type)) {
        return type.getSlice(block, position).toStringUtf8();
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return new Binary(type.getSlice(block, position).getBytes());
    }
    if (type.equals(DateType.DATE)) {
        long days = type.getLong(block, position);
        return new Date(TimeUnit.DAYS.toMillis(days));
    }
    if (type.equals(TimeType.TIME)) {
        long millisUtc = type.getLong(block, position);
        return new Date(millisUtc);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        long millisUtc = type.getLong(block, position);
        return new Date(millisUtc);
    }
    if (type.equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
        long millisUtc = unpackMillisUtc(type.getLong(block, position));
        return new Date(millisUtc);
    }
    if (type instanceof DecimalType) {
        // TODO: decimal type might not support yet
        // TODO: this code is likely wrong and should switch to Decimals.readBigDecimal()
        DecimalType decimalType = (DecimalType) type;
        BigInteger unscaledValue;
        if (decimalType.isShort()) {
            unscaledValue = BigInteger.valueOf(decimalType.getLong(block, position));
        } else {
            unscaledValue = Decimals.decodeUnscaledValue(decimalType.getSlice(block, position));
        }
        return new BigDecimal(unscaledValue);
    }
    if (isArrayType(type)) {
        Type elementType = type.getTypeParameters().get(0);
        Block arrayBlock = block.getBlock(position);
        List<Object> list = new ArrayList<>(arrayBlock.getPositionCount());
        for (int i = 0; i < arrayBlock.getPositionCount(); i++) {
            Object element = getObjectValue(elementType, arrayBlock, i);
            list.add(element);
        }
        return unmodifiableList(list);
    }
    if (isMapType(type)) {
        Type keyType = type.getTypeParameters().get(0);
        Type valueType = type.getTypeParameters().get(1);
        Block mapBlock = block.getBlock(position);
        // map type is converted into list of fixed keys document
        List<Object> values = new ArrayList<>(mapBlock.getPositionCount() / 2);
        for (int i = 0; i < mapBlock.getPositionCount(); i += 2) {
            Map<String, Object> mapValue = new HashMap<>();
            mapValue.put("key", getObjectValue(keyType, mapBlock, i));
            mapValue.put("value", getObjectValue(valueType, mapBlock, i + 1));
            values.add(mapValue);
        }
        return unmodifiableList(values);
    }
    if (isRowType(type)) {
        Block rowBlock = block.getBlock(position);
        List<Type> fieldTypes = type.getTypeParameters();
        if (fieldTypes.size() != rowBlock.getPositionCount()) {
            throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, "Expected row value field count does not match type field count");
        }
        if (isImplicitRowType(type)) {
            List<Object> rowValue = new ArrayList<>();
            for (int i = 0; i < rowBlock.getPositionCount(); i++) {
                Object element = getObjectValue(fieldTypes.get(i), rowBlock, i);
                rowValue.add(element);
            }
            return unmodifiableList(rowValue);
        }
        Map<String, Object> rowValue = new HashMap<>();
        for (int i = 0; i < rowBlock.getPositionCount(); i++) {
            rowValue.put(type.getTypeSignature().getParameters().get(i).getNamedTypeSignature().getName().orElse("field" + i), getObjectValue(fieldTypes.get(i), rowBlock, i));
        }
        return unmodifiableMap(rowValue);
    }
    throw new PrestoException(NOT_SUPPORTED, "unsupported type: " + type);
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PrestoException(com.facebook.presto.spi.PrestoException) Date(java.util.Date) BigDecimal(java.math.BigDecimal) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) TypeUtils.isArrayType(com.facebook.presto.mongodb.TypeUtils.isArrayType) TinyintType(com.facebook.presto.common.type.TinyintType) TypeUtils.isRowType(com.facebook.presto.mongodb.TypeUtils.isRowType) BigintType(com.facebook.presto.common.type.BigintType) TimestampWithTimeZoneType(com.facebook.presto.common.type.TimestampWithTimeZoneType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) TypeUtils.isMapType(com.facebook.presto.mongodb.TypeUtils.isMapType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DecimalType(com.facebook.presto.common.type.DecimalType) BigInteger(java.math.BigInteger) Block(com.facebook.presto.common.block.Block) Binary(org.bson.types.Binary)

Example 27 with DecimalType

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

the class ParquetTestUtils method getHiveType.

private static String getHiveType(Type type) {
    if (type.equals(BOOLEAN) || type.equals(BIGINT) || type.equals(SmallintType.SMALLINT) || type.equals(TinyintType.TINYINT) || type.equals(DOUBLE)) {
        return type.getTypeSignature().toString();
    }
    if (type.equals(INTEGER)) {
        return "int";
    }
    if (type.equals(REAL)) {
        return "float";
    }
    if (type.equals(TIMESTAMP)) {
        return "timestamp";
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return "varchar(" + varcharLength + ")";
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return format("decimal(%d,%d)", decimalType.getPrecision(), decimalType.getScale());
    }
    if (type instanceof ArrayType) {
        return "array<" + getHiveType(((ArrayType) type).getElementType()) + ">";
    }
    if (type instanceof RowType) {
        return "struct<" + Joiner.on(",").join(((RowType) type).getFields().stream().map(t -> t.getName().get() + ":" + getHiveType(t.getType())).collect(toList())) + ">";
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 28 with DecimalType

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

the class ParquetTestUtils method getObjectInspector.

private static ObjectInspector getObjectInspector(Type type) {
    if (type.equals(BOOLEAN)) {
        return writableBooleanObjectInspector;
    }
    if (type.equals(BIGINT)) {
        return writableLongObjectInspector;
    }
    if (type.equals(INTEGER)) {
        return writableIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return writableShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return writableByteObjectInspector;
    }
    if (type.equals(DOUBLE)) {
        return writableDoubleObjectInspector;
    }
    if (type.equals(REAL)) {
        return writableFloatObjectInspector;
    }
    if (type.equals(TIMESTAMP)) {
        return writableTimestampObjectInspector;
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        return getPrimitiveWritableObjectInspector(getVarcharTypeInfo(varcharLength));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveWritableObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (type instanceof ArrayType || type instanceof RowType) {
        return getJavaObjectInspector(type);
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) RowType(com.facebook.presto.common.type.RowType)

Example 29 with DecimalType

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

the class SerDeUtils method serializePrimitive.

private static void serializePrimitive(Type type, BlockBuilder builder, Object object, PrimitiveObjectInspector inspector) {
    requireNonNull(builder, "parent builder is null");
    if (object == null) {
        builder.appendNull();
        return;
    }
    switch(inspector.getPrimitiveCategory()) {
        case BOOLEAN:
            BooleanType.BOOLEAN.writeBoolean(builder, ((BooleanObjectInspector) inspector).get(object));
            return;
        case BYTE:
            TinyintType.TINYINT.writeLong(builder, ((ByteObjectInspector) inspector).get(object));
            return;
        case SHORT:
            SmallintType.SMALLINT.writeLong(builder, ((ShortObjectInspector) inspector).get(object));
            return;
        case INT:
            IntegerType.INTEGER.writeLong(builder, ((IntObjectInspector) inspector).get(object));
            return;
        case LONG:
            BigintType.BIGINT.writeLong(builder, ((LongObjectInspector) inspector).get(object));
            return;
        case FLOAT:
            RealType.REAL.writeLong(builder, floatToRawIntBits(((FloatObjectInspector) inspector).get(object)));
            return;
        case DOUBLE:
            DoubleType.DOUBLE.writeDouble(builder, ((DoubleObjectInspector) inspector).get(object));
            return;
        case STRING:
            type.writeSlice(builder, Slices.utf8Slice(((StringObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case VARCHAR:
            type.writeSlice(builder, Slices.utf8Slice(((HiveVarcharObjectInspector) inspector).getPrimitiveJavaObject(object).getValue()));
            return;
        case CHAR:
            CharType charType = (CharType) type;
            HiveChar hiveChar = ((HiveCharObjectInspector) inspector).getPrimitiveJavaObject(object);
            type.writeSlice(builder, truncateToLengthAndTrimSpaces(Slices.utf8Slice(hiveChar.getValue()), charType.getLength()));
            return;
        case DATE:
            DateType.DATE.writeLong(builder, formatDateAsLong(object, (DateObjectInspector) inspector));
            return;
        case TIMESTAMP:
            TimestampType.TIMESTAMP.writeLong(builder, formatTimestampAsLong(object, (TimestampObjectInspector) inspector));
            return;
        case BINARY:
            VARBINARY.writeSlice(builder, Slices.wrappedBuffer(((BinaryObjectInspector) inspector).getPrimitiveJavaObject(object)));
            return;
        case DECIMAL:
            DecimalType decimalType = (DecimalType) type;
            HiveDecimalWritable hiveDecimal = ((HiveDecimalObjectInspector) inspector).getPrimitiveWritableObject(object);
            if (decimalType.isShort()) {
                decimalType.writeLong(builder, DecimalUtils.getShortDecimalValue(hiveDecimal, decimalType.getScale()));
            } else {
                decimalType.writeSlice(builder, DecimalUtils.getLongDecimalValue(hiveDecimal, decimalType.getScale()));
            }
            return;
    }
    throw new RuntimeException("Unknown primitive type: " + inspector.getPrimitiveCategory());
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) DecimalType(com.facebook.presto.common.type.DecimalType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) CharType(com.facebook.presto.common.type.CharType) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector)

Example 30 with DecimalType

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

the class AggregatedParquetPageSource method writeMinMax.

private void writeMinMax(ParquetMetadata parquetMetadata, int columnIndex, BlockBuilder blockBuilder, Type type, HiveType hiveType, boolean isMin) {
    org.apache.parquet.schema.Type parquetType = parquetMetadata.getFileMetaData().getSchema().getType(columnIndex);
    if (parquetType instanceof GroupType) {
        throw new IllegalArgumentException("Unsupported type : " + parquetType.toString());
    }
    Object value = null;
    for (BlockMetaData blockMetaData : parquetMetadata.getBlocks()) {
        Statistics statistics = blockMetaData.getColumns().get(columnIndex).getStatistics();
        if (!statistics.hasNonNullValue()) {
            throw new UnsupportedOperationException("No min/max found for parquet file. Set session property hive.pushdown_partial_aggregations_into_scan=false and execute query again");
        }
        if (isMin) {
            Object currentValue = statistics.genericGetMin();
            if (currentValue != null && (value == null || ((Comparable) currentValue).compareTo(value) < 0)) {
                value = currentValue;
            }
        } else {
            Object currentValue = statistics.genericGetMax();
            if (currentValue != null && (value == null || ((Comparable) currentValue).compareTo(value) > 0)) {
                value = currentValue;
            }
        }
    }
    if (type instanceof FixedWidthType) {
        completedBytes += ((FixedWidthType) type).getFixedSize();
    }
    if (value == null) {
        blockBuilder.appendNull();
        return;
    }
    PrimitiveType.PrimitiveTypeName parquetTypeName = parquetType.asPrimitiveType().getPrimitiveTypeName();
    switch(parquetTypeName) {
        case INT32:
            {
                blockBuilder.writeLong(Long.valueOf((Integer) value));
                break;
            }
        case INT64:
            {
                blockBuilder.writeLong((Long) value);
                break;
            }
        case INT96:
            {
                blockBuilder.writeLong(getTimestampMillis(((Binary) value).getBytes(), 0));
                break;
            }
        case FLOAT:
            {
                blockBuilder.writeLong(floatToRawIntBits((Float) value));
                break;
            }
        case DOUBLE:
            {
                type.writeDouble(blockBuilder, (Double) value);
                break;
            }
        case FIXED_LEN_BYTE_ARRAY:
            {
                byte[] valBytes = ((Binary) value).getBytes();
                DecimalType decimalType = (DecimalType) hiveType.getType(typeManager);
                if (decimalType.isShort()) {
                    blockBuilder.writeLong(getShortDecimalValue(valBytes));
                } else {
                    BigInteger bigIntValue = new BigInteger(valBytes);
                    type.writeSlice(blockBuilder, encodeUnscaledValue(bigIntValue));
                }
                break;
            }
        case BINARY:
            {
                Slice slice = Slices.wrappedBuffer(((Binary) value).getBytes());
                blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
                completedBytes += slice.length();
                break;
            }
        case BOOLEAN:
        default:
            throw new IllegalArgumentException("Unexpected parquet type name: " + parquetTypeName);
    }
}
Also used : BlockMetaData(org.apache.parquet.hadoop.metadata.BlockMetaData) Statistics(org.apache.parquet.column.statistics.Statistics) GroupType(org.apache.parquet.schema.GroupType) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) BigInteger(java.math.BigInteger) PrimitiveType(org.apache.parquet.schema.PrimitiveType) Binary(org.apache.parquet.io.api.Binary) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Aggregations

DecimalType (com.facebook.presto.common.type.DecimalType)55 VarcharType (com.facebook.presto.common.type.VarcharType)28 Type (com.facebook.presto.common.type.Type)23 CharType (com.facebook.presto.common.type.CharType)20 Block (com.facebook.presto.common.block.Block)15 ArrayType (com.facebook.presto.common.type.ArrayType)15 RowType (com.facebook.presto.common.type.RowType)14 Slice (io.airlift.slice.Slice)14 ArrayList (java.util.ArrayList)13 PrestoException (com.facebook.presto.spi.PrestoException)12 TimestampType (com.facebook.presto.common.type.TimestampType)11 BigDecimal (java.math.BigDecimal)11 MapType (com.facebook.presto.common.type.MapType)10 Chars.isCharType (com.facebook.presto.common.type.Chars.isCharType)9 DateType (com.facebook.presto.common.type.DateType)9 IntegerType (com.facebook.presto.common.type.IntegerType)9 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)9 ImmutableList (com.google.common.collect.ImmutableList)9 BigintType (com.facebook.presto.common.type.BigintType)8 BooleanType (com.facebook.presto.common.type.BooleanType)8