Search in sources :

Example 1 with LongDecimalWithOverflowAndLongStateFactory

use of com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory 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)

Example 2 with LongDecimalWithOverflowAndLongStateFactory

use of com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory in project presto by prestodb.

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

Example 3 with LongDecimalWithOverflowAndLongStateFactory

use of com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory in project presto by prestodb.

the class DecimalAverageAggregation method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type type) {
    checkArgument(type instanceof DecimalType, "type must be Decimal");
    DynamicClassLoader classLoader = new DynamicClassLoader(DecimalAverageAggregation.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    MethodHandle inputFunction;
    MethodHandle outputFunction;
    Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowAndLongState.class;
    AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowAndLongStateSerializer();
    if (((DecimalType) type).isShort()) {
        inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
        outputFunction = SHORT_DECIMAL_OUTPUT_FUNCTION;
    } else {
        inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
        outputFunction = LONG_DECIMAL_OUTPUT_FUNCTION;
    }
    outputFunction = outputFunction.bindTo(type);
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION, outputFunction, ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowAndLongStateFactory())), type);
    Type intermediateType = stateSerializer.getSerializedType();
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), type, true, false, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowAndLongStateFactory(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory) LongDecimalWithOverflowAndLongState(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongState) DecimalType(com.facebook.presto.common.type.DecimalType) Type(com.facebook.presto.common.type.Type) DecimalType(com.facebook.presto.common.type.DecimalType) LongDecimalWithOverflowAndLongStateSerializer(com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

LongDecimalWithOverflowAndLongState (com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongState)3 LongDecimalWithOverflowAndLongStateFactory (com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateFactory)3 BigDecimal (java.math.BigDecimal)2 BigInteger (java.math.BigInteger)2 Test (org.testng.annotations.Test)2 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)1 DecimalType (com.facebook.presto.common.type.DecimalType)1 Type (com.facebook.presto.common.type.Type)1 AccumulatorStateDescriptor (com.facebook.presto.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)1 LongDecimalWithOverflowAndLongStateSerializer (com.facebook.presto.operator.aggregation.state.LongDecimalWithOverflowAndLongStateSerializer)1 MethodHandle (java.lang.invoke.MethodHandle)1