value = DecimalUtil.coerce(value, precision, scale); BigDecimal minValue = DecimalUtil.minValue(precision, scale); BigDecimal maxValue = DecimalUtil.maxValue(precision, scale); Preconditions.checkArgument(value.compareTo(maxValue) <= 0 && value.compareTo(minValue) >= 0, "Decimal value out of range for %s column: %s", column.getType(), value); BigDecimal smallestValue = DecimalUtil.smallestValue(scale);
/** * Get the data type from the common's pb * @param typeAttributes the additional attributes of the type. * @return A DataType */ public DataType getDataType(ColumnTypeAttributes typeAttributes) { if (this == DECIMAL) { return DecimalUtil.precisionToDataType(typeAttributes.getPrecision()); } return this.dataTypes.get(0); }
/** * The size of this type on the wire * @param typeAttributes the additional attributes of the type. * @return A size */ public int getSize(ColumnTypeAttributes typeAttributes) { if (this == DECIMAL) { return DecimalUtil.precisionToSize(typeAttributes.getPrecision()); } return this.size; }
ColumnTypeAttributes typeAttributes = column.getTypeAttributes(); addDecimal(index, DecimalUtil.minValue(typeAttributes.getPrecision(), typeAttributes.getScale())); break; case STRING:
.typeAttributes(DecimalUtil.typeAttributes(9, 2)) .build(); ColumnSchema decCol2 = new ColumnSchemaBuilder("col1", Type.DECIMAL) .typeAttributes(DecimalUtil.typeAttributes(9, 2)) .build(); assertEquals(decCol1, decCol2); .typeAttributes(DecimalUtil.typeAttributes(9, 0)) .build(); assertNotEquals(decCol1, decCol3);
int scale = column.getTypeAttributes().getScale(); BigDecimal existing = Bytes.getDecimal(rowAlloc, offset, precision, scale); BigDecimal max = DecimalUtil.maxValue(precision, scale); if (existing.equals(max)) { return false; BigDecimal smallest = DecimalUtil.smallestValue(scale); Bytes.setBigDecimal(rowAlloc, existing.add(smallest), precision, offset); return true;
/** * Set a BigDecimal for the lower bound * @param lowerBound value for the lower bound */ public void setLowerBound(BigDecimal lowerBound) { checkColumn(Type.DECIMAL); int precision = column.getTypeAttributes().getPrecision(); int scale = column.getTypeAttributes().getScale(); BigDecimal coercedVal = DecimalUtil.coerce(lowerBound, precision, scale); setLowerBoundInternal(Bytes.fromBigDecimal(coercedVal, precision)); }
/** * Returns the minimum value of a Decimal give a precision and scale. * @param precision the precision of the decimal. * @param scale the scale of the decimal. * @return the minimum decimal value. */ public static BigDecimal minValue(int precision, int scale) { return maxValue(precision, scale).negate(); }
int scale = typeAttributes.getScale(); BigDecimal val = Bytes.getDecimal(lower.rowAlloc, offset, precision, scale); BigDecimal smallestVal = DecimalUtil.smallestValue(scale); return val.add(smallestVal).equals( Bytes.getDecimal(upper.rowAlloc, offset, precision, scale));
@Test public void testLess() { Assert.assertEquals(KuduPredicate.newComparisonPredicate(byteCol, LESS, Byte.MIN_VALUE), KuduPredicate.none(byteCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(shortCol, LESS, Short.MIN_VALUE), KuduPredicate.none(shortCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(intCol, LESS, Integer.MIN_VALUE), KuduPredicate.none(intCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(longCol, LESS, Long.MIN_VALUE), KuduPredicate.none(longCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(floatCol, LESS, Float.NEGATIVE_INFINITY), KuduPredicate.none(floatCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(doubleCol, LESS, Double.NEGATIVE_INFINITY), KuduPredicate.none(doubleCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal32Col, LESS, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL32_PRECISION, 2)), KuduPredicate.none(decimal32Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal64Col, LESS, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL64_PRECISION, 2)), KuduPredicate.none(decimal64Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal128Col, LESS, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL128_PRECISION, 2)), KuduPredicate.none(decimal128Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(stringCol, LESS, ""), KuduPredicate.none(stringCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(binaryCol, LESS, new byte[] {}), KuduPredicate.none(binaryCol)); }
@Test public void testToString() { ColumnSchema col1 = new ColumnSchemaBuilder("col1", Type.STRING).build(); ColumnSchema col2 = new ColumnSchemaBuilder("col2", Type.INT64).build(); ColumnSchema col3 = new ColumnSchemaBuilder("col3", Type.DECIMAL) .typeAttributes(DecimalUtil.typeAttributes(5, 2)) .build(); assertEquals("Column name: col1, type: string", col1.toString()); assertEquals("Column name: col2, type: int64", col2.toString()); assertEquals("Column name: col3, type: decimal(5, 2)", col3.toString()); }
/** * Set a BigDecimal for the upper bound * @param upperBound value for the upper bound */ public void setUpperBound(BigDecimal upperBound) { checkColumn(Type.DECIMAL); int precision = column.getTypeAttributes().getPrecision(); int scale = column.getTypeAttributes().getScale(); BigDecimal coercedVal = DecimalUtil.coerce(upperBound, precision, scale); setUpperBoundInternal(Bytes.fromBigDecimal(coercedVal, precision)); }
KuduPredicate.newIsNotNullPredicate(doubleCol)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal32Col, GREATER_EQUAL, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL32_PRECISION, 2)), KuduPredicate.newIsNotNullPredicate(decimal32Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal64Col, GREATER_EQUAL, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL64_PRECISION, 2)), KuduPredicate.newIsNotNullPredicate(decimal64Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(decimal128Col, GREATER_EQUAL, DecimalUtil.minValue(DecimalUtil.MAX_DECIMAL128_PRECISION, 2)), KuduPredicate.newIsNotNullPredicate(decimal128Col)); Assert.assertEquals(KuduPredicate.newComparisonPredicate(stringCol, GREATER_EQUAL, ""),
new ColumnSchemaBuilder("int64", Type.INT64).key(true), new ColumnSchemaBuilder("decimal32", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL32_PRECISION, 0)), new ColumnSchemaBuilder("decimal64", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL64_PRECISION, 0)), new ColumnSchemaBuilder("decimal128", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL128_PRECISION, 0)), new ColumnSchemaBuilder("string", Type.STRING).key(true), new ColumnSchemaBuilder("binary", Type.BINARY).key(true));
/** * Creates a new byte array containing a little-endian BigDecimal. * @param n A BigDecimal. * @param precision The target precision of the decimal value. * @return A new byte array containing the given value. */ public static byte[] fromBigDecimal(final BigDecimal n, int precision) { int size = DecimalUtil.precisionToSize(precision); switch (size) { case DecimalUtil.DECIMAL32_SIZE: return fromInt(n.unscaledValue().intValue()); case DecimalUtil.DECIMAL64_SIZE: return fromLong(n.unscaledValue().longValue()); case DecimalUtil.DECIMAL128_SIZE: return fromBigInteger(n.unscaledValue()); default: throw new IllegalArgumentException("Unsupported decimal type size: " + size); } }
/** * Add a Decimal for the specified column. * @param columnIndex the column's index in the schema * @param val value to add * @throws IllegalArgumentException if the value doesn't match the column's type * @throws IllegalStateException if the row was already applied * @throws IndexOutOfBoundsException if the column doesn't exist */ public void addDecimal(int columnIndex, BigDecimal val) { checkNotFrozen(); ColumnSchema column = schema.getColumnByIndex(columnIndex); ColumnTypeAttributes typeAttributes = column.getTypeAttributes(); checkColumn(column, Type.DECIMAL); BigDecimal coercedVal = DecimalUtil.coerce(val,typeAttributes.getPrecision(), typeAttributes.getScale()); Bytes.setBigDecimal(rowAlloc, coercedVal, typeAttributes.getPrecision(), getPositionInRowAllocAndSetBitSet(columnIndex)); }
new ColumnSchemaBuilder("timestamp", Type.UNIXTIME_MICROS).key(true), new ColumnSchemaBuilder("decimal32", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL32_PRECISION, 0)), new ColumnSchemaBuilder("decimal64", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL64_PRECISION, 0)), new ColumnSchemaBuilder("decimal128", Type.DECIMAL).key(true) .typeAttributes(DecimalUtil.typeAttributes(DecimalUtil.MAX_DECIMAL128_PRECISION, 0)), new ColumnSchemaBuilder("bool", Type.BOOL), // not primary key type new ColumnSchemaBuilder("float", Type.FLOAT), // not primary key type
int size = DecimalUtil.precisionToSize(precision); BigInteger bigInt = n.unscaledValue(); switch (size) {
ColumnSchema key = new ColumnSchema.ColumnSchemaBuilder("key", Type.INT64).key(true).build(); ColumnSchema val = new ColumnSchema.ColumnSchemaBuilder("value", Type.DECIMAL) .typeAttributes(DecimalUtil.typeAttributes(4, 2)).nullable(true).build(); Schema schema = new Schema(ImmutableList.of(key, val));
/** * Reads a decimal from the beginning of the given array. * @param b The array to read from. * @param offset The offset in the array to start reading from. * @param precision The precision of the decimal value. * @return A BigDecimal. * @throws IndexOutOfBoundsException if the byte array is too small. */ public static BigDecimal getDecimal(final byte[] b, final int offset, int precision, int scale) { int size = DecimalUtil.precisionToSize(precision); switch (size) { case DecimalUtil.DECIMAL32_SIZE: int intVal = getInt(b, offset); return BigDecimal.valueOf(intVal, scale); case DecimalUtil.DECIMAL64_SIZE: long longVal = getLong(b, offset); return BigDecimal.valueOf(longVal, scale); case DecimalUtil.DECIMAL128_SIZE: BigInteger int128Val = getBigInteger(b, offset); return new BigDecimal(int128Val, scale); default: throw new IllegalArgumentException("Unsupported decimal type size: " + size); } }