Search in sources :

Example 16 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project presto by prestodb.

the class TestDictionaryBlock method testCompactGetPositions.

@Test
public void testCompactGetPositions() {
    DictionaryBlock block = new DictionaryBlock(createSlicesBlock(createExpectedValues(10)), new int[] { 0, 1, 2, 3, 4, 5 }).compact();
    // 3, 3, 4, 5, 2, 0, 1, 1
    block = (DictionaryBlock) block.getPositions(new int[] { 3, 3, 4, 5, 2, 0, 1, 1 }, 0, 7);
    assertTrue(block.isCompact());
    // 3, 3, 4, 5, 2, 0, 1, 1, 0, 2, 5, 4, 3
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1 }, 0, 12);
    assertTrue(block.isCompact());
    // 3, 4, 3, 4, 3
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 2, 0, 2, 0 }, 0, 5);
    assertFalse(block.isCompact());
    block = block.compact();
    // 3, 4, 4, 4
    block = (DictionaryBlock) block.getPositions(new int[] { 0, 1, 1, 1 }, 0, 4);
    assertTrue(block.isCompact());
    // 4, 4, 4, 4
    block = (DictionaryBlock) block.getPositions(new int[] { 1, 1, 1, 1 }, 0, 4);
    assertFalse(block.isCompact());
    block = block.compact();
    // 4
    block = (DictionaryBlock) block.getPositions(new int[] { 0 }, 0, 1);
    assertTrue(block.isCompact());
    // empty
    block = (DictionaryBlock) block.getPositions(new int[] {}, 0, 0);
    assertFalse(block.isCompact());
    block = block.compact();
    // empty
    block = (DictionaryBlock) block.getPositions(new int[] {}, 0, 0);
    assertTrue(block.isCompact());
}
Also used : BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 17 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project presto by prestodb.

the class AbstractTestBlock method assertBlockSize.

private void assertBlockSize(Block block) {
    // Asserting on `block` is not very effective because most blocks passed to this method is compact.
    // Therefore, we split the `block` into two and assert again.
    // ------------------Test Whole Block Sizes---------------------------------------------------
    // Assert sizeInBytes for the whole block.
    long expectedBlockSize = copyBlockViaBlockSerde(block).getSizeInBytes();
    assertEquals(block.getSizeInBytes(), expectedBlockSize);
    assertEquals(block.getRegionSizeInBytes(0, block.getPositionCount()), expectedBlockSize);
    // Assert logicalSize for the whole block. Note that copyBlockViaBlockSerde would flatten DictionaryBlock or RleBlock
    long logicalSizeInBytes = block.getLogicalSizeInBytes();
    long expectedLogicalBlockSize = copyBlockViaBlockSerde(block).getLogicalSizeInBytes();
    assertEquals(logicalSizeInBytes, expectedLogicalBlockSize);
    assertEquals(block.getRegionLogicalSizeInBytes(0, block.getPositionCount()), expectedLogicalBlockSize);
    // Assert approximateLogicalSize for the whole block
    long approximateLogicalSizeInBytes = block.getApproximateRegionLogicalSizeInBytes(0, block.getPositionCount());
    long expectedApproximateLogicalBlockSize = expectedLogicalBlockSize;
    if (block instanceof DictionaryBlock) {
        int dictionaryPositionCount = ((DictionaryBlock) block).getDictionary().getPositionCount();
        expectedApproximateLogicalBlockSize = ((DictionaryBlock) block).getDictionary().getApproximateRegionLogicalSizeInBytes(0, dictionaryPositionCount) * block.getPositionCount() / dictionaryPositionCount;
    }
    assertEquals(approximateLogicalSizeInBytes, expectedApproximateLogicalBlockSize);
    // ------------------Test First Half Sizes---------------------------------------------------
    List<Block> splitBlock = splitBlock(block, 2);
    Block firstHalf = splitBlock.get(0);
    int firstHalfPositionCount = firstHalf.getPositionCount();
    // Assert sizeInBytes for the firstHalf block.
    long expectedFirstHalfSize = copyBlockViaBlockSerde(firstHalf).getSizeInBytes();
    assertEquals(firstHalf.getSizeInBytes(), expectedFirstHalfSize);
    assertEquals(block.getRegionSizeInBytes(0, firstHalfPositionCount), expectedFirstHalfSize);
    // Assert logicalSize for the firstHalf block
    long firstHalfLogicalSizeInBytes = firstHalf.getLogicalSizeInBytes();
    long expectedFirstHalfLogicalSize = copyBlockViaBlockSerde(firstHalf).getLogicalSizeInBytes();
    assertEquals(firstHalfLogicalSizeInBytes, expectedFirstHalfLogicalSize);
    assertEquals(firstHalf.getRegionLogicalSizeInBytes(0, firstHalfPositionCount), expectedFirstHalfLogicalSize);
    // Assert approximateLogicalSize for the firstHalf block using logicalSize
    long approximateFirstHalfLogicalSize = firstHalf.getApproximateRegionLogicalSizeInBytes(0, firstHalfPositionCount);
    long expectedApproximateFirstHalfLogicalSize = expectedFirstHalfLogicalSize;
    if (firstHalf instanceof DictionaryBlock) {
        int dictionaryPositionCount = ((DictionaryBlock) firstHalf).getDictionary().getPositionCount();
        expectedApproximateFirstHalfLogicalSize = ((DictionaryBlock) firstHalf).getDictionary().getApproximateRegionLogicalSizeInBytes(0, dictionaryPositionCount) * firstHalfPositionCount / dictionaryPositionCount;
    }
    assertEquals(approximateFirstHalfLogicalSize, expectedApproximateFirstHalfLogicalSize);
    // Assert approximateLogicalSize for the firstHalf block using the ratio of firstHalf logicalSize vs whole block logicalSize
    long expectedApproximateFirstHalfLogicalSizeFromUnsplittedBlock = logicalSizeInBytes == 0 ? approximateLogicalSizeInBytes : approximateLogicalSizeInBytes * firstHalfLogicalSizeInBytes / logicalSizeInBytes;
    assertBetweenInclusive(approximateFirstHalfLogicalSize, // Allow for some error margins due to skew in blocks
    min(expectedApproximateFirstHalfLogicalSizeFromUnsplittedBlock - 3, (long) (expectedApproximateFirstHalfLogicalSizeFromUnsplittedBlock * 0.7)), max(expectedApproximateFirstHalfLogicalSizeFromUnsplittedBlock + 3, (long) (expectedApproximateFirstHalfLogicalSizeFromUnsplittedBlock * 1.3)));
    // ------------------Test Second Half Sizes---------------------------------------------------
    Block secondHalf = splitBlock.get(1);
    int secondHalfPositionCount = secondHalf.getPositionCount();
    // Assert sizeInBytes for the secondHalf block.
    long expectedSecondHalfSize = copyBlockViaBlockSerde(secondHalf).getSizeInBytes();
    assertEquals(secondHalf.getSizeInBytes(), expectedSecondHalfSize);
    assertEquals(block.getRegionSizeInBytes(firstHalfPositionCount, secondHalfPositionCount), expectedSecondHalfSize);
    // Assert logicalSize for the secondHalf block.
    long secondHalfLogicalSizeInBytes = secondHalf.getLogicalSizeInBytes();
    long expectedSecondHalfLogicalSize = copyBlockViaBlockSerde(secondHalf).getLogicalSizeInBytes();
    assertEquals(secondHalfLogicalSizeInBytes, expectedSecondHalfLogicalSize);
    assertEquals(secondHalf.getRegionLogicalSizeInBytes(0, secondHalfPositionCount), expectedSecondHalfLogicalSize);
    // Assert approximateLogicalSize for the secondHalf block using logicalSize
    long approximateSecondHalfLogicalSize = secondHalf.getApproximateRegionLogicalSizeInBytes(0, secondHalfPositionCount);
    long expectedApproximateSecondHalfLogicalSize = copyBlockViaBlockSerde(secondHalf).getApproximateRegionLogicalSizeInBytes(0, secondHalfPositionCount);
    if (secondHalf instanceof DictionaryBlock) {
        int dictionaryPositionCount = ((DictionaryBlock) secondHalf).getDictionary().getPositionCount();
        expectedApproximateSecondHalfLogicalSize = ((DictionaryBlock) secondHalf).getDictionary().getApproximateRegionLogicalSizeInBytes(0, dictionaryPositionCount) * secondHalfPositionCount / dictionaryPositionCount;
    }
    assertEquals(approximateSecondHalfLogicalSize, expectedApproximateSecondHalfLogicalSize);
    // Assert approximateLogicalSize for the secondHalf block using the ratio of firstHalf logicalSize vs whole block logicalSize
    long expectedApproximateSecondHalfLogicalSizeFromUnsplittedBlock = logicalSizeInBytes == 0 ? approximateLogicalSizeInBytes : approximateLogicalSizeInBytes * secondHalfLogicalSizeInBytes / logicalSizeInBytes;
    assertBetweenInclusive(approximateSecondHalfLogicalSize, // Allow for some error margins due to skew in blocks
    min(expectedApproximateSecondHalfLogicalSizeFromUnsplittedBlock - 3, (long) (expectedApproximateSecondHalfLogicalSizeFromUnsplittedBlock * 0.7)), max(expectedApproximateSecondHalfLogicalSizeFromUnsplittedBlock + 3, (long) (expectedApproximateSecondHalfLogicalSizeFromUnsplittedBlock * 1.3)));
    // ----------------Test getPositionsSizeInBytes----------------------------------------
    boolean[] positions = new boolean[block.getPositionCount()];
    fill(positions, 0, firstHalfPositionCount, true);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedFirstHalfSize);
    fill(positions, true);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedBlockSize);
    fill(positions, 0, firstHalfPositionCount, false);
    assertEquals(block.getPositionsSizeInBytes(positions), expectedSecondHalfSize);
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block)

Example 18 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project presto by prestodb.

the class TestBlockEncodingBuffers method buildDictionaryBlockStatus.

private BlockStatus buildDictionaryBlockStatus(BlockStatus dictionary, int positionCount) {
    DictionaryBlock dictionaryBlock = createRandomDictionaryBlock(dictionary.block, positionCount);
    int[] mappedExpectedRowSizes = IntStream.range(0, positionCount).map(i -> dictionary.expectedRowSizes[dictionaryBlock.getId(i)]).toArray();
    return new BlockStatus(dictionaryBlock, mappedExpectedRowSizes);
}
Also used : MapBlock.fromKeyValueBlock(com.facebook.presto.common.block.MapBlock.fromKeyValueBlock) Page(com.facebook.presto.common.Page) Arrays(java.util.Arrays) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) GRACE_FACTOR_FOR_MAX_BUFFER_CAPACITY(com.facebook.presto.operator.repartition.AbstractBlockEncodingBuffer.GRACE_FACTOR_FOR_MAX_BUFFER_CAPACITY) Test(org.testng.annotations.Test) SIZE_OF_BYTE(io.airlift.slice.SizeOf.SIZE_OF_BYTE) DICTIONARY(com.facebook.presto.block.BlockAssertions.Encoding.DICTIONARY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AbstractBlockEncodingBuffer.createBlockEncodingBuffers(com.facebook.presto.operator.repartition.AbstractBlockEncodingBuffer.createBlockEncodingBuffers) HASH_MULTIPLIER(com.facebook.presto.operator.repartition.MapBlockEncodingBuffer.HASH_MULTIPLIER) BlockAssertions.assertBlockEquals(com.facebook.presto.block.BlockAssertions.assertBlockEquals) OptimizedPartitionedOutputOperator.decodeBlock(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.decodeBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) SIZE_OF_INT(io.airlift.slice.SizeOf.SIZE_OF_INT) BlockAssertions.createRandomSmallintsBlock(com.facebook.presto.block.BlockAssertions.createRandomSmallintsBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) UncheckedStackArrayAllocator(com.facebook.presto.operator.UncheckedStackArrayAllocator) Encoding(com.facebook.presto.block.BlockAssertions.Encoding) BlockAssertions.createAllNullsBlock(com.facebook.presto.block.BlockAssertions.createAllNullsBlock) String.format(java.lang.String.format) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) Optional(java.util.Optional) BlockAssertions.createRandomBooleansBlock(com.facebook.presto.block.BlockAssertions.createRandomBooleansBlock) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) IntStream(java.util.stream.IntStream) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) SliceOutput(io.airlift.slice.SliceOutput) RowType.withDefaultFieldNames(com.facebook.presto.common.type.RowType.withDefaultFieldNames) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assert.assertEquals(org.testng.Assert.assertEquals) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) ImmutableList(com.google.common.collect.ImmutableList) Closer(com.google.common.io.Closer) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomShortDecimalsBlock) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RowBlock.fromFieldBlocks(com.facebook.presto.common.block.RowBlock.fromFieldBlocks) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) RUN_LENGTH(com.facebook.presto.block.BlockAssertions.Encoding.RUN_LENGTH) Math.toIntExact(java.lang.Math.toIntExact) BlockAssertions.createRleBlockWithRandomValue(com.facebook.presto.block.BlockAssertions.createRleBlockWithRandomValue) Type(com.facebook.presto.common.type.Type) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) BlockFlattener(com.facebook.presto.common.block.BlockFlattener) IOException(java.io.IOException) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) BlockSerdeUtil.readBlock(com.facebook.presto.common.block.BlockSerdeUtil.readBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) SIZE_OF_LONG(io.airlift.slice.SizeOf.SIZE_OF_LONG) BlockAssertions.createRandomIntsBlock(com.facebook.presto.block.BlockAssertions.createRandomIntsBlock) Block(com.facebook.presto.common.block.Block) BlockAssertions.createRandomLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongDecimalsBlock) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock)

Example 19 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project presto by prestodb.

the class TestDictionaryAwarePageProjection method createDictionaryBlockWithFailure.

private static DictionaryBlock createDictionaryBlockWithFailure(int dictionarySize, int blockSize) {
    Block dictionary = createLongSequenceBlock(-10, dictionarySize - 10);
    int[] ids = new int[blockSize];
    Arrays.setAll(ids, index -> index % dictionarySize);
    return new DictionaryBlock(dictionary, ids);
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) RunLengthEncodedBlock(com.facebook.presto.common.block.RunLengthEncodedBlock) BlockAssertions.createLongSequenceBlock(com.facebook.presto.block.BlockAssertions.createLongSequenceBlock) LongArrayBlock(com.facebook.presto.common.block.LongArrayBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Block(com.facebook.presto.common.block.Block) LazyBlock(com.facebook.presto.common.block.LazyBlock)

Example 20 with DictionaryBlock

use of com.facebook.presto.common.block.DictionaryBlock in project presto by prestodb.

the class TestDictionaryAwarePageProjection method testDictionaryBlock.

@Test(dataProvider = "forceYield")
public void testDictionaryBlock(boolean forceYield) {
    DictionaryBlock block = createDictionaryBlock(10, 100);
    testProject(block, DictionaryBlock.class, forceYield);
}
Also used : DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) Test(org.testng.annotations.Test)

Aggregations

DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)123 Test (org.testng.annotations.Test)64 Block (com.facebook.presto.common.block.Block)63 BlockAssertions.createRandomDictionaryBlock (com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock)30 RunLengthEncodedBlock (com.facebook.presto.common.block.RunLengthEncodedBlock)28 Slice (io.airlift.slice.Slice)26 Page (com.facebook.presto.common.Page)25 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)22 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)22 BlockAssertions.createLongSequenceBlock (com.facebook.presto.block.BlockAssertions.createLongSequenceBlock)14 LongArrayBlock (com.facebook.presto.common.block.LongArrayBlock)14 DictionaryId (com.facebook.presto.common.block.DictionaryId)12 LazyBlock (com.facebook.presto.common.block.LazyBlock)12 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)10 DictionaryId.randomDictionaryId (com.facebook.presto.common.block.DictionaryId.randomDictionaryId)10 BlockAssertions.createRLEBlock (com.facebook.presto.block.BlockAssertions.createRLEBlock)8 BlockAssertions.createRandomLongsBlock (com.facebook.presto.block.BlockAssertions.createRandomLongsBlock)8 BlockAssertions.createSlicesBlock (com.facebook.presto.block.BlockAssertions.createSlicesBlock)8 Type (com.facebook.presto.common.type.Type)8 BlockAssertions.createLongDictionaryBlock (com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock)6