Java Code Examples for io.prestosql.spi.block.BlockBuilder#build()

The following examples show how to use io.prestosql.spi.block.BlockBuilder#build() . 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 File: TestIntervalDayTimeType.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Block createTestBlock()
{
    BlockBuilder blockBuilder = INTERVAL_DAY_TIME.createBlockBuilder(null, 15);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 1111);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 1111);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 1111);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 2222);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 2222);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 2222);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 2222);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 2222);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 3333);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 3333);
    INTERVAL_DAY_TIME.writeLong(blockBuilder, 4444);
    return blockBuilder.build();
}
 
Example 2
Source File: TestTypedHistogram.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testMassive()
{
    BlockBuilder inputBlockBuilder = BIGINT.createBlockBuilder(null, 5000);

    TypedHistogram typedHistogram = new SingleTypedHistogram(BIGINT, 1000);
    IntStream.range(1, 2000)
            .flatMap(i -> IntStream.iterate(i, IntUnaryOperator.identity()).limit(i))
            .forEach(j -> BIGINT.writeLong(inputBlockBuilder, j));

    Block inputBlock = inputBlockBuilder.build();
    for (int i = 0; i < inputBlock.getPositionCount(); i++) {
        typedHistogram.add(i, inputBlock, 1);
    }

    MapType mapType = mapType(BIGINT, BIGINT);
    BlockBuilder out = mapType.createBlockBuilder(null, 1);
    typedHistogram.serialize(out);
    Block outputBlock = mapType.getObject(out, 0);
    for (int i = 0; i < outputBlock.getPositionCount(); i += 2) {
        assertEquals(BIGINT.getLong(outputBlock, i + 1), BIGINT.getLong(outputBlock, i));
    }
}
 
Example 3
Source File: TestBigintType.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Block createTestBlock()
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, 15);
    BIGINT.writeLong(blockBuilder, 1111);
    BIGINT.writeLong(blockBuilder, 1111);
    BIGINT.writeLong(blockBuilder, 1111);
    BIGINT.writeLong(blockBuilder, 2222);
    BIGINT.writeLong(blockBuilder, 2222);
    BIGINT.writeLong(blockBuilder, 2222);
    BIGINT.writeLong(blockBuilder, 2222);
    BIGINT.writeLong(blockBuilder, 2222);
    BIGINT.writeLong(blockBuilder, 3333);
    BIGINT.writeLong(blockBuilder, 3333);
    BIGINT.writeLong(blockBuilder, 4444);
    return blockBuilder.build();
}
 
Example 4
Source File: TestTypedHeap.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void test(IntStream inputStream, BlockComparator comparator, PrimitiveIterator.OfInt outputIterator)
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, INPUT_SIZE);
    inputStream.forEach(x -> BIGINT.writeLong(blockBuilder, x));

    TypedHeap heap = new TypedHeap(comparator, BIGINT, OUTPUT_SIZE);
    heap.addAll(blockBuilder);

    BlockBuilder resultBlockBuilder = BIGINT.createBlockBuilder(null, OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);

    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(BIGINT.getLong(resultBlock, i), outputIterator.nextInt());
    }
}
 
Example 5
Source File: TestTypedKeyValueHeap.java    From presto with Apache License 2.0 6 votes vote down vote up
private static void test(IntStream keyInputStream, Stream<String> valueInputStream, BlockComparator comparator, Iterator<String> outputIterator)
{
    BlockBuilder keysBlockBuilder = BIGINT.createBlockBuilder(null, INPUT_SIZE);
    BlockBuilder valuesBlockBuilder = VARCHAR.createBlockBuilder(null, INPUT_SIZE);
    keyInputStream.forEach(x -> BIGINT.writeLong(keysBlockBuilder, x));
    valueInputStream.forEach(x -> VARCHAR.writeString(valuesBlockBuilder, x));

    TypedKeyValueHeap heap = new TypedKeyValueHeap(comparator, BIGINT, VARCHAR, OUTPUT_SIZE);
    heap.addAll(keysBlockBuilder, valuesBlockBuilder);

    BlockBuilder resultBlockBuilder = VARCHAR.createBlockBuilder(null, OUTPUT_SIZE);
    heap.popAll(resultBlockBuilder);

    Block resultBlock = resultBlockBuilder.build();
    assertEquals(resultBlock.getPositionCount(), OUTPUT_SIZE);
    for (int i = 0; i < OUTPUT_SIZE; i++) {
        assertEquals(VARCHAR.getSlice(resultBlock, i).toStringUtf8(), outputIterator.next());
    }
}
 
Example 6
Source File: AbstractTestType.java    From presto with Apache License 2.0 6 votes vote down vote up
private Block toBlock(Object value)
{
    BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
    Class<?> javaType = type.getJavaType();
    if (value == null) {
        blockBuilder.appendNull();
    }
    else if (javaType == boolean.class) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    }
    else if (javaType == long.class) {
        type.writeLong(blockBuilder, (Long) value);
    }
    else if (javaType == double.class) {
        type.writeDouble(blockBuilder, (Double) value);
    }
    else if (javaType == Slice.class) {
        Slice slice = (Slice) value;
        type.writeSlice(blockBuilder, slice, 0, slice.length());
    }
    else {
        type.writeObject(blockBuilder, value);
    }
    return blockBuilder.build();
}
 
Example 7
Source File: RepeatFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
@TypeParameter("T")
@SqlType("array(T)")
public static Block repeat(
        @TypeParameter("T") Type type,
        @SqlNullable @SqlType("T") Object element,
        @SqlType(StandardTypes.INTEGER) long count)
{
    BlockBuilder blockBuilder = createBlockBuilder(type, count);
    if (element == null) {
        return repeatNullValues(blockBuilder, count);
    }
    if (count > 0) {
        type.writeObject(blockBuilder, element);
        checkMaxSize(blockBuilder.getSizeInBytes(), count);
    }
    for (int i = 1; i < count; i++) {
        type.writeObject(blockBuilder, element);
    }
    return blockBuilder.build();
}
 
Example 8
Source File: TestSpillCipherPagesSerde.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void test()
{
    SpillCipher cipher = new AesSpillCipher();
    PagesSerde serde = new TestingPagesSerdeFactory().createPagesSerdeForSpill(Optional.of(cipher));
    List<Type> types = ImmutableList.of(VARCHAR);
    Page emptyPage = new Page(VARCHAR.createBlockBuilder(null, 0).build());
    assertPageEquals(types, serde.deserialize(serde.serialize(emptyPage)), emptyPage);

    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 2);
    VARCHAR.writeString(blockBuilder, "hello");
    VARCHAR.writeString(blockBuilder, "world");
    Page helloWorldPage = new Page(blockBuilder.build());

    SerializedPage serialized = serde.serialize(helloWorldPage);
    assertPageEquals(types, serde.deserialize(serialized), helloWorldPage);
    assertTrue(serialized.isEncrypted(), "page should be encrypted");

    cipher.close();

    assertFailure(() -> serde.serialize(helloWorldPage), "Spill cipher already closed");
    assertFailure(() -> serde.deserialize(serialized), "Spill cipher already closed");
}
 
Example 9
Source File: TestStateCompiler.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testNonPrimitiveSerialization()
{
    AccumulatorStateFactory<SliceState> factory = StateCompiler.generateStateFactory(SliceState.class);
    AccumulatorStateSerializer<SliceState> serializer = StateCompiler.generateStateSerializer(SliceState.class);
    SliceState state = factory.createSingleState();
    SliceState deserializedState = factory.createSingleState();

    state.setSlice(null);
    BlockBuilder nullBlockBuilder = VARCHAR.createBlockBuilder(null, 1);
    serializer.serialize(state, nullBlockBuilder);
    Block nullBlock = nullBlockBuilder.build();
    serializer.deserialize(nullBlock, 0, deserializedState);
    assertEquals(deserializedState.getSlice(), state.getSlice());

    state.setSlice(utf8Slice("test"));
    BlockBuilder builder = VARCHAR.createBlockBuilder(null, 1);
    serializer.serialize(state, builder);
    Block block = builder.build();
    serializer.deserialize(block, 0, deserializedState);
    assertEquals(deserializedState.getSlice(), state.getSlice());
}
 
Example 10
Source File: TestStateCompiler.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrimitiveBooleanSerialization()
{
    AccumulatorStateFactory<BooleanState> factory = StateCompiler.generateStateFactory(BooleanState.class);
    AccumulatorStateSerializer<BooleanState> serializer = StateCompiler.generateStateSerializer(BooleanState.class);
    BooleanState state = factory.createSingleState();
    BooleanState deserializedState = factory.createSingleState();

    state.setBoolean(true);

    BlockBuilder builder = BOOLEAN.createBlockBuilder(null, 1);
    serializer.serialize(state, builder);

    Block block = builder.build();
    serializer.deserialize(block, 0, deserializedState);
    assertEquals(deserializedState.isBoolean(), state.isBoolean());
}
 
Example 11
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block prependElement(Type elementType, Object value, Block block)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);

    elementType.writeObject(blockBuilder, value);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    return blockBuilder.build();
}
 
Example 12
Source File: JoniRegexpFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Description("Group(s) extracted using the given pattern")
@ScalarFunction
@LiteralParameters("x")
@SqlType("array(varchar(x))")
public static Block regexpExtractAll(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern, @SqlType(StandardTypes.BIGINT) long groupIndex)
{
    Matcher matcher = pattern.matcher(source.getBytes());
    validateGroup(groupIndex, matcher.getEagerRegion());
    BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
    int group = toIntExact(groupIndex);

    int nextStart = 0;
    while (true) {
        int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
        if (offset == -1) {
            break;
        }
        nextStart = getNextStart(source, matcher);
        Region region = matcher.getEagerRegion();
        int beg = region.beg[group];
        int end = region.end[group];
        if (beg == -1 || end == -1) {
            blockBuilder.appendNull();
        }
        else {
            Slice slice = source.slice(beg, end - beg);
            VARCHAR.writeSlice(blockBuilder, slice);
        }
    }
    return blockBuilder.build();
}
 
Example 13
Source File: MapKeys.java    From presto with Apache License 2.0 5 votes vote down vote up
@TypeParameter("K")
@TypeParameter("V")
@SqlType("array(K)")
public static Block getKeys(
        @TypeParameter("K") Type keyType,
        @SqlType("map(K,V)") Block block)
{
    BlockBuilder blockBuilder = keyType.createBlockBuilder(null, block.getPositionCount() / 2);
    for (int i = 0; i < block.getPositionCount(); i += 2) {
        keyType.appendTo(block, i, blockBuilder);
    }
    return blockBuilder.build();
}
 
Example 14
Source File: TestLongKurtosisAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        BIGINT.writeLong(blockBuilder, i);
    }
    return new Block[] {blockBuilder.build()};
}
 
Example 15
Source File: AssignUniqueIdOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
private Block generateIdColumn(int positionCount)
{
    BlockBuilder block = BIGINT.createFixedSizeBlockBuilder(positionCount);
    for (int currentPosition = 0; currentPosition < positionCount; currentPosition++) {
        if (rowIdCounter >= maxRowIdCounterValue) {
            requestValues();
        }
        long rowId = rowIdCounter++;
        verify((rowId & uniqueValueMask) == 0, "RowId and uniqueValue mask overlaps");
        BIGINT.writeLong(block, uniqueValueMask | rowId);
    }
    return block.build();
}
 
Example 16
Source File: TestLongAverageAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        BIGINT.writeLong(blockBuilder, i);
    }
    return new Block[] {blockBuilder.build()};
}
 
Example 17
Source File: TestTemporalFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testDateBlock()
{
    BlockBuilder blockBuilder = DATE.createBlockBuilder(null, 2);
    DATE.writeLong(blockBuilder, 13);
    DATE.writeLong(blockBuilder, 42);
    Block block = blockBuilder.build();

    // time zone is not used for dates
    TemporalFunction temporalFunction = new TemporalFunction(PST);
    assertEquals(temporalFunction.getDay(DATE, block, 0), 13);
    assertEquals(temporalFunction.getDay(DATE, block, 1), 42);
}
 
Example 18
Source File: TestingUnnesterUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Block createSimpleBlock(Slice[] values)
{
    BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, values.length);
    for (Slice v : values) {
        if (v == null) {
            elementBlockBuilder.appendNull();
        }
        else {
            VARCHAR.writeSlice(elementBlockBuilder, v);
        }
    }
    return elementBlockBuilder.build();
}
 
Example 19
Source File: SequenceFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block fixedWidthSequence(long start, long stop, long step, FixedWidthType type)
{
    checkValidStep(start, stop, step);

    int length = toIntExact((stop - start) / step + 1L);
    checkMaxEntry(length);

    BlockBuilder blockBuilder = type.createBlockBuilder(null, length);
    for (long i = 0, value = start; i < length; ++i, value += step) {
        type.writeLong(blockBuilder, value);
    }
    return blockBuilder.build();
}
 
Example 20
Source File: TestRealGeometricMeanAggregation.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
protected Block[] getSequenceBlocks(int start, int length)
{
    BlockBuilder blockBuilder = REAL.createBlockBuilder(null, length);
    for (int i = start; i < start + length; i++) {
        REAL.writeLong(blockBuilder, floatToRawIntBits((float) i));
    }
    return new Block[] {blockBuilder.build()};
}