[
https://issues.apache.org/jira/browse/KAFKA-12564?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17311943#comment-17311943
]
Matthias J. Sax commented on KAFKA-12564:
-----------------------------------------
{quote}In the (stateless) filter-method I cannot determine which of the two
records is the current and which the previous. Or do I miss something crucial?
Neither can I find anything in the documentation about the three different
possible outcomes described above, nor the need to combine two calls to filter
to get e.g. a tombstone record.
{quote}
You don't need to worry about it. The runtime knows which call it makes and
will do the right thing. You can just implement the filter in a straightforward
way without any extra logic. (cf
[https://github.com/apache/kafka/blob/trunk/streams/src/main/java/org/apache/kafka/streams/kstream/internals/KTableFilter.java#L108)]
{quote} created a KTable (with the same content) through a new topic:
{quote}
For this case, the filter will work slightly different, because we don't have
access to the "old value" (the filter will be executed stateless by default).
The disadvantage of this stateless execution is, that whenever the filter
return "false" for the new value, a tombstone will be sent. Thus, you might get
"unnecessary/redundant" tombstones:
{code:java}
// input:
<k, 1> <k, 2> <k, 3> <k, 4> <k, 2> <k, 1>
// buidler.table().filter(v > 2) -> output:
<k, null> <k, null> <k, 3> <k, 4> <k, null> <k, null> {code}
As you can see we get a tombstone every time the filter is false: because we
don't know if the result contains an entry for `k` (we don't know the old
value), we need to be conservative to be correct and send a "blind" delete
downstream to make sure we delete _if_ it exists.
For the original example the output would be
{code:java}
// aggregation result:
<k, 1> <k, 2> <k, 3> <k, 4> <k, 2> <k, 1>
// aggregate().filter(v > 2) -> output:
<k, 3> <k, 4> <k, null>
{code}
For this case, we know that the result does not contain an entry fro k when
processing <k,1> and <k,2> and thus we emit nothing. For <k,3> and <k,4> we
just send it down because the new value passes the filter. For the second <k,2>
the new value return false and the old value (ie, <k,4>) is true, and thus we
know that we need to delete. For the last value, we also don't need to delete
as we know that we did delete previously.
You can make the new example stateful via
{code:java}
builder.table().filter(..., Materialize.as("someStoreName"));
// or
builder.table(..., Materialize.as("someStoreName")).filter()
{code}
If you materialize either the upstream table (might be require more storage) or
the downstream result table, the filter can again compare old/new and only emit
a tombstone if necessary. Note, that the execution for both case is different
again: for the first case, the filter will be evaluated only once for the new
value and the "old result" is retrieved from the state store – for the second
case, the filter would be called twice again (for the old and new value).
Hope this sheds some light. It's a little complicated...
> KTable#filter-method called twice after aggregation
> ---------------------------------------------------
>
> Key: KAFKA-12564
> URL: https://issues.apache.org/jira/browse/KAFKA-12564
> Project: Kafka
> Issue Type: Bug
> Components: streams
> Affects Versions: 2.7.0
> Reporter: Jess J.
> Priority: Major
>
> Libraries from build.sbt:
> {{"org.apache.kafka" % "kafka_2.13" % "2.7.0",}}
> {{"org.apache.kafka" % "kafka-streams" % "2.7.0",}}
> {{"org.apache.kafka" % "kafka-clients" % "2.7.0",}}
> {{"org.apache.kafka" % "kafka-streams-scala_2.13" % "2.7.0",}}
> h4.
> h4. Feed the Stream "issue_stream" with:
> {{(1->"A")}}
> {{(1->"B")}}
> h4.
> h4. Topology:
> {{// #1}}
> {{val issueStream:KStream[Int,String] =
> builder.stream[Int,String]("issue_stream")}}
>
> {{// #2}}
> {{val aggTable:KTable[Int,String] =}}
> {{issueStream}}
> {{.groupBy((k,v)=>k)}}
> {{.aggregate[String]("EMPTY")((k,v,agg)=>s"$agg+$v")}}
>
> {{// #3}}
> {{aggTable}}
> {{.toStream}}
> {{.print(Printed.toSysOut)}}
>
> {{// #4}}
> {{aggTable.filter((k,v)=> {}}
> {{ println(s"filter($k, $v) at ${System.nanoTime()}")}}
> {{ true}}
> {{})}}
> {{.toStream}}
> {{.print(Printed.toSysOut)}}
> h4.
> h4. First Tuple: (1->"A")
> #3 Output as expected, the aggregated tuple ("EMPTY"+"+A")
> {{[KTABLE-TOSTREAM-0000000044]: 1, EMPTY+A}}
>
> #4 The filter-method is called twice.
> First call with the expected tuple.
> {{filter(1, EMPTY+A) at 211379567071847}}
> The second call with the empty initialized aggregate.
> {{filter(1, EMPTY) at 211379567120375}}
> Output contains the correct tuple
> {{[KTABLE-TOSTREAM-0000000047]: 1, EMPTY+A}}
> h4.
> h4. Second Tuple: (1->"B")
> #3 Output as expected the aggregated tuple ("EMPTY"+"+A"+"+B")
> {{[KTABLE-TOSTREAM-0000000044]: 1, EMPTY+A+B}}
> #4 Again a second unexpected call to filter(...) with the previous tuple
> before aggregation
> First call:
> {{filter(1, EMPTY+A+B) at 211379567498482}}
> Second call:
> {{filter(1, EMPTY+A) at 211379567524475}}
> But the output contains only one tuple as expected
> {{[KTABLE-TOSTREAM-0000000047]: 1, EMPTY+A+B}}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)