use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TypeSignatureTranslator method toTypeSignature.
private static TypeSignature toTypeSignature(GenericDataType type, Set<String> typeVariables) {
ImmutableList.Builder<TypeSignatureParameter> parameters = ImmutableList.builder();
if (type.getName().getValue().equalsIgnoreCase(StandardTypes.VARCHAR) && type.getArguments().isEmpty()) {
// TODO: Eventually, we should split the types into VARCHAR and VARCHAR(n)
return VarcharType.VARCHAR.getTypeSignature();
}
checkArgument(!typeVariables.contains(type.getName().getValue()), "Base type name cannot be a type variable");
for (DataTypeParameter parameter : type.getArguments()) {
if (parameter instanceof NumericParameter) {
String value = ((NumericParameter) parameter).getValue();
try {
parameters.add(numericParameter(Long.parseLong(value)));
} catch (NumberFormatException e) {
throw semanticException(TYPE_MISMATCH, parameter, "Invalid type parameter: %s", value);
}
} else if (parameter instanceof TypeParameter) {
DataType value = ((TypeParameter) parameter).getValue();
if (value instanceof GenericDataType && ((GenericDataType) value).getArguments().isEmpty() && typeVariables.contains(((GenericDataType) value).getName().getValue())) {
parameters.add(typeVariable(((GenericDataType) value).getName().getValue()));
} else {
parameters.add(typeParameter(toTypeSignature(value, typeVariables)));
}
} else {
throw new UnsupportedOperationException("Unsupported type parameter kind: " + parameter.getClass().getName());
}
}
return new TypeSignature(canonicalize(type.getName()), parameters.build());
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class CastImplementationDependency method getInvoker.
@Override
protected FunctionInvoker getInvoker(FunctionBinding functionBinding, FunctionDependencies functionDependencies, InvocationConvention invocationConvention) {
TypeSignature from = applyBoundVariables(fromType, functionBinding);
TypeSignature to = applyBoundVariables(toType, functionBinding);
return functionDependencies.getCastSignatureInvoker(from, to, invocationConvention);
}
use of io.trino.spi.type.TypeSignature 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();
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class TestAnnotationEngineForScalars method testParametricScalarParse.
@Test
public void testParametricScalarParse() {
Signature expectedSignature = new Signature("parametric_scalar", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ParametricScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
assertImplementationCount(scalar, 0, 2, 0);
FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
assertEquals(functionMetadata.getSignature(), expectedSignature);
assertTrue(functionMetadata.isDeterministic());
assertFalse(functionMetadata.isHidden());
assertEquals(functionMetadata.getDescription(), "Parametric scalar description");
}
use of io.trino.spi.type.TypeSignature in project trino by trinodb.
the class RaptorStorageManager method getType.
private Type getType(ColumnMetadata<OrcType> types, OrcColumnId columnId) {
OrcType type = types.get(columnId);
switch(type.getOrcTypeKind()) {
case BOOLEAN:
return BOOLEAN;
case LONG:
return BIGINT;
case DOUBLE:
return DOUBLE;
case STRING:
return createUnboundedVarcharType();
case VARCHAR:
return createVarcharType(type.getLength().get());
case CHAR:
return createCharType(type.getLength().get());
case BINARY:
return VARBINARY;
case DECIMAL:
return DecimalType.createDecimalType(type.getPrecision().get(), type.getScale().get());
case LIST:
TypeSignature elementType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.ARRAY, ImmutableList.of(TypeSignatureParameter.typeParameter(elementType)));
case MAP:
TypeSignature keyType = getType(types, type.getFieldTypeIndex(0)).getTypeSignature();
TypeSignature valueType = getType(types, type.getFieldTypeIndex(1)).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.typeParameter(keyType), TypeSignatureParameter.typeParameter(valueType)));
case STRUCT:
List<String> fieldNames = type.getFieldNames();
ImmutableList.Builder<TypeSignatureParameter> fieldTypes = ImmutableList.builder();
for (int i = 0; i < type.getFieldCount(); i++) {
fieldTypes.add(TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(Optional.of(new RowFieldName(fieldNames.get(i))), getType(types, type.getFieldTypeIndex(i)).getTypeSignature())));
}
return typeManager.getParameterizedType(StandardTypes.ROW, fieldTypes.build());
default:
throw new TrinoException(RAPTOR_ERROR, "Unhandled ORC type: " + type);
}
}
Aggregations