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(); }
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(); }
private static SqlScalarFunction genericIntegerTypeToDecimalSaturatedFloorCast(Type integerType) { return SqlScalarFunction.builder(DecimalSaturatedFloorCasts.class) .signature(Signature.builder() .kind(SCALAR) .operatorType(SATURATED_FLOOR_CAST) .argumentTypes(integerType.getTypeSignature()) .returnType(parseTypeSignature("decimal(result_precision,result_scale)", ImmutableSet.of("result_precision", "result_scale"))) .build()) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("genericIntegerTypeToShortDecimal", "genericIntegerTypeToLongDecimal") .withExtraParameters((context) -> { int resultPrecision = toIntExact(context.getLiteral("result_precision")); int resultScale = toIntExact(context.getLiteral("result_scale")); return ImmutableList.of(resultPrecision, resultScale); }))) .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(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(); }
private static PolymorphicScalarFunctionBuilder makeBinaryOperatorFunctionBuilder(OperatorType operatorType) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(operatorType) .argumentTypes(DECIMAL_SIGNATURE, DECIMAL_SIGNATURE) .returnType(parseTypeSignature(BOOLEAN)) .build(); return SqlScalarFunction.builder(DecimalInequalityOperators.class) .signature(signature) .deterministic(true); }
private static SqlScalarFunction decimalToGenericIntegerTypeSaturatedFloorCast(Type type, long minValue, long maxValue) { return SqlScalarFunction.builder(DecimalSaturatedFloorCasts.class) .signature(Signature.builder() .kind(SCALAR) .operatorType(SATURATED_FLOOR_CAST) .argumentTypes(parseTypeSignature("decimal(source_precision,source_scale)", ImmutableSet.of("source_precision", "source_scale"))) .returnType(type.getTypeSignature()) .build()) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("shortDecimalToGenericIntegerType", "longDecimalToGenericIntegerType") .withExtraParameters((context) -> { int sourceScale = toIntExact(context.getLiteral("source_scale")); return ImmutableList.of(sourceScale, minValue, maxValue); }))) .build(); }
private static PolymorphicScalarFunctionBuilder makeBinaryOperatorFunctionBuilder(OperatorType operatorType) { Signature signature = Signature.builder() .kind(SCALAR) .operatorType(operatorType) .argumentTypes(DECIMAL_SIGNATURE, DECIMAL_SIGNATURE) .returnType(parseTypeSignature(BOOLEAN)) .build(); return SqlScalarFunction.builder(DecimalInequalityOperators.class) .signature(signature) .deterministic(true); }
@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(); }
@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(); }
@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(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 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 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(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 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); }
@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 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); }