Search in sources :

Example 1 with SqlScalarFunction

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();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 2 with SqlScalarFunction

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();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 3 with SqlScalarFunction

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();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 4 with SqlScalarFunction

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();
}
Also used : MODULUS(io.trino.spi.function.OperatorType.MODULUS) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder) Int128Math.add(io.trino.spi.type.Int128Math.add) Int128Math.subtract(io.trino.spi.type.Int128Math.subtract) TypeSignatureParameter.typeVariable(io.trino.spi.type.TypeSignatureParameter.typeVariable) Int128Math.remainder(io.trino.spi.type.Int128Math.remainder) LiteralParameters(io.trino.spi.function.LiteralParameters) Int128Math.multiply(io.trino.spi.type.Int128Math.multiply) Math.abs(java.lang.Math.abs) SUBTRACT(io.trino.spi.function.OperatorType.SUBTRACT) Int128Math.divideRoundUp(io.trino.spi.type.Int128Math.divideRoundUp) ImmutableList(com.google.common.collect.ImmutableList) SignatureBuilder(io.trino.metadata.SignatureBuilder) SqlType(io.trino.spi.function.SqlType) Objects.requireNonNull(java.util.Objects.requireNonNull) Signature.longVariableExpression(io.trino.metadata.Signature.longVariableExpression) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.trino.metadata.Signature) NUMERIC_VALUE_OUT_OF_RANGE(io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) TypeSignature(io.trino.spi.type.TypeSignature) Long.signum(java.lang.Long.signum) Int128(io.trino.spi.type.Int128) NEGATION(io.trino.spi.function.OperatorType.NEGATION) Decimals.longTenToNth(io.trino.spi.type.Decimals.longTenToNth) ScalarOperator(io.trino.spi.function.ScalarOperator) DIVIDE(io.trino.spi.function.OperatorType.DIVIDE) TrinoException(io.trino.spi.TrinoException) UsedByGeneratedCode(io.trino.annotation.UsedByGeneratedCode) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) DIVISION_BY_ZERO(io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO) SpecializeContext(io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) Int128Math.rescale(io.trino.spi.type.Int128Math.rescale) Integer.max(java.lang.Integer.max) List(java.util.List) Int128Math.negateExact(io.trino.spi.type.Int128Math.negateExact) ADD(io.trino.spi.function.OperatorType.ADD) MULTIPLY(io.trino.spi.function.OperatorType.MULTIPLY) Decimals(io.trino.spi.type.Decimals) DecimalType(io.trino.spi.type.DecimalType) TypeSignature(io.trino.spi.type.TypeSignature) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) PolymorphicScalarFunctionBuilder(io.trino.metadata.PolymorphicScalarFunctionBuilder)

Example 5 with SqlScalarFunction

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());
}
Also used : FunctionMetadata(io.trino.metadata.FunctionMetadata) Signature(io.trino.metadata.Signature) TypeSignature(io.trino.spi.type.TypeSignature) BoundSignature(io.trino.metadata.BoundSignature) BoundSignature(io.trino.metadata.BoundSignature) ParametricScalar(io.trino.operator.scalar.ParametricScalar) ChoicesScalarFunctionImplementation(io.trino.operator.scalar.ChoicesScalarFunctionImplementation) SqlScalarFunction(io.trino.metadata.SqlScalarFunction) FunctionDependencies(io.trino.metadata.FunctionDependencies) Test(org.testng.annotations.Test)

Aggregations

SqlScalarFunction (io.trino.metadata.SqlScalarFunction)18 Signature (io.trino.metadata.Signature)15 TypeSignature (io.trino.spi.type.TypeSignature)15 Test (org.testng.annotations.Test)14 FunctionMetadata (io.trino.metadata.FunctionMetadata)13 ParametricScalar (io.trino.operator.scalar.ParametricScalar)13 BoundSignature (io.trino.metadata.BoundSignature)11 ImmutableList (com.google.common.collect.ImmutableList)6 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 PolymorphicScalarFunctionBuilder (io.trino.metadata.PolymorphicScalarFunctionBuilder)4 SpecializeContext (io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext)4 Signature.longVariableExpression (io.trino.metadata.Signature.longVariableExpression)4 SignatureBuilder (io.trino.metadata.SignatureBuilder)4 DIVISION_BY_ZERO (io.trino.spi.StandardErrorCode.DIVISION_BY_ZERO)4 NUMERIC_VALUE_OUT_OF_RANGE (io.trino.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE)4 TrinoException (io.trino.spi.TrinoException)4 LiteralParameters (io.trino.spi.function.LiteralParameters)4 ADD (io.trino.spi.function.OperatorType.ADD)4 DIVIDE (io.trino.spi.function.OperatorType.DIVIDE)4 MODULUS (io.trino.spi.function.OperatorType.MODULUS)4