Class DecimalUtil

java.lang.Object
io.substrait.util.DecimalUtil

public class DecimalUtil extends Object
A set of utility methods to deal with convertion of decimal values. Part of code is adopted from Apache Arrow /java/vector/src/main/java/org/apache/arrow/vector/util/DecimalUtility.java
  • Constructor Details

    • DecimalUtil

      public DecimalUtil()
  • Method Details

    • getBigDecimalFromBytes

      public static BigDecimal getBigDecimalFromBytes(byte[] value, int scale, int byteWidth)
      Converts a little-endian two's-complement byte array of a scaled big integer into a BigDecimal. Opposite of encodeDecimalIntoBytes(BigDecimal, int, int).
      Parameters:
      value - the little-endian two's-complement bytes
      scale - the decimal scale to apply
      byteWidth - the number of bytes used for the integer representation
      Returns:
      the reconstructed BigDecimal
    • encodeDecimalIntoBytes

      public static byte[] encodeDecimalIntoBytes(BigDecimal decimal, int scale, int byteWidth)
      Encodes a BigDecimal into a scaled big integer and serializes it as little-endian two's-complement bytes of fixed byteWidth.
      Parameters:
      decimal - the decimal value to encode
      scale - the scale used to produce the scaled integer
      byteWidth - the target width in bytes for the output
      Returns:
      the encoded little-endian two's-complement byte array
      Throws:
      UnsupportedOperationException - if the encoded integer exceeds byteWidth