@Override final Decimal getDecimal(int rowId, int precision, int scale) { if (isNullAt(rowId)) return null; return Decimal.apply(accessor.getObject(rowId), precision, scale); } }
@Override public FieldVector getNewVector( String name, FieldType fieldType, BufferAllocator allocator, CallBack schemaChangeCallback) { return new DecimalVector(name, fieldType, allocator); }
public void allocate(){ vector.allocateNew(); }
public void populate(final int count){ for (int i = 0; i < count; i++) { if(value != null) { ((DecimalVector) vector).setSafe(i, value); } } vector.setValueCount(count); }
/** * Same as {@link #set(int, ArrowBuf)} except that it handles the * case when index is greater than or equal to existing * value capacity {@link #getValueCapacity()}. * * @param index position of element * @param buffer ArrowBuf containing decimal value. */ public void setSafe(int index, ArrowBuf buffer) { handleSafe(index); set(index, buffer); }
@Test public void testPatchDecimal() { try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) { DecimalVector decimalVector = new DecimalVector("decimal", allocator, 38, 9); decimalVector.allocateNew(8); decimalVector.set(0, decimal1); decimalVector.set(1, decimal2); decimalVector.set(2, decimal3); decimalVector.set(3, decimal4); decimalVector.set(4, decimal5); decimalVector.set(5, decimal6); decimalVector.set(6, decimal7); decimalVector.set(7, decimal8); decimalVector.setValueCount(8); assertEquals(8, decimalVector.getValueCount()); assertEquals(decimal1, decimalVector.getObject(0)); assertEquals(decimal2, decimalVector.getObject(1)); assertEquals(decimal3, decimalVector.getObject(2)); assertEquals(decimal4, decimalVector.getObject(3)); assertEquals(decimal5, decimalVector.getObject(4)); assertEquals(decimal6, decimalVector.getObject(5)); assertEquals(decimal7, decimalVector.getObject(6)); assertEquals(decimal8, decimalVector.getObject(7)); ByteBuf newBuffer = patchDecimal(allocator, decimalVector.getDataBuffer(), decimalField, childDecimalField); BigDecimal bd = DecimalHelper.getBigDecimalFromSparse(newBuffer, startIndex, DrillBackwardsCompatibilityHandler.NUMBER_DECIMAL_DIGITS, decimalVector.getScale());
private static Pair<DecimalVector, ResultVerifier> testDecimalVector(final int startIndexInCurrentOutput, final int startIndexInJob) { DecimalVector colDecimalV = new DecimalVector("colDecimal", allocator, 10, 10); colDecimalV.allocateNew(5); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(25.03).setScale(5, RoundingMode.HALF_UP), tempBuf, 0); colDecimalV.set(0, tempBuf); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(2524324.034534), tempBuf, 0); colDecimalV.set(1, tempBuf); colDecimalV.setNull(2); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(2523423423424234243234.235), tempBuf, 0); colDecimalV.set(3, tempBuf); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(2523423423424234243234.23524324234234), tempBuf, 0); colDecimalV.set(4, tempBuf); ResultVerifier verifier = new ResultVerifier() { @Override public void verify(DataPOJO output) { int index = startIndexInCurrentOutput; assertEquals(2.503E-4, ((Double)output.extractValue("colDecimal", index++)).doubleValue(), 0.001f); assertEquals(2.524324034534E23, ((Double)output.extractValue("colDecimal", index++)).doubleValue(), 0.001f); assertNull(output.extractValue("colDecimal", index++)); assertEquals(2.523423423424234E11, ((Double)output.extractValue("colDecimal", index++)).doubleValue(), 0.001f); assertEquals(2.523423423424234E11, ((Double)output.extractValue("colDecimal", index++)).doubleValue(), 0.001f); } }; return Pair.of(colDecimalV, verifier); }
static BigDecimal[] populate16ByteValuesWithoutNull(DecimalVector vector, int size){ vector.allocateNew(); BigDecimal values[] = new BigDecimal[size]; for(int i =0; i < values.length; i++){ values[i] = new BigDecimal(RAND.nextLong()); vector.setSafe(i, values[i]); } vector.setValueCount(values.length); return values; }
final int count = 1024; try ( DecimalVector in = new DecimalVector("in", allocator, 38, 0); DecimalVector out = new DecimalVector("out", allocator, 38, 0); ) { in.allocateNew(count); ArrowBuf tempBuf = allocator.buffer(1024); BigDecimal val = new BigDecimal(i + j + ((double) (i + j) / count)).setScale(0, RoundingMode.HALF_UP); DecimalUtility.writeBigDecimalToArrowBuf(val, tempBuf, 0); in.set(i + j, tempBuf); BigDecimal val = new BigDecimal(i + j + ((double) (i + j) / count)).setScale(0, RoundingMode.HALF_UP); DecimalUtility.writeBigDecimalToArrowBuf(val, tempBuf, 0); in.set(i + j, tempBuf); in.setValueCount(count);
public void accumulate(final long memoryAddr, final int count) { final long maxAddr = memoryAddr + count * WIDTH_ORDINAL; FieldVector inputVector = getInput(); final long incomingBit = inputVector.getValidityBufferAddress(); final long incomingValue = inputVector.getDataBufferAddress(); final long[] valueAddresses = this.valueAddresses; final int scale = ((DecimalVector)inputVector).getScale(); int incomingIndex = 0; for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += WIDTH_ORDINAL, incomingIndex++) { final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1; java.math.BigDecimal newVal = DecimalAccumulatorUtilsNoSpill.getBigDecimal(incomingValue + (incomingIndex * WIDTH_INPUT), valBuf, scale); final int tableIndex = PlatformDependent.getInt(ordinalAddr); final long sumAddr = valueAddresses[tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK] + (tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK) * WIDTH_ACCUMULATOR; PlatformDependent.putLong(sumAddr, Double.doubleToLongBits(Double.longBitsToDouble(PlatformDependent.getLong(sumAddr)) + newVal.doubleValue() * bitVal)); } } }
private static void insertIntoDecimalVector(int index, int value, BaseFixedWidthVector vector) { DecimalVector vec = (DecimalVector)vector; DecimalHolder holder = new DecimalHolder(); holder.buffer = vec.getDataBuffer(); DecimalUtility.writeBigDecimalToArrowBuf(new BigDecimal(value), holder.buffer, 0); holder.start = 0; holder.scale = 0; holder.precision = 38; vec.setSafe(index, holder); }
@Override public boolean setSafe(int index, ArrowBuf bytebuf, int start, int length) { /* data read from Parquet into the bytebuf is already in BE format, no need * swap bytes to construct BigDecimal. only when we write BigDecimal to * data buffer of decimal vector, we need to swap bytes which the DecimalUtility * function already does. */ BigDecimal intermediate = DecimalHelper.getBigDecimalFromBEArrowBuf(bytebuf, index, schemaElement.getScale()); if (index >= decimalVector.getValueCapacity()) { return false; } /* this will swap bytes as we are writing to the buffer of DecimalVector */ DecimalUtility.writeBigDecimalToArrowBuf(intermediate, decimalVector.getDataBuffer(), index); decimalVector.setIndexDefined(index); return true; }
public void writeNull() { vector.setNull(idx()); vector.setValueCount(idx()+1); } }
public void loadData(SerializedField metadata, ArrowBuf buffer) { /* clear the current buffers (if any) */ vector.clear(); /* get the metadata children */ final SerializedField bitsField = metadata.getChild(0); final SerializedField valuesField = metadata.getChild(1); final int valuesLength = buffer.capacity(); vector.allocateNew(metadata.getValueCount()); /* set inner validity buffer */ setValidityBuffer(bitsField); /* load inner value buffer */ vector.valueBuffer.close(); loadDataBuffer(valuesField, buffer.slice(0, valuesLength)); }
public void writeBigEndianBytesToDecimal(byte[] value) { vector.setBigEndianSafe(idx(), value); vector.setValueCount(idx()+1); }
@Override public void set(ValueVector v, int index) { if(obj != null){ ((DecimalVector) v).setSafe(index, obj); } }
switch (type.getMode()) { case REQUIRED: ((DecimalVector) vector).setSafe(index, (DecimalHolder) holder); return; case OPTIONAL: if (holder instanceof NullableDecimalHolder) { if (((NullableDecimalHolder) holder).isSet == 1) { ((DecimalVector) vector).setSafe(index, (NullableDecimalHolder) holder); } else { ((DecimalVector) vector).isSafe(index); ((DecimalVector) vector).setSafe(index, (DecimalHolder) holder);
@Override void addNext(int start, int index) { /* data read from Parquet into the bytebuf is already in BE format, no need * swap bytes to construct BigDecimal. only when we write BigDecimal to * data buffer of decimal vector, we need to swap bytes which the DecimalUtility * function already does. */ BigDecimal intermediate = DecimalHelper.getBigDecimalFromBEArrowBuf(bytebuf, index, schemaElement.getScale()); /* this will swap bytes as we are writing to the buffer of DecimalVector */ DecimalUtility.writeBigDecimalToArrowBuf(intermediate, valueVec.getDataBuffer(), index); valueVec.setIndexDefined(index); } }
/** * Store the given value at a particular position in the vector. isSet indicates * whether the value is NULL or not. * * @param index position of the new value * @param isSet 0 for NULL value, 1 otherwise * @param start start position of the value in the buffer * @param buffer buffer containing the value to be stored in the vector */ public void set(int index, int isSet, int start, ArrowBuf buffer) { if (isSet > 0) { set(index, start, buffer); } else { BitVectorHelper.setValidityBit(validityBuffer, index, 0); } }