Search in sources :

Example 16 with TypeSignatureProvider

use of io.trino.sql.analyzer.TypeSignatureProvider 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 17 with TypeSignatureProvider

use of io.trino.sql.analyzer.TypeSignatureProvider in project trino by trinodb.

the class TestMinMaxByAggregation method testMaxDoubleLongArray.

@Test
public void testMaxDoubleLongArray() {
    List<TypeSignatureProvider> parameterTypes = fromTypes(new ArrayType(BIGINT), DOUBLE);
    assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of("max_by"), parameterTypes, null, createArrayBigintBlock(asList(asList(3L, 4L), null, asList(2L, 2L))), createDoublesBlock(1.0, 2.0, null));
    assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of("max_by"), parameterTypes, asList(2L, 2L), createArrayBigintBlock(asList(asList(3L, 4L), null, asList(2L, 2L))), createDoublesBlock(0.0, 1.0, 2.0));
}
Also used : TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 18 with TypeSignatureProvider

use of io.trino.sql.analyzer.TypeSignatureProvider in project trino by trinodb.

the class TestMinMaxByAggregation method testMaxLongArrayLongArray.

@Test
public void testMaxLongArrayLongArray() {
    List<TypeSignatureProvider> parameterTypes = fromTypes(new ArrayType(BIGINT), new ArrayType(BIGINT));
    assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of("max_by"), parameterTypes, asList(3L, 3L), createArrayBigintBlock(asList(asList(3L, 3L), null, asList(1L, 2L))), createArrayBigintBlock(asList(asList(3L, 4L), null, asList(2L, 2L))));
}
Also used : TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 19 with TypeSignatureProvider

use of io.trino.sql.analyzer.TypeSignatureProvider in project trino by trinodb.

the class TestMinMaxByAggregation method testMaxLongArraySlice.

@Test
public void testMaxLongArraySlice() {
    List<TypeSignatureProvider> parameterTypes = fromTypes(VARCHAR, new ArrayType(BIGINT));
    assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of("max_by"), parameterTypes, "a", createStringsBlock("a", "b", "c"), createArrayBigintBlock(asList(asList(3L, 4L), null, asList(2L, 2L))));
}
Also used : TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Example 20 with TypeSignatureProvider

use of io.trino.sql.analyzer.TypeSignatureProvider in project trino by trinodb.

the class TestMinMaxByAggregation method testMinBooleanLongArray.

@Test
public void testMinBooleanLongArray() {
    List<TypeSignatureProvider> parameterTypes = fromTypes(new ArrayType(BIGINT), BOOLEAN);
    assertAggregation(FUNCTION_RESOLUTION, QualifiedName.of("min_by"), parameterTypes, null, createArrayBigintBlock(asList(asList(3L, 4L), null, null)), createBooleansBlock(true, false, true));
}
Also used : TypeSignatureProvider(io.trino.sql.analyzer.TypeSignatureProvider) ArrayType(io.trino.spi.type.ArrayType) Test(org.testng.annotations.Test)

Aggregations

TypeSignatureProvider (io.trino.sql.analyzer.TypeSignatureProvider)22 ArrayType (io.trino.spi.type.ArrayType)21 Test (org.testng.annotations.Test)21 ImmutableList (com.google.common.collect.ImmutableList)2 RowType (io.trino.spi.type.RowType)2 Type (io.trino.spi.type.Type)2 TypeSignature (io.trino.spi.type.TypeSignature)2 TypeSignatureParameter (io.trino.spi.type.TypeSignatureParameter)2 FunctionType (io.trino.type.FunctionType)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 TEST_SESSION (io.trino.SessionTestUtils.TEST_SESSION)1 Signature.castableFromTypeParameter (io.trino.metadata.Signature.castableFromTypeParameter)1 Signature.castableToTypeParameter (io.trino.metadata.Signature.castableToTypeParameter)1 Signature.comparableTypeParameter (io.trino.metadata.Signature.comparableTypeParameter)1 Signature.orderableTypeParameter (io.trino.metadata.Signature.orderableTypeParameter)1 Signature.typeVariable (io.trino.metadata.Signature.typeVariable)1 Signature.withVariadicBound (io.trino.metadata.Signature.withVariadicBound)1 BIGINT (io.trino.spi.type.BigintType.BIGINT)1 BOOLEAN (io.trino.spi.type.BooleanType.BOOLEAN)1