use of io.trino.metadata.SqlScalarFunction in project trino by trinodb.
the class DecimalOperators method decimalSubtractOperator.
private static SqlScalarFunction decimalSubtractOperator() {
TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
Signature signature = Signature.builder().operatorType(SUBTRACT).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
use of io.trino.metadata.SqlScalarFunction in project trino by trinodb.
the class DecimalOperators method decimalAddOperator.
private static SqlScalarFunction decimalAddOperator() {
TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
Signature signature = Signature.builder().operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(methodsGroup -> methodsGroup.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters))).build();
}
use of io.trino.metadata.SqlScalarFunction in project trino by trinodb.
the class DecimalOperators method decimalMultiplyOperator.
private static SqlScalarFunction decimalMultiplyOperator() {
TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
Signature signature = Signature.builder().operatorType(MULTIPLY).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_precision)"), longVariableExpression("r_scale", "a_scale + b_scale")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("multiplyShortShortShort", "multiplyShortShortLong", "multiplyLongLongLong", "multiplyShortLongLong", "multiplyLongShortLong"))).build();
}
use of io.trino.metadata.SqlScalarFunction in project trino by trinodb.
the class DecimalOperators method decimalDivideOperator.
private static SqlScalarFunction decimalDivideOperator() {
TypeSignature decimalLeftSignature = new TypeSignature("decimal", typeVariable("a_precision"), typeVariable("a_scale"));
TypeSignature decimalRightSignature = new TypeSignature("decimal", typeVariable("b_precision"), typeVariable("b_scale"));
TypeSignature decimalResultSignature = new TypeSignature("decimal", typeVariable("r_precision"), typeVariable("r_scale"));
// we extend target precision by b_scale. This is upper bound on how much division result will grow.
// pessimistic case is a / 0.0000001
// if scale of divisor is greater than scale of dividend we extend scale further as we
// want result scale to be maximum of scales of divisor and dividend.
Signature signature = Signature.builder().operatorType(DIVIDE).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_scale + max(b_scale - a_scale, 0))"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
return new PolymorphicScalarFunctionBuilder(DecimalOperators.class).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor))).build();
}
use of io.trino.metadata.SqlScalarFunction in project trino by trinodb.
the class TestAnnotationEngineForScalars method testWithNullableComplexArgScalarParse.
@Test
public void testWithNullableComplexArgScalarParse() {
Signature expectedSignature = new Signature("scalar_with_nullable_complex", DOUBLE.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature(), DOUBLE.getTypeSignature()));
List<SqlScalarFunction> functions = ScalarFromAnnotationsParser.parseFunctionDefinition(WithNullableComplexArgScalarFunction.class);
assertEquals(functions.size(), 1);
ParametricScalar scalar = (ParametricScalar) functions.get(0);
FunctionMetadata functionMetadata = scalar.getFunctionMetadata();
assertEquals(functionMetadata.getSignature(), expectedSignature);
assertTrue(functionMetadata.isDeterministic());
assertFalse(functionMetadata.isHidden());
assertEquals(functionMetadata.getDescription(), "Simple scalar with nullable complex type");
assertFalse(functionMetadata.getFunctionNullability().isArgumentNullable(0));
assertTrue(functionMetadata.getFunctionNullability().isArgumentNullable(1));
BoundSignature boundSignature = new BoundSignature(expectedSignature.getName(), DOUBLE, ImmutableList.of(DOUBLE, DOUBLE));
ChoicesScalarFunctionImplementation specialized = (ChoicesScalarFunctionImplementation) scalar.specialize(boundSignature, new FunctionDependencies(FUNCTION_MANAGER::getScalarFunctionInvoker, ImmutableMap.of(), ImmutableSet.of()));
assertFalse(specialized.getChoices().get(0).getInstanceFactory().isPresent());
}
Aggregations