public static int fastBigIntegerBytesScaled(
      final int fastSignum, long fast0, long fast1, long fast2,
      int fastIntegerDigitCount, int fastScale,
      int serializeScale,
      long[] scratchLongs, byte[] buffer) {

    // Normally, trailing fractional digits are removed.  But to emulate the
    // OldHiveDecimal setScale and OldHiveDecimalWritable internalStorage, we need to trailing zeroes
    // here.
    //
    // NOTE: This can cause a decimal that has too many decimal digits (because of trailing zeroes)
    //       for us to represent.  In that case, we punt and convert with a BigInteger alternate
    //       code.

    if (fastSignum == 0 || serializeScale == fastScale) {
      return
          fastBigIntegerBytesUnscaled(
              fastSignum, fast0, fast1, fast2,
              scratchLongs, buffer);
    } else if (serializeScale > fastScale) {

      final int scaleUp = serializeScale - fastScale;
      final int maxScale = HiveDecimal.MAX_SCALE - fastIntegerDigitCount;
      if (serializeScale > maxScale) {

        // We cannot to scaled up decimals that cannot be represented.
        // Instead, we use a BigInteger instead.

        BigInteger bigInteger =
            fastBigIntegerValueUnscaled(
                fastSignum, fast0, fast1, fast2);

        BigInteger bigIntegerScaled = bigInteger.multiply(BIG_INTEGER_TEN.pow(scaleUp));
        byte[] bigIntegerBytesScaled = bigIntegerScaled.toByteArray();
        final int length = bigIntegerBytesScaled.length;
        System.arraycopy(bigIntegerBytesScaled, 0, buffer, 0, length);
        return length;
      }

      FastHiveDecimal fastTemp = new FastHiveDecimal();
      if (!fastScaleUp(
          fast0, fast1, fast2,
          scaleUp,
          fastTemp)) {
        throw new RuntimeException("Unexpected");
      }
      return
          fastBigIntegerBytesUnscaled(
              fastSignum, fastTemp.fast0, fastTemp.fast1, fastTemp.fast2,
              scratchLongs, buffer);
    } else {

      // serializeScale < fastScale.

      FastHiveDecimal fastTemp = new FastHiveDecimal();
      if (!fastRound(
          fastSignum, fast0, fast1, fast2,
          fastIntegerDigitCount, fastScale,
          serializeScale, BigDecimal.ROUND_HALF_UP,
          fastTemp)) {
        return 0;
      }
      return
          fastBigIntegerBytesUnscaled(
              fastSignum, fastTemp.fast0, fastTemp.fast1, fastTemp.fast2,
              scratchLongs, buffer);
    }
  }
Convert decimal to BigInteger binary bytes with a serialize scale, similar to the formatScale
for toFormatString.  It adds trailing zeroes when a serializeScale is greater than current
scale.  Or, rounds if scale is less than current scale.

Used by Avro and Parquet serialization.

This emulates the OldHiveDecimal setScale / OldHiveDecimal getInternalStorage() behavior.

@param fastSignum the sign number (-1, 0, or +1)
@param fast0 word 0 of the internal representation
@param fast1 word 1
@param fast2 word 2
@param fastIntegerDigitCount the number of integer digits
@param fastScale the scale
@param serializeScale the scale to serialize
@param scratchLongs a scratch array of longs
@param buffer the buffer to serialize into
@return the number of bytes used to serialize the number
storage-api/src/java/org/apache/hadoop/hive/common/type/FastHiveDecimalImpl.java