Search in sources :

Example 41 with MapType

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

the class TestBlockEncodingBuffers method buildMapBlockStatus.

private BlockStatus buildMapBlockStatus(MapType mapType, int positionCount, boolean isView, Optional<boolean[]> isNull, int[] offsets, float primitiveNullRate, float nestedNullRate, List<Encoding> wrappings) {
    BlockStatus blockStatus;
    BlockStatus keyBlockStatus = buildBlockStatusWithType(mapType.getKeyType(), offsets[positionCount], isView, 0.0f, 0.0f, wrappings);
    BlockStatus valueBlockStatus = buildBlockStatusWithType(mapType.getValueType(), offsets[positionCount], isView, primitiveNullRate, nestedNullRate, wrappings);
    int[] expectedKeySizes = keyBlockStatus.expectedRowSizes;
    int[] expectedValueSizes = valueBlockStatus.expectedRowSizes;
    // Use expectedKeySizes for the total size for both key and values
    Arrays.setAll(expectedKeySizes, i -> expectedKeySizes[i] + expectedValueSizes[i]);
    int[] expectedRowSizes = IntStream.range(0, positionCount).map(i -> MapBlockEncodingBuffer.POSITION_SIZE + Arrays.stream(expectedKeySizes, offsets[i], offsets[i + 1]).sum()).toArray();
    Type keyType = mapType.getKeyType();
    blockStatus = new BlockStatus(fromKeyValueBlock(positionCount, isNull, offsets, keyBlockStatus.block, valueBlockStatus.block), expectedRowSizes);
    return blockStatus;
}
Also used : MapBlock.fromKeyValueBlock(com.facebook.presto.common.block.MapBlock.fromKeyValueBlock) Page(com.facebook.presto.common.Page) Arrays(java.util.Arrays) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) GRACE_FACTOR_FOR_MAX_BUFFER_CAPACITY(com.facebook.presto.operator.repartition.AbstractBlockEncodingBuffer.GRACE_FACTOR_FOR_MAX_BUFFER_CAPACITY) Test(org.testng.annotations.Test) SIZE_OF_BYTE(io.airlift.slice.SizeOf.SIZE_OF_BYTE) DICTIONARY(com.facebook.presto.block.BlockAssertions.Encoding.DICTIONARY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) AbstractBlockEncodingBuffer.createBlockEncodingBuffers(com.facebook.presto.operator.repartition.AbstractBlockEncodingBuffer.createBlockEncodingBuffers) HASH_MULTIPLIER(com.facebook.presto.operator.repartition.MapBlockEncodingBuffer.HASH_MULTIPLIER) BlockAssertions.assertBlockEquals(com.facebook.presto.block.BlockAssertions.assertBlockEquals) OptimizedPartitionedOutputOperator.decodeBlock(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.decodeBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) SIZE_OF_INT(io.airlift.slice.SizeOf.SIZE_OF_INT) BlockAssertions.createRandomSmallintsBlock(com.facebook.presto.block.BlockAssertions.createRandomSmallintsBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) UncheckedStackArrayAllocator(com.facebook.presto.operator.UncheckedStackArrayAllocator) Encoding(com.facebook.presto.block.BlockAssertions.Encoding) BlockAssertions.createAllNullsBlock(com.facebook.presto.block.BlockAssertions.createAllNullsBlock) String.format(java.lang.String.format) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) INTEGER(com.facebook.presto.common.type.IntegerType.INTEGER) Optional(java.util.Optional) BlockAssertions.createRandomBooleansBlock(com.facebook.presto.block.BlockAssertions.createRandomBooleansBlock) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) IntStream(java.util.stream.IntStream) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) SliceOutput(io.airlift.slice.SliceOutput) RowType.withDefaultFieldNames(com.facebook.presto.common.type.RowType.withDefaultFieldNames) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) Assert.assertEquals(org.testng.Assert.assertEquals) MAX_SHORT_PRECISION(com.facebook.presto.common.type.Decimals.MAX_SHORT_PRECISION) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) ImmutableList(com.google.common.collect.ImmutableList) Closer(com.google.common.io.Closer) BlockEncodingManager(com.facebook.presto.common.block.BlockEncodingManager) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomShortDecimalsBlock) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RowBlock.fromFieldBlocks(com.facebook.presto.common.block.RowBlock.fromFieldBlocks) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) RUN_LENGTH(com.facebook.presto.block.BlockAssertions.Encoding.RUN_LENGTH) Math.toIntExact(java.lang.Math.toIntExact) BlockAssertions.createRleBlockWithRandomValue(com.facebook.presto.block.BlockAssertions.createRleBlockWithRandomValue) Type(com.facebook.presto.common.type.Type) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) BlockFlattener(com.facebook.presto.common.block.BlockFlattener) IOException(java.io.IOException) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) BlockSerdeUtil.readBlock(com.facebook.presto.common.block.BlockSerdeUtil.readBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) SMALLINT(com.facebook.presto.common.type.SmallintType.SMALLINT) SIZE_OF_LONG(io.airlift.slice.SizeOf.SIZE_OF_LONG) BlockAssertions.createRandomIntsBlock(com.facebook.presto.block.BlockAssertions.createRandomIntsBlock) Block(com.facebook.presto.common.block.Block) BlockAssertions.createRandomLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongDecimalsBlock) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) ArrayType(com.facebook.presto.common.type.ArrayType) StructuralTestUtil.mapType(com.facebook.presto.util.StructuralTestUtil.mapType) Type(com.facebook.presto.common.type.Type) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType)

Example 42 with MapType

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

the class TestHistogram method testMapHistograms.

@Test
public void testMapHistograms() {
    MapType innerMapType = mapType(VARCHAR, VARCHAR);
    InternalAggregationFunction aggregationFunction = getAggregation(innerMapType);
    BlockBuilder builder = innerMapType.createBlockBuilder(null, 3);
    innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("a", "b")));
    innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("c", "d")));
    innerMapType.writeObject(builder, mapBlockOf(VARCHAR, VARCHAR, ImmutableMap.of("e", "f")));
    assertAggregation(aggregationFunction, ImmutableMap.of(ImmutableMap.of("a", "b"), 1L, ImmutableMap.of("c", "d"), 1L, ImmutableMap.of("e", "f"), 1L), builder.build());
}
Also used : MapType(com.facebook.presto.common.type.MapType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) Test(org.testng.annotations.Test)

Example 43 with MapType

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

the class TestMapUnionAggregation method testSimpleWithNulls.

@Test
public void testSimpleWithNulls() {
    MapType mapType = mapType(DOUBLE, VARCHAR);
    FunctionHandle functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
    InternalAggregationFunction aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
    Map<Object, Object> expected = mapOf(23.0, "aaa", 33.0, null, 43.0, "ccc", 53.0, "ddd");
    assertAggregation(aggFunc, expected, arrayBlockOf(mapType, mapBlockOf(DOUBLE, VARCHAR, mapOf(23.0, "aaa", 33.0, null, 53.0, "ddd")), null, mapBlockOf(DOUBLE, VARCHAR, mapOf(43.0, "ccc", 53.0, "ddd"))));
}
Also used : FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 44 with MapType

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

the class TestMapUnionAggregation method testStructural.

@Test
public void testStructural() {
    MapType mapType = mapType(DOUBLE, new ArrayType(VARCHAR));
    FunctionHandle functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
    InternalAggregationFunction aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
    assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f"), 4.0, ImmutableList.of("r", "s")), arrayBlockOf(mapType, mapBlockOf(DOUBLE, new ArrayType(VARCHAR), ImmutableMap.of(1.0, ImmutableList.of("a", "b"), 2.0, ImmutableList.of("c", "d"), 3.0, ImmutableList.of("e", "f"))), mapBlockOf(DOUBLE, new ArrayType(VARCHAR), ImmutableMap.of(1.0, ImmutableList.of("x", "y"), 4.0, ImmutableList.of("r", "s"), 3.0, ImmutableList.of("w", "z")))));
    mapType = mapType(DOUBLE, mapType(VARCHAR, VARCHAR));
    functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
    aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
    assertAggregation(aggFunc, ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"), 3.0, ImmutableMap.of("e", "f")), arrayBlockOf(mapType, mapBlockOf(DOUBLE, mapType(VARCHAR, VARCHAR), ImmutableMap.of(1.0, ImmutableMap.of("a", "b"), 2.0, ImmutableMap.of("c", "d"))), mapBlockOf(DOUBLE, mapType(VARCHAR, VARCHAR), ImmutableMap.of(3.0, ImmutableMap.of("e", "f")))));
    mapType = mapType(new ArrayType(VARCHAR), DOUBLE);
    functionHandle = FUNCTION_AND_TYPE_MANAGER.lookupFunction(NAME, fromTypes(mapType));
    aggFunc = FUNCTION_AND_TYPE_MANAGER.getAggregateFunctionImplementation(functionHandle);
    assertAggregation(aggFunc, ImmutableMap.of(ImmutableList.of("a", "b"), 1.0, ImmutableList.of("c", "d"), 2.0, ImmutableList.of("e", "f"), 3.0), arrayBlockOf(mapType, mapBlockOf(new ArrayType(VARCHAR), DOUBLE, ImmutableMap.of(ImmutableList.of("a", "b"), 1.0, ImmutableList.of("e", "f"), 3.0)), mapBlockOf(new ArrayType(VARCHAR), DOUBLE, ImmutableMap.of(ImmutableList.of("c", "d"), 2.0))));
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) MapType(com.facebook.presto.common.type.MapType) Test(org.testng.annotations.Test)

Example 45 with MapType

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

the class TestBlockEncodingBuffers method buildBlockStatusWithType.

private BlockStatus buildBlockStatusWithType(Type type, int positionCount, boolean isView, float primitiveNullRate, float nestedNullRate, List<Encoding> wrappings) {
    BlockStatus blockStatus = null;
    if (isView) {
        positionCount *= 2;
    }
    if (type == BIGINT) {
        blockStatus = buildBigintBlockStatus(positionCount, primitiveNullRate);
    } else if (type instanceof DecimalType) {
        if (!((DecimalType) type).isShort()) {
            blockStatus = buildLongDecimalBlockStatus(positionCount, primitiveNullRate);
        } else {
            blockStatus = buildShortDecimalBlockStatus(positionCount, primitiveNullRate);
        }
    } else if (type == INTEGER) {
        blockStatus = buildIntegerBlockStatus(positionCount, primitiveNullRate);
    } else if (type == SMALLINT) {
        blockStatus = buildSmallintBlockStatus(positionCount, primitiveNullRate);
    } else if (type == BOOLEAN) {
        blockStatus = buildBooleanBlockStatus(positionCount, primitiveNullRate);
    } else if (type == VARCHAR) {
        blockStatus = buildVarcharBlockStatus(positionCount, primitiveNullRate, 10);
    } else {
        // Nested types
        // Build isNull and offsets of size positionCount
        boolean[] isNull = null;
        if (nestedNullRate > 0) {
            isNull = new boolean[positionCount];
        }
        int[] offsets = new int[positionCount + 1];
        for (int position = 0; position < positionCount; position++) {
            if (nestedNullRate > 0 && ThreadLocalRandom.current().nextDouble(1) < nestedNullRate) {
                isNull[position] = true;
                offsets[position + 1] = offsets[position];
            } else {
                offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : ThreadLocalRandom.current().nextInt(10) + 1);
            }
        }
        // Build the nested block of size offsets[positionCount].
        if (type instanceof ArrayType) {
            blockStatus = buildArrayBlockStatus((ArrayType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof MapType) {
            blockStatus = buildMapBlockStatus((MapType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else if (type instanceof RowType) {
            blockStatus = buildRowBlockStatus((RowType) type, positionCount, isView, Optional.ofNullable(isNull), offsets, primitiveNullRate, nestedNullRate, wrappings);
        } else {
            throw new UnsupportedOperationException(format("type %s is not supported.", type));
        }
    }
    if (isView) {
        positionCount /= 2;
        int offset = positionCount / 2;
        Block blockView = blockStatus.block.getRegion(offset, positionCount);
        int[] expectedRowSizesView = Arrays.stream(blockStatus.expectedRowSizes, offset, offset + positionCount).toArray();
        blockStatus = new BlockStatus(blockView, expectedRowSizesView);
    }
    blockStatus = buildDictRleBlockStatus(blockStatus, positionCount, wrappings);
    return blockStatus;
}
Also used : ArrayType(com.facebook.presto.common.type.ArrayType) DecimalType(com.facebook.presto.common.type.DecimalType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) RowType(com.facebook.presto.common.type.RowType) MapBlock.fromKeyValueBlock(com.facebook.presto.common.block.MapBlock.fromKeyValueBlock) BlockAssertions.createRandomLongsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongsBlock) OptimizedPartitionedOutputOperator.decodeBlock(com.facebook.presto.operator.repartition.OptimizedPartitionedOutputOperator.decodeBlock) BlockAssertions.createRandomDictionaryBlock(com.facebook.presto.block.BlockAssertions.createRandomDictionaryBlock) BlockAssertions.createRandomSmallintsBlock(com.facebook.presto.block.BlockAssertions.createRandomSmallintsBlock) ArrayBlock.fromElementBlock(com.facebook.presto.common.block.ArrayBlock.fromElementBlock) BlockAssertions.createAllNullsBlock(com.facebook.presto.block.BlockAssertions.createAllNullsBlock) BlockAssertions.createRandomBooleansBlock(com.facebook.presto.block.BlockAssertions.createRandomBooleansBlock) BlockAssertions.createRandomStringBlock(com.facebook.presto.block.BlockAssertions.createRandomStringBlock) BlockAssertions.createRLEBlock(com.facebook.presto.block.BlockAssertions.createRLEBlock) BlockAssertions.createRandomShortDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomShortDecimalsBlock) BlockAssertions.createStringsBlock(com.facebook.presto.block.BlockAssertions.createStringsBlock) BlockAssertions.wrapBlock(com.facebook.presto.block.BlockAssertions.wrapBlock) BlockSerdeUtil.readBlock(com.facebook.presto.common.block.BlockSerdeUtil.readBlock) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) BlockAssertions.createRandomIntsBlock(com.facebook.presto.block.BlockAssertions.createRandomIntsBlock) Block(com.facebook.presto.common.block.Block) BlockAssertions.createRandomLongDecimalsBlock(com.facebook.presto.block.BlockAssertions.createRandomLongDecimalsBlock) MapType(com.facebook.presto.common.type.MapType) BlockAssertions.createMapType(com.facebook.presto.block.BlockAssertions.createMapType)

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