use of io.trino.metadata.Signature in project trino by trinodb.
the class TestAnnotationEngineForScalars method testWithNullablePrimitiveArgScalarParse.
@Test
public void testWithNullablePrimitiveArgScalarParse() {
Signature expectedSignature = new Signature("scalar_with_nullable", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature()));
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(WithNullablePrimitiveArgScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
assertEquals(functionMetadata.getSignature(), expectedSignature);
assertTrue(functionMetadata.isDeterministic());
assertFalse(functionMetadata.isHidden());
assertEquals(functionMetadata.getDescription(), "Simple scalar with nullable primitive");
assertFalse(functionMetadata.getFunctionNullability().isArgumentNullable(0));
assertTrue(functionMetadata.getFunctionNullability().isArgumentNullable(1));
BoundSignature boundSignature = new BoundSignature(expectedSignature.getName(), DOUBLE, ImmutableList.of(DOUBLE, DOUBLE));
ChoicesScalarFunctionImplementation specialized = (ChoicesScalarFunctionImplementation) scalar.specialize(boundSignature, new FunctionDependencies(FUNCTION_MANAGER::getScalarFunctionInvoker, ImmutableMap.of(), ImmutableSet.of()));
assertFalse(specialized.getChoices().get(0).getInstanceFactory().isPresent());
}
use of io.trino.metadata.Signature in project trino by trinodb.
the class TestAnnotationEngineForScalars method testConstructorInjectionScalarParse.
@Test
public void testConstructorInjectionScalarParse() {
Signature expectedSignature = new Signature("parametric_scalar_inject_constructor", ImmutableList.of(typeVariable("T")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(arrayType(new TypeSignature("T"))), false);
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(ConstructorInjectionScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
assertImplementationCount(scalar, 2, 0, 1);
List<ParametricScalarImplementationChoice> parametricScalarImplementationChoices = scalar.getImplementations().getGenericImplementations().get(0).getChoices();
assertEquals(parametricScalarImplementationChoices.size(), 1);
List<ImplementationDependency> dependencies = parametricScalarImplementationChoices.get(0).getDependencies();
assertEquals(dependencies.size(), 0);
List<ImplementationDependency> constructorDependencies = parametricScalarImplementationChoices.get(0).getConstructorDependencies();
assertEquals(constructorDependencies.size(), 1);
assertTrue(constructorDependencies.get(0) instanceof TypeImplementationDependency);
FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
assertEquals(functionMetadata.getSignature(), expectedSignature);
assertTrue(functionMetadata.isDeterministic());
assertFalse(functionMetadata.isHidden());
assertEquals(functionMetadata.getDescription(), "Parametric scalar with type injected though constructor");
}
use of io.trino.metadata.Signature in project trino by trinodb.
the class TestAnnotationEngineForAggregates method testInjectTypeAggregateParse.
@Test
public void testInjectTypeAggregateParse() {
Signature expectedSignature = new Signature("inject_type_aggregate", ImmutableList.of(typeVariable("T")), ImmutableList.of(), new TypeSignature("T"), ImmutableList.of(new TypeSignature("T")), false);
ParametricAggregation aggregation = getOnlyElement(parseFunctionDefinitions(InjectTypeAggregateFunction.class));
assertEquals(aggregation.getFunctionMetadata().getDescription(), "Simple aggregate with type injected");
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(), InjectTypeAggregateFunction.class);
assertDependencyCount(implementation, 1, 1, 1);
assertTrue(implementation.getInputDependencies().get(0) instanceof TypeImplementationDependency);
assertTrue(implementation.getCombineDependencies().get(0) instanceof TypeImplementationDependency);
assertTrue(implementation.getOutputDependencies().get(0) instanceof TypeImplementationDependency);
assertFalse(implementation.hasSpecializedTypeParameters());
assertEquals(implementation.getInputParameterKinds(), ImmutableList.of(STATE, INPUT_CHANNEL));
BoundSignature boundSignature = new BoundSignature(aggregation.getFunctionMetadata().getSignature().getName(), DoubleType.DOUBLE, ImmutableList.of(DoubleType.DOUBLE));
specializeAggregationFunction(boundSignature, aggregation);
}
use of io.trino.metadata.Signature in project trino by trinodb.
the class ScalarFromAnnotationsParser method parseParametricScalar.
private static SqlScalarFunction parseParametricScalar(ScalarHeaderAndMethods scalar, Optional<Constructor<?>> constructor, boolean deprecated) {
ScalarImplementationHeader header = scalar.getHeader();
checkArgument(!header.getName().isEmpty());
Map<SpecializedSignature, ParametricScalarImplementation.Builder> signatures = new HashMap<>();
for (Method method : scalar.getMethods()) {
ParametricScalarImplementation.Parser implementation = new ParametricScalarImplementation.Parser(header.getName(), method, constructor);
if (!signatures.containsKey(implementation.getSpecializedSignature())) {
ParametricScalarImplementation.Builder builder = new ParametricScalarImplementation.Builder(implementation.getSignature(), implementation.getArgumentNativeContainerTypes(), implementation.getSpecializedTypeParameters(), implementation.getReturnNativeContainerType());
signatures.put(implementation.getSpecializedSignature(), builder);
builder.addChoice(implementation.getChoice());
} else {
ParametricScalarImplementation.Builder builder = signatures.get(implementation.getSpecializedSignature());
builder.addChoice(implementation.getChoice());
}
}
ParametricImplementationsGroup.Builder<ParametricScalarImplementation> implementationsBuilder = ParametricImplementationsGroup.builder();
for (ParametricScalarImplementation.Builder implementation : signatures.values()) {
implementationsBuilder.addImplementation(implementation.build());
}
ParametricImplementationsGroup<ParametricScalarImplementation> implementations = implementationsBuilder.build();
Signature scalarSignature = implementations.getSignature();
header.getOperatorType().ifPresent(operatorType -> validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
return new ParametricScalar(scalarSignature, header.getHeader(), implementations, deprecated);
}
use of io.trino.metadata.Signature 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));
}
Aggregations