use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestDictionaryBlock method testCopyPositionsUnwrapping.
@Test
public void testCopyPositionsUnwrapping() {
Slice[] expectedValues = createExpectedValues(10);
DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100);
assertTrue(dictionaryBlock.isCompact());
// 0 and 1 position copies unwrap the dictionary
assertInstanceOf(dictionaryBlock.copyPositions(new int[0], 0, 0), VariableWidthBlock.class);
assertInstanceOf(dictionaryBlock.copyPositions(new int[1], 0, 1), VariableWidthBlock.class);
// Dictionaries unwrap when each selected id appears only once
int[] positionsToCopy = new int[] { 9, 7, 5, 3, 2, 1, 0 };
assertInstanceOf(dictionaryBlock.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
// Compact dictionaries unwrap
DictionaryBlock compactSequentialDictionary = createDictionaryBlock(expectedValues, expectedValues.length);
// force uniqueIds to be computed
assertTrue(compactSequentialDictionary.isCompact());
assertInstanceOf(compactSequentialDictionary.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
// Nested dictionaries unwrap, even when not compact
DictionaryBlock outerDictionary = new DictionaryBlock(compactSequentialDictionary, new int[] { 1, 3, 5, 7, 9 });
// force uniqueIds to be computed
assertFalse(outerDictionary.isCompact());
positionsToCopy = new int[] { 0, 3, 2 };
// inner dictionary is compact, and also unwraps
assertInstanceOf(outerDictionary.copyPositions(positionsToCopy, 0, positionsToCopy.length), VariableWidthBlock.class);
// Nested dictionaries unwrap down to a single dictionary layer when ids are repeated
Block innermostRawBlock = createSlicesBlock(expectedValues);
DictionaryBlock firstDictionary = new DictionaryBlock(innermostRawBlock, new int[] { 0, 1, 1, 7, 7, 5, 3 });
DictionaryBlock secondDictionary = new DictionaryBlock(firstDictionary, new int[] { 3, 1, 1, 2 });
DictionaryBlock thirdDictionary = new DictionaryBlock(secondDictionary, new int[] { 0, 0, 0, 2 });
// Result is still a dictionary, but only a single layer
positionsToCopy = new int[] { 2, 1, 0 };
assertInstanceOf(thirdDictionary.copyPositions(positionsToCopy, 0, 2), DictionaryBlock.class);
assertInstanceOf(((DictionaryBlock) thirdDictionary.copyPositions(positionsToCopy, 0, 2)).getDictionary(), VariableWidthBlock.class);
// Re-check after all dictionaries have their uniqueIds computed
assertFalse(firstDictionary.isCompact());
assertFalse(secondDictionary.isCompact());
assertFalse(thirdDictionary.isCompact());
assertInstanceOf(thirdDictionary.copyPositions(positionsToCopy, 0, 2), DictionaryBlock.class);
assertInstanceOf(((DictionaryBlock) thirdDictionary.copyPositions(positionsToCopy, 0, 2)).getDictionary(), VariableWidthBlock.class);
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestDictionaryBlock method testNestedDictionarySizes.
@Test
public void testNestedDictionarySizes() {
// fixed width block
Block fixedWidthBlock = new IntArrayBlock(100, Optional.empty(), IntStream.range(0, 100).toArray());
assertDictionarySizeMethods(fixedWidthBlock);
assertDictionarySizeMethods(new DictionaryBlock(fixedWidthBlock, IntStream.range(0, 50).toArray()));
assertDictionarySizeMethods(new DictionaryBlock(new DictionaryBlock(fixedWidthBlock, IntStream.range(0, 50).toArray()), IntStream.range(0, 10).toArray()));
// variable width block
Block variableWidthBlock = createSlicesBlock(createExpectedValues(100));
assertDictionarySizeMethods(variableWidthBlock);
assertDictionarySizeMethods(new DictionaryBlock(variableWidthBlock, IntStream.range(0, 50).toArray()));
assertDictionarySizeMethods(new DictionaryBlock(new DictionaryBlock(variableWidthBlock, IntStream.range(0, 50).toArray()), IntStream.range(0, 10).toArray()));
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestLookupJoinPageBuilder method testDifferentPositions.
@Test
public void testDifferentPositions() {
int entries = 100;
BlockBuilder blockBuilder = BIGINT.createFixedSizeBlockBuilder(entries);
for (int i = 0; i < entries; i++) {
BIGINT.writeLong(blockBuilder, i);
}
Block block = blockBuilder.build();
Page page = new Page(block);
JoinProbeFactory joinProbeFactory = new JoinProbeFactory(new int[] { 0 }, ImmutableList.of(0), OptionalInt.empty());
LookupSource lookupSource = new TestLookupSource(ImmutableList.of(BIGINT), page);
LookupJoinPageBuilder lookupJoinPageBuilder = new LookupJoinPageBuilder(ImmutableList.of(BIGINT));
// empty
JoinProbe probe = joinProbeFactory.createJoinProbe(page);
Page output = lookupJoinPageBuilder.build(probe);
assertEquals(output.getChannelCount(), 2);
assertTrue(output.getBlock(0) instanceof DictionaryBlock);
assertEquals(output.getPositionCount(), 0);
lookupJoinPageBuilder.reset();
// the probe covers non-sequential positions
probe = joinProbeFactory.createJoinProbe(page);
for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
if (joinPosition % 2 == 1) {
continue;
}
lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
}
output = lookupJoinPageBuilder.build(probe);
assertEquals(output.getChannelCount(), 2);
assertTrue(output.getBlock(0) instanceof DictionaryBlock);
assertEquals(output.getPositionCount(), entries / 2);
for (int i = 0; i < entries / 2; i++) {
assertEquals(output.getBlock(0).getLong(i, 0), i * 2);
assertEquals(output.getBlock(1).getLong(i, 0), i * 2);
}
lookupJoinPageBuilder.reset();
// the probe covers everything
probe = joinProbeFactory.createJoinProbe(page);
for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
}
output = lookupJoinPageBuilder.build(probe);
assertEquals(output.getChannelCount(), 2);
assertFalse(output.getBlock(0) instanceof DictionaryBlock);
assertEquals(output.getPositionCount(), entries);
for (int i = 0; i < entries; i++) {
assertEquals(output.getBlock(0).getLong(i, 0), i);
assertEquals(output.getBlock(1).getLong(i, 0), i);
}
lookupJoinPageBuilder.reset();
// the probe covers some sequential positions
probe = joinProbeFactory.createJoinProbe(page);
for (int joinPosition = 0; probe.advanceNextPosition(); joinPosition++) {
if (joinPosition < 10 || joinPosition >= 50) {
continue;
}
lookupJoinPageBuilder.appendRow(probe, lookupSource, joinPosition);
}
output = lookupJoinPageBuilder.build(probe);
assertEquals(output.getChannelCount(), 2);
assertFalse(output.getBlock(0) instanceof DictionaryBlock);
assertEquals(output.getPositionCount(), 40);
for (int i = 10; i < 50; i++) {
assertEquals(output.getBlock(0).getLong(i - 10, 0), i);
assertEquals(output.getBlock(1).getLong(i - 10, 0), i);
}
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestPartitionedOutputOperator method testOutputForOneValueDictionaryBlock.
@Test
public void testOutputForOneValueDictionaryBlock() {
PartitionedOutputOperator partitionedOutputOperator = partitionedOutputOperator(BIGINT).build();
Page page = new Page(new DictionaryBlock(createLongsBlock(0), new int[] { 0, 0, 0, 0 }));
processPages(partitionedOutputOperator, page);
List<Object> partition0 = readLongs(outputBuffer.getEnqueuedDeserialized(0), 0);
assertThat(partition0).containsExactlyElementsOf(nCopies(4, 0L));
List<Object> partition1 = readLongs(outputBuffer.getEnqueuedDeserialized(1), 0);
assertThat(partition1).isEmpty();
}
use of io.trino.spi.block.DictionaryBlock in project trino by trinodb.
the class TestDictionaryAwarePageFilter method testDictionaryBlockProcessingWithUnusedFailure.
@Test
public void testDictionaryBlockProcessingWithUnusedFailure() {
// match some
testFilter(createDictionaryBlockWithUnusedEntries(20, 100), DictionaryBlock.class);
// match none
testFilter(createDictionaryBlockWithUnusedEntries(20, 0), DictionaryBlock.class);
// match all
testFilter(new DictionaryBlock(createLongsBlock(4, 5, -1), new int[100]), DictionaryBlock.class);
}
Aggregations