Search in sources :

Example 21 with InterleavedBlockBuilder

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

the class TypedHistogram method serialize.

public Block serialize() {
    Block valuesBlock = values.build();
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(type, BIGINT), new BlockBuilderStatus(), valuesBlock.getPositionCount() * 2);
    for (int i = 0; i < valuesBlock.getPositionCount(); i++) {
        type.appendTo(valuesBlock, i, blockBuilder);
        BIGINT.writeLong(blockBuilder, counts.get(i));
    }
    return blockBuilder.build();
}
Also used : Block(com.facebook.presto.spi.block.Block) 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 22 with InterleavedBlockBuilder

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

the class TestMapOperators method testStackRepresentation.

@Test
public void testStackRepresentation() throws Exception {
    Block array = arrayBlockOf(BIGINT, 1L, 2L);
    Block actualBlock = mapBlockOf(DOUBLE, new ArrayType(BIGINT), ImmutableMap.of(1.0, array));
    DynamicSliceOutput actualSliceOutput = new DynamicSliceOutput(100);
    writeBlock(actualSliceOutput, actualBlock);
    Block expectedBlock = new InterleavedBlockBuilder(ImmutableList.of(DOUBLE, new ArrayType(BIGINT)), new BlockBuilderStatus(), 3).writeLong(doubleToLongBits(1.0)).closeEntry().writeObject(BIGINT.createBlockBuilder(new BlockBuilderStatus(), 1).writeLong(1L).closeEntry().writeLong(2L).closeEntry().build()).closeEntry().build();
    DynamicSliceOutput expectedSliceOutput = new DynamicSliceOutput(100);
    writeBlock(expectedSliceOutput, expectedBlock);
    assertEquals(actualSliceOutput.slice(), expectedSliceOutput.slice());
}
Also used : Block(com.facebook.presto.spi.block.Block) BlockSerdeUtil.writeBlock(com.facebook.presto.block.BlockSerdeUtil.writeBlock) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 23 with InterleavedBlockBuilder

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

the class TestArrayOperators method testArrayHashOperator.

@Test
public void testArrayHashOperator() {
    assertArrayHashOperator("ARRAY[1, 2]", INTEGER, ImmutableList.of(1, 2));
    assertArrayHashOperator("ARRAY[true, false]", BOOLEAN, ImmutableList.of(true, false));
    // test with ARRAY[ MAP( ARRAY[1], ARRAY[2] ) ]
    MapType mapType = new MapType(INTEGER, INTEGER);
    BlockBuilder mapBuilder = new InterleavedBlockBuilder(ImmutableList.of(INTEGER, INTEGER), new BlockBuilderStatus(), 2);
    INTEGER.writeLong(mapBuilder, 1);
    INTEGER.writeLong(mapBuilder, 2);
    assertArrayHashOperator("ARRAY[MAP(ARRAY[1], ARRAY[2])]", mapType, ImmutableList.of(mapBuilder.build()));
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) 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 24 with InterleavedBlockBuilder

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

the class TestInterleavedBlock method testGetSizeInBytes.

@Test
private void testGetSizeInBytes() {
    int numEntries = 2000;
    VarcharType unboundedVarcharType = createUnboundedVarcharType();
    InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(unboundedVarcharType, BIGINT), new BlockBuilderStatus(), numEntries);
    for (int i = 0; i < numEntries; i += 2) {
        unboundedVarcharType.writeString(blockBuilder, String.valueOf(ThreadLocalRandom.current().nextLong()));
        BIGINT.writeLong(blockBuilder, ThreadLocalRandom.current().nextLong());
    }
    InterleavedBlock block = blockBuilder.build();
    List<Block> splitQuarter = splitBlock(block, 4);
    int sizeInBytes = block.getSizeInBytes();
    int quarter1size = splitQuarter.get(0).getSizeInBytes();
    int quarter2size = splitQuarter.get(1).getSizeInBytes();
    int quarter3size = splitQuarter.get(2).getSizeInBytes();
    int quarter4size = splitQuarter.get(3).getSizeInBytes();
    double expectedQuarterSizeMin = sizeInBytes * 0.2;
    double expectedQuarterSizeMax = sizeInBytes * 0.3;
    assertTrue(quarter1size > expectedQuarterSizeMin && quarter1size < expectedQuarterSizeMax, format("quarter1size is %s, should be between %s and %s", quarter1size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter2size > expectedQuarterSizeMin && quarter2size < expectedQuarterSizeMax, format("quarter2size is %s, should be between %s and %s", quarter2size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter3size > expectedQuarterSizeMin && quarter3size < expectedQuarterSizeMax, format("quarter3size is %s, should be between %s and %s", quarter3size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter4size > expectedQuarterSizeMin && quarter4size < expectedQuarterSizeMax, format("quarter4size is %s, should be between %s and %s", quarter4size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertEquals(quarter1size + quarter2size + quarter3size + quarter4size, sizeInBytes);
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Block(com.facebook.presto.spi.block.Block) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 25 with InterleavedBlockBuilder

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

the class TestInterleavedBlock method createBlockBuilderWithValues.

private static InterleavedBlockBuilder createBlockBuilderWithValues(Slice[] expectedValues) {
    InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(TYPES, new BlockBuilderStatus(), expectedValues.length);
    int valueIndex = 0;
    while (valueIndex < expectedValues.length) {
        for (Type type : TYPES) {
            Class<?> javaType = type.getJavaType();
            Slice expectedValue = expectedValues[valueIndex];
            if (expectedValue == null) {
                blockBuilder.appendNull();
            } else if (javaType == boolean.class) {
                type.writeBoolean(blockBuilder, expectedValue.getByte(0) != 0);
            } else if (javaType == long.class) {
                type.writeLong(blockBuilder, expectedValue.getLong(0));
            } else if (javaType == double.class) {
                type.writeDouble(blockBuilder, expectedValue.getDouble(0));
            } else {
                blockBuilder.writeBytes(expectedValue, 0, expectedValue.length()).closeEntry();
            }
            valueIndex++;
        }
    }
    return blockBuilder;
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) Slice(io.airlift.slice.Slice) 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