/** * Returns the value as a BigDecimal. */ public BigDecimal toBigDecimal() { return new BigDecimal(toBigInteger(), scale_); }
/** * Creates a decimal value of zero, with precision and scale. * The underlying value can be changed but the precision and scale cannot. */ public Decimal(int precision, int scale) { bytes_ = new byte[computeByteSize(precision, scale)]; precision_ = precision; scale_ = scale; }
protected Record(Schema schema) { recordIdx_ = -1; colOffsets_ = new long[schema.cols.size()]; colData_ = new ByteBuffer[schema.cols.size()]; byteArrayVals_ = new ByteArray[schema.cols.size()]; timestampNanos_ = new TimestampNanos[schema.cols.size()]; decimalVals_ = new Decimal[schema.cols.size()]; nulls_ = new ByteBuffer[schema.cols.size()]; byteArrayLen_ = new int[schema.cols.size()]; schema_ = schema; for (int i = 0; i < colOffsets_.length; ++i) { Schema.TypeDesc type = schema_.cols.get(i).type; if (type.typeId == Schema.Type.STRING || type.typeId == Schema.Type.VARCHAR) { byteArrayVals_[i] = new ByteArray(); } if (type.typeId == Schema.Type.TIMESTAMP_NANOS) { timestampNanos_[i] = new TimestampNanos(); } if (type.typeId == Schema.Type.CHAR) { byteArrayVals_[i] = new ByteArray(); byteArrayLen_[i] = type.len; } else { byteArrayLen_[i] = -1; } if (type.typeId == Schema.Type.DECIMAL) { decimalVals_[i] = new Decimal(type.precision, type.scale); byteArrayLen_[i] = Decimal.computeByteSize(type.precision, type.scale); } } }
public final Decimal nextDecimal(int colIdx) { int len = byteArrayLen_[colIdx]; long offset = colOffsets_[colIdx] - byteArrayOffset; decimalVals_[colIdx].set(colData_[colIdx], (int)offset, len); colOffsets_[colIdx] += len; return decimalVals_[colIdx]; }
case DECIMAL: Decimal decimal = ((DecimalWritable) o).get(); result = decimal.toBigDecimal(); break; default:
@Override public int compareTo(DecimalWritable o) { // Comparing the integer values is correct (fixed scale) and cheaper. // FIXME: This is not correct if the scale for the decimals is different. //assert decimal_.getScale() == o.get().getScale(); return decimal_.toBigInteger().compareTo(o.get().toBigInteger()); } }
@Override // TODO: this assumes a certain usage pattern (the decimal are from the same // task). Is this accurate? public void write(DataOutput out) throws IOException { out.write(decimal_.getBytes()); }