Search in sources :

Example 71 with TypeSignature

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());
}
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 72 with TypeSignature

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);
}
Also used : TypeSignature(io.trino.spi.type.TypeSignature)

Example 73 with TypeSignature

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();
}
Also used : TypeParameterSpecialization(io.trino.spi.function.TypeParameterSpecialization) Arrays(java.util.Arrays) Description(io.trino.spi.function.Description) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) Constraint(io.trino.type.Constraint) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) HashMultimap(com.google.common.collect.HashMultimap) Locale(java.util.Locale) Map(java.util.Map) LongVariableConstraint(io.trino.metadata.LongVariableConstraint) EQUAL(io.trino.spi.function.OperatorType.EQUAL) Method(java.lang.reflect.Method) COMPARISON_UNORDERED_LAST(io.trino.spi.function.OperatorType.COMPARISON_UNORDERED_LAST) TypeSignature(io.trino.spi.type.TypeSignature) ImmutableSortedSet.toImmutableSortedSet(com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet) INDETERMINATE(io.trino.spi.function.OperatorType.INDETERMINATE) ImmutableSet(com.google.common.collect.ImmutableSet) IsNull(io.trino.spi.function.IsNull) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Annotation(java.lang.annotation.Annotation) Optional(java.util.Optional) ImplementationDependency.isImplementationDependencyAnnotation(io.trino.operator.annotations.ImplementationDependency.isImplementationDependencyAnnotation) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) AnnotatedElement(java.lang.reflect.AnnotatedElement) SqlNullable(io.trino.spi.function.SqlNullable) CASE_INSENSITIVE_ORDER(java.lang.String.CASE_INSENSITIVE_ORDER) HashMap(java.util.HashMap) LiteralParameters(io.trino.spi.function.LiteralParameters) Constructor(java.lang.reflect.Constructor) TreeSet(java.util.TreeSet) IS_DISTINCT_FROM(io.trino.spi.function.OperatorType.IS_DISTINCT_FROM) ImmutableList(com.google.common.collect.ImmutableList) SqlType(io.trino.spi.function.SqlType) LESS_THAN_OR_EQUAL(io.trino.spi.function.OperatorType.LESS_THAN_OR_EQUAL) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Signature(io.trino.metadata.Signature) XX_HASH_64(io.trino.spi.function.OperatorType.XX_HASH_64) Nullable(javax.annotation.Nullable) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) OperatorType(io.trino.spi.function.OperatorType) HASH_CODE(io.trino.spi.function.OperatorType.HASH_CODE) COMPARISON_UNORDERED_FIRST(io.trino.spi.function.OperatorType.COMPARISON_UNORDERED_FIRST) TypeParameter(io.trino.spi.function.TypeParameter) LESS_THAN(io.trino.spi.function.OperatorType.LESS_THAN) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) OperatorType(io.trino.spi.function.OperatorType) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) TreeSet(java.util.TreeSet)

Example 74 with TypeSignature

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");
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 75 with TypeSignature

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);
    }
}
Also used : NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) OrcType(io.trino.orc.metadata.OrcType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowFieldName(io.trino.spi.type.RowFieldName) NamedTypeSignature(io.trino.spi.type.NamedTypeSignature) TrinoException(io.trino.spi.TrinoException)

Aggregations

TypeSignature (io.trino.spi.type.TypeSignature)78 Test (org.testng.annotations.Test)49 TypeSignatureTranslator.parseTypeSignature (io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature)35 ImmutableList (com.google.common.collect.ImmutableList)19 Signature (io.trino.metadata.Signature)17 NamedTypeSignature (io.trino.spi.type.NamedTypeSignature)17 Type (io.trino.spi.type.Type)15 ArrayType (io.trino.spi.type.ArrayType)14 List (java.util.List)13 BoundSignature (io.trino.metadata.BoundSignature)11 Optional (java.util.Optional)10 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 ImmutableSet (com.google.common.collect.ImmutableSet)9 TrinoException (io.trino.spi.TrinoException)9 DecimalType (io.trino.spi.type.DecimalType)9 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)9 Objects.requireNonNull (java.util.Objects.requireNonNull)9 BIGINT (io.trino.spi.type.BigintType.BIGINT)8 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)7 ADD (io.trino.spi.function.OperatorType.ADD)7