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

The following examples show how to use io.prestosql.spi.block.BlockBuilder. 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: MLFeaturesFunctions.java    License: Apache License 2.0 6 votes vote down vote up
private static Block featuresHelper(PageBuilder pageBuilder, double... features)
{
    if (pageBuilder.isFull()) {
        pageBuilder.reset();
    }

    BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();

    for (int i = 0; i < features.length; i++) {
        BigintType.BIGINT.writeLong(blockBuilder, i);
        DoubleType.DOUBLE.writeDouble(blockBuilder, features[i]);
    }

    mapBlockBuilder.closeEntry();
    pageBuilder.declarePosition();
    return mapBlockBuilder.getObject(mapBlockBuilder.getPositionCount() - 1, Block.class);
}
 
Example 2
Source Project: presto   Source File: MongoPageSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Page getNextPage()
{
    verify(pageBuilder.isEmpty());
    count = 0;
    for (int i = 0; i < ROWS_PER_REQUEST; i++) {
        if (!cursor.hasNext()) {
            finished = true;
            break;
        }
        currentDoc = cursor.next();
        count++;

        pageBuilder.declarePosition();
        for (int column = 0; column < columnTypes.size(); column++) {
            BlockBuilder output = pageBuilder.getBlockBuilder(column);
            appendTo(columnTypes.get(column), currentDoc.get(columnNames.get(column)), output);
        }
    }

    Page page = pageBuilder.build();
    pageBuilder.reset();
    return page;
}
 
Example 3
Source Project: presto   Source File: TestArrayMaxNAggregation.java    License: Apache License 2.0 6 votes vote down vote up
public static Block createLongArraysBlock(Long[] values)
{
    ArrayType arrayType = new ArrayType(BIGINT);
    BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, values.length);
    for (Long value : values) {
        if (value == null) {
            blockBuilder.appendNull();
        }
        else {
            BlockBuilder elementBlockBuilder = blockBuilder.beginBlockEntry();
            BIGINT.writeLong(elementBlockBuilder, value);
            blockBuilder.closeEntry();
        }
    }
    return blockBuilder.build();
}
 
Example 4
Source Project: presto   Source File: TestMapAggAggregation.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDoubleRowMap()
{
    RowType innerRowType = RowType.from(ImmutableList.of(
            RowType.field("f1", INTEGER),
            RowType.field("f2", DOUBLE)));
    InternalAggregationFunction aggFunc = metadata.getAggregateFunctionImplementation(metadata.resolveFunction(QualifiedName.of(NAME), fromTypes(DOUBLE, innerRowType)));

    BlockBuilder builder = innerRowType.createBlockBuilder(null, 3);
    innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 1L, 1.0));
    innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 2L, 2.0));
    innerRowType.writeObject(builder, toRow(ImmutableList.of(INTEGER, DOUBLE), 3L, 3.0));

    assertAggregation(
            aggFunc,
            ImmutableMap.of(1.0, ImmutableList.of(1, 1.0),
                    2.0, ImmutableList.of(2, 2.0),
                    3.0, ImmutableList.of(3, 3.0)),
            createDoublesBlock(1.0, 2.0, 3.0),
            builder.build());
}
 
Example 5
Source Project: presto   Source File: GeoFunctions.java    License: Apache License 2.0 6 votes vote down vote up
@SqlNullable
@Description("Returns the lower left and upper right corners of bounding rectangular polygon of a Geometry")
@ScalarFunction("ST_EnvelopeAsPts")
@SqlType("array(" + GEOMETRY_TYPE_NAME + ")")
public static Block stEnvelopeAsPts(@SqlType(GEOMETRY_TYPE_NAME) Slice input)
{
    Envelope envelope = deserializeEnvelope(input);
    if (envelope.isEmpty()) {
        return null;
    }
    BlockBuilder blockBuilder = GEOMETRY.createBlockBuilder(null, 2);
    Point lowerLeftCorner = new Point(envelope.getXMin(), envelope.getYMin());
    Point upperRightCorner = new Point(envelope.getXMax(), envelope.getYMax());
    GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(lowerLeftCorner, null, false)));
    GEOMETRY.writeSlice(blockBuilder, serialize(createFromEsriGeometry(upperRightCorner, null, false)));
    return blockBuilder.build();
}
 
Example 6
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 7
public static void output(ArrayType outputType, MinMaxByNState state, BlockBuilder out)
{
    TypedKeyValueHeap heap = state.getTypedKeyValueHeap();
    if (heap == null || heap.isEmpty()) {
        out.appendNull();
        return;
    }

    Type elementType = outputType.getElementType();

    BlockBuilder arrayBlockBuilder = out.beginBlockEntry();
    BlockBuilder reversedBlockBuilder = elementType.createBlockBuilder(null, heap.getCapacity());
    long startSize = heap.getEstimatedSize();
    heap.popAll(reversedBlockBuilder);
    state.addMemoryUsage(heap.getEstimatedSize() - startSize);

    for (int i = reversedBlockBuilder.getPositionCount() - 1; i >= 0; i--) {
        elementType.appendTo(reversedBlockBuilder, i, arrayBlockBuilder);
    }
    out.closeEntry();
}
 
Example 8
Source Project: presto   Source File: BlockEncoding.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public final Block decodeColumn(ColumnData columnData)
        throws RcFileCorruptionException
{
    int size = columnData.rowCount();

    Slice slice = columnData.getSlice();
    BlockBuilder builder = type.createBlockBuilder(null, size);
    for (int i = 0; i < size; i++) {
        int length = columnData.getLength(i);
        int offset = columnData.getOffset(i);
        if (!isNullSequence(slice, offset, length)) {
            decodeValueInto(1, builder, slice, offset, length);
        }
        else {
            builder.appendNull();
        }
    }

    return builder.build();
}
 
Example 9
Source Project: presto   Source File: SingleTypedHistogram.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void serialize(BlockBuilder out)
{
    if (values.getPositionCount() == 0) {
        out.appendNull();
    }
    else {
        Block valuesBlock = values.build();
        BlockBuilder blockBuilder = out.beginBlockEntry();
        for (int i = 0; i < valuesBlock.getPositionCount(); i++) {
            type.appendTo(valuesBlock, i, blockBuilder);
            BIGINT.writeLong(blockBuilder, counts.get(i));
        }
        out.closeEntry();
    }
}
 
Example 10
Source Project: presto   Source File: TypedKeyValueHeap.java    License: Apache License 2.0 6 votes vote down vote up
public void serialize(BlockBuilder out)
{
    BlockBuilder blockBuilder = out.beginBlockEntry();
    BIGINT.writeLong(blockBuilder, getCapacity());

    BlockBuilder keyElements = blockBuilder.beginBlockEntry();
    for (int i = 0; i < positionCount; i++) {
        keyType.appendTo(keyBlockBuilder, heapIndex[i], keyElements);
    }
    blockBuilder.closeEntry();

    BlockBuilder valueElements = blockBuilder.beginBlockEntry();
    for (int i = 0; i < positionCount; i++) {
        valueType.appendTo(valueBlockBuilder, heapIndex[i], valueElements);
    }
    blockBuilder.closeEntry();

    out.closeEntry();
}
 
Example 11
Source Project: presto   Source File: TestArrayBlock.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testWithArrayBlock()
{
    long[][][] expectedValues = createExpectedValues();

    BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);

    assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
    assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
    assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
    assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);

    long[][][] expectedValuesWithNull = alternatingNullValues(expectedValues);
    BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
    assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
    assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
    assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
    assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
}
 
Example 12
Source Project: presto   Source File: TestShortArrayBlock.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLazyBlockBuilderInitialization()
{
    Slice[] expectedValues = createTestValue(100);
    BlockBuilder emptyBlockBuilder = new ShortArrayBlockBuilder(null, 0);

    BlockBuilder blockBuilder = new ShortArrayBlockBuilder(null, expectedValues.length);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());

    writeValues(expectedValues, blockBuilder);
    assertTrue(blockBuilder.getSizeInBytes() > emptyBlockBuilder.getSizeInBytes());
    assertTrue(blockBuilder.getRetainedSizeInBytes() > emptyBlockBuilder.getRetainedSizeInBytes());

    blockBuilder = blockBuilder.newBlockBuilderLike(null);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
}
 
Example 13
Source Project: presto   Source File: TestInt128ArrayBlock.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLazyBlockBuilderInitialization()
{
    Slice[] expectedValues = createTestValue(100);
    BlockBuilder emptyBlockBuilder = new VariableWidthBlockBuilder(null, 0, 0);

    BlockBuilder blockBuilder = new VariableWidthBlockBuilder(null, expectedValues.length, 32 * expectedValues.length);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());

    writeValues(expectedValues, blockBuilder);
    assertTrue(blockBuilder.getSizeInBytes() > emptyBlockBuilder.getSizeInBytes());
    assertTrue(blockBuilder.getRetainedSizeInBytes() > emptyBlockBuilder.getRetainedSizeInBytes());

    blockBuilder = blockBuilder.newBlockBuilderLike(null);
    assertEquals(blockBuilder.getSizeInBytes(), emptyBlockBuilder.getSizeInBytes());
    assertEquals(blockBuilder.getRetainedSizeInBytes(), emptyBlockBuilder.getRetainedSizeInBytes());
}
 
Example 14
Source Project: presto   Source File: TinyintDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output)
{
    Object value = getter.get();
    if (value == null) {
        output.appendNull();
    }
    else if (value instanceof Number) {
        long decoded = ((Number) value).longValue();

        if (decoded < Byte.MIN_VALUE || decoded > Byte.MAX_VALUE) {
            throw new PrestoException(TYPE_MISMATCH, format("Value out of range for field '%s' of type TINYINT: %s", path, decoded));
        }

        TINYINT.writeLong(output, decoded);
    }
    else {
        throw new PrestoException(TYPE_MISMATCH, format("Expected a numeric value for field '%s' of type TINYINT: %s [%s]", path, value, value.getClass().getSimpleName()));
    }
}
 
Example 15
Source Project: presto   Source File: TypeUtils.java    License: Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 16
Source Project: presto   Source File: TestTimestampWithTimeZoneType.java    License: Apache License 2.0 6 votes vote down vote up
public static Block createTestBlock()
{
    BlockBuilder blockBuilder = TIMESTAMP_WITH_TIME_ZONE.createBlockBuilder(null, 15);
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(1111, getTimeZoneKeyForOffset(0)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(1111, getTimeZoneKeyForOffset(1)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(1111, getTimeZoneKeyForOffset(2)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(2222, getTimeZoneKeyForOffset(3)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(2222, getTimeZoneKeyForOffset(4)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(2222, getTimeZoneKeyForOffset(5)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(2222, getTimeZoneKeyForOffset(6)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(2222, getTimeZoneKeyForOffset(7)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(3333, getTimeZoneKeyForOffset(8)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(3333, getTimeZoneKeyForOffset(9)));
    TIMESTAMP_WITH_TIME_ZONE.writeLong(blockBuilder, packDateTimeWithZone(4444, getTimeZoneKeyForOffset(10)));
    return blockBuilder.build();
}
 
Example 17
Source Project: presto   Source File: TimestampMicrosColumnReader.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void readValue(BlockBuilder blockBuilder, Type type)
{
    if (definitionLevel == columnDescriptor.getMaxDefinitionLevel()) {
        long utcMillis = MICROSECONDS.toMillis(valuesReader.readLong());
        // TODO: specialize the class at creation time
        if (type instanceof TimestampWithTimeZoneType) {
            type.writeLong(blockBuilder, packDateTimeWithZone(utcMillis, UTC_KEY));
        }
        else {
            type.writeLong(blockBuilder, utcMillis);
        }
    }
    else if (isValueNull()) {
        blockBuilder.appendNull();
    }
}
 
Example 18
Source Project: presto   Source File: FirstValueFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void processRow(BlockBuilder output, int frameStart, int frameEnd, int currentPosition)
{
    if (frameStart < 0) {
        output.appendNull();
        return;
    }

    int valuePosition = frameStart;

    if (ignoreNulls) {
        while (valuePosition >= 0 && valuePosition <= frameEnd) {
            if (!windowIndex.isNull(argumentChannel, valuePosition)) {
                break;
            }

            valuePosition++;
        }

        if (valuePosition > frameEnd) {
            output.appendNull();
            return;
        }
    }

    windowIndex.appendTo(argumentChannel, valuePosition, output);
}
 
Example 19
Source Project: presto   Source File: TestUuidOperators.java    License: Apache License 2.0 5 votes vote down vote up
private static long hashFromType(String uuidString)
{
    BlockBuilder blockBuilder = UUID.createBlockBuilder(null, 1);
    UUID.writeSlice(blockBuilder, castFromVarcharToUuid(utf8Slice(uuidString)));
    Block block = blockBuilder.build();
    return UUID.hash(block, 0);
}
 
Example 20
Source Project: presto   Source File: TinyintType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeLong(BlockBuilder blockBuilder, long value)
{
    if (value > Byte.MAX_VALUE) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Value %d exceeds MAX_BYTE", value));
    }
    if (value < Byte.MIN_VALUE) {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, format("Value %d is less than MIN_BYTE", value));
    }

    blockBuilder.writeByte((int) value).closeEntry();
}
 
Example 21
Source Project: presto   Source File: StringEncoding.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void decodeValueInto(BlockBuilder builder, Slice slice, int offset, int length)
{
    // Note strings nested in complex structures do no use the empty string marker
    length = calculateTruncationLength(type, slice, offset, length);
    type.writeSlice(builder, slice, offset, length);
}
 
Example 22
Source Project: presto   Source File: TestDoubleSkewnessAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        DOUBLE.writeDouble(blockBuilder, (double) i);
    }
    return new Block[] {blockBuilder.build()};
}
 
Example 23
Source Project: presto   Source File: BlockAssertions.java    License: Apache License 2.0 5 votes vote down vote up
public static Block createDateSequenceBlock(int start, int end)
{
    BlockBuilder builder = DATE.createFixedSizeBlockBuilder(end - start);

    for (int i = start; i < end; i++) {
        DATE.writeLong(builder, i);
    }

    return builder.build();
}
 
Example 24
Source Project: presto   Source File: TypeCoercer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Block apply(Block block)
{
    BlockBuilder blockBuilder = toType.createBlockBuilder(null, block.getPositionCount());
    for (int i = 0; i < block.getPositionCount(); i++) {
        if (block.isNull(i)) {
            blockBuilder.appendNull();
            continue;
        }
        applyCoercedValue(blockBuilder, block, i);
    }
    return blockBuilder.build();
}
 
Example 25
Source Project: presto   Source File: TestOrcDeletedRows.java    License: Apache License 2.0 5 votes vote down vote up
private Page createTestPage(int originalTransactionStart, int originalTransactionEnd)
{
    int size = originalTransactionEnd - originalTransactionStart;
    BlockBuilder originalTransaction = BIGINT.createFixedSizeBlockBuilder(size);
    for (long i = originalTransactionStart; i < originalTransactionEnd; i++) {
        originalTransaction.writeLong(i);
    }

    return new Page(
            size,
            originalTransaction.build(),
            new RunLengthEncodedBlock(bucketBlock, size),
            new RunLengthEncodedBlock(rowIdBlock, size));
}
 
Example 26
Source Project: presto   Source File: BooleanDecoder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void decode(SearchHit hit, Supplier<Object> getter, BlockBuilder output)
{
    Object value = getter.get();
    if (value == null) {
        output.appendNull();
    }
    else if (value instanceof Boolean) {
        BOOLEAN.writeBoolean(output, (Boolean) value);
    }
    else {
        throw new PrestoException(TYPE_MISMATCH, format("Expected a boolean value for field %s of type BOOLEAN: %s [%s]", path, value, value.getClass().getSimpleName()));
    }
}
 
Example 27
Source Project: presto   Source File: StructEncoding.java    License: Apache License 2.0 5 votes vote down vote up
private void decodeElementValueInto(int depth, int fieldIndex, BlockBuilder builder, Slice slice, int offset, int length)
        throws RcFileCorruptionException
{
    // ignore extra fields
    if (fieldIndex > structFields.size()) {
        return;
    }

    if (isNullSequence(slice, offset, length)) {
        builder.appendNull();
    }
    else {
        structFields.get(fieldIndex).decodeValueInto(depth + 1, builder, slice, offset, length);
    }
}
 
Example 28
Source Project: presto   Source File: UuidType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void appendTo(Block block, int position, BlockBuilder blockBuilder)
{
    if (block.isNull(position)) {
        blockBuilder.appendNull();
    }
    else {
        blockBuilder.writeLong(block.getLong(position, 0));
        blockBuilder.writeLong(block.getLong(position, SIZE_OF_LONG));
        blockBuilder.closeEntry();
    }
}
 
Example 29
Source Project: presto   Source File: TestShortDecimalMinAggregation.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = SHORT_DECIMAL.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        SHORT_DECIMAL.writeLong(blockBuilder, i);
    }
    return new Block[] {blockBuilder.build()};
}
 
Example 30
Source Project: presto   Source File: BlockAssertions.java    License: Apache License 2.0 5 votes vote down vote up
public static Block createIntsBlock(Iterable<Integer> values)
{
    BlockBuilder builder = INTEGER.createBlockBuilder(null, 100);

    for (Integer value : values) {
        if (value == null) {
            builder.appendNull();
        }
        else {
            INTEGER.writeLong(builder, value);
        }
    }

    return builder.build();
}