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:
}
}
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));
}
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);
}
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);
}
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);
}
Aggregations