use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class TestStateCompiler method testComplexStateEstimatedSize.
@Test
public void testComplexStateEstimatedSize() {
Map<String, Type> fieldMap = ImmutableMap.of("Block", new ArrayType(BIGINT), "AnotherBlock", new MapType(BIGINT, VARCHAR));
AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
TestComplexState groupedState = factory.createGroupedState();
assertEquals(groupedState.getEstimatedSize(), 76064);
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * (i + 1));
}
for (int i = 0; i < 1000; i++) {
((GroupedAccumulatorState) groupedState).setGroupId(i);
groupedState.setBoolean(true);
groupedState.setLong(1);
groupedState.setDouble(2.0);
groupedState.setByte((byte) 3);
groupedState.setSlice(utf8Slice("test"));
groupedState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
groupedState.setYetAnotherSlice(null);
Block array = createLongsBlock(45);
groupedState.setBlock(array);
BlockBuilder mapBlockBuilder = new InterleavedBlockBuilder(ImmutableList.of(BIGINT, VARCHAR), new BlockBuilderStatus(), 1);
BIGINT.writeLong(mapBlockBuilder, 123L);
VARCHAR.writeSlice(mapBlockBuilder, utf8Slice("testBlock"));
Block map = mapBlockBuilder.build();
groupedState.setAnotherBlock(map);
assertEquals(groupedState.getEstimatedSize(), 76064 + 1274 * 1000);
}
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class SerDeUtils method serializeMap.
private static Block serializeMap(Type type, BlockBuilder builder, Object object, MapObjectInspector inspector, boolean filterNullMapKeys) {
Map<?, ?> map = inspector.getMap(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);
ObjectInspector keyInspector = inspector.getMapKeyObjectInspector();
ObjectInspector valueInspector = inspector.getMapValueObjectInspector();
BlockBuilder currentBuilder;
if (builder != null) {
currentBuilder = builder.beginBlockEntry();
} else {
currentBuilder = new InterleavedBlockBuilder(typeParameters, new BlockBuilderStatus(), map.size());
}
for (Map.Entry<?, ?> entry : map.entrySet()) {
// Hive skips map entries with null keys
if (!filterNullMapKeys || entry.getKey() != null) {
serializeObject(keyType, currentBuilder, entry.getKey(), keyInspector);
serializeObject(valueType, currentBuilder, entry.getValue(), valueInspector);
}
}
if (builder != null) {
builder.closeEntry();
return null;
} else {
Block resultBlock = currentBuilder.build();
return resultBlock;
}
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class SerDeUtils method serializeStruct.
private static Block serializeStruct(Type type, BlockBuilder builder, Object object, StructObjectInspector inspector) {
if (object == null) {
requireNonNull(builder, "parent builder is null").appendNull();
return null;
}
List<Type> typeParameters = type.getTypeParameters();
List<? extends StructField> allStructFieldRefs = inspector.getAllStructFieldRefs();
checkArgument(typeParameters.size() == allStructFieldRefs.size());
BlockBuilder currentBuilder;
if (builder != null) {
currentBuilder = builder.beginBlockEntry();
} else {
currentBuilder = new InterleavedBlockBuilder(typeParameters, new BlockBuilderStatus(), typeParameters.size());
}
for (int i = 0; i < typeParameters.size(); i++) {
StructField field = allStructFieldRefs.get(i);
serializeObject(typeParameters.get(i), currentBuilder, inspector.getStructFieldData(object, field), field.getFieldObjectInspector());
}
if (builder != null) {
builder.closeEntry();
return null;
} else {
Block resultBlock = currentBuilder.build();
return resultBlock;
}
}
use of com.facebook.presto.spi.block.InterleavedBlockBuilder in project presto by prestodb.
the class StructuralTestUtil method mapBlockOf.
public static Block mapBlockOf(Type keyType, Type valueType, Map<?, ?> value) {
BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(keyType, valueType), new BlockBuilderStatus(), value.size() * 2);
for (Map.Entry<?, ?> entry : value.entrySet()) {
appendToBlockBuilder(keyType, entry.getKey(), blockBuilder);
appendToBlockBuilder(valueType, entry.getValue(), blockBuilder);
}
return blockBuilder.build();
}
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();
}
Aggregations