Search in sources :

Example 41 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class OperatorValidator method validateOperator.

public static void validateOperator(OperatorType operatorType, TypeSignature returnType, List<TypeSignature> argumentTypes) {
    switch(operatorType) {
        case ADD:
        case SUBTRACT:
        case MULTIPLY:
        case DIVIDE:
        case MODULUS:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 2);
            break;
        case NEGATION:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
        case EQUAL:
        case COMPARISON_UNORDERED_LAST:
        case COMPARISON_UNORDERED_FIRST:
        case LESS_THAN:
        case LESS_THAN_OR_EQUAL:
            validateComparisonOperatorSignature(operatorType, returnType, argumentTypes, 2);
            break;
        case CAST:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
        case SUBSCRIPT:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 2);
            checkArgument(argumentTypes.get(0).getBase().equals(StandardTypes.ARRAY) || argumentTypes.get(0).getBase().equals(StandardTypes.MAP), "First argument must be an ARRAY or MAP");
            if (argumentTypes.get(0).getBase().equals(StandardTypes.ARRAY)) {
                checkArgument(argumentTypes.get(1).getBase().equals(StandardTypes.BIGINT), "Second argument must be a BIGINT");
                TypeSignature elementType = argumentTypes.get(0).getTypeParametersAsTypeSignatures().get(0);
                checkArgument(returnType.equals(elementType), "[] return type does not match ARRAY element type");
            } else {
                TypeSignature valueType = argumentTypes.get(0).getTypeParametersAsTypeSignatures().get(1);
                checkArgument(returnType.equals(valueType), "[] return type does not match MAP value type");
            }
            break;
        case HASH_CODE:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            checkArgument(returnType.getBase().equals(StandardTypes.BIGINT), "%s operator must return a BIGINT: %s", operatorType, formatSignature(operatorType, returnType, argumentTypes));
            break;
        case SATURATED_FLOOR_CAST:
            validateOperatorSignature(operatorType, returnType, argumentTypes, 1);
            break;
        case IS_DISTINCT_FROM:
        case XX_HASH_64:
        case INDETERMINATE:
    }
}
Also used : TypeSignature(io.trino.spi.type.TypeSignature)

Example 42 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class WindowAnnotationsParser method parse.

private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
    List<TypeVariableConstraint> typeVariables = ImmutableList.of();
    if (!window.typeVariable().isEmpty()) {
        typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
    }
    List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(type -> parseTypeSignature(type, ImmutableSet.of())).collect(toImmutableList());
    Signature signature = new Signature(window.name(), typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType(), ImmutableSet.of()), argumentTypes, false);
    Optional<String> description = Optional.ofNullable(clazz.getAnnotation(Description.class)).map(Description::value);
    boolean deprecated = clazz.getAnnotationsByType(Deprecated.class).length > 0;
    return new SqlWindowFunction(signature, description, deprecated, new ReflectionWindowFunctionSupplier(window.argumentTypes().length, clazz));
}
Also used : WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) Description(io.trino.spi.function.Description) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Stream(java.util.stream.Stream) ImmutableList(com.google.common.collect.ImmutableList) WindowFunction(io.trino.spi.function.WindowFunction) Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Optional(java.util.Optional) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Description(io.trino.spi.function.Description) WindowFunctionSignature(io.trino.spi.function.WindowFunctionSignature) TypeSignatureTranslator.parseTypeSignature(io.trino.sql.analyzer.TypeSignatureTranslator.parseTypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) TypeVariableConstraint(io.trino.metadata.TypeVariableConstraint)

Example 43 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestAnnotationEngineForAggregates method testSimpleGenericAggregationFunctionParse.

@Test
public void testSimpleGenericAggregationFunctionParse() {
    Signature expectedSignature = new Signature("simple_generic_implementations", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(GenericAggregationFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with two generic implementations");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    assertEquals(aggregation.getStateClass(), NullableLongState.class);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertImplementationCount(implementations, 0, 0, 2);
    AggregationImplementation implementationDouble = implementations.getGenericImplementations().stream().filter(impl -> impl.getInputFunction().type().equals(methodType(void.class, NullableLongState.class, double.class))).collect(toImmutableList()).get(0);
    assertEquals(implementationDouble.getDefinitionClass(), GenericAggregationFunction.class);
    assertDependencyCount(implementationDouble, 0, 0, 0);
    assertFalse(implementationDouble.hasSpecializedTypeParameters());
    assertEquals(implementationDouble.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    AggregationImplementation implementationLong = implementations.getGenericImplementations().stream().filter(impl -> impl.getInputFunction().type().equals(methodType(void.class, NullableLongState.class, long.class))).collect(toImmutableList()).get(0);
    assertEquals(implementationLong.getDefinitionClass(), GenericAggregationFunction.class);
    assertDependencyCount(implementationLong, 0, 0, 0);
    assertFalse(implementationLong.hasSpecializedTypeParameters());
    assertEquals(implementationLong.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

Example 44 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestAnnotationEngineForAggregates method testLongConstraintAggregateFunctionParse.

@Test
public void testLongConstraintAggregateFunctionParse() {
    Signature expectedSignature = new Signature("parametric_aggregate_long_constraint", ImmutableList.of(), ImmutableList.of(new LongVariableConstraint("z", "x + y")), new TypeSignature("varchar", TypeSignatureParameter.typeVariable("z")), ImmutableList.of(new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x")), new TypeSignature("varchar", TypeSignatureParameter.typeVariable("y"))), false);
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(LongConstraintAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Parametric aggregate with parametric type returned");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), LongConstraintAggregateFunction.class);
    assertDependencyCount(implementation, 0, 0, 0);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), createVarcharType(30), ImmutableList.of(createVarcharType(17), createVarcharType(13)));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) LongVariableConstraint(io.trino.metadata.LongVariableConstraint) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

Example 45 with TypeSignature

use of io.trino.spi.type.TypeSignature in project trino by trinodb.

the class TestAnnotationEngineForAggregates method testInjectLiteralAggregateParse.

@Test
public void testInjectLiteralAggregateParse() {
    Signature expectedSignature = new Signature("inject_literal_aggregate", new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x")), ImmutableList.of(new TypeSignature("varchar", TypeSignatureParameter.typeVariable("x"))));
    ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(InjectLiteralAggregateFunction.class));
    assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with type literal");
    assertTrue(aggregation.getFunctionMetadata().isDeterministic());
    assertEquals(aggregation.getFunctionMetadata().getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertEquals(implementation.getDefinitionClass(), InjectLiteralAggregateFunction.class);
    assertDependencyCount(implementation, 1, 1, 1);
    assertTrue(implementation.getInputDependencies().get(0) instanceof LiteralImplementationDependency);
    assertTrue(implementation.getCombineDependencies().get(0) instanceof LiteralImplementationDependency);
    assertTrue(implementation.getOutputDependencies().get(0) instanceof LiteralImplementationDependency);
    assertFalse(implementation.hasSpecializedTypeParameters());
    assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
    BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), createVarcharType(17), ImmutableList.of(createVarcharType(17)));
    AggregationFunctionMetadata aggregationMetadata = aggregation.getAggregationMetadata();
    assertFalse(aggregationMetadata.isOrderSensitive());
    assertFalse(aggregationMetadata.getIntermediateTypes().isEmpty());
    aggregation.specialize(boundSignature, NO_FUNCTION_DEPENDENCIES);
}
Also used : AggregationImplementation(io.trino.operator.aggregation.AggregationImplementation) TypeSignature(io.trino.spi.type.TypeSignature) LiteralImplementationDependency(io.trino.operator.annotations.LiteralImplementationDependency) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricAggregation(io.trino.operator.aggregation.ParametricAggregation) AggregationFunctionMetadata(io.trino.metadata.AggregationFunctionMetadata) Test(org.testng.annotations.Test)

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