Search in sources :

Example 1 with TypeVariableConstraint

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));
}
Also used : WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Description(io.trino.spi.function.Description) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) WindowFunction(io.trino.spi.function.WindowFunction) Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Optional(java.util.Optional) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Description(io.trino.spi.function.Description) WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint)

Example 2 with TypeVariableConstraint

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();
}
Also used : TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) Arrays(java.util.Arrays) Description(io.trino.spi.function.Description) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) Constraint(io.trino.type.Constraint) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HashMultimap(com.google.common.collect.HashMultimap) Locale(java.util.Locale) Map(java.util.Map) LongVariableConstraint(io.trino.metadata.LongVariableConstraint) EQUAL(io.trino.spi.function.OperatorType.EQUAL) Method(java.lang.reflect.Method) COMPARISON_UNORDERED_LAST(io.trino.spi.function.OperatorType.COMPARISON_UNORDERED_LAST) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSortedSet.toImmutableSortedSet(com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet) INDETERMINATE(io.trino.spi.function.OperatorType.INDETERMINATE) ImmutableSet(com.google.common.collect.ImmutableSet) IsNull(io.trino.spi.function.IsNull) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ImplementationDependency.isImplementationDependencyAnnotation(io.trino.operator.annotations.ImplementationDependency.isImplementationDependencyAnnotation) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) AnnotatedElement(java.lang.reflect.AnnotatedElement) SqlNullable(io.trino.spi.function.SqlNullable) CASE_INSENSITIVE_ORDER(java.lang.String.CASE_INSENSITIVE_ORDER) HashMap(java.util.HashMap) LiteralParameters(io.trino.spi.function.LiteralParameters) Constructor(java.lang.reflect.Constructor) TreeSet(java.util.TreeSet) IS_DISTINCT_FROM(io.trino.spi.function.OperatorType.IS_DISTINCT_FROM) ImmutableList(com.google.common.collect.ImmutableList) SqlType(io.trino.spi.function.SqlType) LESS_THAN_OR_EQUAL(io.trino.spi.function.OperatorType.LESS_THAN_OR_EQUAL) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Signature(io.trino.metadata.Signature) XX_HASH_64(io.trino.spi.function.OperatorType.XX_HASH_64) Nullable(javax.annotation.Nullable) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) OperatorType(io.trino.spi.function.OperatorType) HASH_CODE(io.trino.spi.function.OperatorType.HASH_CODE) COMPARISON_UNORDERED_FIRST(io.trino.spi.function.OperatorType.COMPARISON_UNORDERED_FIRST) TypeParameter(io.trino.spi.function.TypeParameter) LESS_THAN(io.trino.spi.function.OperatorType.LESS_THAN) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) OperatorType(io.trino.spi.function.OperatorType) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TreeSet(java.util.TreeSet)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Signature (io.trino.metadata.Signature)2 TypeVariableConstraint (io.trino.metadata.TypeVariableConstraint)2 Description (io.trino.spi.function.Description)2 TypeSignature (io.trino.spi.type.TypeSignature)2 TypeSignatureTranslator.parseTypeSignature (io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature)2 List (java.util.List)2 Optional (java.util.Optional)2 Stream (java.util.stream.Stream)2 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 ImmutableSortedSet.toImmutableSortedSet (com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet)1 LongVariableConstraint (io.trino.metadata.LongVariableConstraint)1 Signature.typeVariable (io.trino.metadata.Signature.typeVariable)1 ImplementationDependency.isImplementationDependencyAnnotation (io.trino.operator.annotations.ImplementationDependency.isImplementationDependencyAnnotation)1 IsNull (io.trino.spi.function.IsNull)1 LiteralParameters (io.trino.spi.function.LiteralParameters)1