Package org.apache.drill.exec.util
Class DecimalUtility
java.lang.Object
org.apache.drill.exec.util.DecimalUtility
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckValueOverflow(BigDecimal value, int desiredPrecision, int desiredScale) Checks that the specified value may be fit into the value with specifieddesiredPrecisionprecision anddesiredScalescale.static intcompareVarLenBytes(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 arraystatic intcompareVarLenBytes(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 buffersstatic BigDecimalgetBigDecimalFromDense(DrillBuf data, int startIndex, int nDecimalDigits, int scale, int maxPrecision, int width) Returns a BigDecimal object from the dense decimal representation.static BigDecimalgetBigDecimalFromDrillBuf(io.netty.buffer.ByteBuf data, int startIndex, int nDecimalDigits, int scale, boolean truncateScale) Create a BigDecimal object using the data in the DrillBuf.static BigDecimalgetBigDecimalFromDrillBuf(DrillBuf bytebuf, int start, int length, int scale) Create a BigDecimal object using the data in the DrillBuf.static BigDecimalgetBigDecimalFromPrimitiveTypes(int input, int scale) ReturnsBigDecimalvalue created from specified integer value with specified scale.static BigDecimalgetBigDecimalFromPrimitiveTypes(long input, int scale) ReturnsBigDecimalvalue created from specified long value with specified scale.static BigDecimalgetBigDecimalFromSparse(DrillBuf data, int startIndex, int nDecimalDigits, int scale) Create a BigDecimal object using the data in the DrillBuf.static longgetDecimal18FromBigDecimal(BigDecimal input, int scale) Returns unsigned long value taken from specifiedBigDecimalinput with specified scalestatic intgetDecimal9FromBigDecimal(BigDecimal input, int scale) Returns unsigned int value taken from specifiedBigDecimalinput with specified scale.static intgetDefaultPrecision(TypeProtos.MinorType minorType, int defaultPrecision) Returns default precision for specifiedTypeProtos.MinorTypeor returns specified defaultPrecision ifTypeProtos.MinorTypeisn'tTypeProtos.MinorType.INTorTypeProtos.MinorType.BIGINT.static intgetMaxBytesSizeForPrecision(int precision) Returns max length of byte array, required to store value with specified precision.static voidgetSparseFromBigDecimal(BigDecimal input, io.netty.buffer.ByteBuf data, int startIndex, int scale, int nDecimalDigits) Function converts the BigDecimal and stores it in out internal sparse representationstatic booleanisObsoleteDecimalType(TypeProtos.MinorType minorType) Checks that specified decimal minorType is obsolete.static introundUp(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 billionstatic BigDecimalsqrt(BigDecimal in, int scale) Calculates and returns square root for specified BigDecimal with specified number of digits alter decimal point.
-
Field Details
-
MAX_DIGITS
public static final int MAX_DIGITS- See Also:
-
MAX_DIGITS_INT
public static final int MAX_DIGITS_INT- See Also:
-
MAX_DIGITS_BIGINT
public static final int MAX_DIGITS_BIGINT- See Also:
-
DIGITS_BASE
public static final int DIGITS_BASE- See Also:
-
INTEGER_SIZE
public static final int INTEGER_SIZE- See Also:
-
-
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 byBigInteger. -
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
Returns unsigned long value taken from specifiedBigDecimalinput with specified scale- Parameters:
input-BigDecimalwith desired valuescale- scale of the value- Returns:
- long value taken from specified
BigDecimal
-
getDecimal9FromBigDecimal
Returns unsigned int value taken from specifiedBigDecimalinput with specified scale.- Parameters:
input-BigDecimalwith desired valuescale- scale of the value- Returns:
- int value taken from specified
BigDecimal
-
getBigDecimalFromPrimitiveTypes
ReturnsBigDecimalvalue created from specified integer value with specified scale.- Parameters:
input- integer value to use for creating ofBigDecimalscale- scale for resultingBigDecimal- Returns:
BigDecimalvalue
-
getBigDecimalFromPrimitiveTypes
ReturnsBigDecimalvalue created from specified long value with specified scale.- Parameters:
input- long value to use for creating ofBigDecimalscale- scale for resultingBigDecimal- Returns:
BigDecimalvalue
-
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 bufferleftStart- start offset of left valueleftEnd- end offset of left valueleftScale- scale of left valueright- right value Drill bufferrightStart- start offset of right valuerightEnd- end offset of right valuerightScale- scale of right valueabsCompare- comparison of absolute values is done iff this is true- Returns:
- 1 if left > right, 0 if left = right, -1 if left invalid input: '<' 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 bufferleftStart- start offset of left valueleftEnd- end offset of left valueleftScale- scale of left valueright- right value byte arrayrightScale- scale of right valueabsCompare- comparison of absolute values is done iff this is true- Returns:
- 1 if left > right, 0 if left = right, -1 if left invalid input: '<' 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
Calculates and returns square root for specified BigDecimal with specified number of digits alter decimal point.- Parameters:
in- BigDecimal which square root should be calculatedscale- number of digits alter decimal point in the result value.- Returns:
- square root for specified BigDecimal
-
isObsoleteDecimalType
Checks that specified decimal minorType is obsolete.- Parameters:
minorType- type to check- Returns:
- true if specified decimal minorType is obsolete.
-
getDefaultPrecision
Returns default precision for specifiedTypeProtos.MinorTypeor returns specified defaultPrecision ifTypeProtos.MinorTypeisn'tTypeProtos.MinorType.INTorTypeProtos.MinorType.BIGINT.- Parameters:
minorType- type wich precision should be receiveddefaultPrecision- default value for precision- Returns:
- default precision for specified
TypeProtos.MinorType
-
checkValueOverflow
Checks that the specified value may be fit into the value with specifieddesiredPrecisionprecision anddesiredScalescale. Otherwise, the exception is thrown.- Parameters:
value- BigDecimal value to checkdesiredPrecision- precision for the resulting valuedesiredScale- scale for the resulting value
-