Search in sources :

Example 56 with DictionaryBlock

use of io.prestosql.spi.block.DictionaryBlock in project hetu-core by openlookeng.

the class TestDictionaryBlock method testCopyPositionsWithCompactionsAndReorder.

@Test
public void testCopyPositionsWithCompactionsAndReorder() {
    Slice[] expectedValues = createExpectedValues(10);
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
    int[] positionsToCopy = new int[] { 50, 55, 40, 45, 60 };
    DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy, 0, positionsToCopy.length);
    assertEquals(copiedBlock.getDictionary().getPositionCount(), 2);
    assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);
    assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] { expectedValues[0], expectedValues[5] });
    assertDictionaryIds(copiedBlock, 0, 1, 0, 1, 0);
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 57 with DictionaryBlock

use of io.prestosql.spi.block.DictionaryBlock in project hetu-core by openlookeng.

the class TestDictionaryBlock method testCopyPositionsWithCompaction.

@Test
public void testCopyPositionsWithCompaction() {
    Slice[] expectedValues = createExpectedValues(10);
    Slice firstExpectedValue = expectedValues[0];
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
    int[] positionsToCopy = new int[] { 0, 10, 20, 30, 40 };
    DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy, 0, positionsToCopy.length);
    assertEquals(copiedBlock.getDictionary().getPositionCount(), 1);
    assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);
    assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] { firstExpectedValue });
    assertBlock(copiedBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] { firstExpectedValue, firstExpectedValue, firstExpectedValue, firstExpectedValue, firstExpectedValue });
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 58 with DictionaryBlock

use of io.prestosql.spi.block.DictionaryBlock in project hetu-core by openlookeng.

the class TestDictionaryBlock method testSizeInBytes.

@Test
public void testSizeInBytes() {
    Slice[] expectedValues = createExpectedValues(10);
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
    assertEquals(dictionaryBlock.getSizeInBytes(), dictionaryBlock.getDictionary().getSizeInBytes() + (100 * SIZE_OF_INT));
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 59 with DictionaryBlock

use of io.prestosql.spi.block.DictionaryBlock in project hetu-core by openlookeng.

the class TestDictionaryBlock method testLogicalSizeInBytes.

@Test
public void testLogicalSizeInBytes() {
    // The 10 Slices in the array will be of lengths 0 to 9.
    Slice[] expectedValues = createExpectedValues(10);
    // The dictionary within the dictionary block is expected to be a VariableWidthBlock of size 95 bytes.
    // 45 bytes for the expectedValues Slices (sum of seq(0,9)) and 50 bytes for the position and isNull array (total 10 positions).
    DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
    assertEquals(dictionaryBlock.getDictionary().getLogicalSizeInBytes(), 95);
    // The 100 positions in the dictionary block index to 10 positions in the underlying dictionary (10 each).
    // Logical size calculation accounts for 4 bytes of offset and 1 byte of isNull. Therefore the expected unoptimized
    // size is 10 times the size of the underlying dictionary (VariableWidthBlock).
    assertEquals(dictionaryBlock.getLogicalSizeInBytes(), 95 * 10);
    // With alternating nulls, we have 21 positions, with the same size calculation as above.
    dictionaryBlock = createDictionaryBlock(alternatingNullValues(expectedValues), 210);
    assertEquals(dictionaryBlock.getDictionary().getPositionCount(), 21);
    assertEquals(dictionaryBlock.getDictionary().getLogicalSizeInBytes(), 150);
    // The null positions should be included in the logical size.
    assertEquals(dictionaryBlock.getLogicalSizeInBytes(), 150 * 10);
}
Also used : Slice(io.airlift.slice.Slice) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Test(org.testng.annotations.Test)

Example 60 with DictionaryBlock

use of io.prestosql.spi.block.DictionaryBlock in project hetu-core by openlookeng.

the class TestGroupByHash method testMemoryReservationYieldWithDictionary.

@Test
public void testMemoryReservationYieldWithDictionary() {
    // Create a page with positionCount >> expected size of groupByHash
    int dictionaryLength = 1_000;
    int length = 2_000_000;
    int[] ids = IntStream.range(0, dictionaryLength).toArray();
    DictionaryId dictionaryId = randomDictionaryId();
    Block valuesBlock = new DictionaryBlock(dictionaryLength, createStringSequenceBlock(0, length), ids, dictionaryId);
    Block hashBlock = new DictionaryBlock(dictionaryLength, getHashBlock(ImmutableList.of(VARCHAR), valuesBlock), ids, dictionaryId);
    Page page = new Page(valuesBlock, hashBlock);
    AtomicInteger currentQuota = new AtomicInteger(0);
    AtomicInteger allowedQuota = new AtomicInteger(3);
    UpdateMemory updateMemory = () -> {
        if (currentQuota.get() < allowedQuota.get()) {
            currentQuota.getAndIncrement();
            return true;
        }
        return false;
    };
    int yields = 0;
    // test addPage
    GroupByHash groupByHash = createGroupByHash(ImmutableList.of(VARCHAR), new int[] { 0 }, Optional.of(1), 1, true, JOIN_COMPILER, updateMemory);
    boolean finish = false;
    Work<?> addPageWork = groupByHash.addPage(page);
    while (!finish) {
        finish = addPageWork.process();
        if (!finish) {
            assertEquals(currentQuota.get(), allowedQuota.get());
            // assert if we are blocked, we are going to be blocked again without changing allowedQuota
            assertFalse(addPageWork.process());
            assertEquals(currentQuota.get(), allowedQuota.get());
            yields++;
            allowedQuota.getAndAdd(3);
        }
    }
    // assert there is not anything missing
    assertEquals(dictionaryLength, groupByHash.getGroupCount());
    // assert we yield for every 3 rehashes
    // currentQuota is essentially the count we have successfully rehashed
    // the rehash count is 10 = log(1_000 / 0.75)
    assertEquals(currentQuota.get(), 10);
    assertEquals(currentQuota.get() / 3, yields);
    // test getGroupIds
    currentQuota.set(0);
    allowedQuota.set(3);
    yields = 0;
    groupByHash = createGroupByHash(ImmutableList.of(VARCHAR), new int[] { 0 }, Optional.of(1), 1, true, JOIN_COMPILER, updateMemory);
    finish = false;
    Work<GroupByIdBlock> getGroupIdsWork = groupByHash.getGroupIds(page);
    while (!finish) {
        finish = getGroupIdsWork.process();
        if (!finish) {
            assertEquals(currentQuota.get(), allowedQuota.get());
            // assert if we are blocked, we are going to be blocked again without changing allowedQuota
            assertFalse(getGroupIdsWork.process());
            assertEquals(currentQuota.get(), allowedQuota.get());
            yields++;
            allowedQuota.getAndAdd(3);
        }
    }
    // assert there is not anything missing
    assertEquals(dictionaryLength, groupByHash.getGroupCount());
    assertEquals(dictionaryLength, getGroupIdsWork.getResult().getPositionCount());
    // assert we yield for every 3 rehashes
    // currentQuota is essentially the count we have successfully rehashed
    // the rehash count is 10 = log2(1_000 / 0.75)
    assertEquals(currentQuota.get(), 10);
    assertEquals(currentQuota.get() / 3, yields);
}
Also used : DictionaryId(io.prestosql.spi.block.DictionaryId) DictionaryId.randomDictionaryId(io.prestosql.spi.block.DictionaryId.randomDictionaryId) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Page(io.prestosql.spi.Page) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GroupByHash.createGroupByHash(io.prestosql.operator.GroupByHash.createGroupByHash) DictionaryBlock(io.prestosql.spi.block.DictionaryBlock) Block(io.prestosql.spi.block.Block) BlockAssertions.createLongsBlock(io.prestosql.block.BlockAssertions.createLongsBlock) TypeUtils.getHashBlock(io.prestosql.type.TypeUtils.getHashBlock) BlockAssertions.createLongSequenceBlock(io.prestosql.block.BlockAssertions.createLongSequenceBlock) BlockAssertions.createStringSequenceBlock(io.prestosql.block.BlockAssertions.createStringSequenceBlock) Test(org.testng.annotations.Test)

Aggregations

DictionaryBlock (io.prestosql.spi.block.DictionaryBlock)69 Block (io.prestosql.spi.block.Block)31 Test (org.testng.annotations.Test)29 Slice (io.airlift.slice.Slice)17 BlockBuilder (io.prestosql.spi.block.BlockBuilder)16 RunLengthEncodedBlock (io.prestosql.spi.block.RunLengthEncodedBlock)11 Page (io.prestosql.spi.Page)9 ColumnarTestUtils.assertBlock (io.prestosql.block.ColumnarTestUtils.assertBlock)8 LazyBlock (io.prestosql.spi.block.LazyBlock)8 LongArrayBlock (io.prestosql.spi.block.LongArrayBlock)8 PageBuilderStatus (io.prestosql.spi.block.PageBuilderStatus)8 BlockAssertions.createLongSequenceBlock (io.prestosql.block.BlockAssertions.createLongSequenceBlock)7 DictionaryId (io.prestosql.spi.block.DictionaryId)6 TestUnnesterUtil.createSimpleBlock (io.prestosql.operator.unnest.TestUnnesterUtil.createSimpleBlock)5 DictionaryId.randomDictionaryId (io.prestosql.spi.block.DictionaryId.randomDictionaryId)5 BlockAssertions.createLongsBlock (io.prestosql.block.BlockAssertions.createLongsBlock)4 BlockAssertions.createLongDictionaryBlock (io.prestosql.block.BlockAssertions.createLongDictionaryBlock)3 ColumnarTestUtils.createTestDictionaryBlock (io.prestosql.block.ColumnarTestUtils.createTestDictionaryBlock)3 DriverYieldSignal (io.prestosql.operator.DriverYieldSignal)3 JoinProbeFactory (io.prestosql.operator.JoinProbe.JoinProbeFactory)3