public static void outputShortDecimal(DecimalType type, LongDecimalWithOverflowAndLongState state, BlockBuilder out) { if (state.getLong() == 0) { out.appendNull(); } else { writeShortDecimal(out, average(state, type).unscaledValue().longValueExact()); } }
private static void accumulateValueInState(Slice unscaledValue, LongDecimalWithOverflowAndLongState state) { accumulateAndUpdateOverflow(unscaledValue, state); state.setLong(state.getLong() + 1); }
public static void inputLongDecimal(Type type, LongDecimalWithOverflowAndLongState state, Block block, int position) { accumulateValueInState(type.getSlice(block, position), state); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type type = typeManager.getType(getOnlyElement(applyBoundVariables(getSignature().getArgumentTypes(), boundVariables))); return generateAggregation(type); }
@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)); }
private static void addToState(LongDecimalWithOverflowAndLongState state, BigInteger value) { BlockBuilder blockBuilder = TYPE.createFixedSizeBlockBuilder(1); TYPE.writeSlice(blockBuilder, unscaledDecimal(value)); DecimalAverageAggregation.inputLongDecimal(TYPE, state, blockBuilder.build(), 0); } }
private static void accumulateAndUpdateOverflow(Slice unscaledValue, LongDecimalWithOverflowAndLongState state) { initializeIfNeeded(state); Slice sum = state.getLongDecimal(); long overflow = UnscaledDecimal128Arithmetic.addWithOverflow(sum, unscaledValue, sum); state.setOverflow(state.getOverflow() + overflow); } }
createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION,
@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)); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type type = typeManager.getType(getOnlyElement(applyBoundVariables(getSignature().getArgumentTypes(), boundVariables))); return generateAggregation(type); }
private static void addToState(LongDecimalWithOverflowAndLongState state, BigInteger value) { BlockBuilder blockBuilder = TYPE.createFixedSizeBlockBuilder(1); TYPE.writeSlice(blockBuilder, unscaledDecimal(value)); DecimalAverageAggregation.inputLongDecimal(TYPE, state, blockBuilder.build(), 0); } }
private static void accumulateAndUpdateOverflow(Slice unscaledValue, LongDecimalWithOverflowAndLongState state) { initializeIfNeeded(state); Slice sum = state.getLongDecimal(); long overflow = UnscaledDecimal128Arithmetic.addWithOverflow(sum, unscaledValue, sum); state.setOverflow(state.getOverflow() + overflow); } }
createInputParameterMetadata(type), inputFunction, COMBINE_FUNCTION,
public static void outputLongDecimal(DecimalType type, LongDecimalWithOverflowAndLongState state, BlockBuilder out) { if (state.getLong() == 0) { out.appendNull(); } else { writeBigDecimal(type, out, average(state, type)); } }
@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)); }
private static void accumulateValueInState(Slice unscaledValue, LongDecimalWithOverflowAndLongState state) { accumulateAndUpdateOverflow(unscaledValue, state); state.setLong(state.getLong() + 1); }
public static void inputLongDecimal(Type type, LongDecimalWithOverflowAndLongState state, Block block, int position) { accumulateValueInState(type.getSlice(block, position), state); }
public static void outputShortDecimal(DecimalType type, LongDecimalWithOverflowAndLongState state, BlockBuilder out) { if (state.getLong() == 0) { out.appendNull(); } else { writeShortDecimal(out, average(state, type).unscaledValue().longValueExact()); } }
@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)); }
public static void combine(LongDecimalWithOverflowAndLongState state, LongDecimalWithOverflowAndLongState otherState) { state.setLong(state.getLong() + otherState.getLong()); state.setOverflow(state.getOverflow() + otherState.getOverflow()); if (state.getLongDecimal() == null) { state.setLongDecimal(otherState.getLongDecimal()); } else { accumulateAndUpdateOverflow(otherState.getLongDecimal(), state); } }