@JsonValue @Override public String toString() { return Decimals.toString(unscaledValue, scale); }
private static PrestoException throwCastException(long value, long sourcePrecision, long sourceScale, long resultPrecision, long resultScale) { return new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DECIMAL '%s' to DECIMAL(%d, %d)", Decimals.toString(value, (int) sourceScale), resultPrecision, resultScale)); }
private static PrestoException throwCastException(long value, long sourcePrecision, long sourceScale, long resultPrecision, long resultScale) { return new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DECIMAL '%s' to DECIMAL(%d, %d)", Decimals.toString(value, (int) sourceScale), resultPrecision, resultScale)); }
private static PrestoException throwCastException(BigInteger value, long sourcePrecision, long sourceScale, long resultPrecision, long resultScale) { return new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast DECIMAL '%s' to DECIMAL(%d, %d)", Decimals.toString(value, (int) sourceScale), resultPrecision, resultScale)); } }
@UsedByGeneratedCode public static long longDecimalToReal(Slice decimal, long precision, long scale, BigInteger tenToScale) { // If both decimal and scale can be represented exactly in float then compute rescaled and rounded result directly in float. if (scale < FLOAT_10_POW.length && compareAbsolute(decimal, MAX_EXACT_FLOAT) <= 0) { return floatToRawIntBits((float) unscaledDecimalToUnscaledLongUnsafe(decimal) / FLOAT_10_POW[intScale(scale)]); } // TODO: optimize and convert directly to float in similar fashion as in double to decimal casts return floatToRawIntBits(parseFloat(Decimals.toString(decimal, intScale(scale)))); }
@UsedByGeneratedCode public static long longDecimalToReal(Slice decimal, long precision, long scale, BigInteger tenToScale) { // If both decimal and scale can be represented exactly in float then compute rescaled and rounded result directly in float. if (scale < FLOAT_10_POW.length && compareAbsolute(decimal, MAX_EXACT_FLOAT) <= 0) { return floatToRawIntBits((float) unscaledDecimalToUnscaledLongUnsafe(decimal) / FLOAT_10_POW[intScale(scale)]); } // TODO: optimize and convert directly to float in similar fashion as in double to decimal casts return floatToRawIntBits(parseFloat(Decimals.toString(decimal, intScale(scale)))); }
@UsedByGeneratedCode public static Slice longDecimalToVarchar(Slice decimal, long precision, long scale, BigInteger tenToScale) { return utf8Slice(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static Slice longDecimalToVarchar(Slice decimal, long precision, long scale, BigInteger tenToScale) { return utf8Slice(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static Slice shortDecimalToVarchar(long decimal, long precision, long scale, long tenToScale) { return utf8Slice(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static Slice shortDecimalToVarchar(long decimal, long precision, long scale, long tenToScale) { return utf8Slice(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static long longDecimalToInteger(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return toIntExact(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to INTEGER", Decimals.toString(decimal, intScale(scale)))); } }
@UsedByGeneratedCode public static double longDecimalToDouble(Slice decimal, long precision, long scale, BigInteger tenToScale) { // If both decimal and scale can be represented exactly in double then compute rescaled and rounded result directly in double. if (scale < DOUBLE_10_POW.length && compareAbsolute(decimal, MAX_EXACT_DOUBLE) <= 0) { return (double) unscaledDecimalToUnscaledLongUnsafe(decimal) / DOUBLE_10_POW[intScale(scale)]; } // TODO: optimize and convert directly to double in similar fashion as in double to decimal casts return parseDouble(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static double longDecimalToDouble(Slice decimal, long precision, long scale, BigInteger tenToScale) { // If both decimal and scale can be represented exactly in double then compute rescaled and rounded result directly in double. if (scale < DOUBLE_10_POW.length && compareAbsolute(decimal, MAX_EXACT_DOUBLE) <= 0) { return (double) unscaledDecimalToUnscaledLongUnsafe(decimal) / DOUBLE_10_POW[intScale(scale)]; } // TODO: optimize and convert directly to double in similar fashion as in double to decimal casts return parseDouble(Decimals.toString(decimal, intScale(scale))); }
@UsedByGeneratedCode public static long longDecimalToBigint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale))); } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to BIGINT", Decimals.toString(decimal, intScale(scale)))); } }
@UsedByGeneratedCode public static long longDecimalToBigint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale))); } catch (ArithmeticException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to BIGINT", Decimals.toString(decimal, intScale(scale)))); } }
private void encodeValue(Block block, int position, SliceOutput output) { if (isShortDecimal(type)) { output.writeBytes(utf8Slice(Decimals.toString(type.getLong(block, position), type.getScale()))); } else { output.writeBytes(utf8Slice(Decimals.toString(type.getSlice(block, position), type.getScale()))); } }
@UsedByGeneratedCode public static long longDecimalToTinyint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return SignedBytes.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", Decimals.toString(decimal, intScale(scale)))); } }
@UsedByGeneratedCode public static long longDecimalToSmallint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return Shorts.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", Decimals.toString(decimal, intScale(scale)))); } }
@UsedByGeneratedCode public static long longDecimalToSmallint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return Shorts.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to SMALLINT", Decimals.toString(decimal, intScale(scale)))); } }
@UsedByGeneratedCode public static long longDecimalToTinyint(Slice decimal, long precision, long scale, BigInteger tenToScale) { try { return SignedBytes.checkedCast(unscaledDecimalToUnscaledLong(rescale(decimal, intScale(-scale)))); } catch (ArithmeticException | IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to TINYINT", Decimals.toString(decimal, intScale(scale)))); } }