Search in sources :

Example 36 with BlockBuilder

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

the class AggregatedOrcPageSource method getNextPage.

@Override
public Page getNextPage() {
    if (completed) {
        return null;
    }
    long start = System.nanoTime();
    Block[] blocks = new Block[columnHandles.size()];
    for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
        HiveColumnHandle columnHandle = columnHandles.get(fieldId);
        Aggregation aggregation = columnHandle.getPartialAggregation().get();
        int columnIndex = columnHandle.getHiveColumnIndex();
        Type type = typeManager.getType(columnHandle.getTypeSignature());
        BlockBuilder blockBuilder = type.createBlockBuilder(null, batchSize, 0);
        FunctionHandle functionHandle = aggregation.getFunctionHandle();
        if (functionResolution.isCountFunction(functionHandle)) {
            if (aggregation.getArguments().isEmpty()) {
                blockBuilder = blockBuilder.writeLong(footer.getNumberOfRows());
            } else {
                writeNonNullCount(columnIndex, blockBuilder);
            }
            completedBytes += INTEGER.getFixedSize();
        } else if (functionResolution.isMaxFunction(functionHandle)) {
            writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, false);
        } else if (functionResolution.isMinFunction(functionHandle)) {
            writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, true);
        } else {
            throw new UnsupportedOperationException(aggregation.getFunctionHandle().toString() + " is not supported");
        }
        blocks[fieldId] = blockBuilder.build();
    }
    completed = true;
    readTimeNanos += System.nanoTime() - start;
    return new Page(batchSize, blocks);
}
Also used : Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) HiveType(com.facebook.presto.hive.HiveType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType) OrcType(com.facebook.presto.orc.metadata.OrcType) Type(com.facebook.presto.common.type.Type) Block(com.facebook.presto.common.block.Block) Page(com.facebook.presto.common.Page) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) HiveColumnHandle(com.facebook.presto.hive.HiveColumnHandle) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 37 with BlockBuilder

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

the class ParquetTester method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else {
        if (BOOLEAN.equals(type)) {
            type.writeBoolean(blockBuilder, (Boolean) value);
        } else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
            type.writeLong(blockBuilder, ((Number) value).longValue());
        } else if (Decimals.isShortDecimal(type)) {
            type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue());
        } else if (Decimals.isLongDecimal(type)) {
            if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) {
                type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
            } else {
                type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).getUnscaledValue().longValue()));
            }
        } else if (DOUBLE.equals(type)) {
            type.writeDouble(blockBuilder, ((Number) value).doubleValue());
        } else if (REAL.equals(type)) {
            float floatValue = ((Number) value).floatValue();
            type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
        } else if (type instanceof VarcharType) {
            Slice slice = truncateToLength(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (type instanceof CharType) {
            Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
            type.writeSlice(blockBuilder, slice);
        } else if (VARBINARY.equals(type)) {
            type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
        } else if (DATE.equals(type)) {
            long days = ((SqlDate) value).getDays();
            type.writeLong(blockBuilder, days);
        } else if (TIMESTAMP.equals(type)) {
            long millis = ((SqlTimestamp) value).getMillisUtc();
            type.writeLong(blockBuilder, millis);
        } else {
            if (type instanceof ArrayType) {
                List<?> array = (List<?>) value;
                Type elementType = type.getTypeParameters().get(0);
                BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
                for (Object elementValue : array) {
                    writeValue(elementType, arrayBlockBuilder, elementValue);
                }
                blockBuilder.closeEntry();
            } else if (type instanceof MapType) {
                Map<?, ?> map = (Map<?, ?>) value;
                Type keyType = type.getTypeParameters().get(0);
                Type valueType = type.getTypeParameters().get(1);
                BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    writeValue(keyType, mapBlockBuilder, entry.getKey());
                    writeValue(valueType, mapBlockBuilder, entry.getValue());
                }
                blockBuilder.closeEntry();
            } else if (type instanceof RowType) {
                List<?> array = (List<?>) value;
                List<Type> fieldTypes = type.getTypeParameters();
                BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
                for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
                    Type fieldType = fieldTypes.get(fieldId);
                    writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
                }
                blockBuilder.closeEntry();
            } else {
                throw new IllegalArgumentException("Unsupported type " + type);
            }
        }
    }
}
Also used : Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) VarcharType(com.facebook.presto.common.type.VarcharType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) RowType(com.facebook.presto.common.type.RowType) MetastoreUtil.isRowType(com.facebook.presto.hive.metastore.MetastoreUtil.isRowType) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) MapType(com.facebook.presto.common.type.MapType) MetastoreUtil.isMapType(com.facebook.presto.hive.metastore.MetastoreUtil.isMapType) ArrayType(com.facebook.presto.common.type.ArrayType) MetastoreUtil.isArrayType(com.facebook.presto.hive.metastore.MetastoreUtil.isArrayType) Varchars.isVarcharType(com.facebook.presto.common.type.Varchars.isVarcharType) DecimalType(com.facebook.presto.common.type.DecimalType) HiveUtil.isStructuralType(com.facebook.presto.hive.HiveUtil.isStructuralType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) MetastoreUtil.isArrayType(com.facebook.presto.hive.metastore.MetastoreUtil.isArrayType) RowType(com.facebook.presto.common.type.RowType) MetastoreUtil.isRowType(com.facebook.presto.hive.metastore.MetastoreUtil.isRowType) VarcharType(com.facebook.presto.common.type.VarcharType) MessageType(org.apache.parquet.schema.MessageType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) MetastoreUtil.isMapType(com.facebook.presto.hive.metastore.MetastoreUtil.isMapType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) CharType(com.facebook.presto.common.type.CharType) Map(java.util.Map) HashMap(java.util.HashMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 38 with BlockBuilder

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

the class OperatorAssertion method toRow.

public static Block toRow(List<Type> parameterTypes, Object... values) {
    checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
    RowType rowType = RowType.anonymous(parameterTypes);
    BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
    BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
    for (int i = 0; i < values.length; i++) {
        appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
    }
    blockBuilder.closeEntry();
    return rowType.getObject(blockBuilder, 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) RowType(com.facebook.presto.common.type.RowType) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) StructuralTestUtil.appendToBlockBuilder(com.facebook.presto.util.StructuralTestUtil.appendToBlockBuilder)

Example 39 with BlockBuilder

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

the class AbstractTestType method toBlock.

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();
}
Also used : Slice(io.airlift.slice.Slice) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 40 with BlockBuilder

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

the class TestBigintArrayType method getGreaterValue.

@Override
protected Object getGreaterValue(Object value) {
    Block block = (Block) value;
    BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, block.getPositionCount() + 1);
    for (int i = 0; i < block.getPositionCount(); i++) {
        BIGINT.appendTo(block, i, blockBuilder);
    }
    BIGINT.writeLong(blockBuilder, 1L);
    return blockBuilder.build();
}
Also used : Block(com.facebook.presto.common.block.Block) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

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