use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.
the class TestSignatureBinder method testBindVarcharTemplateStyle.
@Test
public void testBindVarcharTemplateStyle() {
Signature function = functionSignature().returnType(parseTypeSignature("T2")).argumentTypes(parseTypeSignature("T1")).typeVariableConstraints(ImmutableList.of(new TypeVariableConstraint("T1", true, false, "varchar"), new TypeVariableConstraint("T2", true, false, "varchar"))).build();
assertThat(function).boundTo(ImmutableList.of("varchar(42)"), "varchar(1)").produces(new BoundVariables(ImmutableMap.of("T1", type("varchar(42)"), "T2", type("varchar(1)")), ImmutableMap.of()));
}
use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.
the class FunctionsParserHelper method createTypeVariableConstraints.
public static List<TypeVariableConstraint> createTypeVariableConstraints(Iterable<TypeParameter> typeParameters, List<ImplementationDependency> dependencies) {
Set<String> orderableRequired = new HashSet<>();
Set<String> comparableRequired = new HashSet<>();
for (ImplementationDependency dependency : dependencies) {
if (dependency instanceof OperatorImplementationDependency) {
OperatorType operator = ((OperatorImplementationDependency) dependency).getOperator();
if (operator == CAST) {
continue;
}
Set<String> argumentTypes = ((OperatorImplementationDependency) dependency).getArgumentTypes().stream().map(TypeSignature::getBase).collect(toImmutableSet());
checkArgument(argumentTypes.size() == 1, "Operator dependency must only have arguments of a single type");
String argumentType = Iterables.getOnlyElement(argumentTypes);
if (COMPARABLE_TYPE_OPERATORS.contains(operator)) {
comparableRequired.add(argumentType);
}
if (ORDERABLE_TYPE_OPERATORS.contains(operator)) {
orderableRequired.add(argumentType);
}
}
}
ImmutableList.Builder<TypeVariableConstraint> typeVariableConstraints = ImmutableList.builder();
for (TypeParameter typeParameter : typeParameters) {
String name = typeParameter.value();
if (orderableRequired.contains(name)) {
typeVariableConstraints.add(orderableTypeParameter(name));
} else if (comparableRequired.contains(name)) {
typeVariableConstraints.add(comparableTypeParameter(name));
} else {
typeVariableConstraints.add(typeVariable(name));
}
}
return typeVariableConstraints.build();
}
use of io.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.
the class SignatureBinder method appendConstraintSolvers.
private boolean appendConstraintSolvers(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignature formalTypeSignature, TypeSignatureProvider actualTypeSignatureProvider, boolean allowCoercion) {
if (FunctionType.NAME.equals(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 = functionAndTypeManager.getType(actualTypeSignatureProvider.getTypeSignature());
resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBase(), actualType, typeVariableConstraint.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound())));
return true;
}
Type actualType = functionAndTypeManager.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 = TypeSignatureProvider.fromTypes(actualType.getTypeParameters());
}
ImmutableList.Builder<TypeSignature> formalTypeParameterTypeSignatures = ImmutableList.builder();
for (TypeSignatureParameter formalTypeParameter : formalTypeSignature.getParameters()) {
Optional<TypeSignature> typeSignature = formalTypeParameter.getTypeSignatureOrNamedTypeSignature();
if (!typeSignature.isPresent()) {
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.prestosql.spi.function.TypeVariableConstraint in project hetu-core by openlookeng.
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(TypeSignature::parseTypeSignature).collect(toImmutableList());
Signature signature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, window.name()), WINDOW, typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType()), argumentTypes, false);
return new SqlWindowFunction(new ReflectionWindowFunctionSupplier<>(signature, clazz));
}
Aggregations