Java Code Examples for io.prestosql.spi.block.PageBuilderStatus

The following examples show how to use io.prestosql.spi.block.PageBuilderStatus. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: presto   Source File: TestUnnestBlockBuilder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCapacityIncrease()
{
    Slice[] values = new Slice[100];
    for (int i = 0; i < values.length; i++) {
        values[i] = utf8Slice("a");
    }

    UnnestBlockBuilder unnestBlockBuilder = new UnnestBlockBuilder(VARCHAR);
    Block valuesBlock = createSimpleBlock(values);
    unnestBlockBuilder.resetInputBlock(valuesBlock);

    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 20);
    unnestBlockBuilder.appendRange(0, values.length);
    assertBlock(unnestBlockBuilder.buildOutputAndFlush(), values);

    unnestBlockBuilder.clearCurrentOutput();
}
 
Example 2
Source Project: presto   Source File: PageBuilder.java    License: Apache License 2.0 6 votes vote down vote up
private PageBuilder(int initialExpectedEntries, int maxPageBytes, List<? extends Type> types, Optional<BlockBuilder[]> templateBlockBuilders)
{
    this.types = List.copyOf(requireNonNull(types, "types is null"));

    pageBuilderStatus = new PageBuilderStatus(maxPageBytes);
    blockBuilders = new BlockBuilder[types.size()];

    if (templateBlockBuilders.isPresent()) {
        BlockBuilder[] templates = templateBlockBuilders.get();
        checkArgument(templates.length == types.size(), "Size of templates and types should match");
        for (int i = 0; i < blockBuilders.length; i++) {
            blockBuilders[i] = templates[i].newBlockBuilderLike(pageBuilderStatus.createBlockBuilderStatus());
        }
    }
    else {
        for (int i = 0; i < blockBuilders.length; i++) {
            blockBuilders[i] = types.get(i).createBlockBuilder(pageBuilderStatus.createBlockBuilderStatus(), initialExpectedEntries);
        }
    }
}
 
Example 3
Source Project: presto   Source File: AbstractVariableWidthType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }

    // it is guaranteed Math.min will not overflow; safe to cast
    int expectedBytes = (int) min((long) expectedEntries * expectedBytesPerEntry, maxBlockSizeInBytes);
    return new VariableWidthBlockBuilder(
            blockBuilderStatus,
            expectedBytesPerEntry == 0 ? expectedEntries : Math.min(expectedEntries, maxBlockSizeInBytes / expectedBytesPerEntry),
            expectedBytes);
}
 
Example 4
Source Project: presto-hbase-connector   Source File: HBaseRecordCursor.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object getObject(int field) {
    Object fieldValue;
    Type type;
    PageBuilderStatus pageBuilderStatus = new PageBuilderStatus(MAX_BLOCK_SIZE);
    type = this.getType(field);
    fieldValue = this.getFieldValue(field);
    if (fieldValue == null || "".equals(fieldValue)) {
        return this.getType(field).createBlockBuilder(pageBuilderStatus.createBlockBuilderStatus(), 0).build();
    }

    if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY)) {
        String[] values = ((String) fieldValue).split(Constant.ARRAY_STRING_SPLITTER);
        Type elementType = type.getTypeParameters().get(0);
        BlockBuilder builder = elementType.createBlockBuilder(null, values.length);
        for (String value : values) {
            if (value != null && !"".equals(value)) {
                TypeUtils.writeNativeValue(elementType, builder, value);
            }
        }
        return builder.build();
    } else {
        throw new UnsupportedOperationException("OOPS!UNSUPPORTED TYPE:" + type.getDisplayName());
    }
}
 
Example 5
Source Project: transport   Source File: PrestoStruct.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void setField(int index, StdData value) {
  // TODO: This is not the right way to get this object. The status should be passed in from the invocation of the
  // function and propagated to here. See PRESTO-1359 for more details.
  BlockBuilderStatus blockBuilderStatus = new PageBuilderStatus().createBlockBuilderStatus();
  BlockBuilder mutable = _rowType.createBlockBuilder(blockBuilderStatus, 1);
  BlockBuilder rowBlockBuilder = mutable.beginBlockEntry();
  int i = 0;
  for (RowType.Field field : _rowType.getFields()) {
    if (i == index) {
      ((PrestoData) value).writeToBlock(rowBlockBuilder);
    } else {
      if (_block == null) {
        rowBlockBuilder.appendNull();
      } else {
        field.getType().appendTo(_block, i, rowBlockBuilder);
      }
    }
    i++;
  }
  mutable.closeEntry();
  _block = _rowType.getObject(mutable.build(), 0);
}
 
Example 6
Source Project: transport   Source File: PrestoStruct.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void setField(String name, StdData value) {
  BlockBuilder mutable = _rowType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 1);
  BlockBuilder rowBlockBuilder = mutable.beginBlockEntry();
  int i = 0;
  for (RowType.Field field : _rowType.getFields()) {
    if (field.getName().isPresent() && name.equals(field.getName().get())) {
      ((PrestoData) value).writeToBlock(rowBlockBuilder);
    } else {
      if (_block == null) {
        rowBlockBuilder.appendNull();
      } else {
        field.getType().appendTo(_block, i, rowBlockBuilder);
      }
    }
    i++;
  }
  mutable.closeEntry();
  _block = _rowType.getObject(mutable.build(), 0);
}
 
Example 7
Source Project: transport   Source File: PrestoMap.java    License: BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void put(StdData key, StdData value) {
  BlockBuilder mutable = _mapType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 1);
  BlockBuilder entryBuilder = mutable.beginBlockEntry();
  Object prestoKey = ((PlatformData) key).getUnderlyingData();
  int valuePosition = seekKey(prestoKey);
  for (int i = 0; i < _block.getPositionCount(); i += 2) {
    // Write the current key to the map
    _keyType.appendTo(_block, i, entryBuilder);
    // Find out if we need to change the corresponding value
    if (i == valuePosition - 1) {
      // Use the user-supplied value
      ((PrestoData) value).writeToBlock(entryBuilder);
    } else {
      // Use the existing value in original _block
      _valueType.appendTo(_block, i + 1, entryBuilder);
    }
  }
  if (valuePosition == -1) {
    ((PrestoData) key).writeToBlock(entryBuilder);
    ((PrestoData) value).writeToBlock(entryBuilder);
  }

  mutable.closeEntry();
  _block = ((MapType) _mapType).getObject(mutable.build(), 0);
}
 
Example 8
Source Project: presto   Source File: Unnester.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepare for a new output page
 */
public final void startNewOutput(PageBuilderStatus status, int expectedEntries)
{
    for (int i = 0; i < getChannelCount(); i++) {
        unnestBlockBuilders[i].startNewOutput(status, expectedEntries);
    }
}
 
Example 9
Source Project: presto   Source File: UnnestBlockBuilder.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Prepares for a new output block.
 * The caller has to ensure that the source is not null when this method is invoked.
 */
public void startNewOutput(PageBuilderStatus pageBuilderStatus, int expectedEntries)
{
    checkState(source != null, "source is null");
    this.pageBuilderStatus = pageBuilderStatus;

    // Prepare for a new dictionary block
    this.ids = new int[expectedEntries];
    this.positionCount = 0;
    this.usingCopiedBlock = false;
}
 
Example 10
Source Project: presto   Source File: UnnestOperator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page getOutput()
{
    if (currentPage == null) {
        return null;
    }

    PageBuilderStatus pageBuilderStatus = new PageBuilderStatus(MAX_BYTES_PER_PAGE);
    prepareForNewOutput(pageBuilderStatus);

    int outputRowCount = 0;

    while (currentPosition < currentPage.getPositionCount()) {
        outputRowCount += processCurrentPosition();
        currentPosition++;

        if (outputRowCount >= MAX_ROWS_PER_BLOCK || pageBuilderStatus.isFull()) {
            break;
        }
    }

    Block[] outputBlocks = buildOutputBlocks();

    if (currentPosition == currentPage.getPositionCount()) {
        currentPage = null;
        currentPosition = 0;
    }

    return new Page(outputBlocks);
}
 
Example 11
Source Project: presto   Source File: UnnestOperator.java    License: Apache License 2.0 5 votes vote down vote up
private void prepareForNewOutput(PageBuilderStatus pageBuilderStatus)
{
    unnesters.forEach(unnester -> unnester.startNewOutput(pageBuilderStatus, estimatedMaxRowsPerBlock));
    replicatedBlockBuilders.forEach(replicatedBlockBuilder -> replicatedBlockBuilder.startNewOutput(estimatedMaxRowsPerBlock));

    if (withOrdinality) {
        ordinalityBlockBuilder = BIGINT.createBlockBuilder(pageBuilderStatus.createBlockBuilderStatus(), estimatedMaxRowsPerBlock);
    }
}
 
Example 12
Source Project: presto   Source File: ArrayCombinationsFunction.java    License: Apache License 2.0 5 votes vote down vote up
@TypeParameter("T")
@SqlType("array(array(T))")
public static Block combinations(
        @TypeParameter("T") Type elementType,
        @SqlType("array(T)") Block array,
        @SqlType(INTEGER) long n)
{
    int arrayLength = array.getPositionCount();
    int combinationLength = toIntExact(n);
    checkCondition(combinationLength >= 0, INVALID_FUNCTION_ARGUMENT, "combination size must not be negative: %s", combinationLength);
    checkCondition(combinationLength <= MAX_COMBINATION_LENGTH, INVALID_FUNCTION_ARGUMENT, "combination size must not exceed %s: %s", MAX_COMBINATION_LENGTH, combinationLength);

    ArrayType arrayType = new ArrayType(elementType);
    if (combinationLength > arrayLength) {
        return arrayType.createBlockBuilder(new PageBuilderStatus().createBlockBuilderStatus(), 0).build();
    }

    int combinationCount = combinationCount(arrayLength, combinationLength);
    checkCondition(combinationCount * (long) combinationLength <= MAX_RESULT_ELEMENTS, INVALID_FUNCTION_ARGUMENT, "combinations exceed max size");

    int[] ids = new int[combinationCount * combinationLength];
    int idsPosition = 0;

    int[] combination = firstCombination(arrayLength, combinationLength);
    do {
        arraycopy(combination, 0, ids, idsPosition, combinationLength);
        idsPosition += combinationLength;
    }
    while (nextCombination(combination, combinationLength));
    verify(idsPosition == ids.length, "idsPosition != ids.length, %s and %s respectively", idsPosition, ids.length);

    int[] offsets = new int[combinationCount + 1];
    setAll(offsets, i -> i * combinationLength);

    return ArrayBlock.fromElementBlock(combinationCount, Optional.empty(), offsets, new DictionaryBlock(array, ids));
}
 
Example 13
Source Project: presto   Source File: UuidType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new Int128ArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / getFixedSize()));
}
 
Example 14
Source Project: presto   Source File: IpAddressType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new Int128ArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / getFixedSize()));
}
 
Example 15
Source Project: presto   Source File: UnknownType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new ByteArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / getFixedSize()));
}
 
Example 16
Source Project: presto   Source File: TestArrayUnnester.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTrimmedBlocks()
{
    int[] unnestedLengths = {2, 1, 2, 3, 1};

    Slice[][][] elements = column(
            array(toSlices("0.0.0"), toSlices("0.1.0")),
            array(toSlices("1.0.0")),
            array(toSlices("2.0.0"), toSlices("2.1.0")),
            array(toSlices("3.0.0"), toSlices("3.1.0"), toSlices("3.2.0")),
            array(toSlices("4.0.0")));

    Slice[][] slices = getFieldElements(elements, 0);
    Block arrayBlock = createArrayBlock(slices);

    // Remove the first element from the arrayBlock for testing
    int startElement = 1;

    Slice[][] truncatedSlices = Arrays.copyOfRange(slices, startElement, slices.length - startElement + 1);
    int[] truncatedUnnestedLengths = Arrays.copyOfRange(unnestedLengths, startElement, slices.length - startElement + 1);
    Block truncatedBlock = arrayBlock.getRegion(startElement, truncatedSlices.length);
    assertBlock(truncatedBlock, truncatedSlices);

    Unnester arrayUnnester = new ArrayUnnester(VARCHAR);
    arrayUnnester.resetInput(truncatedBlock);

    arrayUnnester.startNewOutput(new PageBuilderStatus(), 20);

    // Process all input entries in the truncated block
    for (int i = 0; i < truncatedBlock.getPositionCount(); i++) {
        arrayUnnester.processCurrentAndAdvance(truncatedUnnestedLengths[i]);
    }

    Block[] output = arrayUnnester.buildOutputBlocksAndFlush();
    assertEquals(Arrays.asList(truncatedSlices).stream().mapToInt(slice -> slice.length).sum(), output[0].getPositionCount());

    Slice[] expectedOutput = computeExpectedUnnestedOutput(truncatedSlices, truncatedUnnestedLengths, 0, truncatedSlices.length);
    assertBlock(output[0], expectedOutput);
}
 
Example 17
Source Project: presto   Source File: TestMapUnnester.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTrimmedBlocks()
{
    int[] unnestedLengths = {1, 2, 1};

    Slice[][][] elements = column(
            array(toSlices("0.0.0", "0.0.1")),
            array(toSlices("1.0.0", "1.0.1"), toSlices("1.1.0", "1.1.1")),
            array(toSlices("2.0.0", "2.0.1")));

    Block mapBlock = createBlockBuilderWithValues(elements).build();

    // Remove the first element from the arrayBlock for testing
    int startElement = 1;

    Slice[][][] truncatedSlices = Arrays.copyOfRange(elements, startElement, elements.length - startElement + 1);
    int[] truncatedUnnestedLengths = Arrays.copyOfRange(unnestedLengths, startElement, elements.length - startElement + 1);
    Block truncatedBlock = mapBlock.getRegion(startElement, elements.length - startElement);
    assertBlock(truncatedBlock, truncatedSlices);

    Unnester mapUnnester = new MapUnnester(VARCHAR, VARCHAR);
    mapUnnester.resetInput(truncatedBlock);

    mapUnnester.startNewOutput(new PageBuilderStatus(), 20);

    // Process all input entries in the truncated block
    for (int i = 0; i < truncatedBlock.getPositionCount(); i++) {
        mapUnnester.processCurrentAndAdvance(truncatedUnnestedLengths[i]);
    }

    Block[] output = mapUnnester.buildOutputBlocksAndFlush();
    assertEquals(Arrays.asList(truncatedSlices).stream().mapToInt(slice -> slice.length).sum(), output[0].getPositionCount());

    Slice[] expectedOutput0 = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, 0), truncatedUnnestedLengths, 0, truncatedSlices.length);
    assertBlock(output[0], expectedOutput0);

    Slice[] expectedOutput1 = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, 1), truncatedUnnestedLengths, 0, truncatedSlices.length);
    assertBlock(output[1], expectedOutput1);
}
 
Example 18
Source Project: presto   Source File: TestArrayOfRowsUnnester.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTrimmedBlocks()
{
    int fieldCount = 3;
    int[] unnestedLengths = {1, 2, 1};

    Slice[][][] elements = column(
            array(toSlices("0.0.0", "0.0.1", "0.0.2")),
            array(toSlices("1.0.0", "1.0.1", "1.0.2"), toSlices("1.1.0", "1.1.1", "1.1.2")),
            array(toSlices("2.0.0", "2.0.1", "2.0.2")));

    RowType rowType = RowType.anonymous(Collections.nCopies(fieldCount, VARCHAR));
    Block arrayOfRowBlock = createArrayBlockOfRowBlocks(elements, rowType);

    // Remove the first element from the arrayBlock for testing
    int startElement = 1;

    Slice[][][] truncatedSlices = Arrays.copyOfRange(elements, startElement, elements.length - startElement + 1);
    int[] truncatedUnnestedLengths = Arrays.copyOfRange(unnestedLengths, startElement, elements.length - startElement + 1);
    Block truncatedBlock = arrayOfRowBlock.getRegion(startElement, truncatedSlices.length);

    Unnester arrayOfRowsUnnester = new ArrayOfRowsUnnester(rowType);
    arrayOfRowsUnnester.resetInput(truncatedBlock);

    arrayOfRowsUnnester.startNewOutput(new PageBuilderStatus(), 20);

    // Process all input entries in the truncated block
    for (int i = 0; i < truncatedBlock.getPositionCount(); i++) {
        arrayOfRowsUnnester.processCurrentAndAdvance(truncatedUnnestedLengths[i]);
    }

    Block[] output = arrayOfRowsUnnester.buildOutputBlocksAndFlush();
    assertEquals(Arrays.asList(truncatedSlices).stream().mapToInt(slice -> slice.length).sum(), output[0].getPositionCount());

    for (int i = 0; i < fieldCount; i++) {
        Slice[] expectedOutput = computeExpectedUnnestedOutput(getFieldElements(truncatedSlices, i), truncatedUnnestedLengths, 0, truncatedSlices.length);
        assertBlock(output[i], expectedOutput);
    }
}
 
Example 19
Source Project: presto   Source File: TestUnnestBlockBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyOutput()
{
    Slice[] values = toSlices(new String[] {"a", "b"});
    UnnestBlockBuilder unnestBlockBuilder = new UnnestBlockBuilder(VARCHAR);
    Block valuesBlock = createSimpleBlock(values);
    unnestBlockBuilder.resetInputBlock(valuesBlock);
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);

    // Nothing added to the output
    Block block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue(block instanceof DictionaryBlock);
    assertBlock(block, new Slice[] {});
}
 
Example 20
Source Project: presto   Source File: TestUnnestBlockBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static void testAppendSingleElement(UnnestBlockBuilder unnestBlockBuilder, Slice[] values)
{
    // Test unnestBlockBuilder output over the course of building output
    for (int testCount = 1; testCount <= values.length; testCount++) {
        unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);

        for (int i = 0; i < testCount; i++) {
            unnestBlockBuilder.appendElement(i);
        }

        Block block = unnestBlockBuilder.buildOutputAndFlush();
        assertTrue(block instanceof DictionaryBlock);
        assertBlock(block, Arrays.copyOf(values, testCount));
    }
}
 
Example 21
Source Project: presto   Source File: TestUnnestBlockBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private static void testAppendNull(UnnestBlockBuilder unnestBlockBuilder, Slice[] values)
{
    assertTrue(values.length >= 1, "values should have at least one element");

    int nullIndex = -1;
    for (int i = 0; i < values.length; i++) {
        if (values[i] == null) {
            nullIndex = i;
            break;
        }
    }

    // Test-1: appending a non-null element
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    Block block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue(block instanceof DictionaryBlock);
    assertBlock(block, new Slice[] {values[0]});

    // Test-2: appending a non-null element and a null.
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    unnestBlockBuilder.appendNull();
    block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue((block instanceof DictionaryBlock) || (nullIndex == -1));
    assertFalse((block instanceof DictionaryBlock) && (nullIndex == -1));
    assertBlock(block, new Slice[] {values[0], null});

    // Test-3: appending a non-null element, a null, and another non-null element.
    unnestBlockBuilder.startNewOutput(new PageBuilderStatus(), 10);
    unnestBlockBuilder.appendElement(0);
    unnestBlockBuilder.appendNull();
    unnestBlockBuilder.appendElement(0);
    block = unnestBlockBuilder.buildOutputAndFlush();
    assertTrue((block instanceof DictionaryBlock) || (nullIndex == -1));
    assertFalse((block instanceof DictionaryBlock) && (nullIndex == -1));
    assertBlock(block, new Slice[] {values[0], null, values[0]});
}
 
Example 22
Source Project: presto   Source File: BenchmarkPagesSort.java    License: Apache License 2.0 5 votes vote down vote up
private void createPages(int totalChannels, int pagesCount)
{
    int positionCount = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES / (totalChannels * 8);
    pages = new ArrayList<>(pagesCount);
    for (int numPage = 0; numPage < pagesCount; numPage++) {
        pages.add(createSequencePage(types, positionCount));
    }
    totalPositions = positionCount * pagesCount;
}
 
Example 23
Source Project: presto   Source File: BenchmarkPagesIndexPageSorter.java    License: Apache License 2.0 5 votes vote down vote up
private static List<Page> createPages(int pageCount, int channelCount, Type type)
{
    int positionCount = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES / (channelCount * 8);

    List<Page> pages = new ArrayList<>(pageCount);
    for (int numPage = 0; numPage < pageCount; numPage++) {
        Block[] blocks = new Block[channelCount];
        for (int numChannel = 0; numChannel < channelCount; numChannel++) {
            if (type.equals(BIGINT)) {
                blocks[numChannel] = BlockAssertions.createLongSequenceBlock(0, positionCount);
            }
            else if (type.equals(VARCHAR)) {
                blocks[numChannel] = BlockAssertions.createStringSequenceBlock(0, positionCount);
            }
            else if (type.equals(DOUBLE)) {
                blocks[numChannel] = BlockAssertions.createDoubleSequenceBlock(0, positionCount);
            }
            else if (type.equals(BOOLEAN)) {
                blocks[numChannel] = BlockAssertions.createBooleanSequenceBlock(0, positionCount);
            }
            else {
                throw new IllegalArgumentException("Unsupported type: " + type);
            }
        }
        pages.add(new Page(blocks));
    }
    return pages;
}
 
Example 24
Source Project: presto   Source File: ScanQueryPageSource.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Page getNextPage()
{
    long size = 0;
    while (size < PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES && iterator.hasNext()) {
        SearchHit hit = iterator.next();
        Map<String, Object> document = hit.getSourceAsMap();

        for (int i = 0; i < decoders.size(); i++) {
            String field = columns.get(i).getName();
            decoders.get(i).decode(hit, () -> getField(document, field), columnBuilders[i]);
        }

        if (hit.getSourceRef() != null) {
            totalBytes += hit.getSourceRef().length();
        }

        size = Arrays.stream(columnBuilders)
                .mapToLong(BlockBuilder::getSizeInBytes)
                .sum();
    }

    Block[] blocks = new Block[columnBuilders.length];
    for (int i = 0; i < columnBuilders.length; i++) {
        blocks[i] = columnBuilders[i].build();
        columnBuilders[i] = columnBuilders[i].newBlockBuilderLike(null);
    }

    return new Page(blocks);
}
 
Example 25
Source Project: presto   Source File: PageBuilder.java    License: Apache License 2.0 5 votes vote down vote up
public void reset()
{
    if (isEmpty()) {
        return;
    }
    pageBuilderStatus = new PageBuilderStatus(pageBuilderStatus.getMaxPageSizeInBytes());

    declaredPositions = 0;

    for (int i = 0; i < types.size(); i++) {
        blockBuilders[i] = blockBuilders[i].newBlockBuilderLike(pageBuilderStatus.createBlockBuilderStatus());
    }
}
 
Example 26
Source Project: presto   Source File: AbstractLongType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new LongArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / Long.BYTES));
}
 
Example 27
Source Project: presto   Source File: DoubleType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new LongArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / Double.BYTES));
}
 
Example 28
Source Project: presto   Source File: TinyintType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new ByteArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / Byte.BYTES));
}
 
Example 29
Source Project: presto   Source File: ShortTimestampWithTimeZoneType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public final BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new LongArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / Long.BYTES));
}
 
Example 30
Source Project: presto   Source File: LongTimestampType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public BlockBuilder createBlockBuilder(BlockBuilderStatus blockBuilderStatus, int expectedEntries, int expectedBytesPerEntry)
{
    int maxBlockSizeInBytes;
    if (blockBuilderStatus == null) {
        maxBlockSizeInBytes = PageBuilderStatus.DEFAULT_MAX_PAGE_SIZE_IN_BYTES;
    }
    else {
        maxBlockSizeInBytes = blockBuilderStatus.getMaxPageSizeInBytes();
    }
    return new Int96ArrayBlockBuilder(
            blockBuilderStatus,
            Math.min(expectedEntries, maxBlockSizeInBytes / getFixedSize()));
}