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 PolymorphicScalarFunctionBuilder builder(Class<?> clazz) { return new PolymorphicScalarFunctionBuilder(clazz); } }
private static SqlScalarFunction comparisonOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("primitiveShortShort", "primitiveLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .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 distinctOperator() { return makeBinaryOperatorFunctionBuilder(IS_DISTINCT_FROM) .choice(choice -> choice .argumentProperties( valueTypeArgumentProperty(USE_NULL_FLAG), valueTypeArgumentProperty(USE_NULL_FLAG)) .implementation(methodsGroup -> methodsGroup .methods("distinctShortShort", "distinctLongLong"))) .choice(choice -> choice .argumentProperties( valueTypeArgumentProperty(BLOCK_AND_POSITION), valueTypeArgumentProperty(BLOCK_AND_POSITION)) .implementation(methodsGroup -> methodsGroup .methodWithExplicitJavaTypes("distinctBlockPositionLongLong", asList(Optional.of(Slice.class), Optional.of(Slice.class))) .methodWithExplicitJavaTypes("distinctBlockPositionShortShort", asList(Optional.of(long.class), Optional.of(long.class))))) .build(); }
public PolymorphicScalarFunctionBuilder signature(Signature signature) { this.signature = requireNonNull(signature, "signature is null"); this.hidden = Optional.of(hidden.orElse(isOperator(signature))); return this; }
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 distinctOperator() { return makeBinaryOperatorFunctionBuilder(IS_DISTINCT_FROM) .choice(choice -> choice .argumentProperties( valueTypeArgumentProperty(USE_NULL_FLAG), valueTypeArgumentProperty(USE_NULL_FLAG)) .implementation(methodsGroup -> methodsGroup .methods("distinctShortShort", "distinctLongLong"))) .choice(choice -> choice .argumentProperties( valueTypeArgumentProperty(BLOCK_AND_POSITION), valueTypeArgumentProperty(BLOCK_AND_POSITION)) .implementation(methodsGroup -> methodsGroup .methodWithExplicitJavaTypes("distinctBlockPositionLongLong", asList(Optional.of(Slice.class), Optional.of(Slice.class))) .methodWithExplicitJavaTypes("distinctBlockPositionShortShort", asList(Optional.of(long.class), Optional.of(long.class))))) .build(); }
public PolymorphicScalarFunctionBuilder signature(Signature signature) { this.signature = requireNonNull(signature, "signature is null"); this.hidden = Optional.of(hidden.orElse(isOperator(signature))); return this; }
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 comparisonOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup .methods("primitiveShortShort", "primitiveLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
public static PolymorphicScalarFunctionBuilder builder(Class<?> clazz) { return new PolymorphicScalarFunctionBuilder(clazz); } }
@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 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 = "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 SqlScalarFunction equalityOperator(OperatorType operatorType, MethodHandle getResultMethodHandle) { return makeBinaryOperatorFunctionBuilder(operatorType) .choice(choice -> choice .nullableResult(true) .implementation(methodsGroup -> methodsGroup .methods("boxedShortShort", "boxedLongLong") .withExtraParameters(constant(getResultMethodHandle)))) .build(); }
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(); }
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 = "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 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(); }