use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class SignatureBinder method appendConstraintSolvers.
private boolean appendConstraintSolvers(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignature formalTypeSignature, TypeSignatureProvider actualTypeSignatureProvider, boolean allowCoercion) {
if (FunctionType.NAME.equalsIgnoreCase(formalTypeSignature.getBase())) {
List<TypeSignature> formalTypeParameterTypeSignatures = formalTypeSignature.getTypeParametersAsTypeSignatures();
resultBuilder.add(new FunctionSolver(getLambdaArgumentTypeSignatures(formalTypeSignature), formalTypeParameterTypeSignatures.get(formalTypeParameterTypeSignatures.size() - 1), actualTypeSignatureProvider));
return true;
}
if (actualTypeSignatureProvider.hasDependency()) {
return false;
}
if (formalTypeSignature.getParameters().isEmpty()) {
TypeVariableConstraint typeVariableConstraint = typeVariableConstraints.get(formalTypeSignature.getBase());
if (typeVariableConstraint == null) {
return true;
}
Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
for (TypeSignature castToSignature : typeVariableConstraint.getCastableTo()) {
appendTypeRelationshipConstraintSolver(resultBuilder, castToSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_TO);
}
for (TypeSignature castFromSignature : typeVariableConstraint.getCastableFrom()) {
appendTypeRelationshipConstraintSolver(resultBuilder, castFromSignature, actualTypeSignatureProvider, EXPLICIT_COERCION_FROM);
}
if (typeVariableConstraint.getVariadicBound() != null && !typeVariableConstraint.getVariadicBound().equalsIgnoreCase(actualType.getTypeSignature().getBase())) {
return actualType == UNKNOWN;
}
resultBuilder.add(new TypeParameterSolver(formalTypeSignature.getBase(), actualType, typeVariableConstraint.isComparableRequired(), typeVariableConstraint.isOrderableRequired(), Optional.ofNullable(typeVariableConstraint.getVariadicBound())));
return true;
}
Type actualType = typeManager.getType(actualTypeSignatureProvider.getTypeSignature());
if (isTypeWithLiteralParameters(formalTypeSignature)) {
resultBuilder.add(new TypeWithLiteralParametersSolver(formalTypeSignature, actualType));
return true;
}
List<TypeSignatureProvider> actualTypeParametersTypeSignatureProvider;
if (UNKNOWN.equals(actualType)) {
actualTypeParametersTypeSignatureProvider = Collections.nCopies(formalTypeSignature.getParameters().size(), new TypeSignatureProvider(UNKNOWN.getTypeSignature()));
} else {
actualTypeParametersTypeSignatureProvider = fromTypes(actualType.getTypeParameters());
}
ImmutableList.Builder<TypeSignature> formalTypeParameterTypeSignatures = ImmutableList.builder();
for (TypeSignatureParameter formalTypeParameter : formalTypeSignature.getParameters()) {
Optional<TypeSignature> typeSignature = formalTypeParameter.getTypeSignatureOrNamedTypeSignature();
if (typeSignature.isEmpty()) {
throw new UnsupportedOperationException("Types with both type parameters and literal parameters at the same time are not supported");
}
formalTypeParameterTypeSignatures.add(typeSignature.get());
}
return appendConstraintSolvers(resultBuilder, formalTypeParameterTypeSignatures.build(), actualTypeParametersTypeSignatureProvider, allowCoercion && isCovariantTypeBase(formalTypeSignature.getBase()));
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class TypeRegistry method instantiateParametricType.
private Type instantiateParametricType(TypeSignature signature) {
List<TypeParameter> parameters = new ArrayList<>();
for (TypeSignatureParameter parameter : signature.getParameters()) {
TypeParameter typeParameter = TypeParameter.of(parameter, typeManager);
parameters.add(typeParameter);
}
ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
if (parametricType == null) {
throw new TypeNotFoundException(signature);
}
Type instantiatedType;
try {
instantiatedType = parametricType.createType(typeManager, parameters);
} catch (IllegalArgumentException e) {
throw new TypeNotFoundException(signature, e);
}
// checkState(instantiatedType.equalsSignature(signature), "Instantiated parametric type name (%s) does not match expected name (%s)", instantiatedType, signature);
return instantiatedType;
}
use of io.trino.spi.type.TypeSignatureParameter in project trino by trinodb.
the class RowParametricType method createType.
@Override
public Type createType(TypeManager typeManager, List<TypeParameter> parameters) {
checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.namedTypeParameter(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
use of io.trino.spi.type.TypeSignatureParameter 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.TypeSignatureParameter in project trino by trinodb.
the class MongoPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (isArrayType(type)) {
if (value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
((List<?>) value).forEach(element -> appendTo(type.getTypeParameters().get(0), element, builder));
output.closeEntry();
return;
}
} else if (isMapType(type)) {
if (value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
if (!(element instanceof Map<?, ?>)) {
continue;
}
Map<?, ?> document = (Map<?, ?>) element;
if (document.containsKey("key") && document.containsKey("value")) {
appendTo(type.getTypeParameters().get(0), document.get("key"), builder);
appendTo(type.getTypeParameters().get(1), document.get("value"), builder);
}
}
output.closeEntry();
return;
} else if (value instanceof Map) {
BlockBuilder builder = output.beginBlockEntry();
Map<?, ?> document = (Map<?, ?>) value;
for (Map.Entry<?, ?> entry : document.entrySet()) {
appendTo(type.getTypeParameters().get(0), entry.getKey(), builder);
appendTo(type.getTypeParameters().get(1), entry.getValue(), builder);
}
output.closeEntry();
return;
}
} else if (isRowType(type)) {
if (value instanceof Map) {
Map<?, ?> mapValue = (Map<?, ?>) value;
BlockBuilder builder = output.beginBlockEntry();
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), mapValue.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
} else if (value instanceof DBRef) {
DBRef dbRefValue = (DBRef) value;
BlockBuilder builder = output.beginBlockEntry();
checkState(type.getTypeParameters().size() == 3, "DBRef should have 3 fields : %s", type);
appendTo(type.getTypeParameters().get(0), dbRefValue.getDatabaseName(), builder);
appendTo(type.getTypeParameters().get(1), dbRefValue.getCollectionName(), builder);
appendTo(type.getTypeParameters().get(2), dbRefValue.getId(), builder);
output.closeEntry();
return;
} else if (value instanceof List<?>) {
List<?> listValue = (List<?>) value;
BlockBuilder builder = output.beginBlockEntry();
for (int index = 0; index < type.getTypeParameters().size(); index++) {
if (index < listValue.size()) {
appendTo(type.getTypeParameters().get(index), listValue.get(index), builder);
} else {
builder.appendNull();
}
}
output.closeEntry();
return;
}
} else {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Unhandled type for Block: " + type.getTypeSignature());
}
// not a convertible value
output.appendNull();
}
Aggregations