Presto UDAF 输出数组(array)

1.版本0.189之前的使用:

@OutputFunction("array(" + StandardTypes.BIGINT + ")")

    public static void output(SliceState state, BlockBuilder out) {

        // 获取状态

        Slice slice = state.getSlice();

        // 数据为空, 返回一个空数组

        if (null == slice) {

            BlockBuilder blockBuilder = BigintType.BIGINT.createBlockBuilder(new BlockBuilderStatus(), 0);

            out.writeObject(blockBuilder.build());

            out.closeEntry();

            return;

        }

        // 构造结果: [A:100, B:50, C:10, ......]

        BlockBuilder blockBuilder = BigintType.BIGINT.createBlockBuilder(new BlockBuilderStatus(), slice.length() / 4);

        for (int index = 0; index < slice.length(); index += 4) {

            BigintType.BIGINT.writeLong(blockBuilder, slice.getInt(index));

        }

        // 返回结果

        out.writeObject(blockBuilder.build());

        out.closeEntry();

    }

 

2.版本0.189之后请使用:

@OutputFunction("array(bigint)")
public static void output(@AggregationState SliceState state, BlockBuilder out) {
    // 获取状态
    Slice slice = state.getSlice();

    // 数据为空, 返回一个空数组
    if (slice == null) {
        out.appendNull();
        return;
    }

    // 构造结果: [A:100, B:50, C:10, ......]
    BlockBuilder blockBuilder = out.beginBlockEntry();

    for (int index = 0; index < slice.length(); index += 4) {
        BIGINT.writeLong(blockBuilder, slice.getInt(index));
    }
    //返回结果
    out.closeEntry();
}

上面两段代码实现的逻辑相同

你可能感兴趣的:(presto)