use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class SignatureBinder method expandVarargFormalTypeSignature.
private static List<TypeSignature> expandVarargFormalTypeSignature(List<TypeSignature> formalTypeSignatures, int actualArity) {
int variableArityArgumentsCount = actualArity - formalTypeSignatures.size() + 1;
if (variableArityArgumentsCount == 0) {
return formalTypeSignatures.subList(0, formalTypeSignatures.size() - 1);
}
if (variableArityArgumentsCount == 1) {
return formalTypeSignatures;
}
checkArgument(variableArityArgumentsCount > 1 && !formalTypeSignatures.isEmpty());
ImmutableList.Builder<TypeSignature> builder = ImmutableList.builder();
builder.addAll(formalTypeSignatures);
TypeSignature lastTypeSignature = formalTypeSignatures.get(formalTypeSignatures.size() - 1);
for (int i = 1; i < variableArityArgumentsCount; i++) {
builder.add(lastTypeSignature);
}
return builder.build();
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class SignatureBinder method applyBoundVariables.
private static Signature applyBoundVariables(Signature signature, TypeVariables typeVariables, int arity) {
List<TypeSignature> argumentSignatures;
if (signature.isVariableArity()) {
argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity);
} else {
checkArgument(signature.getArgumentTypes().size() == arity);
argumentSignatures = signature.getArgumentTypes();
}
List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, typeVariables);
TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), typeVariables);
return new Signature(signature.getName(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false);
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class SignatureBinder method extractBoundVariables.
private static void extractBoundVariables(BoundSignature boundSignature, Signature declaredSignature, Map<String, TypeVariableConstraint> typeVariableConstraints, BoundVariables bindings, Type actualType, TypeSignature declaredTypeSignature) {
// type without nested type parameters
if (declaredTypeSignature.getParameters().isEmpty()) {
String typeVariable = declaredTypeSignature.getBase();
TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(typeVariable);
if (typeVariableConstraint == null) {
return;
}
if (bindings.containsTypeVariable(typeVariable)) {
Type existingTypeBinding = bindings.getTypeVariable(typeVariable);
verifyBoundSignature(actualType.equals(existingTypeBinding), boundSignature, declaredSignature);
} else {
bindings.setTypeVariable(typeVariable, actualType);
}
return;
}
verifyBoundSignature(declaredTypeSignature.getBase().equalsIgnoreCase(actualType.getTypeSignature().getBase()), boundSignature, declaredSignature);
// type with nested literal parameters
if (isTypeWithLiteralParameters(declaredTypeSignature)) {
for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
Long actualLongBinding = actualType.getTypeSignature().getParameters().get(i).getLongLiteral();
if (typeSignatureParameter.getKind() == ParameterKind.VARIABLE) {
if (bindings.containsLongVariable(typeSignatureParameter.getVariable())) {
Long existingLongBinding = bindings.getLongVariable(typeSignatureParameter.getVariable());
verifyBoundSignature(actualLongBinding.equals(existingLongBinding), boundSignature, declaredSignature);
} else {
bindings.setLongVariable(typeSignatureParameter.getVariable(), actualLongBinding);
}
} else {
verify(typeSignatureParameter.getKind() == ParameterKind.LONG);
verifyBoundSignature(actualLongBinding.equals(typeSignatureParameter.getLongLiteral()), boundSignature, declaredSignature);
}
}
return;
}
// type with nested type parameters
List<Type> actualTypeParameters = actualType.getTypeParameters();
// unknown types are assumed to have unknown nested types
if (UNKNOWN.equals(actualType)) {
actualTypeParameters = Collections.nCopies(declaredTypeSignature.getParameters().size(), UNKNOWN);
}
verifyBoundSignature(declaredTypeSignature.getParameters().size() == actualTypeParameters.size(), boundSignature, declaredSignature);
for (int i = 0; i < declaredTypeSignature.getParameters().size(); i++) {
TypeSignatureParameter typeSignatureParameter = declaredTypeSignature.getParameters().get(i);
TypeSignature typeSignature = typeSignatureParameter.getTypeSignatureOrNamedTypeSignature().orElseThrow(() -> new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported"));
Type actualTypeParameter = actualTypeParameters.get(i);
extractBoundVariables(boundSignature, declaredSignature, typeVariableConstraints, bindings, actualTypeParameter, typeSignature);
}
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class SignatureBinder method appendConstraintSolvers.
private boolean appendConstraintSolvers(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignature formalTypeSignature, TypeSignatureProvider actualTypeSignatureProvider, boolean allowCoercion) {
if (FunctionType.NAME.equalsIgnoreCase(formalTypeSignature.getBase())) {
List<TypeSignature> formalTypeParameterTypeSignatures = formalTypeSignature.getTypeParametersAsTypeSignatures();
resultBuilder.add(new FunctionSolver(getLambdaArgumentTypeSignatures(formalTypeSignature), formalTypeParameterTypeSignatures.get(formalTypeParameterTypeSignatures.size() - 1), actualTypeSignatureProvider));
return true;
}
if (actualTypeSignatureProvider.hasDependency()) {
return false;
}
if (formalTypeSignature.getParameters().isEmpty()) {
TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(formalTypeSignature.getBase());
if (typeVariableConstraint == null) {
return true;
}
Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
for (TypeSignature castToSignature : typeVariableConstraint.getCastableTo()) {
appendTypeRelationshipConstraintSolver(resultBuilder, castToSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_TO);
}
for (TypeSignature castFromSignature : typeVariableConstraint.getCastableFrom()) {
appendTypeRelationshipConstraintSolver(resultBuilder, castFromSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_FROM);
}
if (typeVariableConstraint.getVariadicBound() != null && !typeVariableConstraint.getVariadicBound().equalsIgnoreCase(actualType.getTypeSignature().getBase())) {
return actualType == UNKNOWN;
}
resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBase(), actualType, typeVariableConstraint.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound())));
return true;
}
Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
if (isTypeWithLiteralParameters(formalTypeSignature)) {
resultBuilder.add(new TypeWithLiteralParametersSolver(formalTypeSignature, actualType));
return true;
}
List<TypeSignatureProvider> actualTypeParametersTypeSignatureProvider;
if (UNKNOWN.equals(actualType)) {
actualTypeParametersTypeSignatureProvider = Collections.nCopies(formalTypeSignature.getParameters().size(), new TypeSignatureProvider(UNKNOWN.getTypeSignature()));
} else {
actualTypeParametersTypeSignatureProvider = fromTypes(actualType.getTypeParameters());
}
ImmutableList.Builder<TypeSignature> formalTypeParameterTypeSignatures = ImmutableList.builder();
for (TypeSignatureParameter formalTypeParameter : formalTypeSignature.getParameters()) {
Optional<TypeSignature> typeSignature = formalTypeParameter.getTypeSignatureOrNamedTypeSignature();
if (typeSignature.isEmpty()) {
throw new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported");
}
formalTypeParameterTypeSignatures.add(typeSignature.get());
}
return appendConstraintSolvers(resultBuilder, formalTypeParameterTypeSignatures.build(), actualTypeParametersTypeSignatureProvider, allowCoercion && isCovariantTypeBase(formalTypeSignature.getBase()));
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TypeRegistry method addType.
public void addType(String alias, Type type) {
requireNonNull(alias, "alias is null");
requireNonNull(type, "type is null");
Type existingType = types.putIfAbsent(new TypeSignature(alias), type);
checkState(existingType == null || existingType.equals(type), "Alias %s is already mapped to %s", alias, type);
}
Aggregations