Search in sources :

Example 1 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestSerDeUtils method testMapBlock.

@Test
public void testMapBlock() {
    MapHolder holder = new MapHolder();
    holder.map = new TreeMap<>();
    holder.map.put("twelve", new InnerStruct(13, 14L));
    holder.map.put("fifteen", new InnerStruct(16, 17L));
    com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    com.facebook.presto.spi.type.Type mapOfVarcharRowType = new RowType(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), Optional.empty());
    Block actual = toBinaryBlock(mapOfVarcharRowType, holder, getInspector(MapHolder.class));
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(createUnboundedVarcharType(), rowType), new BlockBuilderStatus(), 1024);
    createUnboundedVarcharType().writeString(blockBuilder, "fifteen");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 16, 17L));
    createUnboundedVarcharType().writeString(blockBuilder, "twelve");
    rowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L));
    Block expected = rowBlockOf(ImmutableList.of(new MapType(createUnboundedVarcharType(), rowType)), blockBuilder);
    assertBlockEquals(actual, expected);
}
Also used : RowType(com.facebook.presto.type.RowType) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) MapType(com.facebook.presto.type.MapType) Block(com.facebook.presto.spi.block.Block) 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 2 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestSerDeUtils method testStructBlock.

@Test
public void testStructBlock() {
    // test simple structs
    InnerStruct innerStruct = new InnerStruct(13, 14L);
    com.facebook.presto.spi.type.Type rowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    Block actual = toBinaryBlock(rowType, innerStruct, getInspector(InnerStruct.class));
    Block expected = rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 13, 14L);
    assertBlockEquals(actual, expected);
    // test complex structs
    OuterStruct outerStruct = new OuterStruct();
    outerStruct.byteVal = (byte) 1;
    outerStruct.shortVal = (short) 2;
    outerStruct.intVal = 3;
    outerStruct.longVal = 4L;
    outerStruct.floatVal = 5.01f;
    outerStruct.doubleVal = 6.001d;
    outerStruct.stringVal = "seven";
    outerStruct.byteArray = new byte[] { '2' };
    InnerStruct is1 = new InnerStruct(2, -5L);
    InnerStruct is2 = new InnerStruct(-10, 0L);
    outerStruct.structArray = new ArrayList<>(2);
    outerStruct.structArray.add(is1);
    outerStruct.structArray.add(is2);
    outerStruct.map = new TreeMap<>();
    outerStruct.map.put("twelve", new InnerStruct(0, 5L));
    outerStruct.map.put("fifteen", new InnerStruct(-5, -10L));
    outerStruct.innerStruct = new InnerStruct(18, 19L);
    com.facebook.presto.spi.type.Type innerRowType = new RowType(ImmutableList.of(INTEGER, BIGINT), Optional.empty());
    com.facebook.presto.spi.type.Type arrayOfInnerRowType = new ArrayType(innerRowType);
    com.facebook.presto.spi.type.Type mapOfInnerRowType = new MapType(createUnboundedVarcharType(), innerRowType);
    List<com.facebook.presto.spi.type.Type> outerRowParameterTypes = ImmutableList.of(TINYINT, SMALLINT, INTEGER, BIGINT, REAL, DOUBLE, createUnboundedVarcharType(), createUnboundedVarcharType(), arrayOfInnerRowType, mapOfInnerRowType, innerRowType);
    com.facebook.presto.spi.type.Type outerRowType = new RowType(outerRowParameterTypes, Optional.empty());
    actual = toBinaryBlock(outerRowType, outerStruct, getInspector(OuterStruct.class));
    ImmutableList.Builder<Object> outerRowValues = ImmutableList.builder();
    outerRowValues.add((byte) 1);
    outerRowValues.add((short) 2);
    outerRowValues.add(3);
    outerRowValues.add(4L);
    outerRowValues.add(5.01f);
    outerRowValues.add(6.001d);
    outerRowValues.add("seven");
    outerRowValues.add(new byte[] { '2' });
    outerRowValues.add(arrayBlockOf(innerRowType, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 2, -5L), rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -10, 0L)));
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(createUnboundedVarcharType(), innerRowType), new BlockBuilderStatus(), 1024);
    createUnboundedVarcharType().writeString(blockBuilder, "fifteen");
    innerRowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), -5, -10L));
    createUnboundedVarcharType().writeString(blockBuilder, "twelve");
    innerRowType.writeObject(blockBuilder, rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 0, 5L));
    outerRowValues.add(blockBuilder.build());
    outerRowValues.add(rowBlockOf(ImmutableList.of(INTEGER, BIGINT), 18, 19L));
    assertBlockEquals(actual, rowBlockOf(outerRowParameterTypes, outerRowValues.build().toArray()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) 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) VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) Type(java.lang.reflect.Type) Block(com.facebook.presto.spi.block.Block) SerDeUtils.getBlockObject(com.facebook.presto.hive.util.SerDeUtils.getBlockObject) SerDeUtils.serializeObject(com.facebook.presto.hive.util.SerDeUtils.serializeObject) 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 3 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class TestOrcFileRewriter method testRewrite.

@Test
public void testRewrite() throws Exception {
    ArrayType arrayType = new ArrayType(BIGINT);
    ArrayType arrayOfArrayType = new ArrayType(arrayType);
    MapType mapType = new MapType(createVarcharType(5), BOOLEAN);
    List<Long> columnIds = ImmutableList.of(3L, 7L, 9L, 10L, 11L);
    List<Type> columnTypes = ImmutableList.of(BIGINT, createVarcharType(20), arrayType, mapType, arrayOfArrayType);
    File file = new File(temporary, randomUUID().toString());
    try (OrcFileWriter writer = new OrcFileWriter(columnIds, columnTypes, file)) {
        List<Page> pages = rowPagesBuilder(columnTypes).row(123L, "hello", arrayBlockOf(BIGINT, 1, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))).row(777L, "sky", arrayBlockOf(BIGINT, 3, 4), mapBlockOf(createVarcharType(5), BOOLEAN, "k2", false), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 6))).row(456L, "bye", arrayBlockOf(BIGINT, 5, 6), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", true), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 7))).row(888L, "world", arrayBlockOf(BIGINT, 7, 8), mapBlockOf(createVarcharType(5), BOOLEAN, "k4", true), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 8), null)).row(999L, "done", arrayBlockOf(BIGINT, 9, 10), mapBlockOf(createVarcharType(5), BOOLEAN, "k5", true), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 9, 10))).build();
        writer.appendPages(pages);
    }
    try (OrcDataSource dataSource = fileOrcDataSource(file)) {
        OrcRecordReader reader = createReader(dataSource, columnIds, columnTypes);
        assertEquals(reader.getReaderRowCount(), 5);
        assertEquals(reader.getFileRowCount(), 5);
        assertEquals(reader.getSplitLength(), file.length());
        assertEquals(reader.nextBatch(), 5);
        Block column0 = reader.readBlock(BIGINT, 0);
        assertEquals(column0.getPositionCount(), 5);
        for (int i = 0; i < 5; i++) {
            assertEquals(column0.isNull(i), false);
        }
        assertEquals(BIGINT.getLong(column0, 0), 123L);
        assertEquals(BIGINT.getLong(column0, 1), 777L);
        assertEquals(BIGINT.getLong(column0, 2), 456L);
        assertEquals(BIGINT.getLong(column0, 3), 888L);
        assertEquals(BIGINT.getLong(column0, 4), 999L);
        Block column1 = reader.readBlock(createVarcharType(20), 1);
        assertEquals(column1.getPositionCount(), 5);
        for (int i = 0; i < 5; i++) {
            assertEquals(column1.isNull(i), false);
        }
        assertEquals(createVarcharType(20).getSlice(column1, 0), utf8Slice("hello"));
        assertEquals(createVarcharType(20).getSlice(column1, 1), utf8Slice("sky"));
        assertEquals(createVarcharType(20).getSlice(column1, 2), utf8Slice("bye"));
        assertEquals(createVarcharType(20).getSlice(column1, 3), utf8Slice("world"));
        assertEquals(createVarcharType(20).getSlice(column1, 4), utf8Slice("done"));
        Block column2 = reader.readBlock(arrayType, 2);
        assertEquals(column2.getPositionCount(), 5);
        for (int i = 0; i < 5; i++) {
            assertEquals(column2.isNull(i), false);
        }
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 0), arrayBlockOf(BIGINT, 1, 2)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 1), arrayBlockOf(BIGINT, 3, 4)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 2), arrayBlockOf(BIGINT, 5, 6)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 3), arrayBlockOf(BIGINT, 7, 8)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 4), arrayBlockOf(BIGINT, 9, 10)));
        Block column3 = reader.readBlock(mapType, 3);
        assertEquals(column3.getPositionCount(), 5);
        for (int i = 0; i < 5; i++) {
            assertEquals(column3.isNull(i), false);
        }
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 0), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 1), mapBlockOf(createVarcharType(5), BOOLEAN, "k2", false)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", true)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 3), mapBlockOf(createVarcharType(5), BOOLEAN, "k4", true)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 4), mapBlockOf(createVarcharType(5), BOOLEAN, "k5", true)));
        Block column4 = reader.readBlock(arrayOfArrayType, 4);
        assertEquals(column4.getPositionCount(), 5);
        for (int i = 0; i < 5; i++) {
            assertEquals(column4.isNull(i), false);
        }
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 0), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 1), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 6))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 2), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 7))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 3), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 8), null)));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 4), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 9, 10))));
        assertEquals(reader.nextBatch(), -1);
        OrcFileMetadata orcFileMetadata = METADATA_CODEC.fromJson(reader.getUserMetadata().get(OrcFileMetadata.KEY).getBytes());
        assertEquals(orcFileMetadata, new OrcFileMetadata(ImmutableMap.<Long, TypeSignature>builder().put(3L, BIGINT.getTypeSignature()).put(7L, createVarcharType(20).getTypeSignature()).put(9L, arrayType.getTypeSignature()).put(10L, mapType.getTypeSignature()).put(11L, arrayOfArrayType.getTypeSignature()).build()));
    }
    BitSet rowsToDelete = new BitSet(5);
    rowsToDelete.set(1);
    rowsToDelete.set(3);
    rowsToDelete.set(4);
    File newFile = new File(temporary, randomUUID().toString());
    OrcFileInfo info = OrcFileRewriter.rewrite(file, newFile, rowsToDelete);
    assertEquals(info.getRowCount(), 2);
    assertEquals(info.getUncompressedSize(), 78);
    try (OrcDataSource dataSource = fileOrcDataSource(newFile)) {
        OrcRecordReader reader = createReader(dataSource, columnIds, columnTypes);
        assertEquals(reader.getReaderRowCount(), 2);
        assertEquals(reader.getFileRowCount(), 2);
        assertEquals(reader.getSplitLength(), newFile.length());
        assertEquals(reader.nextBatch(), 2);
        Block column0 = reader.readBlock(BIGINT, 0);
        assertEquals(column0.getPositionCount(), 2);
        for (int i = 0; i < 2; i++) {
            assertEquals(column0.isNull(i), false);
        }
        assertEquals(BIGINT.getLong(column0, 0), 123L);
        assertEquals(BIGINT.getLong(column0, 1), 456L);
        Block column1 = reader.readBlock(createVarcharType(20), 1);
        assertEquals(column1.getPositionCount(), 2);
        for (int i = 0; i < 2; i++) {
            assertEquals(column1.isNull(i), false);
        }
        assertEquals(createVarcharType(20).getSlice(column1, 0), utf8Slice("hello"));
        assertEquals(createVarcharType(20).getSlice(column1, 1), utf8Slice("bye"));
        Block column2 = reader.readBlock(arrayType, 2);
        assertEquals(column2.getPositionCount(), 2);
        for (int i = 0; i < 2; i++) {
            assertEquals(column2.isNull(i), false);
        }
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 0), arrayBlockOf(BIGINT, 1, 2)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column2, 1), arrayBlockOf(BIGINT, 5, 6)));
        Block column3 = reader.readBlock(mapType, 3);
        assertEquals(column3.getPositionCount(), 2);
        for (int i = 0; i < 2; i++) {
            assertEquals(column3.isNull(i), false);
        }
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 0), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column3, 1), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", true)));
        Block column4 = reader.readBlock(arrayOfArrayType, 4);
        assertEquals(column4.getPositionCount(), 2);
        for (int i = 0; i < 2; i++) {
            assertEquals(column4.isNull(i), false);
        }
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 0), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column4, 1), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 7))));
        assertEquals(reader.nextBatch(), -1);
        OrcFileMetadata orcFileMetadata = METADATA_CODEC.fromJson(reader.getUserMetadata().get(OrcFileMetadata.KEY).getBytes());
        assertEquals(orcFileMetadata, new OrcFileMetadata(ImmutableMap.<Long, TypeSignature>builder().put(3L, BIGINT.getTypeSignature()).put(7L, createVarcharType(20).getTypeSignature()).put(9L, arrayType.getTypeSignature()).put(10L, mapType.getTypeSignature()).put(11L, arrayOfArrayType.getTypeSignature()).build()));
    }
}
Also used : OrcDataSource(com.facebook.presto.orc.OrcDataSource) OrcTestingUtil.fileOrcDataSource(com.facebook.presto.raptor.storage.OrcTestingUtil.fileOrcDataSource) BitSet(java.util.BitSet) Page(com.facebook.presto.spi.Page) OrcRecordReader(com.facebook.presto.orc.OrcRecordReader) MapType(com.facebook.presto.type.MapType) 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) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) TypeSignature(com.facebook.presto.spi.type.TypeSignature) OrcFileInfo(com.facebook.presto.raptor.storage.OrcFileRewriter.OrcFileInfo) Block(com.facebook.presto.spi.block.Block) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class UnnestOperator method initializeUnnesters.

private void initializeUnnesters() {
    unnesters.clear();
    for (int i = 0; i < unnestTypes.size(); i++) {
        Type type = unnestTypes.get(i);
        int channel = unnestChannels.get(i);
        Block block = null;
        if (!currentPage.getBlock(channel).isNull(currentPosition)) {
            block = (Block) type.getObject(currentPage.getBlock(channel), currentPosition);
        }
        if (type instanceof ArrayType) {
            unnesters.add(new ArrayUnnester((ArrayType) type, block));
        } else if (type instanceof MapType) {
            unnesters.add(new MapUnnester((MapType) type, block));
        } else {
            throw new IllegalArgumentException("Cannot unnest type: " + type);
        }
    }
    ordinalityCount = 0;
}
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) Block(com.facebook.presto.spi.block.Block) MapType(com.facebook.presto.type.MapType)

Example 5 with MapType

use of com.facebook.presto.type.MapType in project presto by prestodb.

the class Histogram method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type keyType, Type valueType) {
    DynamicClassLoader classLoader = new DynamicClassLoader(Histogram.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(keyType);
    Type outputType = new MapType(keyType, valueType);
    HistogramStateSerializer stateSerializer = new HistogramStateSerializer(keyType);
    Type intermediateType = stateSerializer.getSerializedType();
    MethodHandle inputFunction = INPUT_FUNCTION.bindTo(keyType);
    MethodHandle outputFunction = OUTPUT_FUNCTION.bindTo(outputType);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(keyType), inputFunction, COMBINE_FUNCTION, outputFunction, HistogramState.class, stateSerializer, new HistogramStateFactory(), outputType);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, intermediateType, outputType, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) BigintType(com.facebook.presto.spi.type.BigintType) HistogramStateFactory(com.facebook.presto.operator.aggregation.state.HistogramStateFactory) MapType(com.facebook.presto.type.MapType) HistogramStateSerializer(com.facebook.presto.operator.aggregation.state.HistogramStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MapType (com.facebook.presto.type.MapType)58 Test (org.testng.annotations.Test)42 ArrayType (com.facebook.presto.type.ArrayType)28 Type (com.facebook.presto.spi.type.Type)20 Signature (com.facebook.presto.metadata.Signature)18 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)14 RowType (com.facebook.presto.type.RowType)12 Block (com.facebook.presto.spi.block.Block)10 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)10 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)10 ImmutableList (com.google.common.collect.ImmutableList)7 HashMap (java.util.HashMap)7 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)6 ImmutableMap (com.google.common.collect.ImmutableMap)6 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)5 List (java.util.List)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 BlockAssertions.createLongsBlock (com.facebook.presto.block.BlockAssertions.createLongsBlock)2 KeyValuePairStateSerializer (com.facebook.presto.operator.aggregation.state.KeyValuePairStateSerializer)2