Skip to content

Commit

Permalink
Don't reuse. Simplify creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
nongli committed Mar 10, 2016
1 parent 3b5ad12 commit 50e1244
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ final class Decimal extends Ordered[Decimal] with Serializable {
this
}

/**
* Just updates the underlying value to `v`, assuming precision and scale is unchanged.
*/
def setInternal(v: Long): Unit = {
this.longVal = v
}

/**
* Set this Decimal to the given unscaled Long, with a given precision and scale.
*/
Expand Down Expand Up @@ -383,6 +376,17 @@ object Decimal {

def apply(value: String): Decimal = new Decimal().set(BigDecimal(value))

/**
* Creates a decimal from unscaled, precision and scale without checking the bounds.
*/
def createUnsafe(unscaled: Long, precision: Int, scale: Int): Decimal = {
val dec = new Decimal()
dec.longVal = unscaled
dec._precision = precision
dec._scale = scale
dec
}

// Evidence parameters for Decimal considered either as Fractional or Integral. We provide two
// parameters inheriting from a common trait since both traits define mkNumericOps.
// See scala.math's Numeric.scala for examples for Scala's built-in types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,9 @@ private Array getByteArray(int rowId) {
*/
public final Decimal getDecimal(int rowId, int precision, int scale) {
if (precision <= Decimal.MAX_INT_DIGITS()) {
assert(resultDecimal != null);
resultDecimal.setInternal(getInt(rowId));
return resultDecimal;
return Decimal.createUnsafe(getInt(rowId), precision, scale);
} else if (precision <= Decimal.MAX_LONG_DIGITS()) {
assert (resultDecimal != null);
resultDecimal.setInternal(getLong(rowId));
return resultDecimal;
return Decimal.createUnsafe(getLong(rowId), precision, scale);
} else {
// TODO: best perf?
byte[] bytes = getBinary(rowId);
Expand Down Expand Up @@ -856,11 +852,6 @@ public final int appendStruct(boolean isNull) {
*/
protected final ColumnarBatch.Row resultStruct;

/**
* Reusable object for getDecimal()
*/
private Decimal resultDecimal;

/**
* The Dictionary for this column.
*
Expand Down Expand Up @@ -936,12 +927,5 @@ protected ColumnVector(int capacity, DataType type, MemoryMode memMode) {
this.resultArray = null;
this.resultStruct = null;
}

if (type instanceof DecimalType) {
DecimalType dt = (DecimalType)type;
if (dt.precision() <= Decimal.MAX_LONG_DIGITS()) {
resultDecimal = Decimal.apply(0, dt.precision(), dt.scale());
}
}
}
}

0 comments on commit 50e1244

Please sign in to comment.