use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestPage method testCompactDictionaryBlocks.
@Test
public void testCompactDictionaryBlocks() {
int positionCount = 100;
// Create 2 dictionary blocks with the same source id
DictionaryId commonSourceId = randomDictionaryId();
int commonDictionaryUsedPositions = 20;
int[] commonDictionaryIds = getDictionaryIds(positionCount, commonDictionaryUsedPositions);
// first dictionary contains "varbinary" values
Slice[] dictionaryValues1 = createExpectedValues(50);
Block dictionary1 = createSlicesBlock(dictionaryValues1);
DictionaryBlock commonSourceIdBlock1 = new DictionaryBlock(positionCount, dictionary1, commonDictionaryIds, commonSourceId);
// second dictionary block is "length(firstColumn)"
BlockBuilder dictionary2 = BIGINT.createBlockBuilder(null, dictionary1.getPositionCount());
for (Slice expectedValue : dictionaryValues1) {
BIGINT.writeLong(dictionary2, expectedValue.length());
}
DictionaryBlock commonSourceIdBlock2 = new DictionaryBlock(positionCount, dictionary2.build(), commonDictionaryIds, commonSourceId);
// Create block with a different source id, dictionary size, used
int otherDictionaryUsedPositions = 30;
int[] otherDictionaryIds = getDictionaryIds(positionCount, otherDictionaryUsedPositions);
Block dictionary3 = createSlicesBlock(createExpectedValues(70));
DictionaryBlock randomSourceIdBlock = new DictionaryBlock(dictionary3, otherDictionaryIds);
Page page = new Page(commonSourceIdBlock1, randomSourceIdBlock, commonSourceIdBlock2);
page.compact();
// dictionary blocks should all be compact
assertTrue(((DictionaryBlock) page.getBlock(0)).isCompact());
assertTrue(((DictionaryBlock) page.getBlock(1)).isCompact());
assertTrue(((DictionaryBlock) page.getBlock(2)).isCompact());
assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
assertEquals(((DictionaryBlock) page.getBlock(1)).getDictionary().getPositionCount(), otherDictionaryUsedPositions);
assertEquals(((DictionaryBlock) page.getBlock(2)).getDictionary().getPositionCount(), commonDictionaryUsedPositions);
// Blocks that had the same source id before compacting page should have the same source id after compacting page
assertNotEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(1)).getDictionarySourceId());
assertEquals(((DictionaryBlock) page.getBlock(0)).getDictionarySourceId(), ((DictionaryBlock) page.getBlock(2)).getDictionarySourceId());
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestPage method testCompactNestedDictionary.
@Test
public void testCompactNestedDictionary() {
Slice[] expectedValues = createExpectedValues(10);
Block valuesBlock = createSlicesBlock(expectedValues);
DictionaryBlock nestedDictionary = new DictionaryBlock(valuesBlock, new int[] { 0, 1, 2, 2, 4, 5 });
DictionaryBlock dictionary = new DictionaryBlock(nestedDictionary, new int[] { 2, 3, 2, 0 });
Page page = new Page(dictionary);
page.compact();
// Page#compact does not unnest nested dictionaries
assertFalse(((DictionaryBlock) page.getBlock(0)).isCompact());
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestColumnarArray method assertDictionaryBlock.
private static <T> void assertDictionaryBlock(Block block, T[] expectedValues) {
DictionaryBlock dictionaryBlock = createTestDictionaryBlock(block);
T[] expectedDictionaryValues = createTestDictionaryExpectedValues(expectedValues);
assertBlock(dictionaryBlock, expectedDictionaryValues);
assertColumnarArray(dictionaryBlock, expectedDictionaryValues);
assertRunLengthEncodedBlock(dictionaryBlock, expectedDictionaryValues);
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
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());
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestDictionaryBlock method testCompactAllKeysReferenced.
@Test
public void testCompactAllKeysReferenced() {
Slice[] expectedValues = createExpectedValues(5);
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 10);
DictionaryBlock compactBlock = dictionaryBlock.compact();
// When there is nothing to compact, we return the same block
assertEquals(compactBlock.getDictionary(), dictionaryBlock.getDictionary());
assertEquals(compactBlock.getPositionCount(), dictionaryBlock.getPositionCount());
for (int position = 0; position < compactBlock.getPositionCount(); position++) {
assertEquals(compactBlock.getId(position), dictionaryBlock.getId(position));
}
assertEquals(compactBlock.isCompact(), true);
}
Aggregations