Search in sources :

Example 16 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature in project presto by prestodb.

the class DecimalCasts method castFunctionFromDecimalTo.

private static SqlScalarFunction castFunctionFromDecimalTo(TypeSignature to, String... methodNames) {
    Signature signature = Signature.builder().kind(SCALAR).operatorType(CAST).argumentTypes(parseTypeSignature("decimal(precision,scale)", ImmutableSet.of("precision", "scale"))).returnType(to).build();
    return SqlScalarFunction.builder(DecimalCasts.class).signature(signature).implementation(b -> b.methods(methodNames).withExtraParameters((context) -> {
        long precision = context.getLiteral("precision");
        long scale = context.getLiteral("scale");
        Number tenToScale;
        if (isShortDecimal(context.getParameterTypes().get(0))) {
            tenToScale = longTenToNth(intScale(scale));
        } else {
            tenToScale = bigIntegerTenToNth(intScale(scale));
        }
        return ImmutableList.of(precision, scale, tenToScale);
    })).build();
}
Also used : CAST(com.facebook.presto.spi.function.OperatorType.CAST) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLongUnsafe(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLongUnsafe) BIGINT(com.facebook.presto.spi.type.BigintType.BIGINT) BigDecimal(java.math.BigDecimal) Float.parseFloat(java.lang.Float.parseFloat) DecimalType(com.facebook.presto.spi.type.DecimalType) BOOLEAN(com.facebook.presto.spi.type.BooleanType.BOOLEAN) Slices(io.airlift.slice.Slices) Decimals.decodeUnscaledValue(com.facebook.presto.spi.type.Decimals.decodeUnscaledValue) BigInteger(java.math.BigInteger) StandardTypes(com.facebook.presto.spi.type.StandardTypes) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) Double.parseDouble(java.lang.Double.parseDouble) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Decimals.isShortDecimal(com.facebook.presto.spi.type.Decimals.isShortDecimal) Decimals(com.facebook.presto.spi.type.Decimals) TINYINT(com.facebook.presto.spi.type.TinyintType.TINYINT) ImmutableSet(com.google.common.collect.ImmutableSet) UnscaledDecimal128Arithmetic.multiply(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.multiply) String.format(java.lang.String.format) Preconditions.checkState(com.google.common.base.Preconditions.checkState) UnscaledDecimal128Arithmetic.compareAbsolute(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.compareAbsolute) INTEGER(com.facebook.presto.spi.type.IntegerType.INTEGER) ZERO(java.math.BigInteger.ZERO) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) DOUBLE(com.facebook.presto.spi.type.DoubleType.DOUBLE) Slice(io.airlift.slice.Slice) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) SliceOutput(io.airlift.slice.SliceOutput) JSON(com.facebook.presto.type.JsonType.JSON) Shorts(com.google.common.primitives.Shorts) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) PrestoException(com.facebook.presto.spi.PrestoException) Float.intBitsToFloat(java.lang.Float.intBitsToFloat) DynamicSliceOutput(io.airlift.slice.DynamicSliceOutput) Float.floatToRawIntBits(java.lang.Float.floatToRawIntBits) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) JsonUtil.createJsonGenerator(com.facebook.presto.util.JsonUtil.createJsonGenerator) UnscaledDecimal128Arithmetic.overflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.overflows) ImmutableList(com.google.common.collect.ImmutableList) JsonUtil.createJsonParser(com.facebook.presto.util.JsonUtil.createJsonParser) Math.toIntExact(java.lang.Math.toIntExact) Nullable(javax.annotation.Nullable) Failures.checkCondition(com.facebook.presto.util.Failures.checkCondition) Decimals.bigIntegerTenToNth(com.facebook.presto.spi.type.Decimals.bigIntegerTenToNth) JsonParser(com.fasterxml.jackson.core.JsonParser) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) HALF_UP(java.math.RoundingMode.HALF_UP) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Signature(com.facebook.presto.metadata.Signature) SignedBytes(com.google.common.primitives.SignedBytes) INVALID_CAST_ARGUMENT(com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT) IOException(java.io.IOException) SMALLINT(com.facebook.presto.spi.type.SmallintType.SMALLINT) JSON_FACTORY(com.facebook.presto.operator.scalar.JsonOperators.JSON_FACTORY) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) Math.multiplyExact(java.lang.Math.multiplyExact) REAL(com.facebook.presto.spi.type.RealType.REAL) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Decimals.overflows(com.facebook.presto.spi.type.Decimals.overflows) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 17 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature in project presto by prestodb.

the class DecimalOperators method decimalSubtractOperator.

private static SqlScalarFunction decimalSubtractOperator() {
    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(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 SqlScalarFunction.builder(DecimalOperators.class).signature(signature).implementation(b -> b.methods("subtractShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(b -> b.methods("subtractShortShortLong", "subtractLongLongLong", "subtractShortLongLong", "subtractLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters)).build();
}
Also used : SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) MULTIPLY(com.facebook.presto.spi.function.OperatorType.MULTIPLY) Slice(io.airlift.slice.Slice) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) HASH_CODE(com.facebook.presto.spi.function.OperatorType.HASH_CODE) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) PrestoException(com.facebook.presto.spi.PrestoException) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) Signature.longVariableExpression(com.facebook.presto.metadata.Signature.longVariableExpression) MODULUS(com.facebook.presto.spi.function.OperatorType.MODULUS) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DecimalType(com.facebook.presto.spi.type.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) BigInteger(java.math.BigInteger) Math.toIntExact(java.lang.Math.toIntExact) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Long.signum(java.lang.Long.signum) Decimals(com.facebook.presto.spi.type.Decimals) ImmutableSet(com.google.common.collect.ImmutableSet) DIVIDE(com.facebook.presto.spi.function.OperatorType.DIVIDE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature(com.facebook.presto.metadata.Signature) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.isZero) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) NEGATION(com.facebook.presto.spi.function.OperatorType.NEGATION) Integer.max(java.lang.Integer.max) List(java.util.List) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.remainder) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) SUBTRACT(com.facebook.presto.spi.function.OperatorType.SUBTRACT) SqlType(com.facebook.presto.spi.function.SqlType) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) ADD(com.facebook.presto.spi.function.OperatorType.ADD) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 18 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature in project presto by prestodb.

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).signature(signature).implementation(b -> b.methods("divideShortShortShort", "divideShortLongShort", "divideLongShortShort", "divideShortShortLong", "divideLongLongLong", "divideShortLongLong", "divideLongShortLong").withExtraParameters(DecimalOperators::divideRescaleFactor)).build();
}
Also used : SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) MULTIPLY(com.facebook.presto.spi.function.OperatorType.MULTIPLY) Slice(io.airlift.slice.Slice) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) HASH_CODE(com.facebook.presto.spi.function.OperatorType.HASH_CODE) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) PrestoException(com.facebook.presto.spi.PrestoException) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) Signature.longVariableExpression(com.facebook.presto.metadata.Signature.longVariableExpression) MODULUS(com.facebook.presto.spi.function.OperatorType.MODULUS) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DecimalType(com.facebook.presto.spi.type.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) BigInteger(java.math.BigInteger) Math.toIntExact(java.lang.Math.toIntExact) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Long.signum(java.lang.Long.signum) Decimals(com.facebook.presto.spi.type.Decimals) ImmutableSet(com.google.common.collect.ImmutableSet) DIVIDE(com.facebook.presto.spi.function.OperatorType.DIVIDE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature(com.facebook.presto.metadata.Signature) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.isZero) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) NEGATION(com.facebook.presto.spi.function.OperatorType.NEGATION) Integer.max(java.lang.Integer.max) List(java.util.List) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.remainder) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) SUBTRACT(com.facebook.presto.spi.function.OperatorType.SUBTRACT) SqlType(com.facebook.presto.spi.function.SqlType) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) ADD(com.facebook.presto.spi.function.OperatorType.ADD) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 19 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature in project presto by prestodb.

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(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 20 with TypeSignature

use of com.facebook.presto.spi.type.TypeSignature in project presto by prestodb.

the class TestFunctionRegistry method testExactMatchBeforeCoercion.

@Test
public void testExactMatchBeforeCoercion() {
    TypeRegistry typeManager = new TypeRegistry();
    FunctionRegistry registry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig());
    boolean foundOperator = false;
    for (SqlFunction function : registry.listOperators()) {
        OperatorType operatorType = unmangleOperator(function.getSignature().getName());
        if (operatorType == OperatorType.CAST || operatorType == OperatorType.SATURATED_FLOOR_CAST) {
            continue;
        }
        if (!function.getSignature().getTypeVariableConstraints().isEmpty()) {
            continue;
        }
        if (function.getSignature().getArgumentTypes().stream().anyMatch(TypeSignature::isCalculated)) {
            continue;
        }
        Signature exactOperator = registry.resolveOperator(operatorType, resolveTypes(function.getSignature().getArgumentTypes(), typeManager));
        assertEquals(exactOperator, function.getSignature());
        foundOperator = true;
    }
    assertTrue(foundOperator);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) BlockEncodingManager(com.facebook.presto.block.BlockEncodingManager) FeaturesConfig(com.facebook.presto.sql.analyzer.FeaturesConfig) TypeSignature(com.facebook.presto.spi.type.TypeSignature) FunctionRegistry.getMagicLiteralFunctionSignature(com.facebook.presto.metadata.FunctionRegistry.getMagicLiteralFunctionSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeRegistry(com.facebook.presto.type.TypeRegistry) OperatorType(com.facebook.presto.spi.function.OperatorType) Test(org.testng.annotations.Test)

Aggregations

TypeSignature (com.facebook.presto.spi.type.TypeSignature)34 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)14 NamedTypeSignature (com.facebook.presto.spi.type.NamedTypeSignature)10 Test (org.testng.annotations.Test)10 Signature (com.facebook.presto.metadata.Signature)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Type (com.facebook.presto.spi.type.Type)6 TypeSignatureParameter (com.facebook.presto.spi.type.TypeSignatureParameter)6 ImmutableSet (com.google.common.collect.ImmutableSet)6 Slice (io.airlift.slice.Slice)6 UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)5 SCALAR (com.facebook.presto.metadata.FunctionKind.SCALAR)5 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)5 SqlScalarFunctionBuilder (com.facebook.presto.metadata.SqlScalarFunctionBuilder)5 SpecializeContext (com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext)5 SqlType (com.facebook.presto.spi.function.SqlType)5 DecimalType (com.facebook.presto.spi.type.DecimalType)5 Decimals (com.facebook.presto.spi.type.Decimals)5 Decimals.encodeUnscaledValue (com.facebook.presto.spi.type.Decimals.encodeUnscaledValue)5