Search in sources :

Example 1 with UnknownType

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

the class ArrayJoin method specializeArrayJoin.

private static ScalarFunctionImplementation specializeArrayJoin(Map<String, Type> types, FunctionRegistry functionRegistry, List<Boolean> nullableArguments, MethodHandle methodHandle) {
    Type type = types.get("T");
    if (type instanceof UnknownType) {
        return new ScalarFunctionImplementation(false, nullableArguments, methodHandle.bindTo(null), true);
    } else {
        try {
            ScalarFunctionImplementation castFunction = functionRegistry.getScalarFunctionImplementation(internalOperator(CAST.name(), VARCHAR_TYPE_SIGNATURE, ImmutableList.of(type.getTypeSignature())));
            MethodHandle getter;
            Class<?> elementType = type.getJavaType();
            if (elementType == boolean.class) {
                getter = GET_BOOLEAN;
            } else if (elementType == double.class) {
                getter = GET_DOUBLE;
            } else if (elementType == long.class) {
                getter = GET_LONG;
            } else if (elementType == Slice.class) {
                getter = GET_SLICE;
            } else {
                throw new UnsupportedOperationException("Unsupported type: " + elementType.getClass().getName());
            }
            MethodHandle cast = castFunction.getMethodHandle();
            // if the cast doesn't take a ConnectorSession, create an adapter that drops the provided session
            if (cast.type().parameterArray()[0] != ConnectorSession.class) {
                cast = MethodHandles.dropArguments(cast, 0, ConnectorSession.class);
            }
            // Adapt a target cast that takes (ConnectorSession, ?) to one that takes (Block, int, ConnectorSession), which will be invoked by the implementation
            // The first two arguments (Block, int) are filtered through the element type's getXXX method to produce the underlying value that needs to be passed to
            // the cast.
            cast = MethodHandles.permuteArguments(cast, MethodType.methodType(Slice.class, cast.type().parameterArray()[1], cast.type().parameterArray()[0]), 1, 0);
            cast = MethodHandles.dropArguments(cast, 1, int.class);
            cast = MethodHandles.dropArguments(cast, 1, Block.class);
            cast = MethodHandles.foldArguments(cast, getter.bindTo(type));
            MethodHandle target = MethodHandles.insertArguments(methodHandle, 0, cast);
            return new ScalarFunctionImplementation(false, nullableArguments, target, true);
        } catch (PrestoException e) {
            throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
        }
    }
}
Also used : UnknownType(com.facebook.presto.type.UnknownType) Type(com.facebook.presto.spi.type.Type) UnknownType(com.facebook.presto.type.UnknownType) MethodType(java.lang.invoke.MethodType) Slice(io.airlift.slice.Slice) Block(com.facebook.presto.spi.block.Block) ConnectorSession(com.facebook.presto.spi.ConnectorSession) PrestoException(com.facebook.presto.spi.PrestoException) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

ConnectorSession (com.facebook.presto.spi.ConnectorSession)1 PrestoException (com.facebook.presto.spi.PrestoException)1 Block (com.facebook.presto.spi.block.Block)1 Type (com.facebook.presto.spi.type.Type)1 UnknownType (com.facebook.presto.type.UnknownType)1 Slice (io.airlift.slice.Slice)1 MethodHandle (java.lang.invoke.MethodHandle)1 MethodType (java.lang.invoke.MethodType)1