Search in sources :

Example 6 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus 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 7 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BlockAssertions method createRLEBlock.

public static RunLengthEncodedBlock createRLEBlock(double value, int positionCount) {
    BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(new BlockBuilderStatus(), 1);
    DOUBLE.writeDouble(blockBuilder, value);
    return new RunLengthEncodedBlock(blockBuilder.build(), positionCount);
}
Also used : RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 8 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BlockAssertions method createShortDecimalsBlock.

public static Block createShortDecimalsBlock(Iterable<String> values) {
    DecimalType shortDecimalType = DecimalType.createDecimalType(1);
    BlockBuilder builder = shortDecimalType.createBlockBuilder(new BlockBuilderStatus(), 100);
    for (String value : values) {
        if (value == null) {
            builder.appendNull();
        } else {
            shortDecimalType.writeLong(builder, new BigDecimal(value).unscaledValue().longValue());
        }
    }
    return builder.build();
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Decimals.writeBigDecimal(com.facebook.presto.spi.type.Decimals.writeBigDecimal) BigDecimal(java.math.BigDecimal) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 9 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BlockAssertions method createStringArraysBlock.

public static Block createStringArraysBlock(Iterable<? extends Iterable<String>> values) {
    ArrayType arrayType = new ArrayType(VARCHAR);
    BlockBuilder builder = arrayType.createBlockBuilder(new BlockBuilderStatus(), 100);
    for (Iterable<String> value : values) {
        if (value == null) {
            builder.appendNull();
        } else {
            arrayType.writeObject(builder, createStringsBlock(value));
        }
    }
    return builder.build();
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 10 with BlockBuilderStatus

use of com.facebook.presto.spi.block.BlockBuilderStatus in project presto by prestodb.

the class BlockAssertions method createStringDictionaryBlock.

public static Block createStringDictionaryBlock(int start, int length) {
    checkArgument(length > 5, "block must have more than 5 entries");
    int dictionarySize = length / 5;
    BlockBuilder builder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), dictionarySize);
    for (int i = start; i < start + dictionarySize; i++) {
        VARCHAR.writeString(builder, String.valueOf(i));
    }
    int[] ids = new int[length];
    for (int i = 0; i < length; i++) {
        ids[i] = i % dictionarySize;
    }
    return new DictionaryBlock(length, builder.build(), ids);
}
Also used : DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Aggregations

BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)227 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)210 Block (com.facebook.presto.spi.block.Block)55 Slice (io.airlift.slice.Slice)43 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)37 Test (org.testng.annotations.Test)35 Type (com.facebook.presto.spi.type.Type)24 SqlType (com.facebook.presto.spi.function.SqlType)20 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)15 ArrayType (com.facebook.presto.type.ArrayType)14 Page (com.facebook.presto.spi.Page)12 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)12 TypeParameter (com.facebook.presto.spi.function.TypeParameter)11 RowType (com.facebook.presto.type.RowType)11 OrcCorruptionException (com.facebook.presto.orc.OrcCorruptionException)10 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)10 MapType (com.facebook.presto.type.MapType)10 PrestoException (com.facebook.presto.spi.PrestoException)9 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)8 DecimalType (com.facebook.presto.spi.type.DecimalType)8