use of java.math.BigDecimal in project presto by prestodb.
the class TestHiveDecimalParser method testParseDecimal.
@Test
public void testParseDecimal() {
checkParseDecimal("3", 2, 1, new BigDecimal("3.0"));
checkParseDecimal("3.1", 2, 1, new BigDecimal("3.1"));
// rounding
checkParseDecimal("3.11", 2, 1, new BigDecimal("3.1"));
checkParseDecimal("3.16", 2, 1, new BigDecimal("3.2"));
// rouding of half (odd and even)
checkParseDecimal("3.15", 2, 1, new BigDecimal("3.2"));
checkParseDecimal("3.25", 2, 1, new BigDecimal("3.3"));
// negative
checkParseDecimal("-3", 2, 1, new BigDecimal("-3.0"));
checkParseDecimal("-3.1", 2, 1, new BigDecimal("-3.1"));
// negative rounding
checkParseDecimal("-3.11", 2, 1, new BigDecimal("-3.1"));
checkParseDecimal("-3.16", 2, 1, new BigDecimal("-3.2"));
// negative rounding of half (odd and even)
checkParseDecimal("-3.15", 2, 1, new BigDecimal("-3.2"));
checkParseDecimal("-3.25", 2, 1, new BigDecimal("-3.3"));
}
use of java.math.BigDecimal in project presto by prestodb.
the class TestHiveIntegrationSmokeTest method createTableWithEveryType.
@Test
public void createTableWithEveryType() throws Exception {
@Language("SQL") String query = "" + "CREATE TABLE test_types_table AS " + "SELECT" + " 'foo' _varchar" + ", cast('bar' as varbinary) _varbinary" + ", cast(1 as bigint) _bigint" + ", 2 _integer" + ", CAST('3.14' AS DOUBLE) _double" + ", true _boolean" + ", DATE '1980-05-07' _date" + ", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" + ", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long" + ", CAST('bar' AS CHAR(10)) _char";
assertUpdate(query, 1);
MaterializedResult results = getQueryRunner().execute(getSession(), "SELECT * FROM test_types_table").toJdbcTypes();
assertEquals(results.getRowCount(), 1);
MaterializedRow row = results.getMaterializedRows().get(0);
assertEquals(row.getField(0), "foo");
assertEquals(row.getField(1), "bar".getBytes(UTF_8));
assertEquals(row.getField(2), 1L);
assertEquals(row.getField(3), 2);
assertEquals(row.getField(4), 3.14);
assertEquals(row.getField(5), true);
assertEquals(row.getField(6), new Date(new DateTime(1980, 5, 7, 0, 0, 0, UTC).getMillis()));
assertEquals(row.getField(7), new Timestamp(new DateTime(1980, 5, 7, 11, 22, 33, 456, UTC).getMillis()));
assertEquals(row.getField(8), new BigDecimal("3.14"));
assertEquals(row.getField(9), new BigDecimal("12345678901234567890.0123456789"));
assertEquals(row.getField(10), "bar ");
assertUpdate("DROP TABLE test_types_table");
assertFalse(getQueryRunner().tableExists(getSession(), "test_types_table"));
}
use of java.math.BigDecimal in project presto by prestodb.
the class BlockAssertions method createShortDecimalsBlock.
public static Block createShortDecimalsBlock(Iterable<String> values) {
DecimalType shortDecimalType = DecimalType.createDecimalType(1);
BlockBuilder builder = shortDecimalType.createBlockBuilder(new BlockBuilderStatus(), 100);
for (String value : values) {
if (value == null) {
builder.appendNull();
} else {
shortDecimalType.writeLong(builder, new BigDecimal(value).unscaledValue().longValue());
}
}
return builder.build();
}
use of java.math.BigDecimal in project presto by prestodb.
the class TestDecimalAverageAggregation method testUnderflow.
@Test
public void testUnderflow() {
addToState(state, TWO.pow(126).negate());
assertEquals(state.getLong(), 1);
assertEquals(state.getOverflow(), 0);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
addToState(state, TWO.pow(126).negate());
assertEquals(state.getLong(), 2);
assertEquals(state.getOverflow(), -1);
assertEquals(UnscaledDecimal128Arithmetic.compare(state.getLongDecimal(), unscaledDecimal(0)), 0);
assertEquals(average(state, TYPE), new BigDecimal(TWO.pow(126).negate()));
}
use of java.math.BigDecimal in project presto by prestodb.
the class TestDecimalAverageAggregation method testCombineUnderflow.
@Test
public void testCombineUnderflow() {
addToState(state, TWO.pow(125).negate());
addToState(state, TWO.pow(126).negate());
LongDecimalWithOverflowAndLongState otherState = new LongDecimalWithOverflowAndLongStateFactory().createSingleState();
addToState(otherState, TWO.pow(125).negate());
addToState(otherState, TWO.pow(126).negate());
DecimalAverageAggregation.combine(state, otherState);
assertEquals(state.getLong(), 4);
assertEquals(state.getOverflow(), -1);
assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
BigInteger expectedAverage = BigInteger.ZERO.add(TWO.pow(126)).add(TWO.pow(126)).add(TWO.pow(125)).add(TWO.pow(125)).negate().divide(BigInteger.valueOf(4));
assertEquals(average(state, TYPE), new BigDecimal(expectedAverage));
}
Aggregations