Search in sources :

Example 1 with LongDecimalWithOverflowAndLongState

use of io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState in project hetu-core by openlookeng.

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(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 2 with LongDecimalWithOverflowAndLongState

use of io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState in project hetu-core by openlookeng.

the class TestDecimalAverageAggregation method testCombineOverflow.

@Test
public void testCombineOverflow() {
    addToState(state, TWO.pow(125));
    addToState(state, TWO.pow(126));
    LongDecimalWithOverflowAndLongState otherState = new LongDecimalWithOverflowAndLongStateFactory().createSingleState();
    addToState(otherState, TWO.pow(125));
    addToState(otherState, TWO.pow(126));
    DecimalAverageAggregation.combine(state, otherState);
    assertEquals(state.getLong(), 4);
    assertEquals(state.getOverflow(), 1);
    assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126)));
    BigInteger expectedAverage = BigInteger.ZERO.add(TWO.pow(126)).add(TWO.pow(126)).add(TWO.pow(125)).add(TWO.pow(125)).divide(BigInteger.valueOf(4));
    assertEquals(average(state, TYPE), new BigDecimal(expectedAverage));
}
Also used : BigInteger(java.math.BigInteger) LongDecimalWithOverflowAndLongStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

LongDecimalWithOverflowAndLongState (io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongState)2 LongDecimalWithOverflowAndLongStateFactory (io.prestosql.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory)2 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Test (org.testng.annotations.Test)2