Search in sources :

Example 96 with BlockBuilder

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

the class Re2JRegexp method split.

public Block split(Slice source) {
    Matcher matcher = re2jPattern.matcher(source);
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 32);
    int lastEnd = 0;
    while (matcher.find()) {
        Slice slice = source.slice(lastEnd, matcher.start() - lastEnd);
        lastEnd = matcher.end();
        VARCHAR.writeSlice(blockBuilder, slice);
    }
    VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
    return blockBuilder.build();
}
Also used : Matcher(com.google.re2j.Matcher) Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 97 with BlockBuilder

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

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

the class TypeUtils method getHashBlock.

public static Block getHashBlock(List<? extends Type> hashTypes, Block... hashBlocks) {
    checkArgument(hashTypes.size() == hashBlocks.length);
    int[] hashChannels = new int[hashBlocks.length];
    for (int i = 0; i < hashBlocks.length; i++) {
        hashChannels[i] = i;
    }
    HashGenerator hashGenerator = new InterpretedHashGenerator(ImmutableList.copyOf(hashTypes), hashChannels);
    int positionCount = hashBlocks[0].getPositionCount();
    BlockBuilder builder = BIGINT.createFixedSizeBlockBuilder(positionCount);
    Page page = new Page(hashBlocks);
    for (int i = 0; i < positionCount; i++) {
        BIGINT.writeLong(builder, hashGenerator.hashPosition(i, page));
    }
    return builder.build();
}
Also used : InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator) Page(com.facebook.presto.spi.Page) HashGenerator(com.facebook.presto.operator.HashGenerator) InterpretedHashGenerator(com.facebook.presto.operator.InterpretedHashGenerator) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 99 with BlockBuilder

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

the class MaterializedResult method appendToPage.

private static void appendToPage(PageBuilder pageBuilder, MaterializedRow row) {
    for (int field = 0; field < row.getFieldCount(); field++) {
        Type type = pageBuilder.getType(field);
        Object value = row.getField(field);
        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(field);
        writeValue(type, blockBuilder, value);
    }
    pageBuilder.declarePosition();
}
Also used : RowType(com.facebook.presto.type.RowType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) CharType(com.facebook.presto.spi.type.CharType) VarcharType(com.facebook.presto.spi.type.VarcharType) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder)

Example 100 with BlockBuilder

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

the class RowPageBuilder method append.

private void append(int channel, Object element) {
    BlockBuilder blockBuilder = builders.get(channel);
    Type type = types.get(channel);
    appendToBlockBuilder(type, element, blockBuilder);
}
Also used : Type(com.facebook.presto.spi.type.Type) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)

Aggregations

BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)290 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)211 Block (com.facebook.presto.spi.block.Block)56 Slice (io.airlift.slice.Slice)53 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)39 Test (org.testng.annotations.Test)38 Type (com.facebook.presto.spi.type.Type)33 SqlType (com.facebook.presto.spi.function.SqlType)24 ArrayType (com.facebook.presto.type.ArrayType)19 Page (com.facebook.presto.spi.Page)18 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)17 TypeParameter (com.facebook.presto.spi.function.TypeParameter)15 MapType (com.facebook.presto.type.MapType)14 RowType (com.facebook.presto.type.RowType)14 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)13 PageBuilder (com.facebook.presto.spi.PageBuilder)12 ImmutableList (com.google.common.collect.ImmutableList)12 PrestoException (com.facebook.presto.spi.PrestoException)11 RunLengthEncodedBlock (com.facebook.presto.spi.block.RunLengthEncodedBlock)11 DictionaryBlock (com.facebook.presto.spi.block.DictionaryBlock)10