use of io.trino.spi.type.Type in project trino by trinodb.
the class AbstractGreatestLeast method generate.
private Class<?> generate(List<Class<?>> javaTypes, MethodHandle compareMethod) {
Signature signature = getFunctionMetadata().getSignature();
checkCondition(javaTypes.size() <= 127, NOT_SUPPORTED, "Too many arguments for function call %s()", signature.getName());
String javaTypeName = javaTypes.stream().map(Class::getSimpleName).collect(joining());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(javaTypeName + "$" + signature.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), signature.getName(), type(wrap(javaTypes.get(0))), parameters);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
CallSiteBinder binder = new CallSiteBinder();
Variable value = scope.declareVariable(wrap(javaTypes.get(0)), "value");
BytecodeExpression nullValue = constantNull(wrap(javaTypes.get(0)));
body.append(value.set(nullValue));
LabelNode done = new LabelNode("done");
compareMethod = compareMethod.asType(methodType(boolean.class, compareMethod.type().wrap().parameterList()));
for (int i = 0; i < javaTypes.size(); i++) {
Parameter parameter = parameters.get(i);
BytecodeExpression invokeCompare = invokeDynamic(BOOTSTRAP_METHOD, ImmutableList.of(binder.bind(compareMethod).getBindingId()), "compare", boolean.class, parameter, value);
body.append(new IfStatement().condition(isNull(parameter)).ifTrue(new BytecodeBlock().append(value.set(nullValue)).gotoLabel(done)));
body.append(new IfStatement().condition(or(isNull(value), invokeCompare)).ifTrue(value.set(parameter)));
}
body.visitLabel(done);
body.append(value.ret());
return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(getClass().getClassLoader()));
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class ArrayTransformFunction method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
Type inputType = ((ArrayType) boundSignature.getArgumentTypes().get(0)).getElementType();
Type outputType = ((ArrayType) boundSignature.getReturnType()).getElementType();
Class<?> generatedClass = generateTransform(inputType, outputType);
return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL, FUNCTION), ImmutableList.of(UnaryFunctionInterface.class), methodHandle(generatedClass, "transform", PageBuilder.class, Block.class, UnaryFunctionInterface.class), Optional.of(methodHandle(generatedClass, "createPageBuilder")));
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class CastFromUnknownOperator method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
Type toType = boundSignature.getReturnType();
MethodHandle methodHandle = METHOD_HANDLE_NON_NULL.asType(METHOD_HANDLE_NON_NULL.type().changeReturnType(toType.getJavaType()));
return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, ImmutableList.of(NEVER_NULL), methodHandle);
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class ArrayConstructor method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
ImmutableList.Builder<Class<?>> builder = ImmutableList.builder();
Type type = boundSignature.getArgumentTypes().get(0);
for (int i = 0; i < boundSignature.getArity(); i++) {
if (type.getJavaType().isPrimitive()) {
builder.add(Primitives.wrap(type.getJavaType()));
} else {
builder.add(type.getJavaType());
}
}
ImmutableList<Class<?>> stackTypes = builder.build();
Class<?> clazz = generateArrayConstructor(stackTypes, type);
MethodHandle methodHandle;
try {
Method method = clazz.getMethod("arrayConstructor", stackTypes.toArray(new Class<?>[stackTypes.size()]));
methodHandle = lookup().unreflect(method);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
return new ChoicesScalarFunctionImplementation(boundSignature, FAIL_ON_NULL, nCopies(stackTypes.size(), BOXED_NULLABLE), methodHandle);
}
use of io.trino.spi.type.Type in project trino by trinodb.
the class ArrayReduceFunction method specialize.
@Override
protected ScalarFunctionImplementation specialize(BoundSignature boundSignature) {
ArrayType arrayType = (ArrayType) boundSignature.getArgumentTypes().get(0);
Type inputType = arrayType.getElementType();
Type intermediateType = boundSignature.getArgumentTypes().get(1);
Type outputType = boundSignature.getReturnType();
MethodHandle methodHandle = METHOD_HANDLE.bindTo(inputType);
return new ChoicesScalarFunctionImplementation(boundSignature, NULLABLE_RETURN, ImmutableList.of(NEVER_NULL, BOXED_NULLABLE, FUNCTION, FUNCTION), ImmutableList.of(BinaryFunctionInterface.class, UnaryFunctionInterface.class), methodHandle.asType(methodHandle.type().changeParameterType(1, Primitives.wrap(intermediateType.getJavaType())).changeReturnType(Primitives.wrap(outputType.getJavaType()))), Optional.empty());
}
Aggregations