presto-main/src/main/java/io/prestosql/type/DecimalSaturatedFloorCasts.java/DecimalSaturatedFloorCasts/bigDecimalToBigintFloorSaturatedCast
class DecimalSaturatedFloorCasts:    private static BigInteger bigDecimalToBigintFloorSaturatedCast(BigDecimal bigDecimal, int resultPrecision, int resultScale)
    {
        BigDecimal rescaledValue = bigDecimal.setScale(resultScale, FLOOR);
        BigInteger unscaledValue = rescaledValue.unscaledValue();
        BigInteger maxUnscaledValue = bigIntegerTenToNth(resultPrecision).subtract(ONE);
        if (unscaledValue.compareTo(maxUnscaledValue) > 0) {
            return maxUnscaledValue;
        }
        BigInteger minUnscaledValue = maxUnscaledValue.negate();
        if (unscaledValue.compareTo(minUnscaledValue) < 0) {
            return minUnscaledValue;
        }
        return unscaledValue;
    }