@UsedByGeneratedCode public static Block appendElement(Type elementType, Block block, long value) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } elementType.writeLong(blockBuilder, value); return blockBuilder.build(); }
@Override public void appendTo(Block block, int position, BlockBuilder blockBuilder) { if (block.isNull(position)) { blockBuilder.appendNull(); } else { block.writeBytesTo(position, 0, block.getSliceLength(position), blockBuilder); blockBuilder.closeEntry(); } }
@Override public Object getObjectValue(ConnectorSession session, Block block, int position) { if (block.isNull(position)) { return null; } return block.getSlice(position, 0, block.getSliceLength(position)).toStringUtf8(); }
@Override public Slice getSlice(Block block, int position) { return block.getSlice(position, 0, block.getSliceLength(position)); }
private static boolean equals(Block block1, Block block2) { boolean retval = block1.getPositionCount() == block2.getPositionCount(); for (int i = 0; i < block1.getPositionCount() && retval; ++i) { if (block1 instanceof ArrayBlock && block2 instanceof ArrayBlock) { retval = equals(block1.getObject(i, Block.class), block2.getObject(i, Block.class)); } else { retval = block1.compareTo(i, 0, block1.getSliceLength(i), block2, i, 0, block2.getSliceLength(i)) == 0; } } return retval; }
@Test public void testSanityColumnarDictionary() { PageProcessor processor = compiler.compilePageProcessor(Optional.empty(), ImmutableList.of(field(0, VARCHAR)), MAX_BATCH_SIZE).get(); Page page = new Page(createDictionaryBlock(createExpectedValues(10), 100)); Page outputPage = getOnlyElement( processor.process( null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)) .orElseThrow(() -> new AssertionError("page is not present")); assertEquals(outputPage.getPositionCount(), 100); assertTrue(outputPage.getBlock(0) instanceof DictionaryBlock); DictionaryBlock dictionaryBlock = (DictionaryBlock) outputPage.getBlock(0); assertEquals(dictionaryBlock.getDictionary().getPositionCount(), 10); }
@Test public void testBinaryMergeIteratorOverEmptyPageAndNonEmptyPage() { Page emptyPage = new Page(0, BIGINT.createFixedSizeBlockBuilder(0).build()); Page page = rowPagesBuilder(BIGINT).row(42).build().get(0); WorkProcessor<Page> mergedPage = new MergeHashSort(newSimpleAggregatedMemoryContext()).merge( ImmutableList.of(BIGINT), ImmutableList.of(BIGINT), ImmutableList.of(ImmutableList.of(emptyPage, page).iterator()).stream() .map(WorkProcessor::fromIterator) .collect(toImmutableList()), new DriverYieldSignal()); assertTrue(mergedPage.process()); Page actualPage = mergedPage.getResult(); assertEquals(actualPage.getPositionCount(), 1); assertEquals(actualPage.getChannelCount(), 1); assertEquals(actualPage.getBlock(0).getLong(0, 0), 42); assertFinishes(mergedPage); }
@Test public void testEstimatedDataSizeForStats() { Map<String, Long>[] expectedValues = alternatingNullValues(createTestMap(9, 3, 4, 0, 8, 0, 6, 5)); BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues); Block block = blockBuilder.build(); assertEquals(block.getPositionCount(), expectedValues.length); for (int i = 0; i < block.getPositionCount(); i++) { int expectedSize = getExpectedEstimatedDataSize(expectedValues[i]); assertEquals(blockBuilder.getEstimatedDataSizeForStats(i), expectedSize); assertEquals(block.getEstimatedDataSizeForStats(i), expectedSize); } }
@Test public void testBinaryMergeIteratorOverPageWithDifferentHashes() { Page page = rowPagesBuilder(BIGINT) .row(42) .row(42) .row(52) .row(60) .build().get(0); WorkProcessor<Page> mergedPages = new MergeHashSort(newSimpleAggregatedMemoryContext()).merge( ImmutableList.of(BIGINT), ImmutableList.of(BIGINT), ImmutableList.of(ImmutableList.of(page).iterator()).stream() .map(WorkProcessor::fromIterator) .collect(toImmutableList()), new DriverYieldSignal()); assertTrue(mergedPages.process()); Page resultPage = mergedPages.getResult(); assertEquals(resultPage.getPositionCount(), 4); assertEquals(resultPage.getBlock(0).getLong(0, 0), 42); assertEquals(resultPage.getBlock(0).getLong(1, 0), 42); assertEquals(resultPage.getBlock(0).getLong(2, 0), 52); assertEquals(resultPage.getBlock(0).getLong(3, 0), 60); assertFinishes(mergedPages); } }
@Test public void testComplexSerialization() singleState.setAnotherBlock(mapBlockOf(BIGINT, VARCHAR, ImmutableMap.of(123L, "testBlock"))); BlockBuilder builder = RowType.anonymous(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, INTEGER, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY)) .createBlockBuilder(null, 1); serializer.serialize(singleState, builder); Block block = builder.build(); serializer.deserialize(block, 0, deserializedState); assertEquals(deserializedState.getBoolean(), singleState.getBoolean()); assertEquals(deserializedState.getLong(), singleState.getLong()); assertEquals(deserializedState.getDouble(), singleState.getDouble()); assertEquals(deserializedState.getByte(), singleState.getByte()); assertEquals(deserializedState.getInt(), singleState.getInt()); assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice()); assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice()); assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0)); assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0)); assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
@Test public void testForceRehash() { // Create a page with positionCount >> expected size of groupByHash Block valuesBlock = BlockAssertions.createStringSequenceBlock(0, 100); Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(VARCHAR), valuesBlock); // Create group by hash with extremely small size GroupByHash groupByHash = createGroupByHash(TEST_SESSION, ImmutableList.of(VARCHAR), new int[] {0}, Optional.of(1), 4, JOIN_COMPILER); groupByHash.getGroupIds(new Page(valuesBlock, hashBlock)).process(); // Ensure that all groups are present in group by hash for (int i = 0; i < valuesBlock.getPositionCount(); i++) { assertTrue(groupByHash.contains(i, new Page(valuesBlock, hashBlock), CONTAINS_CHANNELS)); } }
public Block serialize() { Block keys = keyBlockBuilder.build(); Block values = valueBlockBuilder.build(); BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), keys.getPositionCount() * 2); for (int i = 0; i < keys.getPositionCount(); i++) { keyType.appendTo(keys, i, blockBuilder); valueType.appendTo(values, i, blockBuilder); } return blockBuilder.build(); }
@Test public void testCopyPositionsSamePosition() { Slice[] expectedValues = createExpectedValues(10); DictionaryBlock dictionaryBlock = createDictionaryBlock(expectedValues, 100); int[] positionsToCopy = new int[] {52, 52, 52}; 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[] {expectedValues[2]}); assertDictionaryIds(copiedBlock, 0, 0, 0); }
@Test public void testMultipleValuesWithNull() { BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, 10); blockBuilder.appendNull(); BIGINT.writeLong(blockBuilder, 42); blockBuilder.appendNull(); BIGINT.writeLong(blockBuilder, 42); Block block = blockBuilder.build(); assertTrue(block.isNull(0)); assertEquals(BIGINT.getLong(block, 1), 42L); assertTrue(block.isNull(2)); assertEquals(BIGINT.getLong(block, 3), 42L); }
public static Block getHashBlock(List<? extends Type> hashTypes, Block... hashBlocks) { checkArgument(hashTypes.size() == hashBlocks.length); int[] hashChannels = new int[hashBlocks.length]; for (int i = 0; i < hashBlocks.length; i++) { hashChannels[i] = i; } HashGenerator hashGenerator = new InterpretedHashGenerator(ImmutableList.copyOf(hashTypes), hashChannels); int positionCount = hashBlocks[0].getPositionCount(); BlockBuilder builder = BIGINT.createFixedSizeBlockBuilder(positionCount); Page page = new Page(hashBlocks); for (int i = 0; i < positionCount; i++) { BIGINT.writeLong(builder, hashGenerator.hashPosition(i, page)); } return builder.build(); }
@Test public void testNull() { Accumulator accumulator = factory.createAccumulator(); Block result = getFinalBlock(accumulator); assertTrue(result.getPositionCount() == 1); assertTrue(result.isNull(0)); }
@Test public void testGetPositions() { int entries = 10; BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, entries); for (int i = 0; i < entries; i++) { BIGINT.writeLong(blockBuilder, i); } Block block = blockBuilder.build(); Page page = new Page(block, block, block).getPositions(new int[] {0, 1, 1, 1, 2, 5, 5}, 1, 5); assertEquals(page.getPositionCount(), 5); for (int i = 0; i < 3; i++) { assertEquals(page.getBlock(i).getLong(0, 0), 1); assertEquals(page.getBlock(i).getLong(1, 0), 1); assertEquals(page.getBlock(i).getLong(2, 0), 1); assertEquals(page.getBlock(i).getLong(3, 0), 2); assertEquals(page.getBlock(i).getLong(4, 0), 5); } }
@Test public void testCopyRegion() { Slice[] expectedValues = createExpectedValues(100); Block block = createBlockBuilderWithValues(expectedValues).build(); Block actual = block.copyRegion(10, 10); Block expected = createBlockBuilderWithValues(copyOfRange(expectedValues, 10, 20)).build(); assertEquals(actual.getPositionCount(), expected.getPositionCount()); assertEquals(actual.getSizeInBytes(), expected.getSizeInBytes()); }
private static void checkArrayItemIsNull(Map<DecoderColumnHandle, FieldValueProvider> decodedRow, DecoderColumnHandle handle, long[] expected) { Block actualBlock = getBlock(decodedRow, handle); assertEquals(actualBlock.getPositionCount(), expected.length); for (int i = 0; i < actualBlock.getPositionCount(); i++) { assertTrue(actualBlock.isNull(i)); assertEquals(BIGINT.getLong(actualBlock, i), expected[i]); } }
private static void assertPageSource(List<Type> types, Iterator<?>[] valuesByField, ConnectorPageSource pageSource) { Page page; while ((page = pageSource.getNextPage()) != null) { for (int field = 0; field < page.getChannelCount(); field++) { Block block = page.getBlock(field); for (int i = 0; i < block.getPositionCount(); i++) { assertTrue(valuesByField[field].hasNext()); Object expected = valuesByField[field].next(); Object actual = decodeObject(types.get(field), block, i); assertEquals(actual, expected); } } } }