use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.
the class ScalarFromAnnotationsParser method parseParametricScalar.
private static SqlScalarFunction parseParametricScalar(ScalarHeaderAndMethods scalar, Map<Set<TypeParameter>, Constructor<?>> constructors) {
ImmutableMap.Builder<Signature, ScalarImplementation> exactImplementations = ImmutableMap.builder();
ImmutableList.Builder<ScalarImplementation> specializedImplementations = ImmutableList.builder();
ImmutableList.Builder<ScalarImplementation> genericImplementations = ImmutableList.builder();
Optional<Signature> signature = Optional.empty();
ScalarImplementationHeader header = scalar.getHeader();
checkArgument(!header.getName().isEmpty());
for (Method method : scalar.getMethods()) {
ScalarImplementation implementation = ScalarImplementation.Parser.parseImplementation(header.getName(), method, constructors);
if (implementation.getSignature().getTypeVariableConstraints().isEmpty() && implementation.getSignature().getArgumentTypes().stream().noneMatch(TypeSignature::isCalculated) && !implementation.getSignature().getReturnType().isCalculated()) {
exactImplementations.put(implementation.getSignature(), implementation);
continue;
}
if (implementation.hasSpecializedTypeParameters()) {
specializedImplementations.add(implementation);
} else {
genericImplementations.add(implementation);
}
signature = signature.isPresent() ? signature : Optional.of(implementation.getSignature());
validateSignature(signature, implementation.getSignature());
}
Signature scalarSignature = signature.orElseGet(() -> getOnlyElement(exactImplementations.build().keySet()));
header.getOperatorType().ifPresent(operatorType -> validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
ScalarImplementations implementations = new ScalarImplementations(exactImplementations.build(), specializedImplementations.build(), genericImplementations.build());
return new ParametricScalar(scalarSignature, header.getHeader(), implementations);
}
use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.
the class TestAnnotationEngineForScalars method testMultiScalarParse.
@Test
public void testMultiScalarParse() {
Signature expectedSignature1 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_1"), FunctionKind.SCALAR, DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
Signature expectedSignature2 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_2"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
Signature expectedSignature3 = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "static_method_scalar_3"), FunctionKind.SCALAR, BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(MultiScalarFunction.class);
assertEquals(functions.size(), 3);
ParametricScalar scalar1 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature1)).collect(toImmutableList()).get(0);
ParametricScalar scalar2 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature2)).collect(toImmutableList()).get(0);
ParametricScalar scalar3 = (ParametricScalar) functions.stream().filter(signature -> signature.getSignature().equals(expectedSignature3)).collect(toImmutableList()).get(0);
assertImplementationCount(scalar1, 1, 0, 0);
assertImplementationCount(scalar2, 1, 0, 0);
assertEquals(scalar1.getSignature(), expectedSignature1);
assertTrue(scalar1.isDeterministic());
assertEquals(scalar1.getVisibility(), PUBLIC);
assertEquals(scalar1.getDescription(), "Simple scalar with single implementation based on method 1");
assertEquals(scalar2.getSignature(), expectedSignature2);
assertFalse(scalar2.isDeterministic());
assertEquals(scalar2.getVisibility(), HIDDEN);
assertEquals(scalar2.getDescription(), "Simple scalar with single implementation based on method 2");
assertEquals(scalar3.getSignature(), expectedSignature3);
assertFalse(scalar3.isDeterministic());
assertEquals(scalar3.getVisibility(), EXPERIMENTAL);
assertEquals(scalar3.getDescription(), "Simple scalar with single implementation based on method 3");
}
use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.
the class TestAnnotationEngineForScalars method testPartiallyFixedTypeParameterParse.
@Test
public void testPartiallyFixedTypeParameterParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "partially_fixed_type_parameter_scalar_function"), FunctionKind.SCALAR, ImmutableList.of(typeVariable("T1"), typeVariable("T2")), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()), false);
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(PartiallyFixedTypeParameterScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
assertImplementationCount(scalar, 0, 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(), 1);
assertEquals(scalar.getSignature(), expectedSignature);
assertTrue(scalar.isDeterministic());
assertEquals(scalar.getVisibility(), PUBLIC);
assertEquals(scalar.getDescription(), "Parametric scalar that uses TypeParameter with partially fixed type");
}
use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.
the class TestAnnotationEngineForScalars method testSimpleInjectionScalarParse.
@Test
public void testSimpleInjectionScalarParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_scalar_inject"), FunctionKind.SCALAR, ImmutableList.of(), ImmutableList.of(), BIGINT.getTypeSignature(), ImmutableList.of(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))), false);
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(SimpleInjectionScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
assertImplementationCount(scalar, 0, 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(), 1);
assertTrue(dependencies.get(0) instanceof LiteralImplementationDependency);
assertEquals(scalar.getSignature(), expectedSignature);
assertTrue(scalar.isDeterministic());
assertEquals(scalar.getVisibility(), PUBLIC);
assertEquals(scalar.getDescription(), "Parametric scalar with literal injected");
}
use of com.facebook.presto.operator.scalar.ParametricScalar in project presto by prestodb.
the class TestAnnotationEngineForScalars method testWithNullablePrimitiveArgScalarParse.
@Test
public void testWithNullablePrimitiveArgScalarParse() {
Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "scalar_with_nullable"), FunctionKind.SCALAR, 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);
assertEquals(scalar.getSignature(), expectedSignature);
assertTrue(scalar.isDeterministic());
assertEquals(scalar.getVisibility(), PUBLIC);
assertEquals(scalar.getDescription(), "Simple scalar with nullable primitive");
BuiltInScalarFunctionImplementation specialized = scalar.specialize(BoundVariables.builder().build(), 2, FUNCTION_AND_TYPE_MANAGER);
assertFalse(specialized.getInstanceFactory().isPresent());
assertEquals(specialized.getArgumentProperty(0), valueTypeArgumentProperty(RETURN_NULL_ON_NULL));
assertEquals(specialized.getArgumentProperty(1), valueTypeArgumentProperty(USE_NULL_FLAG));
}
Aggregations