@Test(expected = IllegalArgumentException.class) public void testOutOfPrecision() { BigDecimal input = new BigDecimal("66855344214907231736.4924"); ByteBuffer buffer = ByteBuffer.allocate(256); bigDecimalSerializer.serialize(input, buffer); }
@Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); }
BigDecimalSerializer extends DataTypeSerializer<BigDecimal> { @Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); } }
BigDecimalSerializer extends DataTypeSerializer<BigDecimal> { @Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); } BigDecimalSerializer(DataType type); }
BigDecimalSerializer extends DataTypeSerializer<BigDecimal> { @Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); } BigDecimalSerializer(DataType type); @Override void serialize(BigDecimal value, ByteBuffer out); @Override BigDecimal deserialize(ByteBuffer in); @Override int peekLength(ByteBuffer in); @Override int maxLength(); @Override int getStorageBytesEstimate(); @Override BigDecimal valueOf(String str); }
BigDecimalSerializer extends DataTypeSerializer<BigDecimal> { @Override public void serialize(BigDecimal value, ByteBuffer out) { if (value == null) { BytesUtil.writeVInt(0, out); BytesUtil.writeVInt(-1, out); return; } if (value.scale() > type.getScale()) { if (avoidVerbose++ % 10000 == 0) { logger.warn("value's scale has exceeded the " + type.getScale() + ", cut it off, to ensure encoded value do not exceed maxLength " + maxLength + " times:" + (avoidVerbose)); } value = value.setScale(type.getScale(), BigDecimal.ROUND_HALF_EVEN); } byte[] bytes = value.unscaledValue().toByteArray(); if (bytes.length + 2 > maxLength) { throw new IllegalArgumentException("'" + value + "' exceeds the expected length for type " + type); } BytesUtil.writeVInt(value.scale(), out); BytesUtil.writeVInt(bytes.length, out); out.put(bytes); } BigDecimalSerializer(DataType type); @Override void serialize(BigDecimal value, ByteBuffer out); @Override BigDecimal deserialize(ByteBuffer in); @Override int peekLength(ByteBuffer in); @Override int maxLength(); @Override int getStorageBytesEstimate(); @Override BigDecimal valueOf(String str);  }