Search in sources :

Example 11 with ScalarFunctionImplementation

use of com.facebook.presto.operator.scalar.ScalarFunctionImplementation in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSelectsFirstMethodBasedOnPredicate.

@Test
public void testSelectsFirstMethodBasedOnPredicate() throws Throwable {
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(SIGNATURE).implementation(b -> b.methods("varcharToBigint").withPredicate(context -> true)).implementation(b -> b.methods("varcharToBigintReturnExtraParameter")).build();
    ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY);
    assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), VARCHAR_TO_BIGINT_RETURN_VALUE);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) Slice(io.airlift.slice.Slice) ImmutableMap(com.google.common.collect.ImmutableMap) TypeRegistry(com.facebook.presto.type.TypeRegistry) Assert.assertEquals(org.testng.Assert.assertEquals) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Test(org.testng.annotations.Test) Signature.comparableWithVariadicBound(com.facebook.presto.metadata.Signature.comparableWithVariadicBound) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.spi.type.StandardTypes.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Slices(io.airlift.slice.Slices) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) ADD(com.facebook.presto.spi.function.OperatorType.ADD) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Test(org.testng.annotations.Test)

Example 12 with ScalarFunctionImplementation

use of com.facebook.presto.operator.scalar.ScalarFunctionImplementation in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSameLiteralInArgumentsAndReturnValue.

@Test
public void testSameLiteralInArgumentsAndReturnValue() throws Throwable {
    Signature signature = Signature.builder().name("foo").kind(SCALAR).returnType(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).argumentTypes(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))).build();
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(signature).implementation(b -> b.methods("varcharToVarchar")).build();
    ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY);
    Slice slice = (Slice) functionImplementation.getMethodHandle().invoke(INPUT_SLICE);
    assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) Slice(io.airlift.slice.Slice) ImmutableMap(com.google.common.collect.ImmutableMap) TypeRegistry(com.facebook.presto.type.TypeRegistry) Assert.assertEquals(org.testng.Assert.assertEquals) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Test(org.testng.annotations.Test) Signature.comparableWithVariadicBound(com.facebook.presto.metadata.Signature.comparableWithVariadicBound) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.spi.type.StandardTypes.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Slices(io.airlift.slice.Slices) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) ADD(com.facebook.presto.spi.function.OperatorType.ADD) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Slice(io.airlift.slice.Slice) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Test(org.testng.annotations.Test)

Example 13 with ScalarFunctionImplementation

use of com.facebook.presto.operator.scalar.ScalarFunctionImplementation in project presto by prestodb.

the class TestPolymorphicScalarFunction method testSelectsMethodBasedOnReturnType.

@Test
public void testSelectsMethodBasedOnReturnType() throws Throwable {
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(SIGNATURE).implementation(b -> b.methods("varcharToVarcharCreateSliceWithExtraParameterLength")).implementation(b -> b.methods("varcharToBigintReturnExtraParameter").withExtraParameters(context -> ImmutableList.of(42))).build();
    ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY);
    assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), VARCHAR_TO_BIGINT_RETURN_VALUE);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) ImmutableSet(com.google.common.collect.ImmutableSet) Slice(io.airlift.slice.Slice) ImmutableMap(com.google.common.collect.ImmutableMap) TypeRegistry(com.facebook.presto.type.TypeRegistry) Assert.assertEquals(org.testng.Assert.assertEquals) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Test(org.testng.annotations.Test) Signature.comparableWithVariadicBound(com.facebook.presto.metadata.Signature.comparableWithVariadicBound) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) VARCHAR(com.facebook.presto.spi.type.StandardTypes.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) VARCHAR_TO_BIGINT_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE) Slices(io.airlift.slice.Slices) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) StandardTypes(com.facebook.presto.spi.type.StandardTypes) Math.toIntExact(java.lang.Math.toIntExact) VARCHAR_TO_VARCHAR_RETURN_VALUE(com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) ADD(com.facebook.presto.spi.function.OperatorType.ADD) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) Test(org.testng.annotations.Test)

Aggregations

ScalarFunctionImplementation (com.facebook.presto.operator.scalar.ScalarFunctionImplementation)13 TypeSignature (com.facebook.presto.spi.type.TypeSignature)9 BlockEncodingManager (com.facebook.presto.block.BlockEncodingManager)7 SCALAR (com.facebook.presto.metadata.FunctionKind.SCALAR)7 Signature.comparableWithVariadicBound (com.facebook.presto.metadata.Signature.comparableWithVariadicBound)7 VARCHAR_TO_BIGINT_RETURN_VALUE (com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_BIGINT_RETURN_VALUE)7 VARCHAR_TO_VARCHAR_RETURN_VALUE (com.facebook.presto.metadata.TestPolymorphicScalarFunction.TestMethods.VARCHAR_TO_VARCHAR_RETURN_VALUE)7 ADD (com.facebook.presto.spi.function.OperatorType.ADD)7 StandardTypes (com.facebook.presto.spi.type.StandardTypes)7 VARCHAR (com.facebook.presto.spi.type.StandardTypes.VARCHAR)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 FeaturesConfig (com.facebook.presto.sql.analyzer.FeaturesConfig)7 TypeRegistry (com.facebook.presto.type.TypeRegistry)7 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableMap (com.google.common.collect.ImmutableMap)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 Slice (io.airlift.slice.Slice)7 Slices (io.airlift.slice.Slices)7 Math.toIntExact (java.lang.Math.toIntExact)7 Assert.assertEquals (org.testng.Assert.assertEquals)7