Search in sources :

Example 6 with InterleavedBlockBuilder

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

the class TestStateCompiler method testComplexSerialization.

@Test
public void testComplexSerialization() {
    Type arrayType = new ArrayType(BIGINT);
    Type mapType = new MapType(BIGINT, VARCHAR);
    Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
    AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    TestComplexState singleState = factory.createSingleState();
    TestComplexState deserializedState = factory.createSingleState();
    singleState.setBoolean(true);
    singleState.setLong(1);
    singleState.setDouble(2.0);
    singleState.setByte((byte) 3);
    singleState.setSlice(utf8Slice("test"));
    singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
    singleState.setYetAnotherSlice(null);
    Block array = createLongsBlock(45);
    singleState.setBlock(array);
    BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
    BIGINT.writeLong(mapBlockBuilder, 123L);
    VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
    Block map = mapBlockBuilder.build();
    singleState.setAnotherBlock(map);
    BlockBuilder builder = new RowType(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 1);
    serializer.serialize(singleState, builder);
    Block block = builder.build();
    serializer.deserialize(block, 0, deserializedState);
    assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
    assertEquals(deserializedState.getLong(), singleState.getLong());
    assertEquals(deserializedState.getDouble(), singleState.getDouble());
    assertEquals(deserializedState.getByte(), singleState.getByte());
    assertEquals(deserializedState.getSlice(), singleState.getSlice());
    assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
    assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
    assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 7 with InterleavedBlockBuilder

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

the class TestStateCompiler method testComplexStateEstimatedSize.

@Test
public void testComplexStateEstimatedSize() {
    Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", new MapType(BIGINT, VARCHAR));
    AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    TestComplexState groupedState = factory.createGroupedState();
    assertEquals(groupedState.getEstimatedSize(), 76064);
    for (int i = 0; i < 1000; i++) {
        ((GroupedAccumulatorState) groupedState).setGroupId(i);
        groupedState.setBoolean(true);
        groupedState.setLong(1);
        groupedState.setDouble(2.0);
        groupedState.setByte((byte) 3);
        groupedState.setSlice(utf8Slice("test"));
        groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
        groupedState.setYetAnotherSlice(null);
        Block array = createLongsBlock(45);
        groupedState.setBlock(array);
        BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
        BIGINT.writeLong(mapBlockBuilder, 123L);
        VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
        Block map = mapBlockBuilder.build();
        groupedState.setAnotherBlock(map);
        assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * (i + 1));
    }
    for (int i = 0; i < 1000; i++) {
        ((GroupedAccumulatorState) groupedState).setGroupId(i);
        groupedState.setBoolean(true);
        groupedState.setLong(1);
        groupedState.setDouble(2.0);
        groupedState.setByte((byte) 3);
        groupedState.setSlice(utf8Slice("test"));
        groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
        groupedState.setYetAnotherSlice(null);
        Block array = createLongsBlock(45);
        groupedState.setBlock(array);
        BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
        BIGINT.writeLong(mapBlockBuilder, 123L);
        VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
        Block map = mapBlockBuilder.build();
        groupedState.setAnotherBlock(map);
        assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * 1000);
    }
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) GroupedAccumulatorState(com.facebook.presto.spi.function.GroupedAccumulatorState) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 8 with InterleavedBlockBuilder

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

the class AccumuloRowSerializer method getBlockFromMap.

/**
     * Encodes the given map into a Block.
     *
     * @param mapType Presto type of the map
     * @param map Map of key/value pairs to encode
     * @return Presto Block
     */
static Block getBlockFromMap(Type mapType, Map<?, ?> map) {
    Type keyType = mapType.getTypeParameters().get(0);
    Type valueType = mapType.getTypeParameters().get(1);
    BlockBuilder builder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), map.size() * 2);
    for (Entry<?, ?> entry : map.entrySet()) {
        writeObject(builder, keyType, entry.getKey());
        writeObject(builder, valueType, entry.getValue());
    }
    return builder.build();
}
Also used : Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 9 with InterleavedBlockBuilder

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

the class MapTransformKeyFunction method transform.

public static Block transform(Type keyType, Type transformedKeyType, Type valueType, ConnectorSession session, Block block, MethodHandle function) {
    int positionCount = block.getPositionCount();
    BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(transformedKeyType, valueType), new BlockBuilderStatus(), positionCount);
    TypedSet typedSet = new TypedSet(transformedKeyType, positionCount / 2);
    for (int position = 0; position < positionCount; position += 2) {
        Object key = readNativeValue(keyType, block, position);
        Object value = readNativeValue(valueType, block, position + 1);
        Object transformedKey;
        try {
            transformedKey = function.invoke(key, value);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (transformedKey == null) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "map key cannot be null");
        }
        writeNativeValue(transformedKeyType, resultBuilder, transformedKey);
        valueType.appendTo(block, position + 1, resultBuilder);
        if (typedSet.contains(resultBuilder, position)) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Duplicate keys (%s) are not allowed", transformedKeyType.getObjectValue(session, resultBuilder, position)));
        }
        typedSet.add(resultBuilder, position);
    }
    return resultBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 10 with InterleavedBlockBuilder

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

the class MapTransformValueFunction method transform.

public static Block transform(Type keyType, Type valueType, Type transformedValueType, Block block, MethodHandle function) {
    int positionCount = block.getPositionCount();
    BlockBuilder resultBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, transformedValueType), new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position += 2) {
        Object key = readNativeValue(keyType, block, position);
        Object value = readNativeValue(valueType, block, position + 1);
        Object transformedValue;
        try {
            transformedValue = function.invoke(key, value);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        keyType.appendTo(block, position, resultBuilder);
        writeNativeValue(transformedValueType, resultBuilder, transformedValue);
    }
    return resultBuilder.build();
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Aggregations

InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)28 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)26 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)23 Block (com.facebook.presto.spi.block.Block)11 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)9 Type (com.facebook.presto.spi.type.Type)8 Test (org.testng.annotations.Test)8 MapType (com.facebook.presto.type.MapType)5 RowType (com.facebook.presto.type.RowType)5 PrestoException (com.facebook.presto.spi.PrestoException)4 ArrayType (com.facebook.presto.type.ArrayType)4 VarcharType (com.facebook.presto.spi.type.VarcharType)3 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)3 Map (java.util.Map)3 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)2 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)2 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)2 BigintType (com.facebook.presto.spi.type.BigintType)2 BooleanType (com.facebook.presto.spi.type.BooleanType)2