Search in sources :

Example 1 with CAST

use of com.facebook.presto.spi.function.OperatorType.CAST 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)

Aggregations

UsedByGeneratedCode (com.facebook.presto.annotation.UsedByGeneratedCode)1 SCALAR (com.facebook.presto.metadata.FunctionKind.SCALAR)1 Signature (com.facebook.presto.metadata.Signature)1 SqlScalarFunction (com.facebook.presto.metadata.SqlScalarFunction)1 SqlScalarFunctionBuilder (com.facebook.presto.metadata.SqlScalarFunctionBuilder)1 JSON_FACTORY (com.facebook.presto.operator.scalar.JsonOperators.JSON_FACTORY)1 PrestoException (com.facebook.presto.spi.PrestoException)1 INVALID_CAST_ARGUMENT (com.facebook.presto.spi.StandardErrorCode.INVALID_CAST_ARGUMENT)1 CAST (com.facebook.presto.spi.function.OperatorType.CAST)1 BIGINT (com.facebook.presto.spi.type.BigintType.BIGINT)1 BOOLEAN (com.facebook.presto.spi.type.BooleanType.BOOLEAN)1 DecimalType (com.facebook.presto.spi.type.DecimalType)1 Decimals (com.facebook.presto.spi.type.Decimals)1 Decimals.bigIntegerTenToNth (com.facebook.presto.spi.type.Decimals.bigIntegerTenToNth)1 Decimals.decodeUnscaledValue (com.facebook.presto.spi.type.Decimals.decodeUnscaledValue)1 Decimals.encodeUnscaledValue (com.facebook.presto.spi.type.Decimals.encodeUnscaledValue)1 Decimals.isShortDecimal (com.facebook.presto.spi.type.Decimals.isShortDecimal)1 Decimals.longTenToNth (com.facebook.presto.spi.type.Decimals.longTenToNth)1 Decimals.overflows (com.facebook.presto.spi.type.Decimals.overflows)1 DOUBLE (com.facebook.presto.spi.type.DoubleType.DOUBLE)1