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());
}
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());
}
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();
}
Aggregations