@Override protected int getBufferLength() { // Will be used in the aggregate() call return PDecimal.INSTANCE.getByteSize(); }
private PDecimalArray() { super("DECIMAL ARRAY", PDataType.ARRAY_TYPE_BASE + PDecimal.INSTANCE.getSqlType(), PhoenixArray.class, null, 35); }
/** * Produces the next Decimal key relative to the given key. The new key will differ from the * old key in as small a unit as possible while still maintaining accurate serialization. * @param key bytes for the old Decimal key * @return bytes for the new Decimal key, a single unit next from the old one */ private static byte[] nextDecimalKey(byte[] key) { BigDecimal decimal = (BigDecimal) PDecimal.INSTANCE.toObject(key); BigDecimal next = decimal.add(getSmallestUnit(decimal)); return PDecimal.INSTANCE.toBytes(next); }
@Override public void abs(byte[] bytes, int offset, int length, SortOrder sortOrder, ImmutableBytesWritable outPtr) { if (sortOrder == SortOrder.DESC) { bytes = SortOrder.invert(bytes, offset, new byte[length], 0, length); offset = 0; } BigDecimal bigDecimal = toBigDecimal(bytes, offset, length); outPtr.set(toBytes(bigDecimal.abs())); } }
return null; if (equalsAny(actualType, PInteger.INSTANCE, PUnsignedInt.INSTANCE)) { return BigDecimal.valueOf((Integer) object); } else if (equalsAny(actualType, PLong.INSTANCE, PUnsignedLong.INSTANCE)) { return BigDecimal.valueOf((Long) object); } else if (equalsAny(actualType, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE)) { return BigDecimal.valueOf((Short) object); } else if (equalsAny(actualType, PTinyint.INSTANCE, PUnsignedTinyint.INSTANCE)) { return BigDecimal.valueOf((Byte) object); } else if (equalsAny(actualType, PFloat.INSTANCE, PUnsignedFloat.INSTANCE)) { return BigDecimal.valueOf((Float) object); } else if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE)) { return BigDecimal.valueOf((Double) object); } else if (actualType == PDecimal.INSTANCE) { return object; } else if (equalsAny(actualType, PDate.INSTANCE, PUnsignedDate.INSTANCE, PTime.INSTANCE, PUnsignedTime.INSTANCE)) { java.util.Date d = (java.util.Date) object; return BigDecimal.valueOf(d.getTime()); } else if (equalsAny(actualType, PTimestamp.INSTANCE, PUnsignedTimestamp.INSTANCE)) { Timestamp ts = (Timestamp) object; return ((Boolean) object) ? BigDecimal.ONE : BigDecimal.ZERO; return throwConstraintViolationException(actualType, this);
scale = v.scale(); } else { int[] v = getDecimalPrecisionAndScale(ptr.get(), ptr.getOffset(), ptr.getLength(), actualModifier); scale = v[1]; decimal = (BigDecimal) toObject(object, actualType); } else { // only value bytes is passed in, need to convert to object first. decimal = (BigDecimal) toObject(ptr); ptr.set(toBytes(decimal));
private BigDecimal mean() { BigDecimal sum = BigDecimal.ZERO; for (Entry<ImmutableBytesPtr, Integer> entry : valueVsCount.entrySet()) { BigDecimal colValue = (BigDecimal) PDecimal.INSTANCE.toObject(entry.getKey()); sum = sum.add(colValue.multiply(new BigDecimal(entry.getValue()))); } return sum.divide(new BigDecimal(totalCount), PDataType.DEFAULT_MATH_CONTEXT); }
/** * {@inheritDoc } */ @Override protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(stepPrevInScale(result)); byte[] upperRange = PDecimal.INSTANCE.toBytes(result); return KeyRange.getKeyRange(lowerRange, false, upperRange, true); }
o = 0; return toBigDecimal(b, o, l); } else if (equalsAny(actualType, PDate.INSTANCE, PTime.INSTANCE, PUnsignedDate.INSTANCE, PUnsignedTime.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE, PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE, PUnsignedTinyint.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeLong(b, o, sortOrder)); } else if (equalsAny(actualType, PFloat.INSTANCE, PUnsignedFloat.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeFloat(b, o, sortOrder)); } else if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeDouble(b, o, sortOrder)); } else if (equalsAny(actualType, PTimestamp.INSTANCE, PUnsignedTimestamp.INSTANCE)) { long millisPart = DateUtil.getCodecFor(actualType).decodeLong(b, o, sortOrder); int nanoPart = PUnsignedInt.INSTANCE.getCodec().decodeInt(b, o + Bytes.SIZEOF_LONG, sortOrder); BigDecimal.ZERO; return throwConstraintViolationException(actualType, this);
@Override public boolean isComparableTo(PDataType targetType) { return PDecimal.INSTANCE.isComparableTo(targetType); }
scale = v.scale(); } else { this.coerceBytes(ptr, value, srcType, maxLength, scale, sortOrder, desiredMaxLength, desiredScale, sortOrder, true); int[] v = getDecimalPrecisionAndScale(ptr.get(), ptr.getOffset(), ptr.getLength(), sortOrder); maxLength = v[0]; scale = v[1];
@Test public void testDoubleToDecimalComparison() { // Basic tests assertTrue(PDouble.INSTANCE.compareTo(PDouble.INSTANCE.toBytes(1.23), 0, PDouble.INSTANCE.getByteSize(), SortOrder.getDefault(), PDecimal.INSTANCE.toBytes(BigDecimal.valueOf(1.24)), 0, PDecimal.INSTANCE.getByteSize(), SortOrder.getDefault(), PDecimal.INSTANCE) < 0); }
@Override public int estimateByteSize(Object o) { if (o == null) { return 1; } BigDecimal v = (BigDecimal) o; // TODO: should we strip zeros and round here too? return Math.min(getLength(v), MAX_BIG_DECIMAL_BYTES); }
LiteralExpression literal = (LiteralExpression)child; if (literal.getDataType() == PDecimal.INSTANCE) { if (PDecimal.INSTANCE.compareTo(literal.getValue(), BigDecimal.ZERO) == 0) { throw new SQLExceptionInfo.Builder(SQLExceptionCode.DIVIDE_BY_ZERO).build().buildException();
if (value != null) { BigDecimal bd; if (equalsAny(targetType, PUnsignedLong.INSTANCE, PUnsignedInt.INSTANCE, PUnsignedSmallint.INSTANCE, PUnsignedTinyint.INSTANCE)) { bd = (BigDecimal) value;
scale = v.scale(); } else { int[] v = getDecimalPrecisionAndScale(ptr.get(), ptr.getOffset(), ptr.getLength(), actualModifier); scale = v[1]; decimal = (BigDecimal) toObject(object, actualType); } else { // only value bytes is passed in, need to convert to object first. decimal = (BigDecimal) toObject(ptr); ptr.set(toBytes(decimal));
@Override public String toStringLiteral(byte[] b, int offset, int length, Format formatter) { if (formatter == null) { BigDecimal o = (BigDecimal) toObject(b, offset, length); return o.toPlainString(); } return super.toStringLiteral(b, offset, length, formatter); }
/** * {@inheritDoc } */ @Override protected KeyRange getInputRangeProducing(BigDecimal result) { if(!hasEnoughPrecisionToProduce(result)) { throw new IllegalArgumentException("Cannot produce input range for decimal " + result + ", not enough precision with scale " + getRoundingScale()); } byte[] lowerRange = PDecimal.INSTANCE.toBytes(result); byte[] upperRange = PDecimal.INSTANCE.toBytes(stepNextInScale(result)); return KeyRange.getKeyRange(lowerRange, upperRange); }
o = 0; return toBigDecimal(b, o, l); } else if (equalsAny(actualType, PDate.INSTANCE, PTime.INSTANCE, PUnsignedDate.INSTANCE, PUnsignedTime.INSTANCE, PLong.INSTANCE, PUnsignedLong.INSTANCE, PInteger.INSTANCE, PUnsignedInt.INSTANCE, PSmallint.INSTANCE, PUnsignedSmallint.INSTANCE, PTinyint.INSTANCE, PUnsignedTinyint.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeLong(b, o, sortOrder)); } else if (equalsAny(actualType, PFloat.INSTANCE, PUnsignedFloat.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeFloat(b, o, sortOrder)); } else if (equalsAny(actualType, PDouble.INSTANCE, PUnsignedDouble.INSTANCE)) { return BigDecimal.valueOf(actualType.getCodec().decodeDouble(b, o, sortOrder)); } else if (equalsAny(actualType, PTimestamp.INSTANCE, PUnsignedTimestamp.INSTANCE)) { long millisPart = DateUtil.getCodecFor(actualType).decodeLong(b, o, sortOrder); int nanoPart = PUnsignedInt.INSTANCE.getCodec().decodeInt(b, o + Bytes.SIZEOF_LONG, sortOrder); BigDecimal.ZERO; return throwConstraintViolationException(actualType, this);
@Override public boolean isComparableTo(PDataType targetType) { return PDecimal.INSTANCE.isComparableTo(targetType); }