Package org.apache.drill.exec.util
Class DecimalUtility
java.lang.Object
org.apache.drill.exec.util.DecimalUtility
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkValueOverflow
(BigDecimal value, int desiredPrecision, int desiredScale) Checks that the specified value may be fit into the value with specifieddesiredPrecision
precision anddesiredScale
scale.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 arraystatic 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 buffersstatic BigDecimal
getBigDecimalFromDense
(DrillBuf data, int startIndex, int nDecimalDigits, int scale, int maxPrecision, int width) Returns a BigDecimal object from the dense decimal representation.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.static BigDecimal
getBigDecimalFromDrillBuf
(DrillBuf bytebuf, int start, int length, int scale) Create a BigDecimal object using the data in the DrillBuf.static BigDecimal
getBigDecimalFromPrimitiveTypes
(int input, int scale) ReturnsBigDecimal
value created from specified integer value with specified scale.static BigDecimal
getBigDecimalFromPrimitiveTypes
(long input, int scale) ReturnsBigDecimal
value created from specified long value with specified scale.static BigDecimal
getBigDecimalFromSparse
(DrillBuf data, int startIndex, int nDecimalDigits, int scale) Create a BigDecimal object using the data in the DrillBuf.static long
getDecimal18FromBigDecimal
(BigDecimal input, int scale) Returns unsigned long value taken from specifiedBigDecimal
input with specified scalestatic int
getDecimal9FromBigDecimal
(BigDecimal input, int scale) Returns unsigned int value taken from specifiedBigDecimal
input with specified scale.static int
getDefaultPrecision
(TypeProtos.MinorType minorType, int defaultPrecision) Returns default precision for specifiedTypeProtos.MinorType
or returns specified defaultPrecision ifTypeProtos.MinorType
isn'tTypeProtos.MinorType.INT
orTypeProtos.MinorType.BIGINT
.static int
getMaxBytesSizeForPrecision
(int precision) Returns max length of byte array, required to store value with specified precision.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 representationstatic boolean
isObsoleteDecimalType
(TypeProtos.MinorType minorType) Checks that specified decimal minorType is obsolete.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 billionstatic BigDecimal
sqrt
(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 specifiedBigDecimal
input with specified scale- Parameters:
input
-BigDecimal
with desired valuescale
- scale of the value- Returns:
- long value taken from specified
BigDecimal
-
getDecimal9FromBigDecimal
Returns unsigned int value taken from specifiedBigDecimal
input with specified scale.- Parameters:
input
-BigDecimal
with desired valuescale
- scale of the value- Returns:
- int value taken from specified
BigDecimal
-
getBigDecimalFromPrimitiveTypes
ReturnsBigDecimal
value created from specified integer value with specified scale.- Parameters:
input
- integer value to use for creating ofBigDecimal
scale
- scale for resultingBigDecimal
- Returns:
BigDecimal
value
-
getBigDecimalFromPrimitiveTypes
ReturnsBigDecimal
value created from specified long value with specified scale.- Parameters:
input
- long value to use for creating ofBigDecimal
scale
- scale for resultingBigDecimal
- 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 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 < 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 < 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.MinorType
or returns specified defaultPrecision ifTypeProtos.MinorType
isn'tTypeProtos.MinorType.INT
orTypeProtos.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 specifieddesiredPrecision
precision anddesiredScale
scale. Otherwise, the exception is thrown.- Parameters:
value
- BigDecimal value to checkdesiredPrecision
- precision for the resulting valuedesiredScale
- scale for the resulting value
-