Search in sources :

Example 31 with MapType

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

the class TestUnnesterUtil method calculateMaxCardinalities.

static int[] calculateMaxCardinalities(Page page, List<Type> replicatedTypes, List<Type> unnestTypes) {
    int positionCount = page.getPositionCount();
    int[] maxCardinalities = new int[positionCount];
    int replicatedChannelCount = replicatedTypes.size();
    int unnestChannelCount = unnestTypes.size();
    for (int i = 0; i < unnestChannelCount; i++) {
        Type type = unnestTypes.get(i);
        Block block = page.getBlock(replicatedChannelCount + i);
        assertTrue(type instanceof ArrayType || type instanceof MapType);
        if (type instanceof ArrayType) {
            ColumnarArray columnarArray = ColumnarArray.toColumnarArray(block);
            for (int j = 0; j < positionCount; j++) {
                maxCardinalities[j] = max(maxCardinalities[j], columnarArray.getLength(j));
            }
        } else if (type instanceof MapType) {
            ColumnarMap columnarMap = ColumnarMap.toColumnarMap(block);
            for (int j = 0; j < positionCount; j++) {
                maxCardinalities[j] = max(maxCardinalities[j], columnarMap.getEntryCount(j));
            }
        } else {
            fail("expected an ArrayType or MapType, but found " + type);
        }
    }
    return maxCardinalities;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) ColumnarArray(com.facebook.presto.common.block.ColumnarArray) ColumnarMap(com.facebook.presto.common.block.ColumnarMap) Block(com.facebook.presto.common.block.Block) MapType(com.facebook.presto.common.type.MapType)

Example 32 with MapType

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

the class TestUnnesterUtil method buildExpectedPage.

static Page buildExpectedPage(Page page, List<Type> replicatedTypes, List<Type> unnestTypes, List<Type> outputTypes, int[] maxCardinalities, boolean withOrdinality, boolean legacyUnnest) {
    int totalEntries = IntStream.of(maxCardinalities).sum();
    int channelCount = page.getChannelCount();
    assertTrue(channelCount > 1);
    Block[] outputBlocks = new Block[outputTypes.size()];
    int outputChannel = 0;
    for (int i = 0; i < replicatedTypes.size(); i++) {
        outputBlocks[outputChannel++] = buildExpectedReplicatedBlock(page.getBlock(i), replicatedTypes.get(i), maxCardinalities, totalEntries);
    }
    for (int i = 0; i < unnestTypes.size(); i++) {
        Type type = unnestTypes.get(i);
        Block inputBlock = page.getBlock(replicatedTypes.size() + i);
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (elementType instanceof RowType && !legacyUnnest) {
                List<Type> rowTypes = ((RowType) elementType).getTypeParameters();
                Block[] blocks = buildExpectedUnnestedArrayOfRowBlock(inputBlock, rowTypes, maxCardinalities, totalEntries);
                for (Block block : blocks) {
                    outputBlocks[outputChannel++] = block;
                }
            } else {
                outputBlocks[outputChannel++] = buildExpectedUnnestedArrayBlock(inputBlock, ((ArrayType) unnestTypes.get(i)).getElementType(), maxCardinalities, totalEntries);
            }
        } else if (type instanceof MapType) {
            MapType mapType = (MapType) unnestTypes.get(i);
            Block[] blocks = buildExpectedUnnestedMapBlocks(inputBlock, mapType.getKeyType(), mapType.getValueType(), maxCardinalities, totalEntries);
            for (Block block : blocks) {
                outputBlocks[outputChannel++] = block;
            }
        } else {
            fail("expected an ArrayType or MapType, but found " + type);
        }
    }
    if (withOrdinality) {
        outputBlocks[outputChannel++] = buildExpectedOrdinalityBlock(maxCardinalities, totalEntries);
    }
    return new Page(outputBlocks);
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Block(com.facebook.presto.common.block.Block) RowType(com.facebook.presto.common.type.RowType) Page(com.facebook.presto.common.Page) MapType(com.facebook.presto.common.type.MapType)

Example 33 with MapType

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

the class TestDirectEntryBlockBuilder method testNestedRow.

@Test
public void testNestedRow() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayType arrayType = new ArrayType(DOUBLE);
    RowType.Field nestedRowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
    RowType nestedRowType = RowType.from(ImmutableList.of(nestedRowField));
    List<Type> rowType = ImmutableList.of(REAL, mapType, arrayType, nestedRowType);
    RowBlockBuilder beginEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        SingleRowBlockWriter singleRowBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
        REAL.writeLong(singleRowBlockWriter, i);
        // Write Map<Bigint, Varchar> with 5 entries.
        BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(mapWriter, i + j);
            VARCHAR.writeSlice(mapWriter, utf8Slice("Value1" + j));
        }
        singleRowBlockWriter.closeEntry();
        // Write array .
        BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayWriter, i * 3 + j);
        }
        singleRowBlockWriter.closeEntry();
        // Write row type.
        BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
        if (i % 2 == 0) {
            rowWriter.appendNull();
        } else {
            INTEGER.writeLong(rowWriter, i);
        }
        singleRowBlockWriter.closeEntry();
        beginEntryBlockBuilder.closeEntry();
        beginEntryBlockBuilder.appendNull();
    }
    RowBlockBuilder directEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        REAL.writeLong(directEntryBlockBuilder.getBlockBuilder(0), i);
        // Write Map<Bigint, Varchar> with 5 entries.
        MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) directEntryBlockBuilder.getBlockBuilder(1);
        mapBlockBuilder.beginDirectEntry();
        BlockBuilder keyBuilder = mapBlockBuilder.getKeyBlockBuilder();
        BlockBuilder valueBuilder = mapBlockBuilder.getValueBlockBuilder();
        for (int j = 0; j < 5; j++) {
            BIGINT.writeLong(keyBuilder, i + j);
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
        }
        mapBlockBuilder.closeEntry();
        // Write array .
        ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getBlockBuilder(2);
        arrayBuilder.beginDirectEntry();
        for (int j = 0; j < 8; j++) {
            DOUBLE.writeDouble(arrayBuilder.getElementBlockBuilder(), i * 3 + j);
        }
        arrayBuilder.closeEntry();
        // Write row type.
        RowBlockBuilder nestedRowBuilder = (RowBlockBuilder) directEntryBlockBuilder.getBlockBuilder(3);
        nestedRowBuilder.beginDirectEntry();
        BlockBuilder nestedRowValueBuilder = nestedRowBuilder.getBlockBuilder(0);
        if (i % 2 == 0) {
            nestedRowValueBuilder.appendNull();
        } else {
            INTEGER.writeLong(nestedRowValueBuilder, i);
        }
        nestedRowBuilder.closeEntry();
        directEntryBlockBuilder.closeEntry();
        directEntryBlockBuilder.appendNull();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowType(com.facebook.presto.common.type.RowType) SingleRowBlockWriter(com.facebook.presto.common.block.SingleRowBlockWriter) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 34 with MapType

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

the class TestDirectEntryBlockBuilder method testArrayWithNestedMap.

@Test
public void testArrayWithNestedMap() {
    MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
    ArrayBlockBuilder beginEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        SingleArrayBlockWriter singleArrayBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
        for (int j = 0; j < 10; j++) {
            BlockBuilder innerMapWriter = singleArrayBlockWriter.beginBlockEntry();
            BIGINT.writeLong(innerMapWriter, i);
            VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value1" + j));
            BIGINT.writeLong(innerMapWriter, i + 1);
            VARCHAR.writeSlice(innerMapWriter, utf8Slice("Value2" + j));
            singleArrayBlockWriter.closeEntry();
            singleArrayBlockWriter.appendNull();
        }
        beginEntryBlockBuilder.closeEntry();
    }
    ArrayBlockBuilder directEntryBlockBuilder = new ArrayBlockBuilder(mapType, null, POSITION_COUNT);
    for (int i = 0; i < POSITION_COUNT; i++) {
        directEntryBlockBuilder.beginDirectEntry();
        MapBlockBuilder innerBuilder = (MapBlockBuilder) directEntryBlockBuilder.getElementBlockBuilder();
        BlockBuilder keyBuilder = innerBuilder.getKeyBlockBuilder();
        BlockBuilder valueBuilder = innerBuilder.getValueBlockBuilder();
        for (int j = 0; j < 10; j++) {
            innerBuilder.beginDirectEntry();
            BIGINT.writeLong(keyBuilder, i);
            BIGINT.writeLong(keyBuilder, i + 1);
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
            VARCHAR.writeSlice(valueBuilder, utf8Slice("Value2" + j));
            innerBuilder.closeEntry();
            innerBuilder.appendNull();
        }
        directEntryBlockBuilder.closeEntry();
    }
    Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
    Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
    assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
Also used : ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) SingleArrayBlockWriter(com.facebook.presto.common.block.SingleArrayBlockWriter) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) Slice(io.airlift.slice.Slice) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) MapType(com.facebook.presto.common.type.MapType) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) RowBlockBuilder(com.facebook.presto.common.block.RowBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) ArrayBlockBuilder(com.facebook.presto.common.block.ArrayBlockBuilder) Test(org.testng.annotations.Test)

Example 35 with MapType

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

the class TestMapBlock method createBlockBuilderWithValues.

private static BlockBuilder createBlockBuilderWithValues(Map<String, Long>[] maps) {
    MapType mapType = mapType(VARCHAR, BIGINT);
    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, 1);
    for (Map<String, Long> map : maps) {
        createBlockBuilderWithValues(map, mapBlockBuilder);
    }
    return mapBlockBuilder;
}
Also used : MapType(com.facebook.presto.common.type.MapType) MapBlockBuilder(com.facebook.presto.common.block.MapBlockBuilder) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Aggregations

MapType (com.facebook.presto.common.type.MapType)92 Type (com.facebook.presto.common.type.Type)49 ArrayType (com.facebook.presto.common.type.ArrayType)40 Test (org.testng.annotations.Test)32 RowType (com.facebook.presto.common.type.RowType)30 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)24 Block (com.facebook.presto.common.block.Block)21 HashMap (java.util.HashMap)12 DecimalType (com.facebook.presto.common.type.DecimalType)11 ImmutableList (com.google.common.collect.ImmutableList)11 List (java.util.List)11 Map (java.util.Map)11 VarcharType (com.facebook.presto.common.type.VarcharType)9 MethodHandle (java.lang.invoke.MethodHandle)9 ArrayList (java.util.ArrayList)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 SingleMapBlock (com.facebook.presto.common.block.SingleMapBlock)7 PrestoException (com.facebook.presto.spi.PrestoException)7 OperatorType (com.facebook.presto.common.function.OperatorType)6 MapBlockBuilder (com.facebook.presto.common.block.MapBlockBuilder)5