use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TestInterleavedBlock method createBlockBuilderWithValues.
private static InterleavedBlockBuilder createBlockBuilderWithValues(Slice[] expectedValues) {
InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(TYPES, new BlockBuilderStatus(), expectedValues.length);
int valueIndex = 0;
while (valueIndex < expectedValues.length) {
for (Type type : TYPES) {
Class<?> javaType = type.getJavaType();
Slice expectedValue = expectedValues[valueIndex];
if (expectedValue == null) {
blockBuilder.appendNull();
} else if (javaType == boolean.class) {
type.writeBoolean(blockBuilder, expectedValue.getByte(0) != 0);
} else if (javaType == long.class) {
type.writeLong(blockBuilder, expectedValue.getLong(0));
} else if (javaType == double.class) {
type.writeDouble(blockBuilder, expectedValue.getDouble(0));
} else {
blockBuilder.writeBytes(expectedValue, 0, expectedValue.length()).closeEntry();
}
valueIndex++;
}
}
return blockBuilder;
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class MultiKeyValuePairs method toMultimapNativeEncoding.
/**
* Serialize as a multimap: map(key, array(value)), each key can be associated with multiple values
*/
public Block toMultimapNativeEncoding() {
Block keys = keyBlockBuilder.build();
Block values = valueBlockBuilder.build();
// Merge values of the same key into an array
BlockBuilder distinctKeyBlockBuilder = keyType.createBlockBuilder(new BlockBuilderStatus(), keys.getPositionCount(), expectedValueSize(keyType, EXPECTED_ENTRY_SIZE));
ObjectBigArray<BlockBuilder> valueArrayBlockBuilders = new ObjectBigArray<>();
valueArrayBlockBuilders.ensureCapacity(keys.getPositionCount());
TypedSet keySet = new TypedSet(keyType, keys.getPositionCount());
for (int keyValueIndex = 0; keyValueIndex < keys.getPositionCount(); keyValueIndex++) {
if (!keySet.contains(keys, keyValueIndex)) {
keySet.add(keys, keyValueIndex);
keyType.appendTo(keys, keyValueIndex, distinctKeyBlockBuilder);
BlockBuilder valueArrayBuilder = valueType.createBlockBuilder(new BlockBuilderStatus(), 10, expectedValueSize(valueType, EXPECTED_ENTRY_SIZE));
valueArrayBlockBuilders.set(keySet.positionOf(keys, keyValueIndex), valueArrayBuilder);
}
valueType.appendTo(values, keyValueIndex, valueArrayBlockBuilders.get(keySet.positionOf(keys, keyValueIndex)));
}
// Write keys and value arrays into one Block
Block distinctKeys = distinctKeyBlockBuilder.build();
Type valueArrayType = new ArrayType(valueType);
BlockBuilder multimapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueArrayType), new BlockBuilderStatus(), distinctKeyBlockBuilder.getPositionCount());
for (int i = 0; i < distinctKeys.getPositionCount(); i++) {
keyType.appendTo(distinctKeys, i, multimapBlockBuilder);
valueArrayType.writeObject(multimapBlockBuilder, valueArrayBlockBuilders.get(i).build());
}
return multimapBlockBuilder.build();
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TypedHistogram method serialize.
public Block serialize() {
Block valuesBlock = values.build();
BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(type, BIGINT), new BlockBuilderStatus(), valuesBlock.getPositionCount() * 2);
for (int i = 0; i < valuesBlock.getPositionCount(); i++) {
type.appendTo(valuesBlock, i, blockBuilder);
BIGINT.writeLong(blockBuilder, counts.get(i));
}
return blockBuilder.build();
}
Aggregations