Search in sources :

Example 16 with InterleavedBlockBuilder

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);
    }
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) BlockAssertions.createLongsBlock(com.facebook.presto.block.BlockAssertions.createLongsBlock) GroupedAccumulatorState(com.facebook.presto.spi.function.GroupedAccumulatorState) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 17 with InterleavedBlockBuilder

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;
    }
}
Also used : IntegerType(com.facebook.presto.spi.type.IntegerType) DateType(com.facebook.presto.spi.type.DateType) TinyintType(com.facebook.presto.spi.type.TinyintType) DoubleType(com.facebook.presto.spi.type.DoubleType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) SmallintType(com.facebook.presto.spi.type.SmallintType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) CharType(com.facebook.presto.spi.type.CharType) BooleanType(com.facebook.presto.spi.type.BooleanType) RealType(com.facebook.presto.spi.type.RealType) HiveDecimalObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveDecimalObjectInspector) BooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BooleanObjectInspector) ShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ShortObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) FloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) IntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.IntObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) ByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.ByteObjectInspector) DoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DoubleObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) Block(com.facebook.presto.spi.block.Block) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) Map(java.util.Map) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 18 with InterleavedBlockBuilder

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;
    }
}
Also used : IntegerType(com.facebook.presto.spi.type.IntegerType) DateType(com.facebook.presto.spi.type.DateType) TinyintType(com.facebook.presto.spi.type.TinyintType) DoubleType(com.facebook.presto.spi.type.DoubleType) DecimalType(com.facebook.presto.spi.type.DecimalType) TimestampType(com.facebook.presto.spi.type.TimestampType) SmallintType(com.facebook.presto.spi.type.SmallintType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) CharType(com.facebook.presto.spi.type.CharType) BooleanType(com.facebook.presto.spi.type.BooleanType) RealType(com.facebook.presto.spi.type.RealType) StructField(org.apache.hadoop.hive.serde2.objectinspector.StructField) Block(com.facebook.presto.spi.block.Block) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 19 with InterleavedBlockBuilder

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();
}
Also used : InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) Map(java.util.Map) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) TypeJsonUtils.appendToBlockBuilder(com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 20 with InterleavedBlockBuilder

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();
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) RowType(com.facebook.presto.type.RowType) Type(com.facebook.presto.spi.type.Type) Block(com.facebook.presto.spi.block.Block) ObjectBigArray(com.facebook.presto.array.ObjectBigArray) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Aggregations

InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)28 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)26 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)23 Block (com.facebook.presto.spi.block.Block)11 TypeJsonUtils.appendToBlockBuilder (com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder)9 Type (com.facebook.presto.spi.type.Type)8 Test (org.testng.annotations.Test)8 MapType (com.facebook.presto.type.MapType)5 RowType (com.facebook.presto.type.RowType)5 PrestoException (com.facebook.presto.spi.PrestoException)4 ArrayType (com.facebook.presto.type.ArrayType)4 VarcharType (com.facebook.presto.spi.type.VarcharType)3 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)3 Map (java.util.Map)3 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)2 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)2 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)2 BigintType (com.facebook.presto.spi.type.BigintType)2 BooleanType (com.facebook.presto.spi.type.BooleanType)2