@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "methods must be selected first") public void testFailNoMethodsAreSelectedWhenExtraParametersFunctionIsSet() { SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .withExtraParameters(context -> ImmutableList.of(42)))) .build(); }
@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "methods must be selected first") public void testFailNoMethodsAreSelectedWhenExtraParametersFunctionIsSet() { SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .withExtraParameters(context -> ImmutableList.of(42)))) .build(); }
@Test public void testSelectsMethodBasedOnReturnType() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarcharCreateSliceWithExtraParameterLength")) .implementation(methodsGroup -> methodsGroup .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); }
@Test public void testSetsHiddenToTrueForOperators() { Signature signature = Signature.builder() .operatorType(ADD) .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) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); }
@Test public void testSelectsMethodBasedOnReturnType() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarcharCreateSliceWithExtraParameterLength")) .implementation(methodsGroup -> methodsGroup .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); }
@Test public void testSelectsMethodBasedOnArgumentTypes() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("bigintToBigintReturnExtraParameter")) .implementation(methodsGroup -> methodsGroup .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); }
@Test public void testSetsHiddenToTrueForOperators() { Signature signature = Signature.builder() .operatorType(ADD) .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) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); }
private static SqlScalarFunction betweenOperator() { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(BETWEEN) .argumentTypes(DECIMAL_SIGNATURE, DECIMAL_SIGNATURE, DECIMAL_SIGNATURE) .returnType(parseTypeSignature(BOOLEAN)) .build(); return SqlScalarFunction.builder(DecimalInequalityOperators.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("betweenShortShortShort", "betweenLongLongLong"))) .build(); }
@Test public void testSelectsMethodBasedOnArgumentTypes() throws Throwable { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("bigintToBigintReturnExtraParameter")) .implementation(methodsGroup -> methodsGroup .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); }
private static SqlScalarFunction betweenOperator() { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(BETWEEN) .argumentTypes(DECIMAL_SIGNATURE, DECIMAL_SIGNATURE, DECIMAL_SIGNATURE) .returnType(parseTypeSignature(BOOLEAN)) .build(); return SqlScalarFunction.builder(DecimalInequalityOperators.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("betweenShortShortShort", "betweenLongLongLong"))) .build(); }
@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "two matching methods \\(varcharToBigintReturnFirstExtraParameter and varcharToBigintReturnExtraParameter\\) for parameter types \\[varchar\\(10\\)\\]") public void testFailIfTwoMethodsWithSameArguments() { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToBigintReturnFirstExtraParameter")) .implementation(methodsGroup -> methodsGroup.methods("varcharToBigintReturnExtraParameter"))) .build(); function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); }
private static SqlScalarFunction equalityOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .nullableResult(true) .implementation(methodsGroup -> methodsGroup .methods("boxedShortShort", "boxedLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "two matching methods \\(varcharToBigintReturnFirstExtraParameter and varcharToBigintReturnExtraParameter\\) for parameter types \\[varchar\\(10\\)\\]") public void testFailIfTwoMethodsWithSameArguments() { SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToBigintReturnFirstExtraParameter")) .implementation(methodsGroup -> methodsGroup.methods("varcharToBigintReturnExtraParameter"))) .build(); function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); }
private static SqlScalarFunction comparisonOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("primitiveShortShort", "primitiveLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
public static SqlScalarFunction modulusScalarFunction(Signature signature) { return SqlScalarFunction.builder(DecimalOperators.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("modulusShortShortShort", "modulusLongLongLong", "modulusShortLongLong", "modulusShortLongShort", "modulusLongShortShort", "modulusLongShortLong") .withExtraParameters(DecimalOperators::modulusRescaleParameters))) .build(); }
@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "method foo was not found in class io.prestosql.metadata.TestPolymorphicScalarFunction\\$TestMethods") public void testFailIfNotAllMethodsPresent() { SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("bigintToBigintReturnExtraParameter")) .implementation(methodsGroup -> methodsGroup.methods("foo"))) .build(); }
private static SqlScalarFunction comparisonOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("primitiveShortShort", "primitiveLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
@Test(expectedExceptions = {IllegalStateException.class}, expectedExceptionsMessageRegExp = "method foo was not found in class io.prestosql.metadata.TestPolymorphicScalarFunction\\$TestMethods") public void testFailIfNotAllMethodsPresent() { SqlScalarFunction.builder(TestMethods.class) .signature(SIGNATURE) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("bigintToBigintReturnExtraParameter")) .implementation(methodsGroup -> methodsGroup.methods("foo"))) .build(); }
private static SqlScalarFunction equalityOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .nullableResult(true) .implementation(methodsGroup -> methodsGroup .methods("boxedShortShort", "boxedLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
public static SqlScalarFunction modulusScalarFunction(Signature signature) { return SqlScalarFunction.builder(DecimalOperators.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("modulusShortShortShort", "modulusLongLongLong", "modulusShortLongLong", "modulusShortLongShort", "modulusLongShortShort", "modulusLongShortLong") .withExtraParameters(DecimalOperators::modulusRescaleParameters))) .build(); }