[ 
https://issues.apache.org/jira/browse/KAFKA-12564?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17311325#comment-17311325
 ] 

Jessica Johann commented on KAFKA-12564:
----------------------------------------

[~mjsax] Thanks for your feedback. Would you mind taking a deeper look at the 
following?

If the filter method is called twice per input tuple it leaves me with the 
following possible output combinations:

 
||filter-method return value for 1st call||filter-method return value for 2nd 
call||emits||
|true|true|(1, "EMPTY+A")|
|true|false|(1, "EMPTY+A")|
|false|true|(1, null) tombstone|
|false|false|NOTHING (no output)|


So i.e. if I want to emit a tombstone record, I must return true for the 
current record and false for the previous record.
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.



I also did a slightly different test:

I created a KTable (with the same content) through a new topic: 


{{aggTable.toStream.to("issue_stream_2")}}
{{val aggTable2 = builder.table[Int,String]("issue_stream_2")}}

{{aggTable2.filter((k,v)=> {}}
{{ // ...}}
{{})}}
{{.toStream}}
{{.print(Printed.toSysOut)}}



In this case there is no second call to the filter method, I only get:

 

 
||filter-method return value for 1st call||emits||
|true|(1, "EMPTY+A")|
|false|(1, null) tombstone|


In my opinion this is conform with the 
[documentation|https://kafka.apache.org/20/javadoc/org/apache/kafka/streams/kstream/KTable.html#filter-org.apache.kafka.streams.kstream.Predicate-].
 

So my question is:

Why is the behaviour different in dependency of how the KTable is created?


PS:
All described examples refer to an aggregation of a KStream!

>From my understanding:
In case of an aggregation of a KTable it is necessary to pass the current and 
previous tuple to the aggregator (adder and subtractor).
After the aggregation for both KTable and KStream (as I understand it) there 
should be only one (and that is the aggregated record) to be filtered and not 
the combination of two records.

Thank you very much in advance for your help!


----


On an additional note:
If I run the Topology through the org.apache.kafka.streams.TopologyTestDriver, 
there is a slightly different behaviour again:

(1->"A")

//First call to filter(...)
filter(1, EMPTY+A) at 211379567071847
//No second call to filter(...)

(1->"B")

//First call to filter(...)
filter(1, EMPTY+A+B) at 211379567498482
//second call to filter(...)
filter(1, EMPTY+A) at 211379567524475


But this might be a different topic.

 

> 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: Jessica Johann
>            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)

Reply via email to