HeartSaVioR commented on code in PR #60313:
URL: https://github.com/apache/spark/pull/60313#discussion_r4591862584


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/ExternalLookupJoinSuite.scala:
##########
@@ -5574,28 +5764,212 @@ class ExternalLookupJoinRealTimeModeSuite extends 
StreamRealTimeModeE2ESuiteBase
     }
   }
 
-  test("real-time mode rejects external lookup join") {
-    SimpleExternalLookupDataSource.resetCounters()
-    val input = LowLatencyMemoryStream[Int](2)
-    val left = input.toDF().toDF("id")
-    val lookup = 
spark.read.format(classOf[SimpleExternalLookupDataSource].getName).load()
-    val joined = left.join(lookup, left("id") === lookup("id"))
-      .select(concat(col("department"), lit("-"), 
left("id").cast("string")).as("output"))
+  Seq("inner", "left_outer", "left_semi", "left_anti").foreach { joinType =>
+    test(s"real-time mode sends each stream row's lookup request as it reads 
the row: $joinType") {
+      val clock = useManualClock()
+      val input = LowLatencyMemoryStream.singlePartition[Int]
+      val query = startJoin(s"external_lookup_rtm_one_row_$joinType", 
joinType, input,
+        lookupTable(maxBatchSize = 2))
+      try {
+        // The source allows two stream rows per request, but by default a 
real-time request
+        // takes one, so the join sends it and emits its output as soon as it 
reads the row, with
+        // the batch still running. Key 3 has no lookup row, so the inner and 
left semi joins emit
+        // nothing for it, but its request is still sent.
+        val ids = Seq(1, 3, 2, 4)
+        ids.indices.foreach { n =>
+          input.addData(ids(n))
+          awaitQuery(query) {
+            SimpleExternalLookupDataSource.requestCount.get() == n + 1 &&
+              outputs.sorted == joinOutputs(joinType, ids.take(n + 1): 
_*).sorted
+          }
+        }
+        assert(SimpleExternalLookupDataSource.requestSizes.asScala.toSeq === 
Seq(1, 1, 1, 1))
+        endBatch(query, clock)
+        awaitOutputs(query, joinOutputs(joinType, ids: _*))
+        assert(SimpleExternalLookupDataSource.requestCount.get() === 4)
+      } finally {
+        query.stop()
+      }
+      assert(query.exception.isEmpty)
+    }
+
+    test(s"real-time mode fills a request of more rows before it sends it: 
$joinType") {
+      
withSQLConf(SQLConf.JOIN_EXTERNAL_LOOKUP_REAL_TIME_MODE_MAX_BATCH_ROWS.key -> 
"1000") {
+        val clock = useManualClock()
+        val input = LowLatencyMemoryStream.singlePartition[Int]
+        val query = startJoin(s"external_lookup_rtm_$joinType", joinType, 
input,
+          lookupTable(maxBatchSize = 2))
+        try {
+          // A request takes up to the two stream rows that the source allows, 
and its output
+          // comes once the request is full, before the next stream row 
arrives. Each addition
+          // fills a request and starts the next. Each full request has a key 
with lookup rows and
+          // one without, so that every join type emits rows for it.
+          input.addData(1, 3, 2)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3))
+          input.addData(4, 1)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4))
+          input.addData(3, 5)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4, 1, 3))
+          // The last request is not full, so the join sends it when the batch 
ends.
+          endBatch(query, clock)
+          awaitOutputs(query, joinOutputs(joinType, 1, 3, 2, 4, 1, 3, 5))
+          // The third request is served from the cache.
+          assert(SimpleExternalLookupDataSource.requestSizes.asScala.toSeq === 
Seq(2, 2, 1))

Review Comment:
   > [!NOTE]
   > **DISCLAIMER:** This comment was written by an LLM, Claude Opus 5.5 
(`claude-opus-5-5`, 1M context), posting on behalf of @HeartSaVioR.
   
   Thanks for catching it. Fixed in 
[6e8baf0b668](https://github.com/apache/spark/commit/6e8baf0b66801dbd10e275191b543d7924ed8f43),
 as you suggest. The key now goes through `CountingKeyUDF`, and after the 
output of the first full request the test waits until row 5 has been read 
before it ends the batch. It then waits inside `awaitQuery` for the request 
sizes 2, 2 and 1, and checks the outputs last. With a delay injected before the 
task reads row 5, the old test fails with your error, and the new one passes. I 
also checked the other real-time mode tests that end a batch after an output 
wait: each waits for output that needs every row added so far, so none has this 
race.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to