Search in sources :

Example 6 with ScalarFunctionImplementation

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

the class TestPolymorphicScalarFunction method testSelectsMethodBasedOnArgumentTypes.

@Test
public void testSelectsMethodBasedOnArgumentTypes() throws Throwable {
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(SIGNATURE).implementation(b -> b.methods("bigintToBigintReturnExtraParameter")).implementation(b -> b.methods("varcharToBigintReturnExtraParameter").withExtraParameters(context -> ImmutableList.of(context.getLiteral("x")))).build();
    ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY);
    assertEquals(functionImplementation.getMethodHandle().invoke(INPUT_SLICE), INPUT_VARCHAR_LENGTH);
}
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 7 with ScalarFunctionImplementation

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

the class TestPolymorphicScalarFunction method testSelectsSecondMethodBasedOnPredicate.

@Test
public void testSelectsSecondMethodBasedOnPredicate() throws Throwable {
    SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class).signature(SIGNATURE).implementation(b -> b.methods("varcharToBigintReturnExtraParameter").withPredicate(context -> false)).implementation(b -> b.methods("varcharToBigint")).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 8 with ScalarFunctionImplementation

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

the class FunctionCallCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext context, Type returnType, List<RowExpression> arguments) {
    FunctionRegistry registry = context.getRegistry();
    ScalarFunctionImplementation function = registry.getScalarFunctionImplementation(signature);
    List<BytecodeNode> argumentsBytecode = new ArrayList<>();
    for (RowExpression argument : arguments) {
        argumentsBytecode.add(context.generate(argument));
    }
    return context.generateCall(signature.getName(), function, argumentsBytecode);
}
Also used : ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) FunctionRegistry(com.facebook.presto.metadata.FunctionRegistry) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.sql.relational.RowExpression) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 9 with ScalarFunctionImplementation

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

the class InCodeGenerator method buildInCase.

private static BytecodeBlock buildInCase(BytecodeGeneratorContext generatorContext, Scope scope, Type type, LabelNode caseLabel, LabelNode matchLabel, LabelNode noMatchLabel, Collection<BytecodeNode> testValues, boolean checkForNulls) {
    // caseWasNull is set to true the first time a null in `testValues` is encountered
    Variable caseWasNull = null;
    if (checkForNulls) {
        caseWasNull = scope.createTempVariable(boolean.class);
    }
    BytecodeBlock caseBlock = new BytecodeBlock().visitLabel(caseLabel);
    if (checkForNulls) {
        caseBlock.putVariable(caseWasNull, false);
    }
    LabelNode elseLabel = new LabelNode("else");
    BytecodeBlock elseBlock = new BytecodeBlock().visitLabel(elseLabel);
    Variable wasNull = generatorContext.wasNull();
    if (checkForNulls) {
        elseBlock.append(wasNull.set(caseWasNull));
    }
    elseBlock.gotoLabel(noMatchLabel);
    ScalarFunctionImplementation operator = generatorContext.getRegistry().getScalarFunctionImplementation(internalOperator(EQUAL, BOOLEAN, ImmutableList.of(type, type)));
    Binding equalsFunction = generatorContext.getCallSiteBinder().bind(operator.getMethodHandle());
    BytecodeNode elseNode = elseBlock;
    for (BytecodeNode testNode : testValues) {
        LabelNode testLabel = new LabelNode("test");
        IfStatement test = new IfStatement();
        test.condition().visitLabel(testLabel).dup(type.getJavaType()).append(testNode);
        if (checkForNulls) {
            IfStatement wasNullCheck = new IfStatement("if wasNull, set caseWasNull to true, clear wasNull, pop 2 values of type, and goto next test value");
            wasNullCheck.condition(wasNull);
            wasNullCheck.ifTrue(new BytecodeBlock().append(caseWasNull.set(constantTrue())).append(wasNull.set(constantFalse())).pop(type.getJavaType()).pop(type.getJavaType()).gotoLabel(elseLabel));
            test.condition().append(wasNullCheck);
        }
        test.condition().append(invoke(equalsFunction, EQUAL.name()));
        test.ifTrue().gotoLabel(matchLabel);
        test.ifFalse(elseNode);
        elseNode = test;
        elseLabel = testLabel;
    }
    caseBlock.append(elseNode);
    return caseBlock;
}
Also used : LabelNode(com.facebook.presto.bytecode.instruction.LabelNode) ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 10 with ScalarFunctionImplementation

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

the class TestPolymorphicScalarFunction method testTypeParameters.

@Test
public void testTypeParameters() throws Throwable {
    Signature signature = Signature.builder().name("foo").kind(SCALAR).typeVariableConstraints(comparableWithVariadicBound("V", VARCHAR)).returnType(parseTypeSignature("V")).argumentTypes(parseTypeSignature("V")).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)

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