use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class PrestoSparkShufflePageInput method createPage.
private static Page createPage(int rowCount, BasicSliceInput input, List<Type> types) {
checkArgument(rowCount > 0, "rowCount must be greater than zero: %s", rowCount);
if (input.length() == 0) {
// zero column page
verify(types.isEmpty(), "types is expected to be empty");
return new Page(rowCount);
}
PageBuilder pageBuilder = new PageBuilder(types);
while (input.isReadable()) {
pageBuilder.declarePosition();
for (int channel = 0; channel < types.size(); channel++) {
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(channel);
blockBuilder.readPositionFrom(input);
}
}
Page page = pageBuilder.build();
verify(page.getPositionCount() == rowCount, "unexpected row count: %s != %s", page.getPositionCount(), rowCount);
return page;
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class TestMergingPageIterator method testMerging.
@Test
public void testMerging() {
List<Type> types = ImmutableList.of(INTEGER, INTEGER);
List<Integer> sortIndexes = ImmutableList.of(1);
List<SortOrder> sortOrders = ImmutableList.of(SortOrder.ASC_NULLS_FIRST);
List<List<Page>> pageLists = new ArrayList<>();
PageBuilder pageBuilder = new PageBuilder(types);
for (int i = 0; i < 10; i++) {
Iterator<Integer> values = IntStream.range(0, 1000).map(ignored -> ThreadLocalRandom.current().nextInt(100_000)).mapToObj(n -> ((n % 100) == 0) ? null : n).sorted(nullsFirst(naturalOrder())).iterator();
List<Page> pages = new ArrayList<>();
for (int j = 0; j < 10; j++) {
for (int k = 0; k < 100; k++) {
Integer n = values.next();
pageBuilder.declarePosition();
if (n == null) {
pageBuilder.getBlockBuilder(0).appendNull();
pageBuilder.getBlockBuilder(1).appendNull();
} else {
INTEGER.writeLong(pageBuilder.getBlockBuilder(0), n);
INTEGER.writeLong(pageBuilder.getBlockBuilder(1), n * 22L);
}
}
pages.add(pageBuilder.build());
pageBuilder.reset();
}
pageLists.add(pages);
assertFalse(values.hasNext());
}
List<Iterator<Page>> pages = pageLists.stream().map(List::iterator).collect(toList());
Iterator<Page> iterator = new MergingPageIterator(pages, types, sortIndexes, sortOrders);
List<Long> values = new ArrayList<>();
while (iterator.hasNext()) {
Page page = iterator.next();
for (int i = 0; i < page.getPositionCount(); i++) {
if (page.getBlock(0).isNull(i)) {
assertTrue(page.getBlock(1).isNull(i));
values.add(null);
} else {
long x = INTEGER.getLong(page.getBlock(0), i);
long y = INTEGER.getLong(page.getBlock(1), i);
assertEquals(y, x * 22);
values.add(x);
}
}
}
assertThat(values).isSortedAccordingTo(nullsFirst(naturalOrder()));
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class TestGroupByHash method testAppendToMultipleTuplesPerGroup.
@Test
public void testAppendToMultipleTuplesPerGroup() {
List<Long> values = new ArrayList<>();
for (long i = 0; i < 100; i++) {
values.add(i % 50);
}
Block valuesBlock = BlockAssertions.createLongsBlock(values);
Block hashBlock = TypeUtils.getHashBlock(ImmutableList.of(BIGINT), valuesBlock);
GroupByHash groupByHash = createGroupByHash(TEST_SESSION, ImmutableList.of(BIGINT), new int[] { 0 }, Optional.of(1), 100, JOIN_COMPILER);
groupByHash.getGroupIds(new Page(valuesBlock, hashBlock)).process();
assertEquals(groupByHash.getGroupCount(), 50);
PageBuilder pageBuilder = new PageBuilder(groupByHash.getTypes());
for (int i = 0; i < groupByHash.getGroupCount(); i++) {
pageBuilder.declarePosition();
groupByHash.appendValuesTo(i, pageBuilder, 0);
}
Page outputPage = pageBuilder.build();
assertEquals(outputPage.getPositionCount(), 50);
BlockAssertions.assertBlockEquals(BIGINT, outputPage.getBlock(0), BlockAssertions.createLongSequenceBlock(0, 50));
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class BenchmarkGroupedTopNBuilder method createInputPages.
private static List<Page> createInputPages(int positions, List<Type> types, int positionsPerPage, int groupCount, int seed) {
Random random = new Random(seed);
List<Page> pages = new ArrayList<>();
PageBuilder pageBuilder = new PageBuilder(types);
LineItemGenerator lineItemGenerator = new LineItemGenerator(1, 1, 1);
Iterator<LineItem> iterator = lineItemGenerator.iterator();
for (int i = 0; i < positions; i++) {
pageBuilder.declarePosition();
LineItem lineItem = iterator.next();
BIGINT.writeLong(pageBuilder.getBlockBuilder(HASH_GROUP), groupCount > 1 ? random.nextInt(groupCount) : 1);
DOUBLE.writeDouble(pageBuilder.getBlockBuilder(EXTENDED_PRICE), lineItem.getExtendedPrice());
DOUBLE.writeDouble(pageBuilder.getBlockBuilder(DISCOUNT), lineItem.getDiscount());
DATE.writeLong(pageBuilder.getBlockBuilder(SHIP_DATE), lineItem.getShipDate());
DOUBLE.writeDouble(pageBuilder.getBlockBuilder(QUANTITY), lineItem.getQuantity());
if (pageBuilder.getPositionCount() >= positionsPerPage) {
pages.add(pageBuilder.build());
pageBuilder.reset();
}
}
if (!pageBuilder.isEmpty()) {
pages.add(pageBuilder.build());
}
return pages;
}
use of com.facebook.presto.common.PageBuilder in project presto by prestodb.
the class PinotBrokerPageSourcePql method buildBlockAndTypeBuilder.
@Override
@VisibleForTesting
public BlockAndTypeBuilder buildBlockAndTypeBuilder(List<PinotColumnHandle> columnHandles, PinotQueryGenerator.GeneratedPinotQuery brokerPql) {
// When we created the PQL, we came up with some column handles
// however other optimizers post-pushdown can come in and prune/re-order the required column handles
// so we need to map from the column handles the PQL corresponds to, to the actual column handles
// needed in the scan.
List<Type> expectedTypes = columnHandles.stream().map(PinotColumnHandle::getDataType).collect(Collectors.toList());
PageBuilder pageBuilder = new PageBuilder(expectedTypes);
checkState(brokerPql.getExpectedColumnIndices().size() >= expectedHandles.size());
checkState(expectedHandles.size() >= columnHandles.size());
// The expectedColumnHandles are the handles corresponding to the generated PQL
// However, the engine could end up requesting only a permutation/subset of those handles
// during the actual scan
// Map the handles from planning time to the handles asked in the scan
// so that we know which columns to discard.
int[] handleMapping = new int[expectedHandles.size()];
for (int i = 0; i < handleMapping.length; ++i) {
handleMapping[i] = columnHandles.indexOf(expectedHandles.get(i));
}
ArrayList<BlockBuilder> columnBlockBuilders = new ArrayList<>();
ArrayList<Type> columnTypes = new ArrayList<>();
for (int expectedColumnIndex : brokerPql.getExpectedColumnIndices()) {
// columnIndex is the index of this column in the current scan
// It is obtained from the mapping and can be -ve, which means that the
// expectedColumnIndex'th column returned by Pinot can be discarded.
int columnIndex = -1;
if (expectedColumnIndex >= 0) {
columnIndex = handleMapping[expectedColumnIndex];
}
columnBlockBuilders.add(columnIndex >= 0 ? pageBuilder.getBlockBuilder(columnIndex) : null);
columnTypes.add(columnIndex >= 0 ? expectedTypes.get(columnIndex) : null);
}
return new BlockAndTypeBuilder(pageBuilder, columnBlockBuilders, columnTypes);
}
Aggregations