Search in sources :

Example 11 with TypeSignatureParameter

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()));
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) JsonType(io.trino.type.JsonType) Type(io.trino.spi.type.Type) FunctionType(io.trino.type.FunctionType) RowType(io.trino.spi.type.RowType) UnknownType(io.trino.type.UnknownType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter)

Example 12 with TypeSignatureParameter

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;
}
Also used : TypeParameter(io.trino.spi.type.TypeParameter) DecimalParametricType(io.trino.type.DecimalParametricType) VarcharParametricType(io.trino.type.VarcharParametricType) Re2JRegexpType(io.trino.type.Re2JRegexpType) ParametricType(io.trino.spi.type.ParametricType) Type(io.trino.spi.type.Type) CharParametricType(io.trino.type.CharParametricType) OperatorType(io.trino.spi.function.OperatorType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TypeNotFoundException(io.trino.spi.type.TypeNotFoundException) ArrayList(java.util.ArrayList) DecimalParametricType(io.trino.type.DecimalParametricType) VarcharParametricType(io.trino.type.VarcharParametricType) ParametricType(io.trino.spi.type.ParametricType) CharParametricType(io.trino.type.CharParametricType)

Example 13 with TypeSignatureParameter

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);
}
Also used : RowType(io.trino.spi.type.RowType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) Type(io.trino.spi.type.Type) StandardTypes(io.trino.spi.type.StandardTypes) TypeParameter(io.trino.spi.type.TypeParameter) ParameterKind(io.trino.spi.type.ParameterKind) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParametricType(io.trino.spi.type.ParametricType) RowFieldName(io.trino.spi.type.RowFieldName) TypeManager(io.trino.spi.type.TypeManager) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TypeSignature(io.trino.spi.type.TypeSignature) TypeParameter(io.trino.spi.type.TypeParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature)

Example 14 with TypeSignatureParameter

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());
}
Also used : DataTypeParameter(io.trino.sql.tree.DataTypeParameter) TypeSignatureParameter.namedTypeParameter(io.trino.spi.type.TypeSignatureParameter.namedTypeParameter) TypeParameter(io.trino.sql.tree.TypeParameter) DataTypeParameter(io.trino.sql.tree.DataTypeParameter) GenericDataType(io.trino.sql.tree.GenericDataType) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) NumericParameter(io.trino.sql.tree.NumericParameter) DateTimeDataType(io.trino.sql.tree.DateTimeDataType) RowDataType(io.trino.sql.tree.RowDataType) GenericDataType(io.trino.sql.tree.GenericDataType) DataType(io.trino.sql.tree.DataType) IntervalDayTimeDataType(io.trino.sql.tree.IntervalDayTimeDataType)

Example 15 with TypeSignatureParameter

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();
}
Also used : ArrayList(java.util.ArrayList) DBRef(com.mongodb.DBRef) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) TrinoException(io.trino.spi.TrinoException) List(java.util.List) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) Map(java.util.Map) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 Type (io.trino.spi.type.Type)9 ImmutableList (com.google.common.collect.ImmutableList)8 TrinoException (io.trino.spi.TrinoException)8 TypeSignature (io.trino.spi.type.TypeSignature)8 RowType (io.trino.spi.type.RowType)6 VarcharType (io.trino.spi.type.VarcharType)6 ArrayList (java.util.ArrayList)6 List (java.util.List)5 CharType (io.trino.spi.type.CharType)4 DecimalType (io.trino.spi.type.DecimalType)4 RowFieldName (io.trino.spi.type.RowFieldName)4 TimestampType (io.trino.spi.type.TimestampType)3 Collectors.toList (java.util.stream.Collectors.toList)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)3 TypeInfoFactory.getCharTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo)3 TypeInfoFactory.getListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo)3