Search in sources :

Example 1 with DecimalNativeType

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());
}
Also used : DecimalNativeType(org.apache.ignite.internal.schema.DecimalNativeType) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 2 with DecimalNativeType

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;
}
Also used : SchemaMismatchException(org.apache.ignite.internal.schema.SchemaMismatchException) Column(org.apache.ignite.internal.schema.Column) DecimalNativeType(org.apache.ignite.internal.schema.DecimalNativeType)

Aggregations

DecimalNativeType (org.apache.ignite.internal.schema.DecimalNativeType)2 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Column (org.apache.ignite.internal.schema.Column)1 SchemaMismatchException (org.apache.ignite.internal.schema.SchemaMismatchException)1