Search in sources :

Example 56 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class TestStateCompiler method testComplexSerialization.

@Test
public void testComplexSerialization() {
    Type arrayType = new ArrayType(BIGINT);
    Type mapType = new MapType(BIGINT, VARCHAR);
    Map<String, Type> fieldMap = ImmutableMap.of("Block", arrayType, "AnotherBlock", mapType);
    AccumulatorStateFactory<TestComplexState> factory = StateCompiler.generateStateFactory(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    AccumulatorStateSerializer<TestComplexState> serializer = StateCompiler.generateStateSerializer(TestComplexState.class, fieldMap, new DynamicClassLoader(TestComplexState.class.getClassLoader()));
    TestComplexState singleState = factory.createSingleState();
    TestComplexState deserializedState = factory.createSingleState();
    singleState.setBoolean(true);
    singleState.setLong(1);
    singleState.setDouble(2.0);
    singleState.setByte((byte) 3);
    singleState.setSlice(utf8Slice("test"));
    singleState.setAnotherSlice(wrappedDoubleArray(1.0, 2.0, 3.0));
    singleState.setYetAnotherSlice(null);
    Block array = createLongsBlock(45);
    singleState.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();
    singleState.setAnotherBlock(map);
    BlockBuilder builder = new RowType(ImmutableList.of(BOOLEAN, TINYINT, DOUBLE, BIGINT, mapType, VARBINARY, arrayType, VARBINARY, VARBINARY), Optional.empty()).createBlockBuilder(new BlockBuilderStatus(), 1);
    serializer.serialize(singleState, builder);
    Block block = builder.build();
    serializer.deserialize(block, 0, deserializedState);
    assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
    assertEquals(deserializedState.getLong(), singleState.getLong());
    assertEquals(deserializedState.getDouble(), singleState.getDouble());
    assertEquals(deserializedState.getByte(), singleState.getByte());
    assertEquals(deserializedState.getSlice(), singleState.getSlice());
    assertEquals(deserializedState.getAnotherSlice(), singleState.getAnotherSlice());
    assertEquals(deserializedState.getYetAnotherSlice(), singleState.getYetAnotherSlice());
    assertEquals(deserializedState.getBlock().getLong(0, 0), singleState.getBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getLong(0, 0), singleState.getAnotherBlock().getLong(0, 0));
    assertEquals(deserializedState.getAnotherBlock().getSlice(1, 0, 9), singleState.getAnotherBlock().getSlice(1, 0, 9));
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) RowType(com.facebook.presto.type.RowType) 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) 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 57 with Type

use of com.facebook.presto.spi.type.Type 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 58 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class TestMinMaxByAggregation method testAllRegistered.

@Test
public void testAllRegistered() {
    Set<Type> orderableTypes = getTypes().stream().filter(Type::isOrderable).collect(toImmutableSet());
    for (Type keyType : orderableTypes) {
        for (Type valueType : getTypes()) {
            if (StateCompiler.getSupportedFieldTypes().contains(valueType.getJavaType())) {
                assertNotNull(METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("min_by", AGGREGATE, valueType.getTypeSignature(), valueType.getTypeSignature(), keyType.getTypeSignature())));
                assertNotNull(METADATA.getFunctionRegistry().getAggregateFunctionImplementation(new Signature("max_by", AGGREGATE, valueType.getTypeSignature(), valueType.getTypeSignature(), keyType.getTypeSignature())));
            }
        }
    }
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Type(com.facebook.presto.spi.type.Type) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Test(org.testng.annotations.Test)

Example 59 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class AbstractTestAccumuloRowSerializer method testArray.

@Test
public void testArray() throws Exception {
    AccumuloRowSerializer serializer = serializerClass.getConstructor().newInstance();
    Type type = new ArrayType(VARCHAR);
    List<Object> expected = ImmutableList.of("a", "b", "c");
    byte[] data = serializer.encode(type, AccumuloRowSerializer.getBlockFromArray(VARCHAR, expected));
    List<Object> actual = serializer.decode(type, data);
    assertEquals(actual, expected);
    deserializeData(serializer, data);
    actual = AccumuloRowSerializer.getArrayFromBlock(VARCHAR, serializer.getArray(COLUMN_NAME, type));
    assertEquals(actual, expected);
}
Also used : ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) Test(org.testng.annotations.Test)

Example 60 with Type

use of com.facebook.presto.spi.type.Type in project presto by prestodb.

the class Indexer method index.

/**
     * Index the given mutation, adding mutations to the index and metrics table
     * <p>
     * Like typical use of a BatchWriter, this method does not flush mutations to the underlying index table.
     * For higher throughput the modifications to the metrics table are tracked in memory and added to the metrics table when the indexer is flushed or closed.
     *
     * @param mutation Mutation to index
     */
public void index(Mutation mutation) {
    // Increment the cardinality for the number of rows in the table
    metrics.get(METRICS_TABLE_ROW_COUNT).incrementAndGet();
    // Set the first and last row values of the table based on existing row IDs
    if (firstRow == null || byteArrayComparator.compare(mutation.getRow(), firstRow) < 0) {
        firstRow = mutation.getRow();
    }
    if (lastRow == null || byteArrayComparator.compare(mutation.getRow(), lastRow) > 0) {
        lastRow = mutation.getRow();
    }
    // For each column update in this mutation
    for (ColumnUpdate columnUpdate : mutation.getUpdates()) {
        // Get the column qualifiers we want to index for this column family (if any)
        ByteBuffer family = wrap(columnUpdate.getColumnFamily());
        Collection<ByteBuffer> indexQualifiers = indexColumns.get(family);
        // If we have column qualifiers we want to index for this column family
        if (indexQualifiers != null) {
            // Check if we want to index this particular qualifier
            ByteBuffer qualifier = wrap(columnUpdate.getColumnQualifier());
            if (indexQualifiers.contains(qualifier)) {
                // If so, create a mutation using the following mapping:
                // Row ID = column value
                // Column Family = columnqualifier_columnfamily
                // Column Qualifier = row ID
                // Value = empty
                ByteBuffer indexFamily = getIndexColumnFamily(columnUpdate.getColumnFamily(), columnUpdate.getColumnQualifier());
                Type type = indexColumnTypes.get(family).get(qualifier);
                ColumnVisibility visibility = new ColumnVisibility(columnUpdate.getColumnVisibility());
                // If this is an array type, then index each individual element in the array
                if (Types.isArrayType(type)) {
                    Type elementType = Types.getElementType(type);
                    List<?> elements = serializer.decode(type, columnUpdate.getValue());
                    for (Object element : elements) {
                        addIndexMutation(wrap(serializer.encode(elementType, element)), indexFamily, visibility, mutation.getRow());
                    }
                } else {
                    addIndexMutation(wrap(columnUpdate.getValue()), indexFamily, visibility, mutation.getRow());
                }
            }
        }
    }
}
Also used : Type(com.facebook.presto.spi.type.Type) ColumnUpdate(org.apache.accumulo.core.data.ColumnUpdate) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Type (com.facebook.presto.spi.type.Type)392 Test (org.testng.annotations.Test)103 Block (com.facebook.presto.spi.block.Block)83 ImmutableList (com.google.common.collect.ImmutableList)79 ArrayType (com.facebook.presto.type.ArrayType)78 MapType (com.facebook.presto.type.MapType)72 Page (com.facebook.presto.spi.Page)68 PrestoException (com.facebook.presto.spi.PrestoException)51 List (java.util.List)50 VarcharType (com.facebook.presto.spi.type.VarcharType)48 DecimalType (com.facebook.presto.spi.type.DecimalType)44 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)40 MaterializedResult (com.facebook.presto.testing.MaterializedResult)40 ArrayList (java.util.ArrayList)40 MethodHandle (java.lang.invoke.MethodHandle)39 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)37 ImmutableMap (com.google.common.collect.ImmutableMap)36 Map (java.util.Map)36 Slice (io.airlift.slice.Slice)35 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)30