# Understanding Aggregate KTables Subtractor

**URL:** <https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530>\
**Category:** Kafka Streams\
**Created:** [22 March 2022 17:39 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530 "2022-03-22T17:39:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![timst](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@timst](https://forum.confluent.io/u/timst)\
**Post date:** [22 March 2022 17:39 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530/1 "2022-03-22T17:39:56Z")

</div>

Hi, I’m new with Kafka and try to use it more. I am trying to understand the Aggregator or more precisely the Subtractor on KTable. From what I have read in the documentation the subtractor and adder are both called (given it has already been initialized. So often if you see a adder like +1L and subtractor like -1L (as for a simple count) then you would end up with no change after all (if both are called). So what part am I missing here?

Below is the relevant part from the guide: [https://kafka.apache.org/27/documentation/streams/developer-guide/dsl-api.html#aggregating](https://kafka.apache.org/27/documentation/streams/developer-guide/dsl-api.html#aggregating)

> When subsequent non-null values are received for a key (e.g., UPDATE), then (1) the subtractor is called with the old value as stored in the table and (2) the adder is called with the new value of the input record that was just received. The order of execution for the subtractor and adder is not defined.

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [23 March 2022 02:39 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530/2 "2022-03-23T02:39:51Z")

</div>

They will be called for different keys 🙂 Let’s look at an example. Assume you have an input KTable as follows:

```auto
key | value
-----------
 A | (a, 10)
 B | (a, 20)
 C | (b, 15)

```

You `groupBy(value.first)` and `sum(value.second)` and get a result table:

```auto
key | value
-----------
 a | 30
 b | 15

```

Now the input table gets updated: `<B,(a,20)>` changes to `<B,(b,5)>`. For this case, to update the second table, 20 needs to be _subtracted_ for `a`, and 5 needs to be _added_ for `b` resulting in:

```auto
key | value
-----------
 a | 10
 b | 20

```

Hence, the adder and subtractor are applies to different rows in the result table to compute the update.

---

<div class="post-metadata">

**Author:** ![timst](https://avatars.discourse-cdn.com/v4/letter/t/6f9a4e/32.png) [@timst](https://forum.confluent.io/u/timst)\
**Post date:** [23 March 2022 08:20 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530/3 "2022-03-23T08:20:03Z")

</div>

Thank you very much for your detailed explanation Matthias. I have a little follow up question.

In your last part of the example, assuming that instead of an update from `<B,(a,20)>` to `<B,(b,5)>` it changes to `<B,(a,25)>` would it be correct, that the subtractor then is never called? Or would it technically subtract 20 and add 25?

And lastly I tried to find the code for this part, could you tell me if this is the correct link? [kafka/KTableAggregate.java at 77e3ca019efe38195ec28ddab87aed52e1c403c8 · apache/kafka · GitHub](https://github.com/apache/kafka/blob/77e3ca019efe38195ec28ddab87aed52e1c403c8/streams/src/main/java/org/apache/kafka/streams/kstream/internals/KTableAggregate.java)

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [24 March 2022 01:01 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530/4 "2022-03-24T01:01:26Z")

</div>

The “magic” happens in here: [kafka/KTableRepartitionMap.java at 6ce69021fd1daa7bf39a9796152aff260b633b3a · apache/kafka · GitHub](https://github.com/apache/kafka/blob/6ce69021fd1daa7bf39a9796152aff260b633b3a/streams/src/main/java/org/apache/kafka/streams/kstream/internals/KTableRepartitionMap.java#L83-L106)

The “map” processor is put in between the upstream and downstream table, ie, in particular `upstreamKTable -> RepartitionMap -> repartition-topic -> downstreamAggregator -> downstreamKTable`

The upstream KTable will send a `Change` record with the old and new value for the updated row, and the `Change` record is split it into two. Downstream, one of the records will invoke the adder, the other one will invoke the subtractor. So it’s always two records and even if the key does not change (as in your example), it’s still a subtraction and an addition operation that will execute one over the other.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [19 May 2022 18:03 UTC](https://forum.confluent.io/t/understanding-aggregate-ktables-subtractor/4530/5 "2022-05-19T18:03:30Z")

</div>

Closed on 19 May 2022 18:03 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
