Search in sources :

Example 26 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ApproximateCountDistinctAggregation method input.

@InputFunction
@TypeParameter("T")
public static void input(@OperatorDependency(operator = XX_HASH_64, argumentTypes = "T", convention = @Convention(arguments = NEVER_NULL, result = FAIL_ON_NULL)) MethodHandle methodHandle, @AggregationState HyperLogLogState state, @SqlType("T") Object value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) {
    HyperLogLog hll = getOrCreateHyperLogLog(state, maxStandardError);
    state.addMemoryUsage(-hll.estimatedInMemorySize());
    long hash;
    try {
        hash = (long) methodHandle.invoke(value);
    } catch (Throwable t) {
        throw internalError(t);
    }
    hll.addHash(hash);
    state.addMemoryUsage(hll.estimatedInMemorySize());
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) TypeParameter(io.trino.spi.function.TypeParameter) InputFunction(io.trino.spi.function.InputFunction)

Example 27 with TypeParameter

use of io.trino.spi.function.TypeParameter in project trino by trinodb.

the class ApproximateSetGenericAggregation method input.

@InputFunction
@TypeParameter("T")
public static void input(@OperatorDependency(operator = XX_HASH_64, argumentTypes = "T", convention = @Convention(arguments = NEVER_NULL, result = FAIL_ON_NULL)) MethodHandle methodHandle, @AggregationState HyperLogLogState state, @SqlType("T") Object value) {
    HyperLogLog hll = getOrCreateHyperLogLog(state);
    state.addMemoryUsage(-hll.estimatedInMemorySize());
    long hash;
    try {
        hash = (long) methodHandle.invoke(value);
    } catch (Throwable t) {
        throw internalError(t);
    }
    hll.addHash(hash);
    state.addMemoryUsage(hll.estimatedInMemorySize());
}
Also used : HyperLogLog(io.airlift.stats.cardinality.HyperLogLog) TypeParameter(io.trino.spi.function.TypeParameter) InputFunction(io.trino.spi.function.InputFunction)

Example 28 with TypeParameter

use of io.trino.spi.function.TypeParameter 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

TypeParameter (io.trino.spi.function.TypeParameter)28 SqlType (io.trino.spi.function.SqlType)22 BlockBuilder (io.trino.spi.block.BlockBuilder)20 TypeParameterSpecialization (io.trino.spi.function.TypeParameterSpecialization)9 HyperLogLog (io.airlift.stats.cardinality.HyperLogLog)6 TypedSet (io.trino.operator.aggregation.TypedSet)6 InputFunction (io.trino.spi.function.InputFunction)6 TypedSet.createEqualityTypedSet (io.trino.operator.aggregation.TypedSet.createEqualityTypedSet)4 TrinoException (io.trino.spi.TrinoException)3 Block (io.trino.spi.block.Block)3 SqlNullable (io.trino.spi.function.SqlNullable)3 ArrayType (io.trino.spi.type.ArrayType)3 RowType (io.trino.spi.type.RowType)3 Type (io.trino.spi.type.Type)3 TypedSet.createDistinctTypedSet (io.trino.operator.aggregation.TypedSet.createDistinctTypedSet)2 MapType (io.trino.spi.type.MapType)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1