Search in sources :

Example 6 with TypeParameter

use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.

the class ArrayFilterFunction method filterBoolean.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = boolean.class)
@SqlType("array(T)")
public static Block filterBoolean(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Boolean input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getBoolean(arrayBlock, position);
        }
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(input);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 7 with TypeParameter

use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.

the class ArrayFilterFunction method filterVoid.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = void.class)
@SqlType("array(T)")
public static Block filterVoid(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(null);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            resultBuilder.appendNull();
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 8 with TypeParameter

use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.

the class ArrayFilterFunction method filterDouble.

@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = double.class)
@SqlType("array(T)")
public static Block filterDouble(@TypeParameter("T") Type elementType, @SqlType("array(T)") Block arrayBlock, @SqlType("function(T, boolean)") MethodHandle function) {
    int positionCount = arrayBlock.getPositionCount();
    BlockBuilder resultBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
    for (int position = 0; position < positionCount; position++) {
        Double input = null;
        if (!arrayBlock.isNull(position)) {
            input = elementType.getDouble(arrayBlock, position);
        }
        Boolean keep;
        try {
            keep = (Boolean) function.invokeExact(input);
        } catch (Throwable throwable) {
            throw Throwables.propagate(throwable);
        }
        if (TRUE.equals(keep)) {
            elementType.appendTo(arrayBlock, position, resultBuilder);
        }
    }
    return resultBuilder.build();
}
Also used : BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameterSpecialization(com.facebook.presto.spi.function.TypeParameterSpecialization) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 9 with TypeParameter

use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.

the class MapToMapCast method toMap.

@TypeParameter("FK")
@TypeParameter("FV")
@TypeParameter("TK")
@TypeParameter("TV")
@SqlType("map(TK,TV)")
public static Block toMap(@OperatorDependency(operator = EQUAL, returnType = StandardTypes.BOOLEAN, argumentTypes = { "TK", "TK" }) MethodHandle toKeyEqualsFunction, @OperatorDependency(operator = CAST, returnType = "TK", argumentTypes = { "FK" }) MethodHandle keyCastFunction, @OperatorDependency(operator = CAST, returnType = "TV", argumentTypes = { "FV" }) MethodHandle valueCastFunction, @TypeParameter("FK") Type fromKeyType, @TypeParameter("FV") Type fromValueType, @TypeParameter("TK") Type toKeyType, @TypeParameter("TV") Type toValueType, ConnectorSession session, @SqlType("map(FK,FV)") Block fromMap) {
    // loop over all the parameter types and bind ConnectorSession if needed
    // TODO: binding `ConnectorSession` should be done in function invocation framework
    Class<?>[] parameterTypes = keyCastFunction.type().parameterArray();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (parameterTypes[i] == ConnectorSession.class) {
            keyCastFunction = MethodHandles.insertArguments(keyCastFunction, i, session);
            break;
        }
    }
    parameterTypes = valueCastFunction.type().parameterArray();
    for (int i = 0; i < parameterTypes.length; i++) {
        if (parameterTypes[i] == ConnectorSession.class) {
            valueCastFunction = MethodHandles.insertArguments(valueCastFunction, i, session);
            break;
        }
    }
    TypedSet typedSet = new TypedSet(toKeyType, fromMap.getPositionCount() / 2);
    BlockBuilder keyBlockBuilder = toKeyType.createBlockBuilder(new BlockBuilderStatus(), fromMap.getPositionCount() / 2);
    for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
        Object fromKey = readNativeValue(fromKeyType, fromMap, i);
        try {
            Object toKey = keyCastFunction.invoke(fromKey);
            if (toKey == null) {
                throw new PrestoException(StandardErrorCode.INVALID_CAST_ARGUMENT, "map key is null");
            }
            writeNativeValue(toKeyType, keyBlockBuilder, toKey);
        } catch (Throwable t) {
            Throwables.propagateIfInstanceOf(t, Error.class);
            Throwables.propagateIfInstanceOf(t, PrestoException.class);
            throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, t);
        }
    }
    Block keyBlock = keyBlockBuilder.build();
    BlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(toKeyType, toValueType), new BlockBuilderStatus(), fromMap.getPositionCount());
    for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
        if (!typedSet.contains(keyBlock, i / 2)) {
            typedSet.add(keyBlock, i / 2);
            toKeyType.appendTo(keyBlock, i / 2, blockBuilder);
            if (fromMap.isNull(i + 1)) {
                blockBuilder.appendNull();
                continue;
            }
            Object fromValue = readNativeValue(fromValueType, fromMap, i + 1);
            try {
                Object toValue = valueCastFunction.invoke(fromValue);
                writeNativeValue(toValueType, blockBuilder, toValue);
            } catch (Throwable t) {
                Throwables.propagateIfInstanceOf(t, Error.class);
                Throwables.propagateIfInstanceOf(t, PrestoException.class);
                throw new PrestoException(StandardErrorCode.GENERIC_INTERNAL_ERROR, t);
            }
        } else {
            // if there are duplicated keys, fail it!
            throw new PrestoException(StandardErrorCode.INVALID_CAST_ARGUMENT, "duplicate keys");
        }
    }
    return blockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) Block(com.facebook.presto.spi.block.Block) PrestoException(com.facebook.presto.spi.PrestoException) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Example 10 with TypeParameter

use of com.facebook.presto.spi.function.TypeParameter in project presto by prestodb.

the class ArrayUnionFunction method union.

@TypeParameter("E")
@SqlType("array(E)")
public static Block union(@TypeParameter("E") Type type, @SqlType("array(E)") Block leftArray, @SqlType("array(E)") Block rightArray) {
    int leftArrayCount = leftArray.getPositionCount();
    int rightArrayCount = rightArray.getPositionCount();
    TypedSet typedSet = new TypedSet(type, leftArrayCount + rightArrayCount);
    BlockBuilder distinctElementBlockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), leftArrayCount + rightArrayCount);
    appendTypedArray(leftArray, type, typedSet, distinctElementBlockBuilder);
    appendTypedArray(rightArray, type, typedSet, distinctElementBlockBuilder);
    return distinctElementBlockBuilder.build();
}
Also used : TypedSet(com.facebook.presto.operator.aggregation.TypedSet) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) TypeParameter(com.facebook.presto.spi.function.TypeParameter) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

SqlType (com.facebook.presto.spi.function.SqlType)24 TypeParameter (com.facebook.presto.spi.function.TypeParameter)24 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)15 PrestoException (com.facebook.presto.spi.PrestoException)11 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)11 TypeParameterSpecialization (com.facebook.presto.spi.function.TypeParameterSpecialization)9 TypedSet (com.facebook.presto.operator.aggregation.TypedSet)4 Block (com.facebook.presto.spi.block.Block)2 InterleavedBlockBuilder (com.facebook.presto.spi.block.InterleavedBlockBuilder)1 Slice (io.airlift.slice.Slice)1 ArrayList (java.util.ArrayList)1