Github user fhueske commented on a diff in the pull request: https://github.com/apache/flink/pull/3733#discussion_r112080186 --- Diff: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/aggregate/GroupAggProcessFunction.scala --- @@ -84,16 +109,38 @@ class GroupAggProcessFunction( } // Set aggregate result to the final output - i = 0 - while (i < aggregates.length) { - val index = groupings.length + i - val accumulator = accumulators.getField(i).asInstanceOf[Accumulator] - aggregates(i).accumulate(accumulator, input.getField(aggFields(i)(0))) - output.setField(index, aggregates(i).getValue(accumulator)) - i += 1 + if (input.command == Command.Delete) { + i = 0 + while (i < aggregates.length) { + val index = groupings.length + i + val accumulator = accumulators.getField(i).asInstanceOf[Accumulator] + aggregates(i).retract(accumulator, input.getField(aggFields(i)(0))) + output.setField(index, aggregates(i).getValue(accumulator)) + i += 1 + } + } else { + i = 0 + while (i < aggregates.length) { + val index = groupings.length + i + val accumulator = accumulators.getField(i).asInstanceOf[Accumulator] + aggregates(i).accumulate(accumulator, input.getField(aggFields(i)(0))) + output.setField(index, aggregates(i).getValue(accumulator)) + i += 1 + } } - state.update(accumulators) + // if previous is not null, do retraction process + if (null != previous) { + if (previous.equals(output)) { + // ignore same output + return --- End diff -- We still need to update the state even if we do not emit new values. If we have a max aggregation with an accumulator that holds a map of `10->1, 5->2` and we add `8`. The new accumulator will be `10->1, 8->1, 5->2` but the aggregation result will still be 10. If we later retract `10`, the new max would be `5` but should be `8`.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---