public static boolean fastSetFromBigDecimal(
      BigDecimal bigDecimal, boolean allowRounding, FastHiveDecimal fastResult) {

    // We trim the trailing zero fraction digits so we don't cause unnecessary precision
    // overflow later.
    if (bigDecimal.signum() == 0) {
      if (bigDecimal.scale() != 0) {

        // For some strange reason BigDecimal 0 can have a scale.  We do not support that.
        bigDecimal = BigDecimal.ZERO;
      }
    }

    if (!allowRounding) {
      if (bigDecimal.signum() != 0) {
        BigDecimal bigDecimalStripped = bigDecimal.stripTrailingZeros();
        int stripTrailingZerosScale = bigDecimalStripped.scale();
        // System.out.println("FAST_SET_FROM_BIG_DECIMAL bigDecimal " + bigDecimal);
        // System.out.println("FAST_SET_FROM_BIG_DECIMAL bigDecimalStripped " + bigDecimalStripped);
        // System.out.println("FAST_SET_FROM_BIG_DECIMAL stripTrailingZerosScale " + stripTrailingZerosScale);
        if (stripTrailingZerosScale < 0) {

          // The trailing zeroes extend into the integer part -- we only want to eliminate the
          // fractional zero digits.

          bigDecimal = bigDecimal.setScale(0);
        } else {

          // Ok, use result with some or all fractional digits stripped.

          bigDecimal = bigDecimalStripped;
        }
      }
      int scale = bigDecimal.scale();
      if (scale < 0 || scale > HiveDecimal.MAX_SCALE) {
        return false;
      }
      // The digits must fit without rounding.
      if (!fastSetFromBigInteger(bigDecimal.unscaledValue(), fastResult)) {
        return false;
      }
      if (fastResult.fastSignum != 0) {
        fastResult.fastIntegerDigitCount = Math.max(0, fastResult.fastIntegerDigitCount - scale);
        fastResult.fastScale = scale;
      }
      return true;
    }
    // This method will scale down and round to fit, if necessary.
    if (!fastSetFromBigInteger(bigDecimal.unscaledValue(), bigDecimal.scale(),
        bigDecimal.precision(), fastResult)) {
      return false;
    }
    return true;
  }
Create a fast decimal from a BigDecimal.

NOTE: The fastSetFromBigDecimal method requires the caller to pass a fastResult
parameter has been reset for better performance.

@param bigDecimal the big decimal to copy
@param allowRounding is rounding allowed?
@param fastResult an object to reuse
@return True if the BigDecimal could be converted to our decimal representation.
storage-api/src/java/org/apache/hadoop/hive/common/type/FastHiveDecimalImpl.java