use of io.trino.metadata.TypeVariableConstraint in project trino by trinodb.
the class WindowAnnotationsParser method parse.
private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
List<TypeVariableConstraint> typeVariables = ImmutableList.of();
if (!window.typeVariable().isEmpty()) {
typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
}
List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(type -> parseTypeSignature(type, ImmutableSet.of())).collect(toImmutableList());
Signature signature = new Signature(window.name(), typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType(), ImmutableSet.of()), argumentTypes, false);
Optional<String> description = Optional.ofNullable(clazz.getAnnotation(Description.class)).map(Description::value);
boolean deprecated = clazz.getAnnotationsByType(Deprecated.class).length > 0;
return new SqlWindowFunction(signature, description, deprecated, new ReflectionWindowFunctionSupplier(window.argumentTypes().length, clazz));
}
use of io.trino.metadata.TypeVariableConstraint in project trino by trinodb.
the class FunctionsParserHelper method createTypeVariableConstraints.
public static List<TypeVariableConstraint> createTypeVariableConstraints(Collection<TypeParameter> typeParameters, List<ImplementationDependency> dependencies) {
Set<String> typeParameterNames = typeParameters.stream().map(TypeParameter::value).collect(toImmutableSortedSet(CASE_INSENSITIVE_ORDER));
Set<String> orderableRequired = new TreeSet<>(CASE_INSENSITIVE_ORDER);
Set<String> comparableRequired = new TreeSet<>(CASE_INSENSITIVE_ORDER);
HashMultimap<String, String> castableTo = HashMultimap.create();
HashMultimap<String, String> castableFrom = HashMultimap.create();
for (ImplementationDependency dependency : dependencies) {
if (dependency instanceof OperatorImplementationDependency) {
OperatorImplementationDependency operatorDependency = (OperatorImplementationDependency) dependency;
OperatorType operator = operatorDependency.getOperator();
List<TypeSignature> argumentTypes = operatorDependency.getArgumentTypes();
if (COMPARABLE_TYPE_OPERATORS.contains(operator)) {
verifyOperatorSignature(operator, argumentTypes);
TypeSignature typeSignature = argumentTypes.get(0);
if (typeParameterNames.contains(typeSignature.getBase())) {
comparableRequired.add(typeSignature.toString());
} else {
verifyTypeSignatureDoesNotContainAnyTypeParameters(typeSignature, typeSignature, typeParameterNames);
}
} else if (ORDERABLE_TYPE_OPERATORS.contains(operator)) {
verifyOperatorSignature(operator, argumentTypes);
TypeSignature typeSignature = argumentTypes.get(0);
if (typeParameterNames.contains(typeSignature.getBase())) {
orderableRequired.add(typeSignature.toString());
} else {
verifyTypeSignatureDoesNotContainAnyTypeParameters(typeSignature, typeSignature, typeParameterNames);
}
} else {
throw new IllegalArgumentException("Operator dependency on " + operator + " is not allowed");
}
} else if (dependency instanceof CastImplementationDependency) {
CastImplementationDependency castImplementationDependency = (CastImplementationDependency) dependency;
TypeSignature fromType = castImplementationDependency.getFromType();
TypeSignature toType = castImplementationDependency.getToType();
if (typeParameterNames.contains(fromType.getBase())) {
// fromType is a type parameter, so it must be castable to the toType, which might also be a type parameter
castableTo.put(fromType.toString().toLowerCase(Locale.ENGLISH), toType.toString());
} else if (typeParameterNames.contains(toType.getBase())) {
// toType is a type parameter, so it must be castable from the toType, which is not a type parameter
castableFrom.put(toType.toString().toLowerCase(Locale.ENGLISH), fromType.toString());
} else {
verifyTypeSignatureDoesNotContainAnyTypeParameters(fromType, fromType, typeParameterNames);
verifyTypeSignatureDoesNotContainAnyTypeParameters(toType, toType, typeParameterNames);
}
}
}
ImmutableList.Builder<TypeVariableConstraint> typeVariableConstraints = ImmutableList.builder();
for (String name : typeParameterNames) {
typeVariableConstraints.add(new TypeVariableConstraint(name, comparableRequired.contains(name), orderableRequired.contains(name), null, castableTo.get(name).stream().map(type -> parseTypeSignature(type, typeParameterNames)).collect(toImmutableSet()), castableFrom.get(name).stream().map(type -> parseTypeSignature(type, typeParameterNames)).collect(toImmutableSet())));
}
return typeVariableConstraints.build();
}
Aggregations