@JsonValue @Override public String toString() { return Decimals.toString(unscaledValue, scale); }
public static String toString(long unscaledValue, int scale) { return toString(Long.toString(unscaledValue), scale); }
public static String toString(BigInteger unscaledValue, int scale) { return toString(unscaledValue.toString(), scale); }
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)); } }
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)); }
public static String toString(Slice unscaledValue, int scale) { return toString(toUnscaledString(unscaledValue), scale); }
@VisibleForTesting static double convertPartitionValueToDouble(Type type, Object value) { if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(SMALLINT) || type.equals(TINYINT)) { return (Long) value; } if (type.equals(DOUBLE)) { return (Double) value; } if (type.equals(REAL)) { return intBitsToFloat(((Long) value).intValue()); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return parseDouble(Decimals.toString((Long) value, decimalType.getScale())); } if (isLongDecimal(decimalType)) { return parseDouble(Decimals.toString((Slice) value, decimalType.getScale())); } throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); } if (type.equals(DATE)) { return (Long) value; } throw new IllegalArgumentException("Unexpected type: " + type); }
private static double toDouble(Object value, Type type) { if (value instanceof String && type.equals(DATE)) { return LocalDate.parse((CharSequence) value).toEpochDay(); } if (type.equals(BIGINT) || type.equals(INTEGER) || type.equals(DATE)) { return ((Number) value).doubleValue(); } if (type instanceof DecimalType) { DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return parseDouble(Decimals.toString(((Number) value).longValue(), decimalType.getScale())); } if (isLongDecimal(decimalType)) { return parseDouble(Decimals.toString((Slice) value, decimalType.getScale())); } throw new IllegalArgumentException("Unexpected decimal type: " + decimalType); } if (type.equals(DOUBLE)) { return ((Number) value).doubleValue(); } throw new IllegalArgumentException("unsupported column type " + type); } }
@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 shortDecimalToVarchar(long decimal, long precision, long scale, long 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 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 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 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)))); } }
DecimalType decimalType = (DecimalType) type; if (isShortDecimal(decimalType)) { return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale()); return (block, position) -> Decimals.toString( decodeUnscaledValue(type.getSlice(block, position)), decimalType.getScale());