Search in sources :

Example 1 with FunctionInvoker

use of com.facebook.presto.sql.FunctionInvoker in project presto by prestodb.

the class MapSubscriptOperator method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Type keyType = boundVariables.getTypeVariable("K");
    Type valueType = boundVariables.getTypeVariable("V");
    MethodHandle keyEqualsMethod = functionRegistry.getScalarFunctionImplementation(internalOperator(OperatorType.EQUAL, BooleanType.BOOLEAN, ImmutableList.of(keyType, keyType))).getMethodHandle();
    MethodHandle methodHandle;
    if (keyType.getJavaType() == boolean.class) {
        methodHandle = METHOD_HANDLE_BOOLEAN;
    } else if (keyType.getJavaType() == long.class) {
        methodHandle = METHOD_HANDLE_LONG;
    } else if (keyType.getJavaType() == double.class) {
        methodHandle = METHOD_HANDLE_DOUBLE;
    } else if (keyType.getJavaType() == Slice.class) {
        methodHandle = METHOD_HANDLE_SLICE;
    } else {
        methodHandle = METHOD_HANDLE_OBJECT;
    }
    methodHandle = MethodHandles.insertArguments(methodHandle, 0, legacyMissingKey);
    FunctionInvoker functionInvoker = new FunctionInvoker(functionRegistry);
    methodHandle = methodHandle.bindTo(functionInvoker).bindTo(keyEqualsMethod).bindTo(keyType).bindTo(valueType);
    // this casting is necessary because otherwise presto byte code generator will generate illegal byte code
    if (valueType.getJavaType() == void.class) {
        methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(void.class));
    } else {
        methodHandle = methodHandle.asType(methodHandle.type().changeReturnType(Primitives.wrap(valueType.getJavaType())));
    }
    return new ScalarFunctionImplementation(true, ImmutableList.of(false, false), methodHandle, isDeterministic());
}
Also used : Type(com.facebook.presto.spi.type.Type) BooleanType(com.facebook.presto.spi.type.BooleanType) OperatorType(com.facebook.presto.spi.function.OperatorType) VarcharType(com.facebook.presto.spi.type.VarcharType) Slice(io.airlift.slice.Slice) FunctionInvoker(com.facebook.presto.sql.FunctionInvoker) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

OperatorType (com.facebook.presto.spi.function.OperatorType)1 BooleanType (com.facebook.presto.spi.type.BooleanType)1 Type (com.facebook.presto.spi.type.Type)1 VarcharType (com.facebook.presto.spi.type.VarcharType)1 FunctionInvoker (com.facebook.presto.sql.FunctionInvoker)1 Slice (io.airlift.slice.Slice)1 MethodHandle (java.lang.invoke.MethodHandle)1