org.apache.hadoop.hive.common.type
Class Decimal128

java.lang.Object
  extended by java.lang.Number
      extended by org.apache.hadoop.hive.common.type.Decimal128
All Implemented Interfaces:
Serializable, Comparable<Decimal128>

public final class Decimal128
extends Number
implements Comparable<Decimal128>

This code was originally written for Microsoft PolyBase.

A 128-bit fixed-length Decimal value in the ANSI SQL Numeric semantics, representing unscaledValue / 10**scale where scale is 0 or positive.

This class is similar to BigDecimal, but a few things differ to conform to the SQL Numeric semantics.

Scale of this object is specified by the user, not automatically determined like BigDecimal. This means that underflow is possible depending on the scale. BigDecimal controls rounding behaviors by MathContext, possibly throwing errors. But, underflow is NOT an error in ANSI SQL Numeric. "CAST(0.000000000....0001 AS DECIMAL(38,1))" is "0.0" without an error.

Because this object is fixed-length, overflow is also possible. Overflow IS an error in ANSI SQL Numeric. "CAST(10000 AS DECIMAL(38,38))" throws overflow error.

Each arithmetic operator takes scale as a parameter to control its behavior. It's user's (or query optimizer's) responsibility to give an appropriate scale parameter.

Finally, this class performs MUCH faster than java.math.BigDecimal for a few reasons. Its behavior is simple because of the designs above. This class is fixed-length without array expansion and re-allocation. This class is mutable, allowing reuse of the same object without re-allocation. This class and UnsignedInt128 are designed such that minimal heap-object allocations are required for most operations. The only exception is division. Even this class requires a few object allocations for division, though much fewer than BigDecimal.

See Also:
Serialized Form

Field Summary
static short MAX_SCALE
          Maximum value for #scale.
static Decimal128 MAX_VALUE
          Maximum value that can be represented in this class.
static short MIN_SCALE
          Minimum value for #scale.
static Decimal128 MIN_VALUE
          Minimum value that can be represented in this class.
static Decimal128 ONE
           
 
Constructor Summary
Decimal128()
          Construct a zero.
Decimal128(char[] str, int offset, int length, short scale)
          Constructs from the given string with given offset and length.
Decimal128(Decimal128 o)
          Copy constructor.
Decimal128(double val, short scale)
          Translates a double into a Decimal128 in the given scaling.
Decimal128(long val)
          Translates a long into a Decimal128.
Decimal128(long val, short scale)
          Translates a long into a Decimal128 with the given scaling.
Decimal128(String str, short scale)
          Constructs from the given string.
Decimal128(UnsignedInt128 unscaledVal, short scale, boolean negative)
          Translates a UnsignedInt128 unscaled value, an int scale, and sign flag into a Decimal128 .
 
Method Summary
 void absDestructive()
          Makes this Decimal128 a positive number.
static void add(Decimal128 left, Decimal128 right, Decimal128 result, short scale)
          Calculates addition and puts the result into the given object.
 Decimal128 addDestructive(Decimal128 right, short scale)
          Calculates addition and stores the result into this object.
 void changeScaleDestructive(short scale)
          Changes the scaling of this Decimal128, preserving the represented value.
 void checkPrecisionOverflow(int precision)
          Throws an exception if the value of this object exceeds the maximum value for the given precision.
 int compareTo(Decimal128 val)
          Compares this Decimal128 with the specified Decimal128.
static void divide(Decimal128 left, Decimal128 right, Decimal128 quotient, short scale)
          Performs division and puts the result into the given object.
 void divideDestructive(Decimal128 right, short newScale)
          Divide the target object by right, and scale the result to newScale.
 void divideDestructiveNativeDecimal128(Decimal128 right, short newScale, Decimal128 remainder)
          As of 1/20/2014 this has a known bug in division.
 double doubleValue()
          Converts this Decimal128 to a double.
 boolean equals(Object x)
          Compares this Decimal128 with the specified Object for equality.
 int fastSerializeForHiveDecimal(Decimal128FastBuffer scratch)
          Serializes the value in a format compatible with the BigDecimal's own representation
 Decimal128 fastUpdateFromInternalStorage(byte[] internalStorage, short scale)
          Fats update from BigInteger two's complement representation
 float floatValue()
          Converts this Decimal128 to a float.
 String getHiveDecimalString()
          Returns the string representation of this value.
static int getIntsPerElement(int precision)
          Determines the number of ints to store one value.
 short getScale()
          Returns the scale of this Decimal128.
 byte getSignum()
          Returns the signum of this Decimal128.
 UnsignedInt128 getUnscaledValue()
          Returns unscaled value of this Decimal128.
 int hashCode()
          Returns the hash code for this Decimal128.
 int intValue()
          Converts this Decimal128 to an int.
 boolean isZero()
           
 long longValue()
          Converts this Decimal128 to a long.
static void modulo(Decimal128 left, Decimal128 right, Decimal128 result, short scale)
          Performs decimal modulo
static void multiply(Decimal128 left, Decimal128 right, Decimal128 result, short scale)
          Calculates multiplication and puts the result into the given object.
 void multiplyDestructive(Decimal128 right, short newScale)
          Performs multiplication, changing the scale of this object to the specified value.
 void multiplyDestructiveNativeDecimal128(Decimal128 right, short newScale)
          As of 2/11/2014 this has a known bug in multiplication.
 void negateDestructive()
          Reverses the sign of this Decimal128.
 double powAsDouble(double n)
           Calculate the power of this value in double precision.
 void serializeTo(int[] array, int offset, int precision)
          Serialize this object to the given array, putting the required number of ints for the given precision.
 void serializeTo(IntBuffer buf, int precision)
          Serialize this object to the given ByteBuffer, putting the required number of ints for the given precision.
 void serializeTo128(int[] array, int offset)
          Serialize this object to the given integers, putting 128+32 bits of data (full range).
 void serializeTo128(IntBuffer buf)
          Serialize this object to the given ByteBuffer, putting 128+32 bits of data (full range).
 void serializeTo32(int[] array, int offset)
          Serialize this object to the given integers, putting only 32+32 bits of data.
 void serializeTo32(IntBuffer buf)
          Serialize this object to the given ByteBuffer, putting only 32+32 bits of data.
 void serializeTo64(int[] array, int offset)
          Serialize this object to the given integers, putting only 64+32 bits of data.
 void serializeTo64(IntBuffer buf)
          Serialize this object to the given ByteBuffer, putting only 64+32 bits of data.
 void serializeTo96(int[] array, int offset)
          Serialize this object to the given integers, putting only 96+32 bits of data.
 void serializeTo96(IntBuffer buf)
          Serialize this object to the given ByteBuffer, putting only 96+32 bits of data.
 void setNullDataValue()
          Vectorized execution uses the smallest possible positive non-zero value to prevent possible later zero-divide exceptions.
 void setScale(short scale)
          This setter is only for de-serialization, should not be used otherwise.
 void setSignum(byte signum)
          This setter is only for de-serialization, should not be used otherwise.
 void setUnscaledValue(UnsignedInt128 unscaledValue)
          This setter is only for de-serialization, should not be used otherwise.
 double sqrtAsDouble()
          Calculate the square root of this value in double precision.
 Decimal128 squareDestructive()
          Multiplies this with this, updating this
static void subtract(Decimal128 left, Decimal128 right, Decimal128 result, short scale)
          Calculates subtraction and puts the result into the given object.
 Decimal128 subtractDestructive(Decimal128 right, short scale)
          Calculates subtraction and stores the result into this object.
 BigDecimal toBigDecimal()
          Converts this object to BigDecimal.
 String toFormalString()
          Returns the formal string representation of this value.
 String toString()
           
 Decimal128 update(BigDecimal bigDecimal)
          Updates the value of this object with the given BigDecimal.
 Decimal128 update(BigInteger bigInt, short scale)
          Updates the value of this object with the given BigInteger and scale.
 Decimal128 update(char[] str, int offset, int length, short scale)
          Updates the value of this object from the given string with given offset and length.
 Decimal128 update(Decimal128 o)
          Copy the value of given object.
 Decimal128 update(Decimal128 o, short scale)
          Copy the value of given object and assigns a custom scale.
 Decimal128 update(double val, short scale)
          Update the value of this object with the given double.
 Decimal128 update(int[] array, int offset, int precision)
          Updates the value of this object by reading from the given array, using the required number of ints for the given precision.
 Decimal128 update(IntBuffer buf, int precision)
          Updates the value of this object by reading from ByteBuffer, using the required number of ints for the given precision.
 Decimal128 update(long val)
          Update the value of this object with the given long.
 Decimal128 update(long val, short scale)
          Update the value of this object with the given long with the given scal.
 Decimal128 update(String str, short scale)
          Updates the value of this object with the given string.
 Decimal128 update128(int[] array, int offset)
          Updates the value of this object by reading from the given integers, receiving 128+32 bits of data (full range).
 Decimal128 update128(IntBuffer buf)
          Updates the value of this object by reading from ByteBuffer, receiving 128+32 bits data (full ranges).
 Decimal128 update32(int[] array, int offset)
          Updates the value of this object by reading from the given integers, receiving only 32+32 bits data.
 Decimal128 update32(IntBuffer buf)
          Updates the value of this object by reading from ByteBuffer, receiving only 32+32 bits data.
 Decimal128 update64(int[] array, int offset)
          Updates the value of this object by reading from the given integers, receiving only 64+32 bits data.
 Decimal128 update64(IntBuffer buf)
          Updates the value of this object by reading from ByteBuffer, receiving only 64+32 bits data.
 Decimal128 update96(int[] array, int offset)
          Updates the value of this object by reading from the given integers, receiving only 96+32 bits data.
 Decimal128 update96(IntBuffer buf)
          Updates the value of this object by reading from ByteBuffer, receiving only 96+32 bits data.
 void updateFixedPoint(long val, short scale)
          Update the value to a decimal value with the decimal point equal to val but with the decimal point inserted scale digits from the right.
 Decimal128 updateVarianceDestructive(Decimal128 scratch, Decimal128 value, Decimal128 sum, long count)
          For UDAF variance we use the algorithm described by Chan, Golub, and LeVeque in "Algorithms for computing the sample variance: analysis and recommendations" The American Statistician, 37 (1983) pp.
 Decimal128 zeroClear()
          Reset the value of this object to zero.
 void zeroFractionPart()
           
 void zeroFractionPart(UnsignedInt128 scratch)
          Zero the fractional part of value.
 
Methods inherited from class java.lang.Number
byteValue, shortValue
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

MAX_SCALE

public static final short MAX_SCALE
Maximum value for #scale.

See Also:
Constant Field Values

MIN_SCALE

public static final short MIN_SCALE
Minimum value for #scale.

See Also:
Constant Field Values

ONE

public static final Decimal128 ONE

MAX_VALUE

public static final Decimal128 MAX_VALUE
Maximum value that can be represented in this class.


MIN_VALUE

public static final Decimal128 MIN_VALUE
Minimum value that can be represented in this class.

Constructor Detail

Decimal128

public Decimal128()
Construct a zero.


Decimal128

public Decimal128(Decimal128 o)
Copy constructor.

Parameters:
o - object to copy from

Decimal128

public Decimal128(double val,
                  short scale)
Translates a double into a Decimal128 in the given scaling. Note that, unlike java.math.BigDecimal, the scaling is given as the parameter, not automatically detected. This is one of the differences between ANSI SQL Numeric and Java's BigDecimal. See class comments for more details.

Unchecked exceptions: ArithmeticException if val overflows in the scaling. NumberFormatException if val is infinite or NaN.

Parameters:
val - double value to be converted to Decimal128.
scale - scale of the Decimal128.

Decimal128

public Decimal128(UnsignedInt128 unscaledVal,
                  short scale,
                  boolean negative)
Translates a UnsignedInt128 unscaled value, an int scale, and sign flag into a Decimal128 . The value of the Decimal128 is (unscaledVal × 10-scale).

Parameters:
unscaledVal - unscaled value of the Decimal128.
scale - scale of the Decimal128.
negative - whether the value is negative

Decimal128

public Decimal128(long val)
Translates a long into a Decimal128. The scale of the Decimal128 is zero.

Parameters:
val - long value to be converted to Decimal128.

Decimal128

public Decimal128(long val,
                  short scale)
Translates a long into a Decimal128 with the given scaling.

Parameters:
val - long value to be converted to Decimal128.
scale - scale of the Decimal128.

Decimal128

public Decimal128(String str,
                  short scale)
Constructs from the given string.

Parameters:
str - string
scale - scale of the Decimal128.

Decimal128

public Decimal128(char[] str,
                  int offset,
                  int length,
                  short scale)
Constructs from the given string with given offset and length.

Parameters:
str - string
offset - offset
length - length
scale - scale of the Decimal128.
Method Detail

getIntsPerElement

public static int getIntsPerElement(int precision)
Determines the number of ints to store one value.

Parameters:
precision - precision (0-38)
Returns:
the number of ints to store one value

zeroClear

public Decimal128 zeroClear()
Reset the value of this object to zero.


isZero

public boolean isZero()
Returns:
whether this value represents zero.

update

public Decimal128 update(Decimal128 o)
Copy the value of given object.

Parameters:
o - object to copy from

update

public Decimal128 update(Decimal128 o,
                         short scale)
Copy the value of given object and assigns a custom scale.

Parameters:
o - object to copy from

update

public Decimal128 update(long val)
Update the value of this object with the given long. The scale of the Decimal128 is zero.

Parameters:
val - long value to be set to Decimal128.

update

public Decimal128 update(long val,
                         short scale)
Update the value of this object with the given long with the given scal.

Parameters:
val - long value to be set to Decimal128.
scale - scale of the Decimal128.

update

public Decimal128 update(double val,
                         short scale)
Update the value of this object with the given double. in the given scaling. Note that, unlike java.math.BigDecimal, the scaling is given as the parameter, not automatically detected. This is one of the differences between ANSI SQL Numeric and Java's BigDecimal. See class comments for more details.

Unchecked exceptions: ArithmeticException if val overflows in the scaling. NumberFormatException if val is infinite or NaN.

Parameters:
val - double value to be converted to Decimal128.
scale - scale of the Decimal128.

update

public Decimal128 update(IntBuffer buf,
                         int precision)
Updates the value of this object by reading from ByteBuffer, using the required number of ints for the given precision.

Parameters:
buf - ByteBuffer to read values from
precision - 0 to 38. Decimal digits.

update128

public Decimal128 update128(IntBuffer buf)
Updates the value of this object by reading from ByteBuffer, receiving 128+32 bits data (full ranges).

Parameters:
buf - ByteBuffer to read values from

update96

public Decimal128 update96(IntBuffer buf)
Updates the value of this object by reading from ByteBuffer, receiving only 96+32 bits data.

Parameters:
buf - ByteBuffer to read values from

update64

public Decimal128 update64(IntBuffer buf)
Updates the value of this object by reading from ByteBuffer, receiving only 64+32 bits data.

Parameters:
buf - ByteBuffer to read values from

update32

public Decimal128 update32(IntBuffer buf)
Updates the value of this object by reading from ByteBuffer, receiving only 32+32 bits data.

Parameters:
buf - ByteBuffer to read values from

update

public Decimal128 update(int[] array,
                         int offset,
                         int precision)
Updates the value of this object by reading from the given array, using the required number of ints for the given precision.

Parameters:
array - array to read values from
offset - offset of the long array
precision - 0 to 38. Decimal digits.

update128

public Decimal128 update128(int[] array,
                            int offset)
Updates the value of this object by reading from the given integers, receiving 128+32 bits of data (full range).

Parameters:
array - array to read from
offset - offset of the int array

update96

public Decimal128 update96(int[] array,
                           int offset)
Updates the value of this object by reading from the given integers, receiving only 96+32 bits data.

Parameters:
array - array to read from
offset - offset of the int array

update64

public Decimal128 update64(int[] array,
                           int offset)
Updates the value of this object by reading from the given integers, receiving only 64+32 bits data.

Parameters:
array - array to read from
offset - offset of the int array

update32

public Decimal128 update32(int[] array,
                           int offset)
Updates the value of this object by reading from the given integers, receiving only 32+32 bits data.

Parameters:
array - array to read from
offset - offset of the int array

update

public Decimal128 update(BigDecimal bigDecimal)
Updates the value of this object with the given BigDecimal.

Parameters:
bigDecimal - BigDecimal

update

public Decimal128 update(BigInteger bigInt,
                         short scale)
Updates the value of this object with the given BigInteger and scale.

Parameters:
bigInt - BigInteger
scale -

update

public Decimal128 update(String str,
                         short scale)
Updates the value of this object with the given string.

Parameters:
str - string
scale - scale of the Decimal128.

update

public Decimal128 update(char[] str,
                         int offset,
                         int length,
                         short scale)
Updates the value of this object from the given string with given offset and length.

Parameters:
str - string
offset - offset
length - length
scale - scale of the Decimal128.

fastSerializeForHiveDecimal

public int fastSerializeForHiveDecimal(Decimal128FastBuffer scratch)
Serializes the value in a format compatible with the BigDecimal's own representation

Parameters:
scratch -

serializeTo

public void serializeTo(int[] array,
                        int offset,
                        int precision)
Serialize this object to the given array, putting the required number of ints for the given precision.

Parameters:
array - array to write values to
offset - offset of the int array
precision - 0 to 38. Decimal digits.

serializeTo128

public void serializeTo128(int[] array,
                           int offset)
Serialize this object to the given integers, putting 128+32 bits of data (full range).

Parameters:
array - array to write values to
offset - offset of the int array

serializeTo96

public void serializeTo96(int[] array,
                          int offset)
Serialize this object to the given integers, putting only 96+32 bits of data.

Parameters:
array - array to write values to
offset - offset of the int array

serializeTo64

public void serializeTo64(int[] array,
                          int offset)
Serialize this object to the given integers, putting only 64+32 bits of data.

Parameters:
array - array to write values to
offset - offset of the int array

serializeTo32

public void serializeTo32(int[] array,
                          int offset)
Serialize this object to the given integers, putting only 32+32 bits of data.

Parameters:
array - array to write values to
offset - offset of the int array

serializeTo

public void serializeTo(IntBuffer buf,
                        int precision)
Serialize this object to the given ByteBuffer, putting the required number of ints for the given precision.

Parameters:
buf - ByteBuffer to write values to
precision - 0 to 38. Decimal digits.

serializeTo128

public void serializeTo128(IntBuffer buf)
Serialize this object to the given ByteBuffer, putting 128+32 bits of data (full range).

Parameters:
buf - ByteBuffer to write values to

serializeTo96

public void serializeTo96(IntBuffer buf)
Serialize this object to the given ByteBuffer, putting only 96+32 bits of data.

Parameters:
buf - ByteBuffer to write values to

serializeTo64

public void serializeTo64(IntBuffer buf)
Serialize this object to the given ByteBuffer, putting only 64+32 bits of data.

Parameters:
buf - ByteBuffer to write values to

serializeTo32

public void serializeTo32(IntBuffer buf)
Serialize this object to the given ByteBuffer, putting only 32+32 bits of data.

Parameters:
buf - ByteBuffer to write values to

changeScaleDestructive

public void changeScaleDestructive(short scale)
Changes the scaling of this Decimal128, preserving the represented value. This method is destructive.

This method is NOT just a setter for #scale. It also adjusts the unscaled value to preserve the represented value.

When the given scaling is larger than the current scaling, this method shrinks the unscaled value accordingly. It will NOT throw any error even if underflow happens.

When the given scaling is smaller than the current scaling, this method expands the unscaled value accordingly. It does throw an error if overflow happens.

Unchecked exceptions: ArithmeticException a negative value is specified or overflow.

Parameters:
scale - new scale. must be 0 or positive.

add

public static void add(Decimal128 left,
                       Decimal128 right,
                       Decimal128 result,
                       short scale)
Calculates addition and puts the result into the given object. Both operands are first scaled up/down to the specified scale, then this method performs the operation. This method is static and not destructive (except the result object).

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
left - left operand
right - right operand
scale - scale of the result. must be 0 or positive.
result - object to receive the calculation result

addDestructive

public Decimal128 addDestructive(Decimal128 right,
                                 short scale)
Calculates addition and stores the result into this object. This method is destructive.

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
right - right operand
scale - scale of the result. must be 0 or positive.

subtract

public static void subtract(Decimal128 left,
                            Decimal128 right,
                            Decimal128 result,
                            short scale)
Calculates subtraction and puts the result into the given object. Both operands are first scaled up/down to the specified scale, then this method performs the operation. This method is static and not destructive (except the result object).

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
left - left operand
right - right operand
result - object to receive the calculation result
scale - scale of the result. must be 0 or positive.

subtractDestructive

public Decimal128 subtractDestructive(Decimal128 right,
                                      short scale)
Calculates subtraction and stores the result into this object. This method is destructive.

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
right - right operand
scale - scale of the result. must be 0 or positive.

multiply

public static void multiply(Decimal128 left,
                            Decimal128 right,
                            Decimal128 result,
                            short scale)
Calculates multiplication and puts the result into the given object. Both operands are first scaled up/down to the specified scale, then this method performs the operation. This method is static and not destructive (except the result object).

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
left - left operand
right - right operand
result - object to receive the calculation result
scale - scale of the result. must be 0 or positive.

multiplyDestructiveNativeDecimal128

public void multiplyDestructiveNativeDecimal128(Decimal128 right,
                                                short newScale)
As of 2/11/2014 this has a known bug in multiplication. See TestDecimal128.testKnownPriorErrors(). Keeping this source code available since eventually it is better to fix this. The fix employed now is to replace this code with code that uses Java BigDecimal multiply. Performs multiplication, changing the scale of this object to the specified value.

Parameters:
right - right operand. this object is not modified.
newScale - scale of the result. must be 0 or positive.

multiplyDestructive

public void multiplyDestructive(Decimal128 right,
                                short newScale)
Performs multiplication, changing the scale of this object to the specified value.

Parameters:
right - right operand. this object is not modified.
newScale - scale of the result. must be 0 or positive.

divide

public static void divide(Decimal128 left,
                          Decimal128 right,
                          Decimal128 quotient,
                          short scale)
Performs division and puts the result into the given object. Both operands are first scaled up/down to the specified scale, then this method performs the operation. This method is static and not destructive (except the result object).

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
left - left operand
right - right operand
quotient - result object to receive the calculation result
scale - scale of the result. must be 0 or positive.

divideDestructiveNativeDecimal128

public void divideDestructiveNativeDecimal128(Decimal128 right,
                                              short newScale,
                                              Decimal128 remainder)
As of 1/20/2014 this has a known bug in division. See TestDecimal128.testKnownPriorErrors(). Keeping this source code available since eventually it is better to fix this. The fix employed now is to replace this code with code that uses Java BigDecimal divide. Performs division, changing the scale of this object to the specified value.

Unchecked exceptions: ArithmeticException an invalid scale is specified or overflow.

Parameters:
right - right operand. this object is not modified.
newScale - scale of the result. must be 0 or positive.
remainder - object to receive remainder

divideDestructive

public void divideDestructive(Decimal128 right,
                              short newScale)
Divide the target object by right, and scale the result to newScale. This uses HiveDecimal to get a correct answer with the same rounding behavior as HiveDecimal, but it is expensive. In the future, a native implementation could be faster.


modulo

public static void modulo(Decimal128 left,
                          Decimal128 right,
                          Decimal128 result,
                          short scale)
Performs decimal modulo

The definition of modulo (x % p) is: x - IntegerPart(x / p, resultScale) * p


absDestructive

public void absDestructive()
Makes this Decimal128 a positive number. Unlike java.math.BigDecimal, this method is destructive.


negateDestructive

public void negateDestructive()
Reverses the sign of this Decimal128. Unlike java.math.BigDecimal, this method is destructive.


sqrtAsDouble

public double sqrtAsDouble()
Calculate the square root of this value in double precision.

Note that this does NOT perform the calculation in infinite accuracy. Although this sounds weird, it's at least how SQLServer does it. The SQL below demonstrates that SQLServer actually converts it into FLOAT (Java's double).

create table bb (col1 DECIMAL(38,36), col2 DECIMAL (38,36)); insert into bb values(1.00435134913958923485982394892384,1.00345982739817298323423423); select sqrt(col1) as cola,sqrt(col2) colb into asdasd from bb; sp_columns asdasd;

This SQL tells that the result is in FLOAT data type. One justification is that, because DECIMAL is at most 38 digits, double precision is enough for sqrt/pow.

This method does not modify this object.

This method throws exception if this value is negative rather than returning NaN. This is the SQL semantics (at least in SQLServer).

Returns:
square root of this value

powAsDouble

public double powAsDouble(double n)

Calculate the power of this value in double precision. Note that this does NOT perform the calculation in infinite accuracy. Although this sounds weird, it's at least how SQLServer does it. The SQL below demonstrates that SQLServer actually converts it into FLOAT (Java's double).

create table bb (col1 DECIMAL(38,36), col2 DECIMAL (38,36)); insert into bb values(1.00435134913958923485982394892384,1.00345982739817298323423423); select power(col1, col2);

This SQL returns '1.004366436877081000000000000000000000', which is merely a power calculated in double precision ( "java.lang.Math.pow(1.00435134913958923485982394892384d, 1.00345982739817298323423423d)" returns 1.0043664368770813), and then scaled into DECIMAL. The same thing happens even when "n" is a positive integer. One justification is that, because DECIMAL is at most 38 digits, double precision is enough for sqrt/pow.

This method does not modify this object.

This method throws exception if the calculated value is Infinite. This is the SQL semantics (at least in SQLServer).

Parameters:
n - power to raise this object. Unlike BigDecimal.pow(int), this can receive a fractional number. Instead, this method calculates the value in double precision.
Returns:
power of this value

getSignum

public byte getSignum()
Returns the signum of this Decimal128.

Returns:
-1, 0, or 1 as the value of this Decimal128 is negative, zero, or positive.

getScale

public short getScale()
Returns the scale of this Decimal128. The scale is the positive number of digits to the right of the decimal point.

Returns:
the scale of this Decimal128.

getUnscaledValue

public UnsignedInt128 getUnscaledValue()
Returns unscaled value of this Decimal128. Be careful because changes on the returned object will affect the object. It is not recommended to modify the object this method returns.

Returns:
the unscaled value of this Decimal128.

compareTo

public int compareTo(Decimal128 val)
Compares this Decimal128 with the specified Decimal128. Two Decimal128 objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.

Specified by:
compareTo in interface Comparable<Decimal128>
Parameters:
val - Decimal128 to which this Decimal128 is to be compared.
Returns:
a negative integer, zero, or a positive integer as this Decimal128 is numerically less than, equal to, or greater than val.

equals

public boolean equals(Object x)
Compares this Decimal128 with the specified Object for equality. Unlike compareTo, this method considers two Decimal128 objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). This somewhat confusing behavior is, however, same as java.math.BigDecimal.

Overrides:
equals in class Object
Parameters:
x - Object to which this Decimal128 is to be compared.
Returns:
true if and only if the specified Object is a Decimal128 whose value and scale are equal to this Decimal128's.
See Also:
compareTo(Decimal128), hashCode()

hashCode

public int hashCode()
Returns the hash code for this Decimal128. Note that two Decimal128 objects that are numerically equal but differ in scale (like 2.0 and 2.00) will generally not have the same hash code. This somewhat confusing behavior is, however, same as java.math.BigDecimal.

Overrides:
hashCode in class Object
Returns:
hash code for this Decimal128.
See Also:
equals(Object)

longValue

public long longValue()
Converts this Decimal128 to a long. This conversion is analogous to the narrowing primitive conversion from double to short as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this Decimal128 will be discarded, and if the resulting " UnsignedInt128" is too big to fit in a long, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this Decimal128 value.

Specified by:
longValue in class Number
Returns:
this Decimal128 converted to a long.

intValue

public int intValue()
Converts this Decimal128 to an int. This conversion is analogous to the narrowing primitive conversion from double to short as defined in section 5.1.3 of The Java™ Language Specification: any fractional part of this Decimal128 will be discarded, and if the resulting " UnsignedInt128" is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude and precision of this Decimal128 value.

Specified by:
intValue in class Number
Returns:
this Decimal128 converted to an int.

floatValue

public float floatValue()
Converts this Decimal128 to a float. This conversion is similar to the narrowing primitive conversion from double to float as defined in section 5.1.3 of The Java™ Language Specification: if this Decimal128 has too great a magnitude to represent as a float, it will be converted to Float.NEGATIVE_INFINITY or Float.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the Decimal128 value.

Specified by:
floatValue in class Number
Returns:
this Decimal128 converted to a float.

doubleValue

public double doubleValue()
Converts this Decimal128 to a double. This conversion is similar to the narrowing primitive conversion from double to float as defined in section 5.1.3 of The Java™ Language Specification: if this Decimal128 has too great a magnitude represent as a double, it will be converted to Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY as appropriate. Note that even when the return value is finite, this conversion can lose information about the precision of the Decimal128 value.

Specified by:
doubleValue in class Number
Returns:
this Decimal128 converted to a double.

toBigDecimal

public BigDecimal toBigDecimal()
Converts this object to BigDecimal. This method is not supposed to be used in a performance-sensitive place.

Returns:
BigDecimal object equivalent to this object.

checkPrecisionOverflow

public void checkPrecisionOverflow(int precision)
Throws an exception if the value of this object exceeds the maximum value for the given precision. Remember that underflow is not an error, but overflow is an immediate error.

Parameters:
precision - maximum precision

getHiveDecimalString

public String getHiveDecimalString()
Returns the string representation of this value. It discards the trailing zeros in the fractional part to match the HiveDecimal's string representation. However, don't use this string representation for the reconstruction of the object.

Returns:
string representation of this value

toFormalString

public String toFormalString()
Returns the formal string representation of this value. Unlike the debug string returned by toString(), this method returns a string that can be used to re-construct this object. Remember, toString() is only for debugging.

Returns:
string representation of this value

toString

public String toString()
Overrides:
toString in class Object

setNullDataValue

public void setNullDataValue()
Vectorized execution uses the smallest possible positive non-zero value to prevent possible later zero-divide exceptions. Set the field to this value (1 in the internal unsigned 128 bit int).


updateFixedPoint

public void updateFixedPoint(long val,
                             short scale)
Update the value to a decimal value with the decimal point equal to val but with the decimal point inserted scale digits from the right. Behavior is undefined if scale is > 38 or < 0. For example, updateFixedPoint(123456789L, (short) 3) changes the target to the value 123456.789 with scale 3.


zeroFractionPart

public void zeroFractionPart(UnsignedInt128 scratch)
Zero the fractional part of value. Argument scratch is needed to hold unused remainder output, to avoid need to create a new object.


zeroFractionPart

public void zeroFractionPart()

squareDestructive

public Decimal128 squareDestructive()
Multiplies this with this, updating this

Returns:
self

updateVarianceDestructive

public Decimal128 updateVarianceDestructive(Decimal128 scratch,
                                            Decimal128 value,
                                            Decimal128 sum,
                                            long count)
For UDAF variance we use the algorithm described by Chan, Golub, and LeVeque in "Algorithms for computing the sample variance: analysis and recommendations" The American Statistician, 37 (1983) pp. 242--247. variance = variance1 + variance2 + n/(m*(m+n)) * pow(((m/n)*t1 - t2),2) where: - variance is sum[x-avg^2] (this is actually n times the variance) and is updated at every step. - n is the count of elements in chunk1 - m is the count of elements in chunk2 - t1 = sum of elements in chunk1, t2 = sum of elements in chunk2. This is a helper function doing the intermediate computation: t = myagg.count*value - myagg.sum; myagg.variance += (t*t) / ((double)myagg.count*(myagg.count-1));

Returns:
self

fastUpdateFromInternalStorage

public Decimal128 fastUpdateFromInternalStorage(byte[] internalStorage,
                                                short scale)
Fats update from BigInteger two's complement representation

Parameters:
internalStorage - BigInteger two's complement representation of the unscaled value
scale -

setUnscaledValue

public void setUnscaledValue(UnsignedInt128 unscaledValue)
This setter is only for de-serialization, should not be used otherwise.


setScale

public void setScale(short scale)
This setter is only for de-serialization, should not be used otherwise.


setSignum

public void setSignum(byte signum)
This setter is only for de-serialization, should not be used otherwise.



Copyright © 2014 The Apache Software Foundation. All rights reserved.