Refine search
private BigDecimal parseBigDecimal(Slice slice, int offset, int length) { checkArgument(length < buffer.length); for (int i = 0; i < length; i++) { buffer[i] = (char) slice.getByte(offset + i); } BigDecimal decimal = new BigDecimal(buffer, 0, length); checkState(decimal.scale() <= type.getScale(), "Read decimal value scale larger than column scale"); decimal = decimal.setScale(type.getScale(), HALF_UP); checkState(decimal.precision() <= type.getPrecision(), "Read decimal precision larger than column precision"); return decimal; } }
public Number divideImpl(Number left, Number right) { BigDecimal bigLeft = toBigDecimal(left); BigDecimal bigRight = toBigDecimal(right); try { return bigLeft.divide(bigRight); } catch (ArithmeticException e) { // set a DEFAULT precision if otherwise non-terminating int precision = Math.max(bigLeft.precision(), bigRight.precision()) + DIVISION_EXTRA_PRECISION; BigDecimal result = bigLeft.divide(bigRight, new MathContext(precision)); int scale = Math.max(Math.max(bigLeft.scale(), bigRight.scale()), DIVISION_MIN_SCALE); if (result.scale() > scale) result = result.setScale(scale, BigDecimal.ROUND_HALF_UP); return result; } }
/** * Returns the number of digits (integer and fractional) in the number, which is equivalent * to SQL decimal precision. Note that this is different from BigDecimal.precision(), * which returns the precision of the unscaled value (BigDecimal.valueOf(0.01).precision() = 1, * whereas HiveDecimal.create("0.01").precision() = 2). * If you want the BigDecimal precision, use HiveDecimal.bigDecimalValue().precision() * @return */ @HiveDecimalVersionV1 public int precision() { int bdPrecision = bd.precision(); int bdScale = bd.scale(); if (bdPrecision < bdScale) { // This can happen for numbers less than 0.1 // For 0.001234: bdPrecision=4, bdScale=6 // In this case, we'll set the type to have the same precision as the scale. return bdScale; } return bdPrecision; }
private static BigDecimal enforcePrecisionScale(BigDecimal bd, int maxPrecision, int maxScale) { if (bd == null) { return null; } /** * Specially handling the case that bd=0, and we are converting it to a type where precision=scale, * such as decimal(1, 1). */ if (bd.compareTo(BigDecimal.ZERO) == 0 && bd.scale() == 0 && maxPrecision == maxScale) { return bd.setScale(maxScale); } bd = trim(bd); if (bd.scale() > maxScale) { bd = bd.setScale(maxScale, RoundingMode.HALF_UP); } int maxIntDigits = maxPrecision - maxScale; int intDigits = bd.precision() - bd.scale(); if (intDigits > maxIntDigits) { return null; } return bd; }
private static BigDecimal normalize(BigDecimal bd, boolean allowRounding) { if (bd == null) { return null; } bd = trim(bd); int intDigits = bd.precision() - bd.scale(); if (intDigits > MAX_PRECISION) { return null; } int maxScale = Math.min(MAX_SCALE, Math.min(MAX_PRECISION - intDigits, bd.scale())); if (bd.scale() > maxScale ) { if (allowRounding) { bd = bd.setScale(maxScale, RoundingMode.HALF_UP); // Trimming is again necessary, because rounding may introduce new trailing 0's. bd = trim(bd); } else { bd = null; } } return bd; }
public static VarDecimalHolder getVarDecimalHolder(DrillBuf buf, BigDecimal bigDecimal) { VarDecimalHolder dch = new VarDecimalHolder(); byte[] bytes = bigDecimal.unscaledValue().toByteArray(); int length = bytes.length; dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); dch.start = 0; dch.end = length; dch.buffer = buf.reallocIfNeeded(length); dch.buffer.setBytes(0, bytes); return dch; } }
private BigDecimal clampAndRound(final BigDecimal initialValue) { BigDecimal value; if (-initialValue.scale() > MAX_EXPONENT) { int diff = -initialValue.scale() - MAX_EXPONENT; if (initialValue.unscaledValue().equals(BIG_INT_ZERO)) { value = new BigDecimal(initialValue.unscaledValue(), -MAX_EXPONENT); } else if (diff + initialValue.precision() > 34) { throw new NumberFormatException("Exponent is out of range for Decimal128 encoding of " + initialValue); } else { BigInteger multiplier = BIG_INT_TEN.pow(diff); value = new BigDecimal(initialValue.unscaledValue().multiply(multiplier), initialValue.scale() + diff); } else if (-initialValue.scale() < MIN_EXPONENT) { } else { value = initialValue.round(DECIMAL128); int extraPrecision = initialValue.precision() - value.precision(); if (extraPrecision > 0) {
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } BigDecimal avg = BigDecimal.ZERO; for (int i = start; i < start + length; i++) { avg = avg.add(getBigDecimalForCounter(i)); } avg = avg.divide(BigDecimal.valueOf(length), ROUND_HALF_UP); return new SqlDecimal(avg.unscaledValue(), avg.precision(), avg.scale()); }
if (bigDecimal.scale() != 0) { if (bigDecimal.signum() != 0) { BigDecimal bigDecimalStripped = bigDecimal.stripTrailingZeros(); int stripTrailingZerosScale = bigDecimalStripped.scale(); int scale = bigDecimal.scale(); if (scale < 0 || scale > HiveDecimal.MAX_SCALE) { return false; if (!fastSetFromBigInteger(bigDecimal.unscaledValue(), bigDecimal.scale(), bigDecimal.precision(), fastResult)) { return false;
@Override public SqlDecimal getExpectedValue(int start, int length) { if (length == 0) { return null; } BigDecimal sum = BigDecimal.ZERO; for (int i = start; i < start + length; i++) { sum = sum.add(getBigDecimalForCounter(i)); } return new SqlDecimal(sum.unscaledValue(), sum.precision(), sum.scale()); }
public static Decimal38SparseHolder getDecimal38Holder(DrillBuf buf, BigDecimal bigDecimal) { Decimal38SparseHolder dch = new Decimal38SparseHolder(); dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); Decimal38SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer); dch.start = 0; dch.buffer = buf.reallocIfNeeded(Decimal38SparseHolder.maxPrecision * DecimalUtility.INTEGER_SIZE); DecimalUtility .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal38SparseHolder.nDecimalDigits); return dch; }
public static Decimal28SparseHolder getDecimal28Holder(DrillBuf buf, BigDecimal bigDecimal) { Decimal28SparseHolder dch = new Decimal28SparseHolder(); dch.scale = bigDecimal.scale(); dch.precision = bigDecimal.precision(); Decimal28SparseHolder.setSign(bigDecimal.signum() == -1, dch.start, dch.buffer); dch.start = 0; dch.buffer = buf.reallocIfNeeded(5 * DecimalUtility.INTEGER_SIZE); DecimalUtility .getSparseFromBigDecimal(bigDecimal, dch.buffer, dch.start, dch.scale, Decimal28SparseHolder.nDecimalDigits); return dch; }