Search in sources :

Example 1 with LongDecimalWithOverflowStateFactory

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

the class DecimalSumAggregation method generateAggregation.

private static InternalAggregationFunction generateAggregation(Type inputType, Type outputType) {
    checkArgument(inputType instanceof DecimalType, "type must be Decimal");
    DynamicClassLoader classLoader = new DynamicClassLoader(DecimalSumAggregation.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(inputType);
    MethodHandle inputFunction;
    Class<? extends AccumulatorState> stateInterface = LongDecimalWithOverflowState.class;
    AccumulatorStateSerializer<?> stateSerializer = new LongDecimalWithOverflowStateSerializer();
    if (((DecimalType) inputType).isShort()) {
        inputFunction = SHORT_DECIMAL_INPUT_FUNCTION;
    } else {
        inputFunction = LONG_DECIMAL_INPUT_FUNCTION;
    }
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(NAME, outputType.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createInputParameterMetadata(inputType), inputFunction.bindTo(inputType), COMBINE_FUNCTION, LONG_DECIMAL_OUTPUT_FUNCTION.bindTo(outputType), ImmutableList.of(new AccumulatorStateDescriptor(stateInterface, stateSerializer, new LongDecimalWithOverflowStateFactory())), outputType);
    Type intermediateType = stateSerializer.getSerializedType();
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(NAME, inputTypes, ImmutableList.of(intermediateType), outputType, true, false, factory);
}
Also used : DynamicClassLoader(io.airlift.bytecode.DynamicClassLoader) AccumulatorStateDescriptor(io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor) LongDecimalWithOverflowState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowState) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) LongDecimalWithOverflowStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateFactory) DecimalType(io.prestosql.spi.type.DecimalType) LongDecimalWithOverflowStateSerializer(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateSerializer) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with LongDecimalWithOverflowStateFactory

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

the class TestDecimalSumAggregation method testCombineOverflow.

@Test
public void testCombineOverflow() {
    addToState(state, TWO.pow(125));
    addToState(state, TWO.pow(126));
    LongDecimalWithOverflowState otherState = new LongDecimalWithOverflowStateFactory().createSingleState();
    addToState(otherState, TWO.pow(125));
    addToState(otherState, TWO.pow(126));
    DecimalSumAggregation.combine(state, otherState);
    assertEquals(state.getOverflow(), 1);
    assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126)));
}
Also used : LongDecimalWithOverflowStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateFactory) LongDecimalWithOverflowState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowState) Test(org.testng.annotations.Test)

Example 3 with LongDecimalWithOverflowStateFactory

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

the class TestDecimalSumAggregation method testCombineUnderflow.

@Test
public void testCombineUnderflow() {
    addToState(state, TWO.pow(125).negate());
    addToState(state, TWO.pow(126).negate());
    LongDecimalWithOverflowState otherState = new LongDecimalWithOverflowStateFactory().createSingleState();
    addToState(otherState, TWO.pow(125).negate());
    addToState(otherState, TWO.pow(126).negate());
    DecimalSumAggregation.combine(state, otherState);
    assertEquals(state.getOverflow(), -1);
    assertEquals(state.getLongDecimal(), unscaledDecimal(TWO.pow(126).negate()));
}
Also used : LongDecimalWithOverflowStateFactory(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateFactory) LongDecimalWithOverflowState(io.prestosql.operator.aggregation.state.LongDecimalWithOverflowState) Test(org.testng.annotations.Test)

Aggregations

LongDecimalWithOverflowState (io.prestosql.operator.aggregation.state.LongDecimalWithOverflowState)3 LongDecimalWithOverflowStateFactory (io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateFactory)3 Test (org.testng.annotations.Test)2 DynamicClassLoader (io.airlift.bytecode.DynamicClassLoader)1 AccumulatorStateDescriptor (io.prestosql.operator.aggregation.AggregationMetadata.AccumulatorStateDescriptor)1 LongDecimalWithOverflowStateSerializer (io.prestosql.operator.aggregation.state.LongDecimalWithOverflowStateSerializer)1 DecimalType (io.prestosql.spi.type.DecimalType)1 Type (io.prestosql.spi.type.Type)1 MethodHandle (java.lang.invoke.MethodHandle)1