io.prestosql.spi.block.BlockBuilder Java Examples

The following examples show how to use io.prestosql.spi.block.BlockBuilder. 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: BlockEncoding.java    From presto with Apache License 2.0 7 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 #2
Source File: MLFeaturesFunctions.java    From presto with 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 #3
Source File: TestTimestampWithTimeZoneType.java    From presto with 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 #4
Source File: TimestampMicrosColumnReader.java    From presto with 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 #5
Source File: TestShortArrayBlock.java    From presto with 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 #6
Source File: TypeUtils.java    From presto with 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 #7
Source File: TinyintDecoder.java    From presto with 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 #8
Source File: TestArrayBlock.java    From presto with 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 #9
Source File: TestInt128ArrayBlock.java    From presto with 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 #10
Source File: AbstractMinMaxByNAggregationFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
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 #11
Source File: AbstractVariableWidthType.java    From presto with 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 #12
Source File: GeoFunctions.java    From presto with 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 #13
Source File: TestMapAggAggregation.java    From presto with 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 #14
Source File: TestArrayMaxNAggregation.java    From presto with 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 #15
Source File: MongoPageSource.java    From presto with 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 #16
Source File: SingleTypedHistogram.java    From presto with 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 #17
Source File: TypedKeyValueHeap.java    From presto with 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 #18
Source File: ArrayConcatUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, boolean value)
{
    BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        elementType.appendTo(block, i, blockBuilder);
    }

    elementType.writeBoolean(blockBuilder, value);

    return blockBuilder.build();
}
 
Example #19
Source File: TestVariableWidthBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyPositions()
{
    Slice[] expectedValues = alternatingNullValues(createExpectedValues(100));
    BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
    assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 2, 4, 6, 7, 9, 10, 16);
}
 
Example #20
Source File: ArrayAggregationFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
public static void output(Type elementType, ArrayAggregationState state, BlockBuilder out)
{
    if (state.isEmpty()) {
        out.appendNull();
    }
    else {
        BlockBuilder entryBuilder = out.beginBlockEntry();
        state.forEach((block, position) -> elementType.appendTo(block, position, entryBuilder));
        out.closeEntry();
    }
}
 
Example #21
Source File: ArrayAggregationStateSerializer.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(ArrayAggregationState state, BlockBuilder out)
{
    if (state.isEmpty()) {
        out.appendNull();
    }
    else {
        BlockBuilder entryBuilder = out.beginBlockEntry();
        state.forEach((block, position) -> elementType.appendTo(block, position, entryBuilder));
        out.closeEntry();
    }
}
 
Example #22
Source File: ArrayFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@ScalarFunction(hidden = true)
@SqlType("array(unknown)")
public static Block arrayConstructor()
{
    BlockBuilder blockBuilder = new ArrayType(UNKNOWN).createBlockBuilder(null, 0);
    return blockBuilder.build();
}
 
Example #23
Source File: MultimapAggregationFunction.java    From presto with Apache License 2.0 5 votes vote down vote up
public static void output(Type keyType, Type valueType, MultimapAggregationState state, BlockBuilder out)
{
    if (state.isEmpty()) {
        out.appendNull();
    }
    else {
        // TODO: Avoid copy value block associated with the same key by using strategy similar to multimap_from_entries
        ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
        valueArrayBlockBuilders.ensureCapacity(state.getEntryCount());
        BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(null, state.getEntryCount(), expectedValueSize(keyType, 100));
        TypedSet keySet = new TypedSet(keyType, state.getEntryCount(), MultimapAggregationFunction.NAME);

        state.forEach((key, value, keyValueIndex) -> {
            // Merge values of the same key into an array
            if (!keySet.contains(key, keyValueIndex)) {
                keySet.add(key, keyValueIndex);
                keyType.appendTo(key, keyValueIndex, distinctKeyBlockBuilder);
                BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(null, 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
                valueArrayBlockBuilders.set(keySet.positionOf(key, keyValueIndex), valueArrayBuilder);
            }
            valueType.appendTo(value, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(key, keyValueIndex)));
        });

        // Write keys and value arrays into one Block
        Type valueArrayType = new ArrayType(valueType);
        BlockBuilder multimapBlockBuilder = out.beginBlockEntry();
        for (int i = 0; i < distinctKeyBlockBuilder.getPositionCount(); i++) {
            keyType.appendTo(distinctKeyBlockBuilder, i, multimapBlockBuilder);
            valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
        }
        out.closeEntry();
    }
}
 
Example #24
Source File: TestByteArrayBlock.java    From presto with Apache License 2.0 5 votes vote down vote up
private static void writeValues(Slice[] expectedValues, BlockBuilder blockBuilder)
{
    for (Slice expectedValue : expectedValues) {
        if (expectedValue == null) {
            blockBuilder.appendNull();
        }
        else {
            blockBuilder.writeByte(expectedValue.getByte(0)).closeEntry();
        }
    }
}
 
Example #25
Source File: TestRowNumberOperator.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Block getRowNumberColumn(List<Page> pages)
{
    BlockBuilder builder = BIGINT.createBlockBuilder(null, pages.size() * 100);
    for (Page page : pages) {
        int rowNumberChannel = page.getChannelCount() - 1;
        for (int i = 0; i < page.getPositionCount(); i++) {
            BIGINT.writeLong(builder, page.getBlock(rowNumberChannel).getLong(i, 0));
        }
    }
    return builder.build();
}
 
Example #26
Source File: TestSphericalGeoFunctions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectValue()
{
    List<String> wktList = ImmutableList.of(
            "POINT EMPTY",
            "MULTIPOINT EMPTY",
            "LINESTRING EMPTY",
            "MULTILINESTRING EMPTY",
            "POLYGON EMPTY",
            "MULTIPOLYGON EMPTY",
            "GEOMETRYCOLLECTION EMPTY",
            "POINT (-40.2 28.9)",
            "MULTIPOINT ((-40.2 28.9), (-40.2 31.9))",
            "LINESTRING (-40.2 28.9, -40.2 31.9, -37.2 31.9)",
            "MULTILINESTRING ((-40.2 28.9, -40.2 31.9), (-40.2 31.9, -37.2 31.9))",
            "POLYGON ((-40.2 28.9, -37.2 28.9, -37.2 31.9, -40.2 31.9, -40.2 28.9))",
            "POLYGON ((-40.2 28.9, -37.2 28.9, -37.2 31.9, -40.2 31.9, -40.2 28.9), (-39.2 29.9, -39.2 30.9, -38.2 30.9, -38.2 29.9, -39.2 29.9))",
            "MULTIPOLYGON (((-40.2 28.9, -37.2 28.9, -37.2 31.9, -40.2 31.9, -40.2 28.9)), ((-39.2 29.9, -38.2 29.9, -38.2 30.9, -39.2 30.9, -39.2 29.9)))",
            "GEOMETRYCOLLECTION (POINT (-40.2 28.9), LINESTRING (-40.2 28.9, -40.2 31.9, -37.2 31.9), POLYGON ((-40.2 28.9, -37.2 28.9, -37.2 31.9, -40.2 31.9, -40.2 28.9)))");

    BlockBuilder builder = SPHERICAL_GEOGRAPHY.createBlockBuilder(null, wktList.size());
    for (String wkt : wktList) {
        SPHERICAL_GEOGRAPHY.writeSlice(builder, GeoFunctions.toSphericalGeography(GeoFunctions.stGeometryFromText(utf8Slice(wkt))));
    }
    Block block = builder.build();
    for (int i = 0; i < wktList.size(); i++) {
        assertEquals(wktList.get(i), SPHERICAL_GEOGRAPHY.getObjectValue(null, block, i));
    }
}
 
Example #27
Source File: BytecodeUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static BytecodeNode generateWrite(CallSiteBinder callSiteBinder, Scope scope, Variable wasNullVariable, Type type)
{
    Class<?> valueJavaType = type.getJavaType();
    if (!valueJavaType.isPrimitive() && valueJavaType != Slice.class) {
        valueJavaType = Object.class;
    }
    String methodName = "write" + Primitives.wrap(valueJavaType).getSimpleName();

    // the stack contains [output, value]

    // We should be able to insert the code to get the output variable and compute the value
    // at the right place instead of assuming they are in the stack. We should also not need to
    // use temp variables to re-shuffle the stack to the right shape before Type.writeXXX is called
    // Unfortunately, because of the assumptions made by try_cast, we can't get around it yet.
    // TODO: clean up once try_cast is fixed
    Variable tempValue = scope.createTempVariable(valueJavaType);
    Variable tempOutput = scope.createTempVariable(BlockBuilder.class);
    return new BytecodeBlock()
            .comment("if (wasNull)")
            .append(new IfStatement()
                    .condition(wasNullVariable)
                    .ifTrue(new BytecodeBlock()
                            .comment("output.appendNull();")
                            .pop(valueJavaType)
                            .invokeInterface(BlockBuilder.class, "appendNull", BlockBuilder.class)
                            .pop())
                    .ifFalse(new BytecodeBlock()
                            .comment("%s.%s(output, %s)", type.getTypeSignature(), methodName, valueJavaType.getSimpleName())
                            .putVariable(tempValue)
                            .putVariable(tempOutput)
                            .append(loadConstant(callSiteBinder.bind(type, Type.class)))
                            .getVariable(tempOutput)
                            .getVariable(tempValue)
                            .invokeInterface(Type.class, methodName, void.class, BlockBuilder.class, valueJavaType)));
}
 
Example #28
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()};
}
 
Example #29
Source File: GeometryType.java    From presto with 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 {
        block.writeBytesTo(position, 0, block.getSliceLength(position), blockBuilder);
        blockBuilder.closeEntry();
    }
}
 
Example #30
Source File: TypeUtils.java    From presto with Apache License 2.0 5 votes vote down vote up
public static Block jdbcObjectArrayToBlock(ConnectorSession session, Type type, Object[] elements)
{
    BlockBuilder builder = type.createBlockBuilder(null, elements.length);
    for (Object element : elements) {
        writeNativeValue(type, builder, jdbcObjectToPrestoNative(session, element, type));
    }
    return builder.build();
}