use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class ArrayToJsonCast method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
checkArgument(arity == 1, "Expected arity to be 1");
Type type = boundVariables.getTypeVariable("T");
Type arrayType = typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.of(type.getTypeSignature())));
MethodHandle methodHandle = METHOD_HANDLE.bindTo(arrayType);
return new ScalarFunctionImplementation(false, ImmutableList.of(false), methodHandle, isDeterministic());
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class ArrayTransformFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
Type inputType = boundVariables.getTypeVariable("T");
Type outputType = boundVariables.getTypeVariable("U");
Class<?> generatedClass = generateTransform(inputType, outputType);
return new ScalarFunctionImplementation(false, ImmutableList.of(false, false), ImmutableList.of(false, false), methodHandle(generatedClass, "transform", PageBuilder.class, Block.class, MethodHandle.class), Optional.of(methodHandle(generatedClass, "createPageBuilder")), isDeterministic());
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class CastFromUnknownOperator method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
Type toType = boundVariables.getTypeVariable("E");
MethodHandle methodHandle;
if (toType.getJavaType() == long.class) {
methodHandle = METHOD_HANDLE_LONG;
} else if (toType.getJavaType() == double.class) {
methodHandle = METHOD_HANDLE_DOUBLE;
} else if (toType.getJavaType() == boolean.class) {
methodHandle = METHOD_HANDLE_BOOLEAN;
} else if (toType.getJavaType() == Slice.class) {
methodHandle = METHOD_HANDLE_SLICE;
} else {
methodHandle = METHOD_HANDLE_OBJECT.asType(METHOD_HANDLE_OBJECT.type().changeReturnType(toType.getJavaType()));
}
return new ScalarFunctionImplementation(true, ImmutableList.of(true), methodHandle, isDeterministic());
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class AbstractGreatestLeast method generate.
private Class<?> generate(List<Class<?>> javaTypes, Type type, MethodHandle compareMethod) {
String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(javaTypeName + "$" + getSignature().getName()), type(Object.class));
definition.declareDefaultConstructor(a(PRIVATE));
List<Parameter> parameters = IntStream.range(0, javaTypes.size()).mapToObj(i -> arg("arg" + i, javaTypes.get(i))).collect(toImmutableList());
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), getSignature().getName(), type(javaTypes.get(0)), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
CallSiteBinder binder = new CallSiteBinder();
if (type.getTypeSignature().getBase().equals(StandardTypes.DOUBLE)) {
for (Parameter parameter : parameters) {
body.append(parameter);
body.append(invoke(binder.bind(CHECK_NOT_NAN.bindTo(getSignature().getName())), "checkNotNaN"));
}
}
Variable value = scope.declareVariable(javaTypes.get(0), "value");
body.append(value.set(parameters.get(0)));
for (int i = 1; i < javaTypes.size(); i++) {
body.append(new IfStatement().condition(new BytecodeBlock().append(parameters.get(i)).append(value).append(invoke(binder.bind(compareMethod), "compare"))).ifTrue(value.set(parameters.get(i))));
}
body.append(value.ret());
return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
use of java.lang.invoke.MethodHandle in project presto by prestodb.
the class ArrayReduceFunction method specialize.
@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
Type inputType = boundVariables.getTypeVariable("T");
Type intermediateType = boundVariables.getTypeVariable("S");
Type outputType = boundVariables.getTypeVariable("R");
MethodHandle methodHandle = METHOD_HANDLE.bindTo(inputType);
return new ScalarFunctionImplementation(true, ImmutableList.of(false, true, false, false), methodHandle.asType(methodHandle.type().changeParameterType(1, Primitives.wrap(intermediateType.getJavaType())).changeReturnType(Primitives.wrap(outputType.getJavaType()))), isDeterministic());
}
Aggregations