Search in sources :

Example 1 with PolymorphicScalarFunctionBuilder

use of io.trino.metadata.PolymorphicScalarFunctionBuilder 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 PolymorphicScalarFunctionBuilder

use of io.trino.metadata.PolymorphicScalarFunctionBuilder 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 PolymorphicScalarFunctionBuilder

use of io.trino.metadata.PolymorphicScalarFunctionBuilder 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 PolymorphicScalarFunctionBuilder

use of io.trino.metadata.PolymorphicScalarFunctionBuilder 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)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)4 UsedByGeneratedCode (io.trino.annotation.UsedByGeneratedCode)4 PolymorphicScalarFunctionBuilder (io.trino.metadata.PolymorphicScalarFunctionBuilder)4 SpecializeContext (io.trino.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext)4 Signature (io.trino.metadata.Signature)4 Signature.longVariableExpression (io.trino.metadata.Signature.longVariableExpression)4 SignatureBuilder (io.trino.metadata.SignatureBuilder)4 SqlScalarFunction (io.trino.metadata.SqlScalarFunction)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 MULTIPLY (io.trino.spi.function.OperatorType.MULTIPLY)4 NEGATION (io.trino.spi.function.OperatorType.NEGATION)4 SUBTRACT (io.trino.spi.function.OperatorType.SUBTRACT)4 ScalarOperator (io.trino.spi.function.ScalarOperator)4 SqlType (io.trino.spi.function.SqlType)4