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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations