Search in sources :

Example 6 with TypeSignature

use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.

the class DecimalOperators method decimalDivideOperator.

private static SqlScalarFunction decimalDivideOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "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().kind(SCALAR).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 SqlScalarFunction.builder(DecimalOperators.class, DIVIDE).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor))).build();
}
Also used : ADD(io.prestosql.spi.function.OperatorType.ADD) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) LiteralParameters(io.prestosql.spi.function.LiteralParameters) DecimalType(io.prestosql.spi.type.DecimalType) Math.abs(java.lang.Math.abs) UnscaledDecimal128Arithmetic.unscaledDecimal(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) DIVISION_BY_ZERO(io.prestosql.spi.StandardErrorCode.DIVISION_BY_ZERO) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) IsNull(io.prestosql.spi.function.IsNull) ScalarOperator(io.prestosql.spi.function.ScalarOperator) INDETERMINATE(io.prestosql.spi.function.OperatorType.INDETERMINATE) OperatorType(io.prestosql.spi.function.OperatorType) BigInteger(java.math.BigInteger) PolymorphicScalarFunctionBuilder(io.prestosql.metadata.PolymorphicScalarFunctionBuilder) Long.signum(java.lang.Long.signum) MULTIPLY(io.prestosql.spi.function.OperatorType.MULTIPLY) UnscaledDecimal128Arithmetic(io.prestosql.spi.type.UnscaledDecimal128Arithmetic) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) Decimals.longTenToNth(io.prestosql.spi.type.Decimals.longTenToNth) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) List(java.util.List) UnscaledDecimal128Arithmetic.divideRoundUp(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) SqlType(io.prestosql.spi.function.SqlType) TypeSignature(io.prestosql.spi.type.TypeSignature) MODULUS(io.prestosql.spi.function.OperatorType.MODULUS) HASH_CODE(io.prestosql.spi.function.OperatorType.HASH_CODE) DIVIDE(io.prestosql.spi.function.OperatorType.DIVIDE) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) UnscaledDecimal128Arithmetic.throwIfOverflows(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) SpecializeContext(io.prestosql.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) NUMERIC_VALUE_OUT_OF_RANGE(io.prestosql.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) Decimals(io.prestosql.spi.type.Decimals) UnscaledDecimal128Arithmetic.isZero(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) SignatureBuilder(io.prestosql.spi.function.SignatureBuilder) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Signature.longVariableExpression(io.prestosql.spi.function.Signature.longVariableExpression) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) XX_HASH_64(io.prestosql.spi.function.OperatorType.XX_HASH_64) XxHash64(io.airlift.slice.XxHash64) Decimals.encodeUnscaledValue(io.prestosql.spi.type.Decimals.encodeUnscaledValue) SUBTRACT(io.prestosql.spi.function.OperatorType.SUBTRACT) Integer.max(java.lang.Integer.max) UnscaledDecimal128Arithmetic.remainder(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(io.prestosql.spi.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.rescale) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature)

Example 7 with TypeSignature

use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.

the class DecimalOperators method decimalAddOperator.

private static SqlScalarFunction decimalAddOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = Signature.builder().kind(SCALAR).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 SqlScalarFunction.builder(DecimalOperators.class, ADD).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 : ADD(io.prestosql.spi.function.OperatorType.ADD) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) LiteralParameters(io.prestosql.spi.function.LiteralParameters) DecimalType(io.prestosql.spi.type.DecimalType) Math.abs(java.lang.Math.abs) UnscaledDecimal128Arithmetic.unscaledDecimal(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) DIVISION_BY_ZERO(io.prestosql.spi.StandardErrorCode.DIVISION_BY_ZERO) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) IsNull(io.prestosql.spi.function.IsNull) ScalarOperator(io.prestosql.spi.function.ScalarOperator) INDETERMINATE(io.prestosql.spi.function.OperatorType.INDETERMINATE) OperatorType(io.prestosql.spi.function.OperatorType) BigInteger(java.math.BigInteger) PolymorphicScalarFunctionBuilder(io.prestosql.metadata.PolymorphicScalarFunctionBuilder) Long.signum(java.lang.Long.signum) MULTIPLY(io.prestosql.spi.function.OperatorType.MULTIPLY) UnscaledDecimal128Arithmetic(io.prestosql.spi.type.UnscaledDecimal128Arithmetic) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) Decimals.longTenToNth(io.prestosql.spi.type.Decimals.longTenToNth) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) List(java.util.List) UnscaledDecimal128Arithmetic.divideRoundUp(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) SqlType(io.prestosql.spi.function.SqlType) TypeSignature(io.prestosql.spi.type.TypeSignature) MODULUS(io.prestosql.spi.function.OperatorType.MODULUS) HASH_CODE(io.prestosql.spi.function.OperatorType.HASH_CODE) DIVIDE(io.prestosql.spi.function.OperatorType.DIVIDE) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) UnscaledDecimal128Arithmetic.throwIfOverflows(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) SpecializeContext(io.prestosql.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) NUMERIC_VALUE_OUT_OF_RANGE(io.prestosql.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) Decimals(io.prestosql.spi.type.Decimals) UnscaledDecimal128Arithmetic.isZero(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) SignatureBuilder(io.prestosql.spi.function.SignatureBuilder) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Signature.longVariableExpression(io.prestosql.spi.function.Signature.longVariableExpression) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) XX_HASH_64(io.prestosql.spi.function.OperatorType.XX_HASH_64) XxHash64(io.airlift.slice.XxHash64) Decimals.encodeUnscaledValue(io.prestosql.spi.type.Decimals.encodeUnscaledValue) SUBTRACT(io.prestosql.spi.function.OperatorType.SUBTRACT) Integer.max(java.lang.Integer.max) UnscaledDecimal128Arithmetic.remainder(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(io.prestosql.spi.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.rescale) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature)

Example 8 with TypeSignature

use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.

the class DecimalOperators method modulusSignatureBuilder.

public static SignatureBuilder modulusSignatureBuilder() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    return Signature.builder().longVariableConstraints(longVariableExpression("r_precision", "min(b_precision - b_scale, a_precision - a_scale) + max(a_scale, b_scale)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature);
}
Also used : TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature)

Example 9 with TypeSignature

use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.

the class DecimalOperators method decimalMultiplyOperator.

private static SqlScalarFunction decimalMultiplyOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = Signature.builder().kind(SCALAR).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 SqlScalarFunction.builder(DecimalOperators.class, MULTIPLY).signature(signature).deterministic(true).choice(choice -> choice.implementation(methodsGroup -> methodsGroup.methods("multiplyShortShortShort", "multiplyShortShortLong", "multiplyLongLongLong", "multiplyShortLongLong", "multiplyLongShortLong"))).build();
}
Also used : ADD(io.prestosql.spi.function.OperatorType.ADD) SCALAR(io.prestosql.spi.function.FunctionKind.SCALAR) LiteralParameters(io.prestosql.spi.function.LiteralParameters) DecimalType(io.prestosql.spi.type.DecimalType) Math.abs(java.lang.Math.abs) UnscaledDecimal128Arithmetic.unscaledDecimal(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) UsedByGeneratedCode(io.prestosql.spi.annotation.UsedByGeneratedCode) DIVISION_BY_ZERO(io.prestosql.spi.StandardErrorCode.DIVISION_BY_ZERO) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) IsNull(io.prestosql.spi.function.IsNull) ScalarOperator(io.prestosql.spi.function.ScalarOperator) INDETERMINATE(io.prestosql.spi.function.OperatorType.INDETERMINATE) OperatorType(io.prestosql.spi.function.OperatorType) BigInteger(java.math.BigInteger) PolymorphicScalarFunctionBuilder(io.prestosql.metadata.PolymorphicScalarFunctionBuilder) Long.signum(java.lang.Long.signum) MULTIPLY(io.prestosql.spi.function.OperatorType.MULTIPLY) UnscaledDecimal128Arithmetic(io.prestosql.spi.type.UnscaledDecimal128Arithmetic) PrestoException(io.prestosql.spi.PrestoException) ImmutableSet(com.google.common.collect.ImmutableSet) Decimals.longTenToNth(io.prestosql.spi.type.Decimals.longTenToNth) SqlScalarFunction(io.prestosql.metadata.SqlScalarFunction) List(java.util.List) UnscaledDecimal128Arithmetic.divideRoundUp(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) SqlType(io.prestosql.spi.function.SqlType) TypeSignature(io.prestosql.spi.type.TypeSignature) MODULUS(io.prestosql.spi.function.OperatorType.MODULUS) HASH_CODE(io.prestosql.spi.function.OperatorType.HASH_CODE) DIVIDE(io.prestosql.spi.function.OperatorType.DIVIDE) Slice(io.airlift.slice.Slice) StandardTypes(io.prestosql.spi.type.StandardTypes) UnscaledDecimal128Arithmetic.throwIfOverflows(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) SpecializeContext(io.prestosql.metadata.PolymorphicScalarFunctionBuilder.SpecializeContext) NUMERIC_VALUE_OUT_OF_RANGE(io.prestosql.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) Decimals(io.prestosql.spi.type.Decimals) UnscaledDecimal128Arithmetic.isZero(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.isZero) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) SignatureBuilder(io.prestosql.spi.function.SignatureBuilder) Math.toIntExact(java.lang.Math.toIntExact) Signature(io.prestosql.spi.function.Signature) Signature.longVariableExpression(io.prestosql.spi.function.Signature.longVariableExpression) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) XX_HASH_64(io.prestosql.spi.function.OperatorType.XX_HASH_64) XxHash64(io.airlift.slice.XxHash64) Decimals.encodeUnscaledValue(io.prestosql.spi.type.Decimals.encodeUnscaledValue) SUBTRACT(io.prestosql.spi.function.OperatorType.SUBTRACT) Integer.max(java.lang.Integer.max) UnscaledDecimal128Arithmetic.remainder(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.remainder) NEGATION(io.prestosql.spi.function.OperatorType.NEGATION) UnscaledDecimal128Arithmetic.rescale(io.prestosql.spi.type.UnscaledDecimal128Arithmetic.rescale) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeSignature.parseTypeSignature(io.prestosql.spi.type.TypeSignature.parseTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) Signature(io.prestosql.spi.function.Signature)

Example 10 with TypeSignature

use of io.prestosql.spi.type.TypeSignature in project hetu-core by openlookeng.

the class SignatureBinder method appendConstraintSolversForArguments.

private boolean appendConstraintSolversForArguments(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, List<? extends TypeSignatureProvider> actualTypes) {
    boolean variableArity = declaredSignature.isVariableArity();
    List<TypeSignature> formalTypeSignatures = declaredSignature.getArgumentTypes();
    if (variableArity) {
        if (actualTypes.size() < formalTypeSignatures.size() - 1) {
            return false;
        }
        formalTypeSignatures = expandVarargFormalTypeSignature(formalTypeSignatures, actualTypes.size());
    }
    if (formalTypeSignatures.size() != actualTypes.size()) {
        return false;
    }
    for (int i = 0; i < formalTypeSignatures.size(); i++) {
        if (!appendTypeRelationshipConstraintSolver(resultBuilder, formalTypeSignatures.get(i), actualTypes.get(i), allowCoercion)) {
            return false;
        }
    }
    return appendConstraintSolvers(resultBuilder, formalTypeSignatures, actualTypes, allowCoercion);
}
Also used : NamedTypeSignature(io.prestosql.spi.type.NamedTypeSignature) TypeSignature(io.prestosql.spi.type.TypeSignature) TypeVariableConstraint(io.prestosql.spi.function.TypeVariableConstraint) LongVariableConstraint(io.prestosql.spi.function.LongVariableConstraint)

Aggregations

TypeSignature (io.prestosql.spi.type.TypeSignature)79 Signature (io.prestosql.spi.function.Signature)36 Type (io.prestosql.spi.type.Type)27 Test (org.testng.annotations.Test)24 TypeSignature.parseTypeSignature (io.prestosql.spi.type.TypeSignature.parseTypeSignature)22 ImmutableList (com.google.common.collect.ImmutableList)18 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)18 CallExpression (io.prestosql.spi.relation.CallExpression)18 PrestoException (io.prestosql.spi.PrestoException)17 List (java.util.List)14 RowExpression (io.prestosql.spi.relation.RowExpression)12 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)11 RowType (io.prestosql.spi.type.RowType)11 ArrayList (java.util.ArrayList)11 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)10 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 ArrayType (io.prestosql.spi.type.ArrayType)10 VarcharType.createUnboundedVarcharType (io.prestosql.spi.type.VarcharType.createUnboundedVarcharType)10 HashSet (java.util.HashSet)10 Domain (io.prestosql.spi.predicate.Domain)9