use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class MetadataManager method resolve.
@VisibleForTesting
public ResolvedFunction resolve(Session session, FunctionBinding functionBinding, FunctionMetadata functionMetadata, FunctionDependencyDeclaration declaration) {
Map<TypeSignature, Type> dependentTypes = declaration.getTypeDependencies().stream().map(typeSignature -> applyBoundVariables(typeSignature, functionBinding)).collect(toImmutableMap(Function.identity(), typeManager::getType, (left, right) -> left));
ImmutableSet.Builder<ResolvedFunction> functions = ImmutableSet.builder();
declaration.getFunctionDependencies().stream().map(functionDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(functionDependency.getArgumentTypes(), functionBinding);
return resolvedFunctionInternal(session, functionDependency.getName(), fromTypeSignatures(argumentTypes));
} catch (TrinoException e) {
if (functionDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
declaration.getOperatorDependencies().stream().map(operatorDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(operatorDependency.getArgumentTypes(), functionBinding);
return resolvedFunctionInternal(session, QualifiedName.of(mangleOperatorName(operatorDependency.getOperatorType())), fromTypeSignatures(argumentTypes));
} catch (TrinoException e) {
if (operatorDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
declaration.getCastDependencies().stream().map(castDependency -> {
try {
Type fromType = typeManager.getType(applyBoundVariables(castDependency.getFromType(), functionBinding));
Type toType = typeManager.getType(applyBoundVariables(castDependency.getToType(), functionBinding));
return getCoercion(session, fromType, toType);
} catch (TrinoException e) {
if (castDependency.isOptional()) {
return null;
}
throw e;
}
}).filter(Objects::nonNull).forEach(functions::add);
return new ResolvedFunction(functionBinding.getBoundSignature(), functionBinding.getFunctionId(), functionMetadata.getKind(), functionMetadata.isDeterministic(), functionMetadata.getFunctionNullability(), dependentTypes, functions.build());
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class GlobalFunctionCatalog method checkNotSpecializedTypeOperator.
/**
* Type operators are handled automatically by the engine, so custom operator implementations
* cannot be registered for these.
*/
private static void checkNotSpecializedTypeOperator(Signature signature) {
String name = signature.getName();
if (!isOperatorName(name)) {
return;
}
OperatorType operatorType = unmangleOperator(name);
// The trick here is the Generic*Operator implementations implement these exact signatures,
// so we only these exact signatures to be registered. Since, only a single function with
// a specific signature can be registered, it prevents others from being registered.
Type expectedReturnType;
TypeVariableConstraint typeParameter;
switch(operatorType) {
case EQUAL:
case IS_DISTINCT_FROM:
case INDETERMINATE:
expectedReturnType = BOOLEAN;
typeParameter = comparableTypeParameter("T");
break;
case HASH_CODE:
case XX_HASH_64:
expectedReturnType = BIGINT;
typeParameter = comparableTypeParameter("T");
break;
case COMPARISON_UNORDERED_FIRST:
case COMPARISON_UNORDERED_LAST:
expectedReturnType = INTEGER;
typeParameter = orderableTypeParameter("T");
break;
case LESS_THAN:
case LESS_THAN_OR_EQUAL:
expectedReturnType = BOOLEAN;
typeParameter = orderableTypeParameter("T");
break;
default:
return;
}
Signature expectedSignature = new Signature(signature.getName(), ImmutableList.of(typeParameter), ImmutableList.of(), expectedReturnType.getTypeSignature(), Collections.nCopies(operatorType.getArgumentCount(), new TypeSignature("T")), false);
checkArgument(signature.equals(expectedSignature), "Can not register %s functionMetadata: %s", operatorType, signature);
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class ExtractSpatialJoins method addPartitioningNodes.
private static PlanNode addPartitioningNodes(PlannerContext plannerContext, Context context, PlanNode node, Symbol partitionSymbol, KdbTree kdbTree, Expression geometry, Optional<Expression> radius) {
Assignments.Builder projections = Assignments.builder();
for (Symbol outputSymbol : node.getOutputSymbols()) {
projections.putIdentity(outputSymbol);
}
TypeSignature typeSignature = new TypeSignature(KDB_TREE_TYPENAME);
FunctionCallBuilder spatialPartitionsCall = FunctionCallBuilder.resolve(context.getSession(), plannerContext.getMetadata()).setName(QualifiedName.of("spatial_partitions")).addArgument(typeSignature, new Cast(new StringLiteral(KdbTreeUtils.toJson(kdbTree)), toSqlType(plannerContext.getTypeManager().getType(typeSignature)))).addArgument(GEOMETRY_TYPE_SIGNATURE, geometry);
radius.ifPresent(value -> spatialPartitionsCall.addArgument(DOUBLE, value));
FunctionCall partitioningFunction = spatialPartitionsCall.build();
Symbol partitionsSymbol = context.getSymbolAllocator().newSymbol(partitioningFunction, new ArrayType(INTEGER));
projections.put(partitionsSymbol, partitioningFunction);
return new UnnestNode(context.getIdAllocator().getNextId(), new ProjectNode(context.getIdAllocator().getNextId(), node, projections.build()), node.getOutputSymbols(), ImmutableList.of(new UnnestNode.Mapping(partitionsSymbol, ImmutableList.of(partitionSymbol))), Optional.empty(), INNER, Optional.empty());
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class SignatureBinder method checkNoLiteralVariableUsageAcrossTypes.
private static void checkNoLiteralVariableUsageAcrossTypes(TypeSignature typeSignature, Map<String, TypeSignature> existingUsages) {
List<TypeSignatureParameter> variables = typeSignature.getParameters().stream().filter(TypeSignatureParameter::isVariable).collect(toList());
for (TypeSignatureParameter variable : variables) {
TypeSignature existing = existingUsages.get(variable.getVariable());
if (existing != null && !existing.equals(typeSignature)) {
throw new UnsupportedOperationException("Literal parameters may not be shared across different types");
}
existingUsages.put(variable.getVariable(), typeSignature);
}
for (TypeSignatureParameter parameter : typeSignature.getParameters()) {
if (parameter.isLongLiteral() || parameter.isVariable()) {
continue;
}
checkNoLiteralVariableUsageAcrossTypes(parameter.getTypeSignatureOrNamedTypeSignature().get(), existingUsages);
}
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class HiveTypeTranslator method toTypeSignature.
public static TypeSignature toTypeSignature(TypeInfo typeInfo, HiveTimestampPrecision timestampPrecision) {
switch(typeInfo.getCategory()) {
case PRIMITIVE:
Type primitiveType = fromPrimitiveType((PrimitiveTypeInfo) typeInfo, timestampPrecision);
if (primitiveType == null) {
break;
}
return primitiveType.getTypeSignature();
case MAP:
MapTypeInfo mapTypeInfo = (MapTypeInfo) typeInfo;
return mapType(toTypeSignature(mapTypeInfo.getMapKeyTypeInfo(), timestampPrecision), toTypeSignature(mapTypeInfo.getMapValueTypeInfo(), timestampPrecision));
case LIST:
ListTypeInfo listTypeInfo = (ListTypeInfo) typeInfo;
TypeSignature elementType = toTypeSignature(listTypeInfo.getListElementTypeInfo(), timestampPrecision);
return arrayType(typeParameter(elementType));
case STRUCT:
StructTypeInfo structTypeInfo = (StructTypeInfo) typeInfo;
List<TypeInfo> fieldTypes = structTypeInfo.getAllStructFieldTypeInfos();
List<String> fieldNames = structTypeInfo.getAllStructFieldNames();
if (fieldTypes.size() != fieldNames.size()) {
throw new TrinoException(HiveErrorCode.HIVE_INVALID_METADATA, format("Invalid Hive struct type: %s", typeInfo));
}
return rowType(Streams.zip(// TODO: This is a hack. Trino engine should be able to handle identifiers in a case insensitive way where necessary.
fieldNames.stream().map(s -> s.toLowerCase(Locale.US)), fieldTypes.stream().map(type -> toTypeSignature(type, timestampPrecision)), TypeSignatureParameter::namedField).collect(Collectors.toList()));
case UNION:
// Use a row type to represent a union type in Hive for reading
UnionTypeInfo unionTypeInfo = (UnionTypeInfo) typeInfo;
List<TypeInfo> unionObjectTypes = unionTypeInfo.getAllUnionObjectTypeInfos();
ImmutableList.Builder<TypeSignatureParameter> typeSignatures = ImmutableList.builder();
typeSignatures.add(namedField("tag", TINYINT.getTypeSignature()));
for (int i = 0; i < unionObjectTypes.size(); i++) {
TypeInfo unionObjectType = unionObjectTypes.get(i);
typeSignatures.add(namedField("field" + i, toTypeSignature(unionObjectType, timestampPrecision)));
}
return rowType(typeSignatures.build());
}
throw new TrinoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", typeInfo));
}
Aggregations