Search in sources :

Example 61 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class TestHiveBucketing method toNativeContainerValue.

private static Object toNativeContainerValue(Type type, Object hiveValue) {
    String typeBase = type.getTypeSignature().getBase();
    if (hiveValue == null) {
        return null;
    }
    switch(typeBase) {
        case StandardTypes.ARRAY:
            {
                BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
                BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object subElement : (Iterable<?>) hiveValue) {
                    appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
                }
                blockBuilder.closeEntry();
                return type.getObject(blockBuilder, 0);
            }
        case StandardTypes.ROW:
            {
                BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
                BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
                int field = 0;
                for (Object subElement : (Iterable<?>) hiveValue) {
                    appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
                    field++;
                }
                blockBuilder.closeEntry();
                return type.getObject(blockBuilder, 0);
            }
        case StandardTypes.MAP:
            {
                BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
                BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
                for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
                    appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
                    appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
                }
                blockBuilder.closeEntry();
                return type.getObject(blockBuilder, 0);
            }
        case StandardTypes.BOOLEAN:
            return hiveValue;
        case StandardTypes.TINYINT:
            return (long) (byte) hiveValue;
        case StandardTypes.SMALLINT:
            return (long) (short) hiveValue;
        case StandardTypes.INTEGER:
            return (long) (int) hiveValue;
        case StandardTypes.BIGINT:
            return hiveValue;
        case StandardTypes.REAL:
            return (long) Float.floatToRawIntBits((float) hiveValue);
        case StandardTypes.DOUBLE:
            return hiveValue;
        case StandardTypes.VARCHAR:
            return Slices.utf8Slice(hiveValue.toString());
        case StandardTypes.DATE:
            long daysSinceEpochInLocalZone = ((Date) hiveValue).toLocalDate().toEpochDay();
            assertEquals(daysSinceEpochInLocalZone, DateWritable.dateToDays((Date) hiveValue));
            return daysSinceEpochInLocalZone;
        case StandardTypes.TIMESTAMP:
            Instant instant = ((Timestamp) hiveValue).toInstant();
            long epochSecond = instant.getEpochSecond();
            int nano = instant.getNano();
            assertEquals(nano % 1_000_000, 0);
            return epochSecond * 1000 + nano / 1_000_000;
        default:
            throw new UnsupportedOperationException("unknown type");
    }
}
Also used : Entry(java.util.Map.Entry) Instant(java.time.Instant) Timestamp(java.sql.Timestamp) Date(java.sql.Date) LocalDate(java.time.LocalDate) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 62 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class TableWriterOperator method createFragmentsPage.

// Fragments page layout:
// 
// row     fragments     context
// X         null          X
// null        X            X
// null        X            X
// null        X            X
// ...
private Page createFragmentsPage() {
    Collection<Slice> fragments = getFutureValue(finishFuture);
    int positionCount = fragments.size() + 1;
    committed = true;
    updateWrittenBytes();
    // Output page will only be constructed once, and the table commit context channel will be constructed using RunLengthEncodedBlock.
    // Thus individual BlockBuilder is used for each channel, instead of using PageBuilder.
    BlockBuilder rowsBuilder = BIGINT.createBlockBuilder(null, positionCount);
    BlockBuilder fragmentBuilder = VARBINARY.createBlockBuilder(null, positionCount);
    // write row count
    BIGINT.writeLong(rowsBuilder, rowCount);
    fragmentBuilder.appendNull();
    // write fragments
    for (Slice fragment : fragments) {
        rowsBuilder.appendNull();
        VARBINARY.writeSlice(fragmentBuilder, fragment);
    }
    return new Page(positionCount, rowsBuilder.build(), fragmentBuilder.build(), RunLengthEncodedBlock.create(VARBINARY, createTableCommitContext(true), positionCount));
}
Also used : Slice(io.airlift.slice.Slice) Page(com.facebook.presto.common.Page) TableWriterUtils.createStatisticsPage(com.facebook.presto.operator.TableWriterUtils.createStatisticsPage) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 63 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class BigQueryResultPageSource method getNextPage.

@Override
public Page getNextPage() {
    checkState(pageBuilder.isEmpty(), "PageBuilder is not empty at the beginning of a new page");
    Storage.ReadRowsResponse response = responses.next();
    Iterable<GenericRecord> records = parse(response);
    for (GenericRecord record : records) {
        pageBuilder.declarePosition();
        for (int column = 0; column < columnTypes.size(); column++) {
            BlockBuilder output = pageBuilder.getBlockBuilder(column);
            appendTo(columnTypes.get(column), record.get(column), output);
        }
    }
    Page page = pageBuilder.build();
    completedPositions += page.getPositionCount();
    pageBuilder.reset();
    return page;
}
Also used : Storage(com.google.cloud.bigquery.storage.v1beta1.Storage) Page(com.facebook.presto.common.Page) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 64 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class BigQueryResultPageSource method writeBlock.

private void writeBlock(BlockBuilder output, Type type, Object value) {
    if (type instanceof ArrayType && value instanceof List<?>) {
        BlockBuilder builder = output.beginBlockEntry();
        for (Object element : (List<?>) value) {
            appendTo(type.getTypeParameters().get(0), element, builder);
        }
        output.closeEntry();
        return;
    }
    if (type instanceof RowType && value instanceof GenericRecord) {
        GenericRecord record = (GenericRecord) value;
        BlockBuilder builder = output.beginBlockEntry();
        List<String> fieldNames = new ArrayList<>();
        for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
            TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
            fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
        }
        checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
        for (int index = 0; index < type.getTypeParameters().size(); index++) {
            appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
        }
        output.closeEntry();
        return;
    }
    throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
Also used : ArrayList(java.util.ArrayList) RowType(com.facebook.presto.common.type.RowType) ArrayType(com.facebook.presto.common.type.ArrayType) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) GenericRecord(org.apache.avro.generic.GenericRecord) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 65 with BlockBuilder

use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.

the class TestGeoFunctions method testGeometryGetObjectValue.

@Test
public void testGeometryGetObjectValue() {
    BlockBuilder builder = GEOMETRY.createBlockBuilder(null, 1);
    GEOMETRY.writeSlice(builder, GeoFunctions.stPoint(1.2, 3.4));
    Block block = builder.build();
    assertEquals("POINT (1.2 3.4)", GEOMETRY.getObjectValue(null, block, 0));
}
Also used : Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Aggregations

BlockBuilder (com.facebook.presto.common.block.BlockBuilder)493 Block (com.facebook.presto.common.block.Block)124 Test (org.testng.annotations.Test)106 Slice (io.airlift.slice.Slice)85 Type (com.facebook.presto.common.type.Type)76 Page (com.facebook.presto.common.Page)49 SqlType (com.facebook.presto.spi.function.SqlType)46 ArrayType (com.facebook.presto.common.type.ArrayType)44 MapType (com.facebook.presto.common.type.MapType)32 RowType (com.facebook.presto.common.type.RowType)28 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)26 RowBlockBuilder (com.facebook.presto.common.block.RowBlockBuilder)22 PrestoException (com.facebook.presto.spi.PrestoException)22 PageBuilder (com.facebook.presto.common.PageBuilder)21 StructuralTestUtil.appendToBlockBuilder (com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)21 Map (java.util.Map)21 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)20 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)19 DictionaryBlock (com.facebook.presto.common.block.DictionaryBlock)18 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)18