Class DecimalUtility

java.lang.Object
org.apache.drill.exec.util.DecimalUtility

public class DecimalUtility extends Object
  • Field Details

  • Constructor Details

    • DecimalUtility

      public DecimalUtility()
  • Method Details

    • roundUp

      public static int roundUp(int ndigits)
      Given the number of actual digits this function returns the number of indexes it will occupy in the array of integers which are stored in base 1 billion
    • getBigDecimalFromSparse

      public static BigDecimal getBigDecimalFromSparse(DrillBuf data, int startIndex, int nDecimalDigits, int scale)
      Create a BigDecimal object using the data in the DrillBuf. This function assumes that data is provided in a sparse format.
    • getBigDecimalFromDrillBuf

      public static BigDecimal getBigDecimalFromDrillBuf(DrillBuf bytebuf, int start, int length, int scale)
      Create a BigDecimal object using the data in the DrillBuf. This function assumes that data is provided in format supported by BigInteger.
    • getBigDecimalFromDrillBuf

      public static BigDecimal getBigDecimalFromDrillBuf(io.netty.buffer.ByteBuf data, int startIndex, int nDecimalDigits, int scale, boolean truncateScale)
      Create a BigDecimal object using the data in the DrillBuf. This function assumes that data is provided in a non-dense format It works on both sparse and intermediate representations.
    • getBigDecimalFromDense

      public static BigDecimal getBigDecimalFromDense(DrillBuf data, int startIndex, int nDecimalDigits, int scale, int maxPrecision, int width)
      Returns a BigDecimal object from the dense decimal representation. First step is to convert the dense representation into an intermediate representation and then invoke getBigDecimalFromDrillBuf() to get the BigDecimal object
    • getSparseFromBigDecimal

      public static void getSparseFromBigDecimal(BigDecimal input, io.netty.buffer.ByteBuf data, int startIndex, int scale, int nDecimalDigits)
      Function converts the BigDecimal and stores it in out internal sparse representation
    • getDecimal18FromBigDecimal

      public static long getDecimal18FromBigDecimal(BigDecimal input, int scale)
      Returns unsigned long value taken from specified BigDecimal input with specified scale
      Parameters:
      input - BigDecimal with desired value
      scale - scale of the value
      Returns:
      long value taken from specified BigDecimal
    • getDecimal9FromBigDecimal

      public static int getDecimal9FromBigDecimal(BigDecimal input, int scale)
      Returns unsigned int value taken from specified BigDecimal input with specified scale.
      Parameters:
      input - BigDecimal with desired value
      scale - scale of the value
      Returns:
      int value taken from specified BigDecimal
    • getBigDecimalFromPrimitiveTypes

      public static BigDecimal getBigDecimalFromPrimitiveTypes(int input, int scale)
      Returns BigDecimal value created from specified integer value with specified scale.
      Parameters:
      input - integer value to use for creating of BigDecimal
      scale - scale for resulting BigDecimal
      Returns:
      BigDecimal value
    • getBigDecimalFromPrimitiveTypes

      public static BigDecimal getBigDecimalFromPrimitiveTypes(long input, int scale)
      Returns BigDecimal value created from specified long value with specified scale.
      Parameters:
      input - long value to use for creating of BigDecimal
      scale - scale for resulting BigDecimal
      Returns:
      BigDecimal value
    • compareVarLenBytes

      public static int compareVarLenBytes(DrillBuf left, int leftStart, int leftEnd, int leftScale, DrillBuf right, int rightStart, int rightEnd, int rightScale, boolean absCompare)
      Compares two VarDecimal values, still stored in their respective Drill buffers
      Parameters:
      left - left value Drill buffer
      leftStart - start offset of left value
      leftEnd - end offset of left value
      leftScale - scale of left value
      right - right value Drill buffer
      rightStart - start offset of right value
      rightEnd - end offset of right value
      rightScale - scale of right value
      absCompare - comparison of absolute values is done iff this is true
      Returns:
      1 if left > right, 0 if left = right, -1 if left < right. two values that are numerically equal, but with different scales (e.g., 2.00 and 2), are considered equal.
    • compareVarLenBytes

      public static int compareVarLenBytes(DrillBuf left, int leftStart, int leftEnd, int leftScale, byte[] right, int rightScale, boolean absCompare)
      Compares two VarDecimal values, still stored in Drill buffer and byte array
      Parameters:
      left - left value Drill buffer
      leftStart - start offset of left value
      leftEnd - end offset of left value
      leftScale - scale of left value
      right - right value byte array
      rightScale - scale of right value
      absCompare - comparison of absolute values is done iff this is true
      Returns:
      1 if left > right, 0 if left = right, -1 if left < right. two values that are numerically equal, but with different scales (e.g., 2.00 and 2), are considered equal.
    • getMaxBytesSizeForPrecision

      public static int getMaxBytesSizeForPrecision(int precision)
      Returns max length of byte array, required to store value with specified precision.
      Parameters:
      precision - the precision of value
      Returns:
      max length of byte array
    • sqrt

      public static BigDecimal sqrt(BigDecimal in, int scale)
      Calculates and returns square root for specified BigDecimal with specified number of digits alter decimal point.
      Parameters:
      in - BigDecimal which square root should be calculated
      scale - number of digits alter decimal point in the result value.
      Returns:
      square root for specified BigDecimal
    • isObsoleteDecimalType

      public static boolean isObsoleteDecimalType(TypeProtos.MinorType minorType)
      Checks that specified decimal minorType is obsolete.
      Parameters:
      minorType - type to check
      Returns:
      true if specified decimal minorType is obsolete.
    • getDefaultPrecision

      public static int getDefaultPrecision(TypeProtos.MinorType minorType, int defaultPrecision)
      Returns default precision for specified TypeProtos.MinorType or returns specified defaultPrecision if TypeProtos.MinorType isn't TypeProtos.MinorType.INT or TypeProtos.MinorType.BIGINT.
      Parameters:
      minorType - type wich precision should be received
      defaultPrecision - default value for precision
      Returns:
      default precision for specified TypeProtos.MinorType
    • checkValueOverflow

      public static void checkValueOverflow(BigDecimal value, int desiredPrecision, int desiredScale)
      Checks that the specified value may be fit into the value with specified desiredPrecision precision and desiredScale scale. Otherwise, the exception is thrown.
      Parameters:
      value - BigDecimal value to check
      desiredPrecision - precision for the resulting value
      desiredScale - scale for the resulting value