Search in sources :

Example 6 with ParametricScalar

use of io.trino.operator.scalar.ParametricScalar 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());
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) FunctionDependencies(io.trino.metadata.FunctionDependencies) Test(org.testng.annotations.Test)

Example 7 with ParametricScalar

use of io.trino.operator.scalar.ParametricScalar 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");
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeSignature(io.trino.spi.type.TypeSignature) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) LiteralImplementationDependency(io.trino.operator.annotations.LiteralImplementationDependency) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalarImplementationChoice(io.trino.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) ParametricScalar(io.trino.operator.scalar.ParametricScalar) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) Test(org.testng.annotations.Test)

Example 8 with ParametricScalar

use of io.trino.operator.scalar.ParametricScalar 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);
}
Also used : SpecializedSignature(io.trino.operator.scalar.annotations.ParametricScalarImplementation.SpecializedSignature) HashMap(java.util.HashMap) ParametricImplementationsGroup(io.trino.operator.ParametricImplementationsGroup) Method(java.lang.reflect.Method) SpecializedSignature(io.trino.operator.scalar.annotations.ParametricScalarImplementation.SpecializedSignature) Signature(io.trino.metadata.Signature) ParametricScalar(io.trino.operator.scalar.ParametricScalar)

Example 9 with ParametricScalar

use of io.trino.operator.scalar.ParametricScalar in project trino by trinodb.

the class TestAnnotationEngineForScalars method testStaticMethodScalarParse.

@Test
public void testStaticMethodScalarParse() {
    Signature expectedSignature = new Signature("static_method_scalar", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(StaticMethodScalarFunction.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 single implementation based on method");
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Example 10 with ParametricScalar

use of io.trino.operator.scalar.ParametricScalar in project trino by trinodb.

the class TestAnnotationEngineForScalars method testMultiScalarParse.

@Test
public void testMultiScalarParse() {
    Signature expectedSignature1 = new Signature("static_method_scalar_1", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()));
    Signature expectedSignature2 = new Signature("static_method_scalar_2", BIGINT.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature()));
    List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinitions(MultiScalarFunction.class);
    assertEquals(functions.size(), 2);
    ParametricScalar scalar1 = (ParametricScalar) functions.stream().filter(function -> function.getFunctionMetadata().getSignature().equals(expectedSignature1)).collect(toImmutableList()).get(0);
    ParametricScalar scalar2 = (ParametricScalar) functions.stream().filter(function -> function.getFunctionMetadata().getSignature().equals(expectedSignature2)).collect(toImmutableList()).get(0);
    assertImplementationCount(scalar1, 1, 0, 0);
    assertImplementationCount(scalar2, 1, 0, 0);
    FunctionMetadata functionMetadata1 = scalar1.getFunctionMetadata();
    assertEquals(functionMetadata1.getSignature(), expectedSignature1);
    assertTrue(functionMetadata1.isDeterministic());
    assertFalse(functionMetadata1.isHidden());
    assertEquals(functionMetadata1.getDescription(), "Simple scalar with single implementation based on method 1");
    FunctionMetadata functionMetadata2 = scalar2.getFunctionMetadata();
    assertEquals(functionMetadata2.getSignature(), expectedSignature2);
    assertFalse(functionMetadata2.isDeterministic());
    assertTrue(functionMetadata2.isHidden());
    assertEquals(functionMetadata2.getDescription(), "Simple scalar with single implementation based on method 2");
}
Also used : SqlNullable(io.trino.spi.function.SqlNullable) Slice(io.airlift.slice.Slice) Description(io.trino.spi.function.Description) FunctionDependencies(io.trino.metadata.FunctionDependencies) ImplementationDependency(io.trino.operator.annotations.ImplementationDependency) ParametricScalar(io.trino.operator.scalar.ParametricScalar) Type(io.trino.spi.type.Type) BOOLEAN(io.trino.spi.type.BooleanType.BOOLEAN) Assert.assertEquals(org.testng.Assert.assertEquals) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) ParametricScalarImplementationChoice(io.trino.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice) Test(org.testng.annotations.Test) LiteralParameters(io.trino.spi.function.LiteralParameters) FunctionManager.createTestingFunctionManager(io.trino.metadata.FunctionManager.createTestingFunctionManager) ImmutableList(com.google.common.collect.ImmutableList) Block(io.trino.spi.block.Block) SqlType(io.trino.spi.function.SqlType) ScalarFromAnnotationsParser(io.trino.operator.scalar.annotations.ScalarFromAnnotationsParser) Signature(io.trino.metadata.Signature) Assert.assertFalse(org.testng.Assert.assertFalse) FunctionMetadata(io.trino.metadata.FunctionMetadata) TypeImplementationDependency(io.trino.operator.annotations.TypeImplementationDependency) TypeSignature(io.trino.spi.type.TypeSignature) TypeSignature.arrayType(io.trino.spi.type.TypeSignature.arrayType) ImmutableSet(com.google.common.collect.ImmutableSet) IsNull(io.trino.spi.function.IsNull) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) StandardTypes(io.trino.spi.type.StandardTypes) ScalarFunction(io.trino.spi.function.ScalarFunction) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) LiteralImplementationDependency(io.trino.operator.annotations.LiteralImplementationDependency) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) FunctionManager(io.trino.metadata.FunctionManager) DOUBLE(io.trino.spi.type.DoubleType.DOUBLE) List(java.util.List) BIGINT(io.trino.spi.type.BigintType.BIGINT) TypeParameter(io.trino.spi.function.TypeParameter) BoundSignature(io.trino.metadata.BoundSignature) Signature.typeVariable(io.trino.metadata.Signature.typeVariable) Assert.assertTrue(org.testng.Assert.assertTrue) LiteralParameter(io.trino.spi.function.LiteralParameter) VarcharType.createVarcharType(io.trino.spi.type.VarcharType.createVarcharType) TypeSignatureParameter(io.trino.spi.type.TypeSignatureParameter) FunctionMetadata(io.trino.metadata.FunctionMetadata) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) Test(org.testng.annotations.Test)

Aggregations

ParametricScalar (io.trino.operator.scalar.ParametricScalar)14 FunctionMetadata (io.trino.metadata.FunctionMetadata)13 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)13 Test (org.testng.annotations.Test)13 Signature (io.trino.metadata.Signature)12 BoundSignature (io.trino.metadata.BoundSignature)11 TypeSignature (io.trino.spi.type.TypeSignature)11 FunctionDependencies (io.trino.metadata.FunctionDependencies)4 ImplementationDependency (io.trino.operator.annotations.ImplementationDependency)4 LiteralImplementationDependency (io.trino.operator.annotations.LiteralImplementationDependency)4 TypeImplementationDependency (io.trino.operator.annotations.TypeImplementationDependency)4 ChoicesScalarFunctionImplementation (io.trino.operator.scalar.ChoicesScalarFunctionImplementation)4 ParametricScalarImplementationChoice (io.trino.operator.scalar.annotations.ParametricScalarImplementation.ParametricScalarImplementationChoice)4 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 Slice (io.airlift.slice.Slice)1 FunctionManager (io.trino.metadata.FunctionManager)1