com.facebook.presto.spi.block.BlockBuilder Java Examples

The following examples show how to use com.facebook.presto.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: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
private void writeSlice(BlockBuilder output, Type type, Object value)
{
    String base = type.getTypeSignature().getBase();
    if (base.equals(StandardTypes.VARCHAR)) {
        type.writeSlice(output, utf8Slice(toVarcharValue(value)));
    }
    else if (type.equals(VARBINARY)) {
        if (value instanceof byte[]) {
            type.writeSlice(output, wrappedBuffer(((byte[]) value)));
        }
        else {
            output.appendNull();
        }
    }
    else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Slice: " + type.getTypeSignature());
    }
}
 
Example #2
Source File: HbaseRowSerializerUtil.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Encodes the given map into a Block.
 *
 * @param mapType Presto type of the map
 * @param bytes hbase byte[]
 * @return Presto Block
 */
public static Block getBlockFromMap(Type mapType, byte[] bytes)
        throws IOException
{
    Type keyType = mapType.getTypeParameters().get(0);
    Type valueType = mapType.getTypeParameters().get(1);
    Map<?, ?> map = MAPPER.readValue(bytes, new MyTypeReference(keyType.getJavaType(), valueType.getJavaType()));

    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
    BlockBuilder builder = mapBlockBuilder.beginBlockEntry();

    for (Map.Entry<?, ?> entry : map.entrySet()) {
        writeObject(builder, keyType, entry.getKey());
        writeObject(builder, valueType, entry.getValue());
    }

    mapBlockBuilder.closeEntry();
    return (Block) mapType.getObject(mapBlockBuilder, 0);
}
 
Example #3
Source File: HbaseRowSerializerUtil.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Recursive helper function used by {@link this#getArrayFromBlock} and
 * {@link this#getMapFromBlock} to add the given object to the given block
 * builder. Supports nested complex types!
 *
 * @param builder Block builder
 * @param type Presto type
 * @param obj Object to write to the block builder
 */
private static void writeObject(BlockBuilder builder, Type type, Object obj)
{
    if (Types.isArrayType(type)) {
        BlockBuilder arrayBldr = builder.beginBlockEntry();
        Type elementType = Types.getElementType(type);
        for (Object item : (List<?>) obj) {
            writeObject(arrayBldr, elementType, item);
        }
        builder.closeEntry();
    }
    else if (Types.isMapType(type)) {
        BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
            writeObject(mapBlockBuilder, Types.getKeyType(type), entry.getKey());
            writeObject(mapBlockBuilder, Types.getValueType(type), entry.getValue());
        }
        builder.closeEntry();
    }
    else {
        TypeUtils.writeNativeValue(type, builder, obj);
    }
}
 
Example #4
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 6 votes vote down vote up
private static void serializePrimitive(Type type, BlockBuilder builder, Object object) {
    requireNonNull(builder, "parent builder is null");

    if (object == null) {
        builder.appendNull();
        return;
    }

    if (BOOLEAN.equals(type)) {
        BOOLEAN.writeBoolean(builder, (Boolean) object);
    } else if (BIGINT.equals(type) || INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)
            || REAL.equals(type) || DATE.equals(type) || TIMESTAMP.equals(type)) {
        type.writeLong(builder, getLongExpressedValue(object));
    } else if (DOUBLE.equals(type)) {
        DOUBLE.writeDouble(builder, ((Number) object).doubleValue());
    } else if (isVarcharType(type) || VARBINARY.equals(type) || isCharType(type)) {
        type.writeSlice(builder, getSliceExpressedValue(object, type));
    } else {
        throw new UnsupportedOperationException("Unsupported primitive type: " + type);
    }
}
 
Example #5
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 6 votes vote down vote up
void accept()
{
    for (int i = 1; i < rowlisteMap.size(); i++) {
        pageBuilder.declarePosition();
        for (int column = 0; column < columnTypes.size(); column++) {
            BlockBuilder output = pageBuilder.getBlockBuilder(column);
            Type type = columnTypes.get(column);
            Object value = docMap.get(columnNames.get(column));
            if (column == rowlisteMapColumn) {
                appendTo(type, rowlisteMap.get(i), output);
            }
            else {
                appendTo(type, value, output);
            }
        }
    }
}
 
Example #6
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
private static Block serializeList(Type type, BlockBuilder builder, Object object) {
    List<?> list = (List) object;
    if (list == null) {
        requireNonNull(builder, "parent builder is null").appendNull();
        return null;
    }

    List<Type> typeParameters = type.getTypeParameters();
    checkArgument(typeParameters.size() == 1, "list must have exactly 1 type parameter");
    Type elementType = typeParameters.get(0);

    BlockBuilder currentBuilder;
    if (builder != null) {
        currentBuilder = builder.beginBlockEntry();
    } else {
        currentBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), list.size());
    }

    for (Object element : list) {
        serializeObject(elementType, currentBuilder, element);
    }

    if (builder != null) {
        builder.closeEntry();
        return null;
    } else {
        Block resultBlock = currentBuilder.build();
        return resultBlock;
    }
}
 
Example #7
Source File: TestBloomFilterAggregation.java    From presto-bloomfilter with Apache License 2.0 5 votes vote down vote up
@Test
public void testBloomFilterOutput()
{
    BloomFilterStateFactory f = new BloomFilterStateFactory();
    BloomFilterState state = f.createSingleState();
    BloomFilterAggregation.input(state, Slices.utf8Slice("robin"));
    BlockBuilderStatus bbs = new BlockBuilderStatus();
    BlockBuilder bb = new VariableWidthBlockBuilder(bbs, 1, 5); // @todo check 1 and 5 params
    BloomFilterAggregation.output(state, bb);

    assertTrue(bb.build().getSizeInBytes() > 1);
}
 
Example #8
Source File: BloomFilterType.java    From presto-bloomfilter 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 #9
Source File: BloomFilterStateSerializer.java    From presto-bloomfilter with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(BloomFilterState state, BlockBuilder out)
{
    if (state.getBloomFilter() == null) {
        out.appendNull();
    }
    else {
        BloomFilterType.BLOOM_FILTER.writeSlice(out, state.getBloomFilter().serialize());
    }
}
 
Example #10
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
private static Block serializeStruct(Type type, BlockBuilder builder, Object object) {
    if (object == null) {
        requireNonNull(builder, "parent builder is null").appendNull();
        return null;
    }

    List<Type> typeParameters = type.getTypeParameters();
    EthBlock.TransactionObject structData = (EthBlock.TransactionObject) object;
    boolean builderSynthesized = false;
    if (builder == null) {
        builderSynthesized = true;
        builder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
    }
    BlockBuilder currentBuilder = builder.beginBlockEntry();

    ImmutableList.Builder<Supplier> lstBuilder = ImmutableList.builder();
    lstBuilder.add(structData::getHash);
    lstBuilder.add(structData::getNonce);
    lstBuilder.add(structData::getBlockHash);
    lstBuilder.add(structData::getBlockNumber);
    lstBuilder.add(structData::getTransactionIndex);
    lstBuilder.add(structData::getFrom);
    lstBuilder.add(structData::getTo);
    lstBuilder.add(structData::getValue);
    lstBuilder.add(structData::getGas);
    lstBuilder.add(structData::getGasPrice);
    lstBuilder.add(structData::getInput);
    ImmutableList<Supplier> txColumns = lstBuilder.build();

    for (int i = 0; i < typeParameters.size(); i++) {
        serializeObject(typeParameters.get(i), currentBuilder, txColumns.get(i).get());
    }

    builder.closeEntry();
    if (builderSynthesized) {
        return (Block) type.getObject(builder, 0);
    } else {
        return null;
    }
}
 
Example #11
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
private static Block serializeMap(Type type, BlockBuilder builder, Object object) {
    Map<?, ?> map = (Map) object;
    if (map == null) {
        requireNonNull(builder, "parent builder is null").appendNull();
        return null;
    }

    List<Type> typeParameters = type.getTypeParameters();
    checkArgument(typeParameters.size() == 2, "map must have exactly 2 type parameter");
    Type keyType = typeParameters.get(0);
    Type valueType = typeParameters.get(1);
    boolean builderSynthesized = false;

    if (builder == null) {
        builderSynthesized = true;
        builder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
    }
    BlockBuilder currentBuilder = builder.beginBlockEntry();

    for (Map.Entry<?, ?> entry : map.entrySet()) {
        // Hive skips map entries with null keys
        if (entry.getKey() != null) {
            serializeObject(keyType, currentBuilder, entry.getKey());
            serializeObject(valueType, currentBuilder, entry.getValue());
        }
    }

    builder.closeEntry();
    if (builderSynthesized) {
        return (Block) type.getObject(builder, 0);
    } else {
        return null;
    }
}
 
Example #12
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
private static Block serializeObject(Type type, BlockBuilder builder, Object object) {
    if (!isStructuralType(type)) {
        serializePrimitive(type, builder, object);
        return null;
    } else if (isArrayType(type)) {
        return serializeList(type, builder, object);
    } else if (isMapType(type)) {
        return serializeMap(type, builder, object);
    } else if (isRowType(type)) {
        return serializeStruct(type, builder, object);
    }
    throw new RuntimeException("Unknown object type: " + type);
}
 
Example #13
Source File: HbaseRowSerializerUtil.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes the given list into a Block.
 *
 * @param elementType Element type of the array
 * @param bytes hbase byte[]
 * @return Presto Block
 */
public static Block getBlockFromArray(Type elementType, byte[] bytes)
        throws IOException
{
    List<?> array = MAPPER.readValue(bytes, List.class);
    BlockBuilder builder = elementType.createBlockBuilder(null, array.size());
    for (Object item : array) {
        writeObject(builder, elementType, item);
    }
    return builder.build();
}
 
Example #14
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public Page getNextPage()
{
    verify(pageBuilder.isEmpty());
    count = 0;
    for (int i = 0; i < ROWS_PER_REQUEST; i++) {
        if (!searchResult.hasNext()) {
            finished = true;
            break;
        }
        Map<String, Object> docMap = searchResult.next();
        count++;

        pageBuilder.declarePosition();
        ImmutableList.Builder<ArrayDocForeachFunc> funcs = ImmutableList.builder();
        for (int column = 0; column < columnTypes.size(); column++) {
            BlockBuilder output = pageBuilder.getBlockBuilder(column);
            Type type = columnTypes.get(column);
            Object value = docMap.get(columnNames.get(column));
            //如果是 array[] 字段 则下面会进行打平处理 es head展示时则只显示了array第一个元素
            if (isRowType(type) && value instanceof List && ((List) value).size() > 0 && ((List) value).get(0) instanceof Map) {
                final List<Map> listSourceMap = (List<Map>) value;
                appendTo(type, listSourceMap.get(0), output);
                //----下面是展平逻辑 目前存在如果有多个array字段 展平时笛卡尔积join问题---
                // 此处不做展平处理, 逻辑将和<es head `基本查询`>保持一致
                //funcs.add(new ArrayDocForeachFunc(column, docMap));
            }
            else {
                appendTo(type, value, output);
            }
        }
        funcs.build().forEach(func -> func.accept());
    }

    Page page = pageBuilder.build();
    pageBuilder.reset();
    return page;
}
 
Example #15
Source File: UnknownType.java    From paraflow with Apache License 2.0 4 votes vote down vote up
@Override
public void appendTo(Block block, int position, BlockBuilder blockBuilder)
{
    blockBuilder.appendNull();
}
 
Example #16
Source File: AbstractBloomFilterAggregation.java    From presto-bloomfilter with Apache License 2.0 4 votes vote down vote up
@OutputFunction(BloomFilterType.TYPE)
public static void output(BloomFilterState state, BlockBuilder out)
{
    BloomFilter bf = getOrCreateBloomFilter(state, BloomFilter.DEFAULT_BLOOM_FILTER_EXPECTED_INSERTIONS, BloomFilter.DEFAULT_BLOOM_FILTER_FALSE_POSITIVE_PERCENTAGE);
    BloomFilterType.BLOOM_FILTER.writeSlice(out, bf.serialize());
}
 
Example #17
Source File: ElasticsearchPageSource.java    From presto-connectors with Apache License 2.0 4 votes vote down vote up
private void appendTo(Type type, Object value, BlockBuilder output)
{
    if (value == null) {
        output.appendNull();
        return;
    }
    Class<?> javaType = type.getJavaType();
    try {
        if (javaType == boolean.class) {
            type.writeBoolean(output, (Boolean) value);
        }
        else if (javaType == long.class) {
            if (type.equals(BIGINT)) {
                type.writeLong(output, ((Number) value).longValue());
            }
            else if (type.equals(INTEGER)) {
                type.writeLong(output, ((Number) value).intValue());
            }
            else if (type.equals(DATE)) {
                long millis = (Long) value;
                type.writeLong(output, TimeUnit.MILLISECONDS.toDays(millis));
            }
            else if (type.equals(TIME)) {
                type.writeLong(output, (Long) value);
            }
            else if (type.equals(TIMESTAMP)) {
                type.writeLong(output, (Long) value);
            }
            else {
                throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
            }
        }
        else if (javaType == double.class) {
            type.writeDouble(output, ((Number) value).doubleValue());
        }
        else if (javaType == Slice.class) {
            writeSlice(output, type, value);
        }
        else if (javaType == Block.class) {
            writeBlock(output, type, value);
        }
        else {
            throw new PrestoException(GENERIC_INTERNAL_ERROR, "Unhandled type for " + javaType.getSimpleName() + ":" + type.getTypeSignature());
        }
    }
    catch (ClassCastException ignore) {
        // returns null instead of raising exception
        output.appendNull();
    }
}
 
Example #18
Source File: BloomFilterType.java    From presto-bloomfilter with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSlice(BlockBuilder blockBuilder, Slice value)
{
    writeSlice(blockBuilder, value, 0, value.length());
}
 
Example #19
Source File: BloomFilterType.java    From presto-bloomfilter with Apache License 2.0 4 votes vote down vote up
@Override
public void writeSlice(BlockBuilder blockBuilder, Slice value, int offset, int length)
{
    blockBuilder.writeBytes(value, offset, length).closeEntry();
}