use of com.facebook.presto.operator.scalar.ScalarFunctionImplementationChoice.NullConvention.USE_BOXED_TYPE in project presto by prestodb.
the class ArrayJoin method specializeArrayJoin.
private static BuiltInScalarFunctionImplementation specializeArrayJoin(Map<String, Type> types, FunctionAndTypeManager functionAndTypeManager, List<Boolean> nullableArguments, MethodHandle methodHandleStack, MethodHandle methodHandleProvidedBlock) {
Type type = types.get("T");
List<ArgumentProperty> argumentProperties = nullableArguments.stream().map(nullable -> nullable ? valueTypeArgumentProperty(USE_BOXED_TYPE) : valueTypeArgumentProperty(RETURN_NULL_ON_NULL)).collect(toImmutableList());
if (type instanceof UnknownType) {
return new BuiltInScalarFunctionImplementation(false, argumentProperties, methodHandleStack.bindTo(null), Optional.empty());
} else {
try {
MethodHandle cast = functionAndTypeManager.getJavaScalarFunctionImplementation(functionAndTypeManager.lookupCast(CAST, type.getTypeSignature(), VARCHAR_TYPE_SIGNATURE)).getMethodHandle();
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.getName());
}
// if the cast doesn't take a SqlFunctionProperties, create an adapter that drops the provided session
if (cast.type().parameterArray()[0] != SqlFunctionProperties.class) {
cast = MethodHandles.dropArguments(cast, 0, SqlFunctionProperties.class);
}
// Adapt a target cast that takes (SqlFunctionProperties, ?) to one that takes (Block, int, SqlFunctionProperties), 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 targetStack = MethodHandles.insertArguments(methodHandleStack, 0, cast);
MethodHandle targetProvidedBlock = MethodHandles.insertArguments(methodHandleProvidedBlock, 0, cast);
return new BuiltInScalarFunctionImplementation(ImmutableList.of(new ScalarFunctionImplementationChoice(false, argumentProperties, STACK, targetStack, Optional.empty()), new ScalarFunctionImplementationChoice(false, argumentProperties, PROVIDED_BLOCKBUILDER, targetProvidedBlock, Optional.empty())));
} catch (PrestoException e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Input type %s not supported", type), e);
}
}
}
Aggregations