Search in sources :

Example 1 with DecimalType

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

the class DecimalStreamReader method readBlock.

@Override
public Block readBlock(Type type) throws IOException {
    DecimalType decimalType = (DecimalType) type;
    if (!rowGroupOpen) {
        openRowGroup();
    }
    seekToOffset();
    allocateVectors();
    BlockBuilder builder = decimalType.createBlockBuilder(new BlockBuilderStatus(), nextBatchSize);
    if (presentStream == null) {
        if (decimalStream == null) {
            throw new OrcCorruptionException("Value is not null but decimal stream is not present");
        }
        if (scaleStream == null) {
            throw new OrcCorruptionException("Value is not null but scale stream is not present");
        }
        Arrays.fill(nullVector, false);
        scaleStream.nextLongVector(nextBatchSize, scaleVector);
        if (decimalType.isShort()) {
            decimalStream.nextShortDecimalVector(nextBatchSize, builder, decimalType, scaleVector);
        } else {
            decimalStream.nextLongDecimalVector(nextBatchSize, builder, decimalType, scaleVector);
        }
    } else {
        int nullValues = presentStream.getUnsetBits(nextBatchSize, nullVector);
        if (nullValues != nextBatchSize) {
            if (decimalStream == null) {
                throw new OrcCorruptionException("Value is not null but decimal stream is not present");
            }
            if (scaleStream == null) {
                throw new OrcCorruptionException("Value is not null but scale stream is not present");
            }
            scaleStream.nextLongVector(nextBatchSize, scaleVector, nullVector);
            if (decimalType.isShort()) {
                decimalStream.nextShortDecimalVector(nextBatchSize, builder, decimalType, scaleVector, nullVector);
            } else {
                decimalStream.nextLongDecimalVector(nextBatchSize, builder, decimalType, scaleVector, nullVector);
            }
        } else {
            for (int i = 0; i < nextBatchSize; i++) {
                builder.appendNull();
            }
        }
    }
    readOffset = 0;
    nextBatchSize = 0;
    return builder.build();
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) OrcCorruptionException(com.facebook.presto.orc.OrcCorruptionException) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 2 with DecimalType

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

the class TypeJsonUtils method stackRepresentationToObjectHelper.

private static Object stackRepresentationToObjectHelper(ConnectorSession session, JsonParser parser, Type type) throws IOException {
    // cast('[null]', array(json)) should be casted to a single item array containing a json document "null" instead of sql null.
    if (type instanceof JsonType) {
        return OBJECT_MAPPER.writeValueAsString(parser.readValueAsTree());
    }
    if (parser.getCurrentToken() == JsonToken.VALUE_NULL) {
        return null;
    }
    if (type instanceof ArrayType) {
        List<Object> list = new ArrayList<>();
        checkState(parser.getCurrentToken() == JsonToken.START_ARRAY, "Expected a json array");
        while (parser.nextToken() != JsonToken.END_ARRAY) {
            list.add(stackRepresentationToObjectHelper(session, parser, ((ArrayType) type).getElementType()));
        }
        return Collections.unmodifiableList(list);
    }
    if (type instanceof MapType) {
        Map<Object, Object> map = new LinkedHashMap<>();
        checkState(parser.getCurrentToken() == JsonToken.START_OBJECT, "Expected a json object");
        while (parser.nextValue() != JsonToken.END_OBJECT) {
            Object key = mapKeyToObject(session, parser.getCurrentName(), ((MapType) type).getKeyType());
            Object value = stackRepresentationToObjectHelper(session, parser, ((MapType) type).getValueType());
            map.put(key, value);
        }
        return Collections.unmodifiableMap(map);
    }
    if (type instanceof RowType) {
        List<Object> list = new ArrayList<>();
        checkState(parser.getCurrentToken() == JsonToken.START_ARRAY, "Expected a json array");
        int field = 0;
        RowType rowType = (RowType) type;
        while (parser.nextValue() != JsonToken.END_ARRAY) {
            checkArgument(field < rowType.getFields().size(), "Unexpected field for type %s", type);
            Object value = stackRepresentationToObjectHelper(session, parser, rowType.getFields().get(field).getType());
            list.add(value);
            field++;
        }
        checkArgument(field == rowType.getFields().size(), "Expected %s fields for type %s", rowType.getFields().size(), type);
        return Collections.unmodifiableList(list);
    }
    Slice sliceValue = null;
    if (type.getJavaType() == Slice.class) {
        sliceValue = Slices.utf8Slice(parser.getValueAsString());
    }
    BlockBuilder blockBuilder;
    if (type instanceof FixedWidthType) {
        blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
    } else {
        blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1, requireNonNull(sliceValue, "sliceValue is null").length());
    }
    if (type instanceof DecimalType) {
        return getSqlDecimal((DecimalType) type, parser.getDecimalValue());
    } else if (type.getJavaType() == boolean.class) {
        type.writeBoolean(blockBuilder, parser.getBooleanValue());
    } else if (type.getJavaType() == long.class) {
        type.writeLong(blockBuilder, parser.getLongValue());
    } else if (type.getJavaType() == double.class) {
        type.writeDouble(blockBuilder, getDoubleValue(parser));
    } else if (type.getJavaType() == Slice.class) {
        type.writeSlice(blockBuilder, requireNonNull(sliceValue, "sliceValue is null"));
    }
    return type.getObjectValue(session, blockBuilder.build(), 0);
}
Also used : ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.spi.type.DecimalType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) FixedWidthType(com.facebook.presto.spi.type.FixedWidthType) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 3 with DecimalType

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

the class CarbondataRecordCursor method getLong.

@Override
public long getLong(int field) {
    Object obj = getFieldValue(field);
    Long timeStr = 0L;
    if (obj instanceof Integer) {
        timeStr = ((Integer) obj).longValue();
    } else if (obj instanceof Long) {
        timeStr = (Long) obj;
    } else {
        timeStr = Math.round(Double.parseDouble(obj.toString()));
    }
    Type actual = getType(field);
    if (actual instanceof TimestampType) {
        return new Timestamp(timeStr).getTime() / 1000;
    } else if (isShortDecimal(actual)) {
        return shortDecimalPartitionKey(obj.toString(), (DecimalType) actual, columnHandles.get(field).getColumnName());
    }
    // suppose the
    return timeStr;
}
Also used : BigInteger(java.math.BigInteger) 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) TimestampType(com.facebook.presto.spi.type.TimestampType) DecimalType(com.facebook.presto.spi.type.DecimalType) Timestamp(java.sql.Timestamp)

Example 4 with DecimalType

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

the class DecimalSliceStreamReader method readBlock.

/**
 * Create Block for DecimalType
 * @param type
 * @return
 * @throws IOException
 */
public Block readBlock(Type type) throws IOException {
    int numberOfRows = 0;
    BlockBuilder builder = null;
    if (isVectorReader) {
        numberOfRows = batchSize;
        builder = type.createBlockBuilder(new BlockBuilderStatus(), numberOfRows);
        if (columnVector != null) {
            if (columnVector.anyNullsSet()) {
                handleNullInVector(type, numberOfRows, builder);
            } else {
                if (isShortDecimal(type)) {
                    populateShortDecimalVector(type, numberOfRows, builder);
                } else {
                    populateLongDecimalVector(type, numberOfRows, builder);
                }
            }
        }
    } else {
        if (streamData != null) {
            numberOfRows = streamData.length;
            builder = type.createBlockBuilder(new BlockBuilderStatus(), numberOfRows);
            for (int i = 0; i < numberOfRows; i++) {
                Slice slice = getSlice(streamData[i], type);
                if (isShortDecimal(type)) {
                    type.writeLong(builder, parseLong((DecimalType) type, slice, 0, slice.length()));
                } else {
                    type.writeSlice(builder, parseSlice((DecimalType) type, slice, 0, slice.length()));
                }
            }
        }
    }
    if (builder == null) {
        return null;
    }
    return builder.build();
}
Also used : Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) DecimalType(com.facebook.presto.spi.type.DecimalType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 5 with DecimalType

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

the class DecimalSliceStreamReader method handleNullInVector.

private void handleNullInVector(Type type, int numberOfRows, BlockBuilder builder) {
    for (int i = 0; i < numberOfRows; i++) {
        if (columnVector.isNullAt(i)) {
            builder.appendNull();
        } else {
            if (isShortDecimal(type)) {
                BigDecimal decimalValue = (BigDecimal) columnVector.getData(i);
                long rescaledDecimal = Decimals.rescale(decimalValue.unscaledValue().longValue(), decimalValue.scale(), ((DecimalType) type).getScale());
                type.writeLong(builder, rescaledDecimal);
            } else {
                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