Search in sources :

Example 21 with MapType

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

the class MapZipWithFunction method mapZipWith.

public static Block mapZipWith(Type keyType, Type leftValueType, Type rightValueType, MapType outputMapType, MethodHandle keyNativeHashCode, MethodHandle keyBlockNativeEquals, MethodHandle keyBlockHashCode, Block leftBlock, Block rightBlock, MapZipWithLambda function) {
    SingleMapBlock leftMapBlock = (SingleMapBlock) leftBlock;
    SingleMapBlock rightMapBlock = (SingleMapBlock) rightBlock;
    Type outputValueType = outputMapType.getValueType();
    BlockBuilder mapBlockBuilder = outputMapType.createBlockBuilder(null, max(leftMapBlock.getPositionCount(), rightMapBlock.getPositionCount()) / 2);
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    // seekKey() can take non-trivial time when key is complicated value, such as a long VARCHAR or ROW.
    boolean[] keyFound = new boolean[rightMapBlock.getPositionCount()];
    for (int leftKeyPosition = 0; leftKeyPosition < leftMapBlock.getPositionCount(); leftKeyPosition += 2) {
        Object key = readNativeValue(keyType, leftMapBlock, leftKeyPosition);
        Object leftValue = readNativeValue(leftValueType, leftMapBlock, leftKeyPosition + 1);
        int rightValuePosition;
        try {
            rightValuePosition = rightMapBlock.seekKey(key, keyNativeHashCode, keyBlockNativeEquals, keyBlockHashCode);
        } catch (NotSupportedException e) {
            throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
        }
        Object rightValue = null;
        if (rightValuePosition != -1) {
            rightValue = readNativeValue(rightValueType, rightMapBlock, rightValuePosition);
            keyFound[rightValuePosition / 2] = true;
        }
        Object outputValue;
        try {
            outputValue = function.apply(key, leftValue, rightValue);
        } catch (Throwable throwable) {
            // Restore pageBuilder into a consistent state.
            mapBlockBuilder.closeEntry();
            throwIfUnchecked(throwable);
            throw new RuntimeException(throwable);
        }
        keyType.appendTo(leftMapBlock, leftKeyPosition, blockBuilder);
        writeNativeValue(outputValueType, blockBuilder, outputValue);
    }
    // iterate over keys that only exists in rightMapBlock
    for (int rightKeyPosition = 0; rightKeyPosition < rightMapBlock.getPositionCount(); rightKeyPosition += 2) {
        if (!keyFound[rightKeyPosition / 2]) {
            Object key = readNativeValue(keyType, rightMapBlock, rightKeyPosition);
            Object rightValue = readNativeValue(rightValueType, rightMapBlock, rightKeyPosition + 1);
            Object outputValue;
            try {
                outputValue = function.apply(key, null, rightValue);
            } catch (Throwable throwable) {
                // Restore pageBuilder into a consistent state.
                mapBlockBuilder.closeEntry();
                throwIfUnchecked(throwable);
                throw new RuntimeException(throwable);
            }
            keyType.appendTo(rightMapBlock, rightKeyPosition, blockBuilder);
            writeNativeValue(outputValueType, blockBuilder, outputValue);
        }
    }
    mapBlockBuilder.closeEntry();
    return outputMapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) PrestoException(com.facebook.presto.spi.PrestoException) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) NotSupportedException(com.facebook.presto.common.NotSupportedException) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 22 with MapType

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

the class JsonToMapCast method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    checkArgument(arity == 1, "Expected arity to be 1");
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType mapType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    checkCondition(canCastFromJson(mapType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", mapType);
    BlockBuilderAppender keyAppender = createBlockBuilderAppender(mapType.getKeyType());
    BlockBuilderAppender valueAppender = createBlockBuilderAppender(mapType.getValueType());
    MethodHandle methodHandle = METHOD_HANDLE.bindTo(mapType).bindTo(keyAppender).bindTo(valueAppender);
    return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) OperatorType(com.facebook.presto.common.function.OperatorType) BlockBuilderAppender.createBlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender.createBlockBuilderAppender) BlockBuilderAppender(com.facebook.presto.util.JsonUtil.BlockBuilderAppender) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle)

Example 23 with MapType

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

the class MapConcatFunction method specialize.

@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
    if (arity < 2) {
        throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "There must be two or more concatenation arguments to " + FUNCTION_NAME);
    }
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MapType mapType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
    VarArgMethodHandle varArgMethodHandle = generateVarArgsToArrayAdapter(Block.class, Block.class, arity, METHOD_HANDLE.bindTo(mapType));
    return new BuiltInScalarFunctionImplementation(false, nCopies(arity, valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), varArgMethodHandle.getMethodHandle());
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) VarArgMethodHandle(com.facebook.presto.sql.gen.VarArgsToArrayAdapterGenerator.VarArgMethodHandle) PrestoException(com.facebook.presto.spi.PrestoException) MapType(com.facebook.presto.common.type.MapType)

Example 24 with MapType

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

the class MapConcatFunction method mapConcat.

@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Block[] maps) {
    int entries = 0;
    int lastMapIndex = maps.length - 1;
    int firstMapIndex = lastMapIndex;
    for (int i = 0; i < maps.length; i++) {
        entries += maps[i].getPositionCount();
        if (maps[i].getPositionCount() > 0) {
            lastMapIndex = i;
            firstMapIndex = min(firstMapIndex, i);
        }
    }
    if (lastMapIndex == firstMapIndex) {
        return maps[lastMapIndex];
    }
    Type keyType = mapType.getKeyType();
    Type valueType = mapType.getValueType();
    // We need to divide the entries by 2 because the maps array is SingleMapBlocks and it had the positionCount twice as large as a normal Block
    OptimizedTypedSet typedSet = new OptimizedTypedSet(keyType, maps.length, entries / 2);
    for (int i = lastMapIndex; i >= firstMapIndex; i--) {
        SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
        Block keyBlock = singleMapBlock.getKeyBlock();
        typedSet.union(keyBlock);
    }
    List<SelectedPositions> selectedPositionsList = typedSet.getPositionsForBlocks();
    BlockBuilder mapBlockBuilder = mapType.createBlockBuilder(null, selectedPositionsList.size());
    BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
    for (int i = lastMapIndex; i >= firstMapIndex; i--) {
        SingleMapBlock singleMapBlock = (SingleMapBlock) maps[i];
        // selectedPositions was ordered by addUnion sequence therefore the order should be reversed.
        SelectedPositions selectedPositions = selectedPositionsList.get(lastMapIndex - i);
        assert selectedPositions.isList();
        int[] positions = selectedPositions.getPositions();
        for (int j = 0; j < selectedPositions.size(); j++) {
            int position = positions[j];
            keyType.appendTo(singleMapBlock, 2 * position, blockBuilder);
            valueType.appendTo(singleMapBlock, 2 * position + 1, blockBuilder);
        }
    }
    mapBlockBuilder.closeEntry();
    return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Also used : MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) OptimizedTypedSet(com.facebook.presto.operator.aggregation.OptimizedTypedSet) SelectedPositions(com.facebook.presto.operator.project.SelectedPositions) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) Block(com.facebook.presto.common.block.Block) SingleMapBlock(com.facebook.presto.common.block.SingleMapBlock) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 25 with MapType

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

the class MapDistinctFromOperator method isDistinctFrom.

@TypeParameter("K")
@TypeParameter("V")
@SqlType(StandardTypes.BOOLEAN)
public static boolean isDistinctFrom(@OperatorDependency(operator = EQUAL, argumentTypes = { "K", "K" }) MethodHandle keyEqualsFunction, @OperatorDependency(operator = HASH_CODE, argumentTypes = { "K" }) MethodHandle keyHashcodeFunction, @OperatorDependency(operator = IS_DISTINCT_FROM, argumentTypes = { "V", "V" }, convention = @Convention(arguments = { BLOCK_POSITION, BLOCK_POSITION }, result = FAIL_ON_NULL)) MethodHandle valueDistinctFromFunction, @TypeParameter("map(K, V)") Type mapType, @SqlType("map(K,V)") Block leftMapBlock, @IsNull boolean leftMapNull, @SqlType("map(K,V)") Block rightMapBlock, @IsNull boolean rightMapNull) {
    if (leftMapNull != rightMapNull) {
        return true;
    }
    if (leftMapNull) {
        return false;
    }
    Type keyType = ((MapType) mapType).getKeyType();
    MethodHandle keyBlockEqualsFunction = compose(keyEqualsFunction, nativeValueGetter(keyType));
    MethodHandle keyBlockHashCodeFunction = compose(keyHashcodeFunction, nativeValueGetter(keyType));
    // Note that we compare to NOT distinct here and so negate the result.
    return !MapGenericEquality.genericEqual(keyType, keyHashcodeFunction, keyBlockEqualsFunction, keyBlockHashCodeFunction, leftMapBlock, rightMapBlock, (leftMapIndex, rightMapIndex) -> !(boolean) valueDistinctFromFunction.invokeExact(leftMapBlock, leftMapIndex, rightMapBlock, rightMapIndex));
}
Also used : Convention(com.facebook.presto.spi.function.Convention) HASH_CODE(com.facebook.presto.common.function.OperatorType.HASH_CODE) MethodHandle(java.lang.invoke.MethodHandle) MapType(com.facebook.presto.common.type.MapType) StandardTypes(com.facebook.presto.common.type.StandardTypes) OperatorDependency(com.facebook.presto.spi.function.OperatorDependency) TypeParameter(com.facebook.presto.spi.function.TypeParameter) BLOCK_POSITION(com.facebook.presto.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION) MethodHandleUtil.compose(com.facebook.presto.common.block.MethodHandleUtil.compose) BlockPosition(com.facebook.presto.spi.function.BlockPosition) IS_DISTINCT_FROM(com.facebook.presto.common.function.OperatorType.IS_DISTINCT_FROM) BlockIndex(com.facebook.presto.spi.function.BlockIndex) MethodHandleUtil.nativeValueGetter(com.facebook.presto.common.block.MethodHandleUtil.nativeValueGetter) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) IsNull(com.facebook.presto.spi.function.IsNull) EQUAL(com.facebook.presto.common.function.OperatorType.EQUAL) FAIL_ON_NULL(com.facebook.presto.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL) Block(com.facebook.presto.common.block.Block) Type(com.facebook.presto.common.type.Type) SqlType(com.facebook.presto.spi.function.SqlType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) SqlType(com.facebook.presto.spi.function.SqlType) MapType(com.facebook.presto.common.type.MapType) MethodHandle(java.lang.invoke.MethodHandle) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

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