Search in sources :

Example 51 with BigDecimal

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"));
}
Also used : BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 52 with BigDecimal

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"));
}
Also used : Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Timestamp(java.sql.Timestamp) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Date(java.sql.Date) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 53 with BigDecimal

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();
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Decimals.writeBigDecimal(com.facebook.presto.spi.type.Decimals.writeBigDecimal) BigDecimal(java.math.BigDecimal) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus)

Example 54 with BigDecimal

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()));
}
Also used : BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 55 with BigDecimal

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));
}
Also used : BigInteger(java.math.BigInteger) LongDecimalWithOverflowAndLongStateFactory(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongState) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

BigDecimal (java.math.BigDecimal)5274 Test (org.junit.Test)834 BigInteger (java.math.BigInteger)657 Test (org.testng.annotations.Test)626 LocalDate (org.joda.time.LocalDate)409 ArrayList (java.util.ArrayList)393 ResultSet (java.sql.ResultSet)251 MathContext (java.math.MathContext)220 Timestamp (java.sql.Timestamp)211 PreparedStatement (java.sql.PreparedStatement)207 Date (java.util.Date)175 SQLException (java.sql.SQLException)170 HashMap (java.util.HashMap)157 UUID (java.util.UUID)149 Invoice (org.killbill.billing.invoice.api.Invoice)148 List (java.util.List)136 DateTime (org.joda.time.DateTime)132 RoundingMode (java.math.RoundingMode)129 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)104 Session (org.hibernate.Session)96