use of org.apache.ignite.internal.schema.DecimalNativeType in project ignite-3 by apache.
the class Row method decimalValue.
/**
* Reads value from specified column.
*
* @param col Column index.
* @return Column value.
* @throws InvalidTypeException If actual column type does not match the requested column type.
*/
public BigDecimal decimalValue(int col) throws InvalidTypeException {
long offLen = findColumn(col, NativeTypeSpec.DECIMAL);
if (offLen < 0) {
return null;
}
int off = offset(offLen);
int len = length(offLen);
DecimalNativeType type = (DecimalNativeType) schema.column(col).type();
byte[] bytes = readBytes(off, len);
return new BigDecimal(new BigInteger(bytes), type.scale());
}
use of org.apache.ignite.internal.schema.DecimalNativeType in project ignite-3 by apache.
the class RowAssembler method appendDecimal.
/**
* Appends BigDecimal value for the current column to the chunk.
*
* @param val Column value.
* @return {@code this} for chaining.
* @throws SchemaMismatchException If a value doesn't match the current column type.
*/
public RowAssembler appendDecimal(BigDecimal val) throws SchemaMismatchException {
checkType(NativeTypeSpec.DECIMAL);
Column col = curCols.column(curCol);
DecimalNativeType type = (DecimalNativeType) col.type();
val = val.setScale(type.scale(), RoundingMode.HALF_UP);
if (val.precision() > type.precision()) {
throw new SchemaMismatchException("Failed to set decimal value for column '" + col.name() + "' " + "(max precision exceeds allocated precision)" + " [decimal=" + val + ", max precision=" + type.precision() + "]");
}
byte[] bytes = val.unscaledValue().toByteArray();
buf.putBytes(curOff, bytes);
writeVarlenOffset(curVartblEntry, curOff - dataOff);
curVartblEntry++;
shiftColumn(bytes.length);
return this;
}
Aggregations